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
Copy link

ndiogou commented Oct 12, 2017

Thank you for this tutorial. Really helpfull

@harllos
Copy link

harllos commented Jul 1, 2018

Really helpful! It works just fine! Thank you!

@NYCJacob
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
Copy link

NYCJacob commented Jul 16, 2018

any advice on setting coordinate_precision for web applications?

@lukaszmoginas
Copy link

lukaszmoginas commented Jan 20, 2021

Right now I have file over 200mb file size, maybe someone know how to chunk this file into two separate files, to avoid one big size file?

@FirestarterUA
Copy link

FirestarterUA commented Jan 27, 2021

Right now I have file over 200mb file size, maybe someone know how to chunk this file into two separate files, to avoid one big size file?

https://mapshaper.org , Select File ->Simplify

@warren9k
Copy link

warren9k commented Feb 1, 2021

As someone said above, I'd spent hours analysing, trying and failing with different advice, web-tools. Used this, worked correctly first time. I could have cried with happiness... haha. Thanks so much for taking time to post this!!

@aspiringguru
Copy link

aspiringguru commented Jul 12, 2021

Right now I have file over 200mb file size, maybe someone know how to chunk this file into two separate files, to avoid one big size file?

the tutorial left out mention of co-ordinate precision in the layer options section of saving the vector layer.
default is 15.
for smaller filesizes choose less accuracy, try 5.

I'm using this guide. this gist method is still good. there are many different options during export
https://community.geodesignhub.com/t/converting-your-data-to-geojson/53

@creinemann
Copy link

creinemann commented Jul 23, 2021

Hello,
I am Brand new to QGIS. I have a question on the conversion of a shapefile
(.shp) to a geojson format. I am using data produced by NOAA,NASA to map
out on satellite imagery, current fire hotspots onto satellite imagery I
receive and process at home.

The data I download is from here
https://firms.modaps.eosdis.nasa.gov/active_fire/ (I use the shp files)

I have a small script that downloads it every 12 hours.

I then use QGIS this way, to convert it into the type I need to apply it to
my imagery.

This is the current way I process it with QGIS 3.16.8

  1. Add a vector layer and open the .shp file I want to convert.
  2. Goto Vector-
    -Geoprocessing Tools
    -Buffer
  3. I then change the distance to .01
  4. then I run the Buffer (this is to create the geometry I need, rather
    than point)
  5. I then export the Buffered file "Save Features as"
    --selecting GEOJSON
    -saving it as a file.

This works and provides me the data in the format I need.

My questions this- Can this be done via a command line?
I wish to use my PC to automate the process so that I can have it run in a
batch file after the data download.

I am open to other methods as well.

An example of the imagery with the data applied is here.
https://imgur.com/a/2OEU8tj

Carl
http://usradioguy.com

@Egwim-Michael
Copy link

Egwim-Michael commented Apr 20, 2022

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

@lappemic
Copy link

lappemic commented Aug 18, 2022

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

@Egwim-Michael
Copy link

Egwim-Michael commented Oct 11, 2022

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