Skip to content

Instantly share code, notes, and snippets.

@YKCzoli
Last active July 10, 2025 13:23
Show Gist options
  • Save YKCzoli/b7f5ff0e0f641faba0f47fa5d16c4d8d to your computer and use it in GitHub Desktop.
Save YKCzoli/b7f5ff0e0f641faba0f47fa5d16c4d8d to your computer and use it in GitHub Desktop.
Step through of converting shapefile to geojson with qgis.

Converting shapefiles to geojson

####Assumptions

Get the data

We will use the Electoral District and Polling Division shapefiles found on the Elections Ontario site:

http://www.elections.on.ca/en/voting-in-ontario/electoral-districts/electoral-districts--maps--shapefiles-and-street-index-guide/limited-use-data-product-licence-agreement/download-shapefiles.html

Procedure

1 Unzip the files.

2 Open the 'Electoral District Shapefile' folder up in Windows Explorer (Windows) or Finder (Mac).

shapefile_finder_window

As we can see in the folder view, a shapefile is a collection of files.

The breakdown is as follows:

Mandatory files

.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

source: (https://en.wikipedia.org/wiki/Shapefile)

We are going to transform this current 7 file folder into a single, compact format called GeoJSON (http://geojson.org/). Geojson differs from a shapefile in a few ways. The first is that GeoJSON is an open standard format. This is great because the data can be read by a greater number of software packages. Additionally, GeoJSON is similar to JSON. Meaning it is more easily machine read, and can easily be consumed by web maps! Imagine loading multiple files into javascript vs one file.

3 Open QGIS.

qgis_blank

4 A shapefile is a vector file format. We will add our shapefile to our project through the 'Add Vector Layer'.

load_vector

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.

shapefile_open

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.

general_properties

7 Right click on the shapefile in the Table of Contents, and click Save as.

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.

crs

9 Enter '4326' in the Filter box and selct EPSG: 4326. Click OK.

crssave

10 Browse to your location and save the file.

The file will be automatically added to the current project view

geojson

11 If you browse to the location of the geojson file, you will see that it consists of one file.

geojsonfinder

12 Optionally, you can open the geojson file in a text editor to view the json structure.

screen shot 2016-04-25 at 10 07 26 pm

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.

@a64mahmo
Copy link

I love you so much

@s-kennedy
Copy link

s-kennedy commented Jul 10, 2025

I'm using QGIS v3.42.1 and the process was slightly different from above. I'm converting FSA shapefiles from StatsCan to geoJSON. Instead of going into the layer properties and changing the CRS, I had to use the "Reproject layer" tool.

  • open the Processing menu
  • open Toolbox
  • in the Processing Toolbox, open "Vector general" and then click "Reproject layer"
  • in the dialog box, make sure your shapefile layer is selected as the "input layer"
  • select ESPG:4326 as the "Target CRS"
  • hit Run

That should add a new layer called "Reprojected", and you can then convert it to geoJSON as follows:

  • right click on the new layer
  • open Export > Save Features As...
  • make sure the format is GeoJSON and CRS is EPSG:4326 - WGS 84
  • enter in where to save the file
  • hit OK

And that's it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment