Represent Boundaries loads geospatial data in the shapefile format. Other formats like KML and GeoJSON can be converted to shapefile using tools like ogr2ogr. Before using Represent Boundaries, collect the geospatial data that you need. For example, collect Canada’s federal electoral districts:
curl -O https://www12.statcan.gc.ca/census-recensement/2011/geo/bound-limit/files-fichiers/gfed000a11a_e.zip
You’ve got your shapefiles? Next, write definition files that describe how to load shapefiles into the API. When a shapefile is loaded, a boundary set is created for the shapefile and a boundary is created for each polygon feature in the shapefile. See the sample definition file to learn how to control how shapefiles are loaded. Most parameters in a definition file are optional. Here’s a minimal definition file for Canada’s federal electoral districts:
from datetime import date
import boundaries
boundaries.register('Federal electoral districts',
encoding='iso-8859-1',
last_updated=date(2011, 11, 28),
name_func=boundaries.clean_attr('FEDENAME'),
)
The loadshapefiles
command looks for definition files, ending in definition.py
or definitions.py
, in my_project/data/shapefiles
. You can change this path by setting BOUNDARIES_SHAPEFILES_DIR
in my_project/settings.py
. Represent Boundaries will walk the directory tree looking for definition files, so you may organize your shapefiles any way you like. OpenStates.org puts the shapefiles in subdirectories with a single top-level definition file, while Represent creates a tree with a definition file in each directory containing a shapefile.
If you’ve been following the example, move the shapefile and definition file for Canada’s federal electoral districts into data/shapefiles
. You may need to create the directory before moving the files:
mkdir -p data/shapefiles
You can now run:
python manage.py loadshapefiles
The data should now be available via the API. Let’s explore the API.