Changing dataset projection with OGR2OGR

Previously we used OGR2OGR to extract a couple of features from a large geographical dataset. OGR2OGR can do so much more – today we’ll look at its reprojecting capabilities. Reprojection is a mathematical translation of a dataset’s coordinate reference system to another one, like Albers to Mercator. Sometimes the geographical data we receive has to be reprojected to conform to our other datasets before further use. My first encounter with mismatching projections came about during the works on my master thesis project. I struggled with a point-in-polygon function that was supposed to filter a set of points based on a geographical boundary and stubbornly just wouldn’t return anything. I soon found out that my map was digitalised in EPSG:3857 (AKA web mercator used by Google maps) projection and my village coordinates used WGS84 coordinate system. That’s how OGR2OGR and me met.

To demonstrate reprojection, we can use our previously downloaded Eurostat’s 1:10M Scale World Boundaries dataset. What’s really magical about OGR2OGR is that it doesn’t require you to know the original projection of the dataset as long as it’s specified in the file metadata. Eurostat is a serious statistical body and always ships its datasets annotated. In geojson the coordinate information is prepended as “crs”: { “type”: “name”, “properties”: { “name”: “urn:ogc:def:crs:OGC:1.3:CRS84” } }, while in .shp database it’s included in the .prj file.

OGR2OGR data reprojection of the file CNTRLB20163857.geojson_ from EPSG:3857 spatial reference system to WGS84 (AKA EPSG:4326) is done using the t_srs function:

ogr2ogr output.geojson -t_srs "EPSG:4326" CNTR_LB_2016_3857.geojson

Besides t_srs, where t stands for translation, OGR2OGR includes s_trs for overriding the source projection type and a_srs for assigning one. The latter is very useful if the dataset’s metadata doesn’t indicate the coordinate reference system. OGR2OGR requires the original spatial system information to be able to perform the translation. Tools such as Prj2ESPG can help you identify the projection based on a file excerpt, however based on my own experience the matches they produce are often hit and miss.

Now off to creating a map in D3.js!