3

I want to convert between different coordinates. If I'm in the command line, it's as simple as:

gdaltransform -s_srs EPSG:4326 -t_srs EPSG:32616
-122 46

And I receive the output

-2193988.77788563 5724599.39928024 0

I'm trying to use this in a python program, and it seems like I need the input in the same command. Something like this (although it doesn't work)

gdaltransform -s_srs EPSG:4326 -122 46 -t_srs EPSG:32616

In my python program, I have tried to make separate calls:

command = "gdaltransform -s_srs EPSG:4326 -t_srs EPSG:" + data['crs']
os.system(command)
command = data['lon'] + ' ' + data['lat']
output = subprocess.check_output(command, shell=True)

but get an error:

CalledProcessError: Command '-122 46' returned non-zero exit status 2

I'm not sure of the correct way of doing this.

2 Answers 2

11

You can do this in Python without a call to an external process using the GDAL python bindings.

Here's an example:

from osgeo import osr

src = osr.SpatialReference()
tgt = osr.SpatialReference()
src.ImportFromEPSG(4326)
tgt.ImportFromEPSG(int(data['crs']))

transform = osr.CoordinateTransformation(src, tgt)
coords = transform.TransformPoint(-122, 46)
x,y = coords[0:2]
3
  • First call I received an error ValueError: too many values to unpack because my crs was using UTM, so I added the transform variables to x,y,z and it worked! I'm new to GIS, will this work for any reference system? Like for EPSG:32616 I needed to add the z var. Are there any other crs that require more than 3 vars?
    – amallard
    Jan 28, 2017 at 16:53
  • @amallard not sure when z will be returned, but don't think there is ever more than x, y & z. See edit.
    – user2856
    Jan 28, 2017 at 20:39
  • As of GDAL 3.0 a transformation may return X, Y, Z & time values.
    – user2856
    May 10, 2019 at 2:28
3

In my case, gdaltransform -s_srs EPSG:4326 -122 46 -t_srs EPSG:32616 or gdaltransform -s_srs EPSG:4326 -t_srs EPSG:32616 -122 46 doesn't work because the coordinates are read by the program itself from its stdin and not the shell (I have to press Enter before entering the coordinates, look at Multiple line shell commands using Python commands module)

$ gdaltransform -s_srs EPSG:4326 -t_srs EPSG:32616 -122 46
Usage: gdaltransform [--help-general]
[-i] [-s_srs srs_def] [-t_srs srs_def] [-to "NAME=VALUE"]
[-order n] [-tps] [-rpc] [-geoloc] 
[-gcp pixel line easting northing [elevation]]*
[srcfile [dstfile]]
FAILURE: Unknown option name '-122'

and

$ gdaltransform -s_srs EPSG:4326 -t_srs EPSG:32616
-122 46
-2193988.77788563 5724599.39928024 0

or

$ echo -122 46 | gdaltransform -s_srs EPSG:4326 -t_srs EPSG:32616
-2193988.77788563 5724599.39928024 0

Therefore, in my case

import subprocess
command = ["gdaltransform", "-s_srs", "EPSG:4326", "-t_srs", "EPSG:32616", "-122", "46"]
output = subprocess.check_output(command)

raise an error

But

cmd = 'echo {} {} | gdaltransform -s_srs EPSG:4326 -t_srs EPSG:{}'.format(-122, 46, 32616)
print subprocess.check_output(cmd, shell=True),
-2193988.77788563 5724599.39928024 0

or

p = subprocess.Popen(['gdaltransform', '-s_srs', 'epsg:4326', '-t_srs', 'epsg:32616'],stdin=subprocess.PIPE,stdout=subprocess.PIPE)
print p.communicate("%s %s" % (-122, 46))[0]
-2193988.77788563 5724599.39928024 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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