Converting shapefiles to geojson
- QGIS installed (if not: http://qgis.com/)
Get the data
We will use the Electoral District and Polling Division shapefiles found on the Elections Ontario site:
1 Unzip the files.
2 Open the 'Electoral District Shapefile' folder up in Windows Explorer (Windows) or Finder (Mac).
As we can see in the folder view, a shapefile is a collection of files.
The breakdown is as follows:
.shp — shape format; the feature geometry itself .shx — shape index format; a positional index of the feature geometry to allow seeking forwards and backwards quickly .dbf — attribute format; columnar attributes for each shape, in dBase IV format
Other common files
.prj — projection format; the coordinate system and projection information, a plain text file describing the projection using well-known text format .sbn and .sbx — a spatial index of the features
3 Open QGIS.
4 A shapefile is a vector file format. We will add our shapefile to our project through the 'Add Vector Layer'.
5 Everything in the Source type dialogue can be left as is. Use the browser to navigate to the location of your shapefile. Click open to load the shapefile into QGIS.
6 In a new QGIS project, a great first question is what map projection is our shapefile in? Double click on the shapefile in the Table of Contents to pull up the Properties Dialogue Box. In the General tab, under 'Coordinate reference system' you will see a value representing our map projection. For our purposes we will convert our shapefile into a GeoJson and into a geographic projection for web mapping. EPSG is a coded value that is used to identify certain projections, which in this case is 5320. We want to conver this to 4326, a more suitable web mapping projection.
7 Right click on the shapefile in the Table of Contents, and click Save as.
8 In the Save As dialogue, switch the format to GeoJson. Then click the small globe-ish icon to the right of the CRS drop down option.
9 Enter '4326' in the Filter box and selct EPSG: 4326. Click OK.
10 Browse to your location and save the file.
The file will be automatically added to the current project view
11 If you browse to the location of the geojson file, you will see that it consists of one file.
12 Optionally, you can open the geojson file in a text editor to view the json structure.
Now we have a single geojson file opposed to a complicated shapefile structure, and a geojson file in a more appropriate projection for web mapping that is readily consumable by any web mapping platform.
Thank you for this tutorial. Really helpfull