Oct 142014

The shapefile (SHP) format is one of the most commonly used data formats in Geographical Information Systems (GIS). Most geographical information editing tools, including free open source tools such as QGIS, can read and generate files in shapefile format.

But sometimes it may be necessary, or simply desirable, to use other formats for the handling of geographical information. One of the most popular formats used in the development of web applications is GeoJSON, that can be directly processed in javascript.

This post explains by means of an example how to convert a file from SHP to GeoJSON format.

Obtaining some example files in shapefile format

Let’s start by obtaining some SHP files that will be used to illustrate this post. From the GADM website, we can download a file with the administrative areas of Portugal in SHP format. The file downloaded  is PRT_adm.zip, and is 34 MB in size.

The content of the compressed zip file is a set of files for each of the four top administrative levels of Portugal:

$ unzip PRT_adm.zip 
Archive:  PRT_adm.zip
  inflating: PRT_adm0.csv            
  inflating: PRT_adm0.dbf            
  inflating: PRT_adm0.prj            
  inflating: PRT_adm0.shp            
  inflating: PRT_adm0.shx            
  inflating: PRT_adm1.csv            
  inflating: PRT_adm1.dbf            
  inflating: PRT_adm1.prj            
  inflating: PRT_adm1.shp            
  inflating: PRT_adm1.shx            
  inflating: PRT_adm2.csv            
  inflating: PRT_adm2.dbf            
  inflating: PRT_adm2.prj            
  inflating: PRT_adm2.shp            
  inflating: PRT_adm2.shx            
  inflating: PRT_adm3.csv            
  inflating: PRT_adm3.dbf            
  inflating: PRT_adm3.prj            
  inflating: PRT_adm3.shp            
  inflating: PRT_adm3.shx            
  inflating: read_me.pdf


The set of files PRT_adm0.* contain the country boundaries, including the continental boundary, the Açores and the Madeira islands. The file can be opened in qgis to have a look at the content:


PRT_adm1.* files contain the district boundaries:


PRT_adm2.* contain the second level administrative areas of Portugal (the “Concelhos”):

Finally, the set of files PRT_adm3.* contain the third level administrative areas (“Freguesías”):


Converting from SHP to GeoJSON

The conversion can be done with the ogr2ogr tool. ogr2ogr is part of the gdal-bin software package available in the the package repositories of most linux distributions.

The computer where we have downloaded the shapefiles is running a Debian OS. In Debian, apt-get can be used to download and install software packages such as gdal-bin from the repository:

$ sudo apt-get install gdal-bin

Once installed, ogr2ogr is run on each of the shapefiles to perform the conversion to GeoJSON:

$ ogr2ogr -f GeoJSON PRT_adm0.json PRT_adm0.shp
$ ogr2ogr -f GeoJSON PRT_adm1.json PRT_adm1.shp
$ ogr2ogr -f GeoJSON PRT_adm2.json PRT_adm2.shp
$ ogr2ogr -f GeoJSON PRT_adm3.json PRT_adm3.shp

The generated “.json” files are text files in GeoJSON format. They can be opened with any text editor to examine their content. For instance, opening the file “PRT_adm1.json”, we can have a look a the structure of the data inside:

  "type": "FeatureCollection",
  "features": [
  { "type": "Feature",
    "properties": {
        "ID_0": 180, "ISO": "PRT", "NAME_0": "Portugal", "ID_1": 1,
        "NAME_1": "Aveiro", "NL_NAME_1": "", "VARNAME_1": "",
        "TYPE_1": "Distrito", "ENGTYPE_1": "District" },
    "geometry": { "type": "MultiPolygon",
        "coordinates": [ [ [ [ -8.682082, 40.694584 ], ...  [ -8.272008, 41.064754 ] ] ] ] } }

As we can see, the “.json” files are standard GeoJSON files that can be read by any GIS application that accepts this format as input. For instance, we can load the file in the on-line tool “Show me the GeoJSON“:


Handling non-default Coordinate Reference Systems

In GeoJSON, the default Coordinate Reference System uses the WGS84 datum, with longitude and latitude units of decimal degrees.

Adding a “crs” element to the JSON object, it is possible to specify other coordinate systems. for instance:

<code class="javascript"> <span class="s2">"crs"</span><span class="o">:</span> <span class="p">{</span>
    <span class="s2">"type"</span><span class="o">:</span> <span class="s2">"name"</span><span class="p">,</span>
    <span class="s2">"properties"</span><span class="o">:</span> <span class="p">{</span>
      <span class="s2">"name"</span><span class="o">:</span> <span class="s2">"urn:ogc:def:crs:OGC:1.3:CRS84"</span>
      <span class="p">}</span>
    <span class="p">}</span></code>

The CRS used by the shapefile is also found in the file with “.prj” extension.

However, the ogr2ogr tool does not write this element to the generated output. To fix this issue, there two possibilities:

Manually edit the output file to add the right CRS

Or else, just use the “-t” option in the call to ogr2ogr, to perform a CRS conversion to the default WGS84 CRS:

$ ogr2ogr -f GeoJSON PRT_adm0.json PRT_adm0.shp -t_srs EPSG:4326


GeoJSON website

 Posted by at 11:30 am

 Leave a Reply