← Back to list

Get coordinates and boundary box (shapefile) for a location from Geolocation Service Canada with…

I often need to use shapefiles in my projects. Recently, I am trying to get coordinates and boundary boxes for cities in Canada. There…

JV.S · 2021-02-01 22:41 · 59 claps · 1.5 min read
#coordinates #bbox #python #jupyter-notebook
Open on Medium ↗

Get coordinates and boundary box (shapefile) for a location from Geolocation Service Canada with Python

Photo by British Library on Unsplash

Photo by British Library on Unsplash

I often need to use shapefiles in my projects. Recently, I am trying to get coordinates and boundary boxes for cities in Canada. There could be existing files somewhere to use, but here is what I did to get this information. This works for any location search in Canada. See details here. I will show how to get the geolocation information for a list of places in Jupyter Notebook. This is quite convenient when you want to search locations with a key word like lake in Canada.

First, import necessary packages to run the notebook.

import numpy as np
import pandas as pd
pd.set_option('display.max_columns', 500)
pd.set_option('display.max_rows', 600)
pd.set_option('display.max_colwidth', 500)
import json
import requests
from requests import Request
from bs4 import BeautifulSoup
import urllib.request

Now let’s take a look at what we get when search Regina with Geolocation Service Canada.

places = ['Regina']
for place in places:    
    url  = 'http://geogratis.gc.ca/services/geolocation/en/locate? q=' + place   
    page = requests.get(url)
    soup = BeautifulSoup(page.content, 'html.parser')
    ls   = json.loads(str(soup)) # a list of dictionaries
    df   = pd.DataFrame(ls)
df.head()

It can be seen that we can get a dataframe with all locations containing Regina in the title. Usually, if you are searching for city, town, village, hamlet etc., you can add some conditions to filter the results.

You can always adjust the filter conditions to keep the data you need. When you find this tool, it would be quite easy to get the coordinates and bbox for a location.


메타데이터
post_id
ce315e7f7fec
slug
get-coordinates-and-boundary-box-shapefile-for-a-location-from-geolocation-service-canada-with-ce315e7f7fec
url
https://medium.com/@jvshi-96341/get-coordinates-and-boundary-box-shapefile-for-a-location-from-geolocation-service-canada-with-ce315e7f7fec
canonical_url
https://medium.com/@jvshi-96341/get-coordinates-and-boundary-box-shapefile-for-a-location-from-geolocation-service-canada-with-ce315e7f7fec
author_url
https://medium.com/@jvshi-96341
status
ok
fetched_at
2026-07-28 13:38:14