Skip to content

Instantly share code, notes, and snippets.

@YKCzoli
Last active December 3, 2023 03:37
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • 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.

@Egwim-Michael
Copy link

Please, I need to know how to overlay an estate layout on a web map.

@lappemic
Copy link

Hello, thanks for this tutorial. If i export it in this way, the polygons get exported as 'MultyPolygon' but for my further processing i need just type 'Polygon'. Do you know a way of doing this?

@Egwim-Michael
Copy link

Egwim-Michael commented Aug 18, 2022 via email

@Egwim-Michael
Copy link

Egwim-Michael commented Oct 11, 2022 via email

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