Tuesday, September 27, 2016

Converting Shapefile to GeoJSON


Introduction


If you are like me that often produce web based maps often, you will definitely come across situations were you have to convert the most popular geospatial file format (Shapefile) into the most popular web programming language file format - JSON (thta is: GeoJSON in our case since we care more about location elements).

Before I gone to show how you will convert a shapefile to a GeoJSON, let me briefly explain what a JSON file format is and why it is important to us.


What is JSON?


JSON is a short hand for "JavaScript Object Notation". Though JSON has the name JavaScript it can be used in all different languages since most languages have libraries to parse the JSON data.

JSON is a minimal, readable format for structuring data. It is used primarily to transmit data between a server and web application, as an alternative to XML. It is a lightweight format that is used for data interchanging. It is based on a subset of JavaScript language (the way objects are built in JavaScript). It is a much-more compact way of transmitting sets of data across network connections as compared to XML.

JSON values can consist of: objects (collections of name-value pairs) arrays (ordered lists of values) strings (in double quotes) numbers true, false, or null. JSON is language independent.


What are JSON files?


JSON files are the files which contain JSON data. They are saved with a file extension:  .json

Conventions used by JSON are known to programmers, which include C, C++, Java, Python, Perl, etc. For instance, in Python programming the awareness of the built-in data structures such as Tuple, List, and Dictionary will help you understand JSON/GeoJSON object.



What does it look like?


A simple json structure may look like this:-

"author":
{
"name" : "Umar Yusuf",
"age" : "23",
"gender" : "male",
"url" : "http://www.UmarYusuf.com"
}

Storing JSON: data as a simple example, information about me might be written in JSON as above.

Storing JSON Data in Arrays: a slightly more complicated example involves storing information about two people. To do this, we enclose multiple objects in square brackets, which signifies an array. For instance, if I needed to include information about myself and my brother, it will look like this below:-

[{
"name" : "Umar Yusuf",
"age" : "23",
"gender" : "male"
"url" : "http://www.UmarYusuf.com"
},
{
"name" : "Musa Kallamu",
"age" : "37",
"gender" : "male"
"url" : "http://www.mk.com"
}, ...]


Nesting JSON Data: Another way to store information about multiple people would be to nest objects. To do this, we would create something similar to the following:

{
"Umar":{
"name" : "Umar Yusuf",
"age" : "23",
"gender" : "male"
"url" : "http://www.UmarYusuf.com"
},

"Musa":{
"name" : "Musa Kallamu",
"age" : "37",
"gender" : "male"
"url" : "http://www.mk.com"
}, ...

}


Uses of JSON


~ It is used while writing JavaScript based applications that includes browser extensions and websites.
~ JSON format is used for serializing and transmitting structured data over network connection.
~ It is primarily used to transmit data between a server and web applications.
~ Web services and APIs use JSON format to provide public data.
~ It can be used with modern programming languages.

What is GeoJSON?


GeoJSON s the spatial version of JSON. It is a format for encoding a variety of geographic data structures. GeoJSON makes it easy to load geographic layers onto a web map. GeoJSON is based on JavaScript Object Notation. GeoJSON is plain text format that stores geometries and attributes in a standardized and human readable way. It's file extension is .geojson

This format has gradually become the defacto standard when loading geographic data onto leaflet powered maps. In addition, geojson format incorporates both the spatial and attribute data of the geographic features.

If you understood how JSON works then understanding GeoJSON won't be difficult. The major difference between the two is the data contents, unlike JSON, a GeoJSON most contain spatial dataset (Longitude and Latitude).


Examples of GeoJSON data:


The three basic object representation in shapefile are the point, line and polygon. Lets see the basic structure of these representations in a GeoJSON object.

Empty Shapefile

A point, line or polygon shapefile with no features in it will look like below in GeoJSON format.

{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [

]
}



Single Feature Shapefile

The GeoJSON format of a shapefile that contains just one single feature will look like this for:-

a) Point shapefile


The point has the following attributes/properties id", "name", and "size".

{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "id": 1, "name": "name1", "size": 21.0 }, "geometry": { "type": "Point", "coordinates": [ -2.949515777294685, 14.213891761352743 ] } }
]
}



b) Line  shapefile


The line has the following attributes/properties id", "name", and "length".

{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "id": 1, "name": "name1", "length": 200.0 }, "geometry": { "type": "LineString", "coordinates": [ [ -2.591611147342995, 21.4370579294687 ], [ -0.655672467149753, 19.110677834782706 ] ] } }
]
}



c) Polygon shapefile


The polygon has eight vertices and the following attributes/properties id", "name", and "area".

{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "id": 1, "name": "name1", "area": 12.89 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -7.488397220772956, 21.453326321739233 ], [ -5.861557993719814, 20.347075647343097 ], [ -5.568726932850248, 18.280989828985604 ], [ -3.453835937681161, 18.915457127536328 ], [ -4.869186065217396, 14.392844076328588 ], [ -8.106596127053152, 15.320142435748881 ], [ -9.294188762801946, 18.280989828985604 ], [ -7.488397220772956, 21.453326321739233 ] ] ] } }
]
}



Multi Feature Shapefile

The GeoJSON format of a shapefile that contains multiple amount features will look like this for:-

a) Points shapefile

The points have the following attributes/properties: id", "name", and "size".

{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "id": 1, "name": "name1", "size": 21.0 }, "geometry": { "type": "Point", "coordinates": [ -2.949515777294685, 14.213891761352743 ] } },
{ "type": "Feature", "properties": { "id": 2, "name": "name2", "size": 90.0 }, "geometry": { "type": "Point", "coordinates": [ -1.940875456521736, 16.621613817391395 ] } },
{ "type": "Feature", "properties": { "id": 3, "name": "name3", "size": 21.0 }, "geometry": { "type": "Point", "coordinates": [ -2.99832095410628, 18.167111083091882 ] } },
{ "type": "Feature", "properties": { "id": 4, "name": "name4", "size": 256.0 }, "geometry": { "type": "Point", "coordinates": [ -9.456872685507261, 16.068488480193327 ] } },
{ "type": "Feature", "properties": { "id": 5, "name": "name5", "size": 500.0 }, "geometry": { "type": "Point", "coordinates": [ -5.340969441062807, 21.420789537198171 ] } },
{ "type": "Feature", "properties": { "id": 6, "name": "name6", "size": 309.0 }, "geometry": { "type": "Point", "coordinates": [ 1.508023704830928, 18.557552497584638 ] } },
{ "type": "Feature", "properties": { "id": 7, "name": "name7", "size": 90.0 }, "geometry": { "type": "Point", "coordinates": [ 1.150119074879235, 15.954609734299606 ] } },
{ "type": "Feature", "properties": { "id": 8, "name": "name8", "size": 78.0 }, "geometry": { "type": "Point", "coordinates": [ -9.359262331884072, 13.709571600966269 ] } },
{ "type": "Feature", "properties": { "id": 9, "name": "name9", "size": 190.0 }, "geometry": { "type": "Point", "coordinates": [ 1.166387467149768, 13.465545716908297 ] } },
{ "type": "Feature", "properties": { "id": 10, "name": "name10", "size": 190.0 }, "geometry": { "type": "Point", "coordinates": [ -6.756319568599042, 16.654150601932457 ] } }
]
}



b) Lines shapefile

The lines are of varying length and shape. They all have the following attributes/properties: id", "name", and "length".

{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "id": 1, "name": "Line1", "length": 200.0 }, "geometry": { "type": "LineString", "coordinates": [ [ -2.591611147342995, 21.4370579294687 ], [ -0.655672467149753, 19.110677834782706 ] ] } },
{ "type": "Feature", "properties": { "id": 2, "name": "Line2", "length": 1002.0 }, "geometry": { "type": "LineString", "coordinates": [ [ 1.394144958937208, 19.761413525603963 ], [ -1.452823688405793, 15.547899927536321 ], [ 1.573097273913053, 14.132549800000087 ] ] } },
{ "type": "Feature", "properties": { "id": 3, "name": "line3", "length": 2901.0 }, "geometry": { "type": "LineString", "coordinates": [ [ -10.23775551449277, 13.384203755555641 ], [ -4.413671081642516, 13.286593401932452 ], [ -2.916978992753624, 12.831078418357571 ], [ -2.315048478743959, 13.465545716908297 ], [ -0.28149944492753, 13.254056617391388 ], [ 1.394144958937208, 12.635857711111194 ], [ 2.793226694202911, 13.514350893719891 ], [ 4.110966468115958, 13.400472147826171 ], [ 4.631555020772964, 15.076116551690909 ], [ 4.322455567632867, 16.8819080937199 ], [ 4.127234860386489, 16.410124717874488 ], [ 3.850672191787455, 16.475198286956612 ], [ 4.159771644927552, 18.069500729468693 ], [ 5.054533219806782, 18.557552497584638 ], [ 4.908117689371998, 19.940365840579808 ], [ 4.159771644927552, 19.598729602898651 ], [ 4.989459650724655, 21.258105614492855 ], [ 4.647823413043495, 21.550936675362422 ], [ 3.443962385024168, 21.794962559420394 ], [ 3.443962385024168, 22.201672366183679 ], [ 4.745433766666684, 22.185403973913147 ], [ 5.266022319323689, 21.681083813526673 ] ] } }
]
}




c) Polygons shapefile

The polygons are of varying shapes and size. They all have the following attributes/properties: id", "name", and "area".

{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "id": 1, "name": "name1", "area": 12.89 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -7.488397220772956, 21.453326321739233 ], [ -5.861557993719814, 20.347075647343097 ], [ -5.568726932850248, 18.280989828985604 ], [ -3.453835937681161, 18.915457127536328 ], [ -4.869186065217396, 14.392844076328588 ], [ -8.106596127053152, 15.320142435748881 ], [ -9.294188762801946, 18.280989828985604 ], [ -7.488397220772956, 21.453326321739233 ] ] ] } },
{ "type": "Feature", "properties": { "id": 2, "name": "name2", "area": 123.0 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 3.704256661352671, 18.557552497584638 ], [ 3.313815246859917, 15.255068866666754 ], [ 2.711884732850255, 17.256081115942123 ], [ 3.704256661352671, 18.557552497584638 ] ] ] } },
{ "type": "Feature", "properties": { "id": 3, "name": "name3", "area": 209.0 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 1.231461036231893, 22.136598797101552 ], [ 1.914733511594214, 22.315551112077397 ], [ 1.97980708067634, 21.794962559420394 ], [ 1.150119074879235, 20.607369923671598 ], [ 2.044880649758465, 20.347075647343097 ], [ 3.476499169565232, 21.388252752657106 ], [ 4.192308429468614, 20.672443492753725 ], [ 2.77695830193238, 19.712608348792369 ], [ -0.200157483574873, 20.542296354589471 ], [ 1.231461036231893, 22.136598797101552 ] ] ] } },
{ "type": "Feature", "properties": { "id": 4, "name": "name4", "area": 210.0 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -4.055766451690824, 21.941378089855174 ], [ -2.217438125120772, 19.598729602898651 ], [ -4.950528026570053, 20.005439409661935 ], [ -4.055766451690824, 21.941378089855174 ] ] ] } },
{ "type": "Feature", "properties": { "id": 5, "name": "name5", "area": 608.0 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -10.758344067149777, 21.925109697584645 ], [ -9.050162878743976, 22.071525228019429 ], [ -8.5458427183575, 20.249465293719908 ], [ -10.693270498067649, 20.021707801932465 ], [ -10.758344067149777, 21.925109697584645 ] ] ] } }
]
}


You can know the amount of features in each GeoJSON by counting the "geometry" entries for each.

Different methods to convert Shapefile to GeoJSON


There are different methods to convert Shapefile to GeoJSON online, or offline or with the help of API which programmers can make use of.

1) Online Method : Convert Shapefile to GeoJSON:

Probably the easiest method fro the convertion is through the online method, as you don’t need to install and configure any tool on your PC. Just drag the Shapefile files and click on button to convert Shapefile to GeoJSON.

There are many website which provides such conversion tools. Some of them are listed below:-

a) ogr2ogr web client
b) Geojson.io
c) GIS Map Engine
d) Map Shapper


2) Offline Method : Convert Shapefile to GeoJSON:

With this method, you most install a tool to do it. There several tools for this conversion which include:
~ Command line ogr2ogr tool
~ Software - most GIS software like QGIS or ArcGIS
~ Programming languages like C/C++, Python, R, JavaScript, C# etc


Convert Shapefiles into GeoJSON format using QGIS


This post will demonstrate how to convert Shapefiles into GeoJSON format using QGIS desktop offline method.



Steps 1:
Launch QGIS and load in point, line and polygon shapefile. Or create on if you don't have the shapefiles

Steps 2:
Right Click on the point shapefile layer and "Save As..."


Steps 3:
On "Save As" dialog box that opened, under "Format" select "GeoJSON", under "Save As" choose the folder to save the generated GeoJSON file. Finally select your coordinate reference system under "CRS" and click "OK" to save.



Note: If you don't want to add the generated GeoJSON file to the map canvas, unckeck the "Add saved file to map" option


Steps 4:
Repeat step 3 above for the line and polygon layers. You should now have three GeoJSON files in you working folder/directory for the point, line and polygon shapefile layers.

That is how you convert a shapefile to GeoJSON on the desktop using QGIS.

A view of the working folder of our example is as seen below;-




Thank you for following the blog.

No comments:

Post a Comment