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:


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:

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

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:

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:

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:


GeoJSON website

 Posted by at 11:30 am

 Leave a Reply