I'm trying to start plotting maps in python with matplotlib's basemap, but every shapefile I've tried to far seems to have coordinates in metres instead of lat/lon, so I get the following error from readshapefile:

ValueError: shapefile must have lat/lon vertices - it looks like this one has vertices in map projection coordinates.

Here's output from fiona about the shapefile I'm using:

shape = fiona.open('./data/ESRI/London_Ward.shp') bounds = shape.bounds print(shape.crs)

: {'proj': 'tmerc', 'lat_0': 49, 'lon_0': -2, 'k': 0.999601272, 'x_0': 400000, 'y_0': -100000, 'datum': 'OSGB36', 'units': 'm', 'no_defs': True}

: (503568.2, 155850.8, 561957.5, 200933.9)

ogr2ogr is the only conversion tool I've managed to install so far, is it possible to use it to make the necessary conversion?


Yes, it is. Use it like this from the command line (might be necessary to cd into the folder where ogr2ogr has been installed to):

ogr2ogr -t_srs EPSG:4326 -f "ESRI Shapefile" transformed.shp London_Ward.shp

If you want to run it from Python, you can use subprocess to set up the command and run it from the script, e.g. like this:

import subprocess as sub

input = r'./data/ESRI/London_Ward.shp'
output = r'./data/ESRI/London_Ward_WGS84.shp'
cmd = 'ogr2ogr -t_srs EPSG:4326 -f "ESRI Shapefile" {0} {1}'.format(output, input)
p = sub.Popen(cmd, stdout=sub.PIPE, stderr=sub.PIPE)
stdout, stderr = p.communicate()
if p.exitcode != 0:
    print stdout
    print stderr

Alternatively, pass all parts of the command as a list like this:

p = sub.Popen([ogr2ogr -t_srs EPSG:4326 -f "ESRI Shapefile", output, input], stdout=sub.PIPE, stderr=sub.PIPE)

In case you really want to stay within Python and don't use the command line, use the accepted answer from Python - OGR: Transform coordinates from meter to decimal degrees

Also have a look at the Documentation

  • Thanks for the answer. I think it was mainly the command-line options for ogr2ogr I needed help with. Is there any reason you picked subprocess over os.system for running it in python? – thosphor Mar 19 '18 at 9:14
  • Not really... I was just trusting the answer from Difference between subprocess.Popen and os.system – s6hebern Mar 19 '18 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.