Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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.

@ndiogou

This comment has been minimized.

Copy link

ndiogou commented Oct 12, 2017

Thank you for this tutorial. Really helpfull

@harllos

This comment has been minimized.

Copy link

harllos commented Jul 1, 2018

Really helpful! It works just fine! Thank you!

@NYCJacob

This comment has been minimized.

Copy link

NYCJacob commented Jul 16, 2018

yea, I just can't say thank you enough after endless google searches and trying to figure out ogr2ogr for my project this was a huge help.

@NYCJacob

This comment has been minimized.

Copy link

NYCJacob commented Jul 16, 2018

any advice on setting coordinate_precision for web applications?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.