0

I'm trying to write a Python function that transforms a given coordinate system to another using gdal. Problem is that I'm trying to execute the command as one string, but in shell, I have to press enter before entering the coordinates.

x = 1815421
y = 557301

ret = []

tmp = commands.getoutput( 'gdaltransform -s_srs \'+proj=lcc +lat_1=34.03333333333333 
+lat_2=35.46666666666667 +lat_0=33.5 +lon_0=-118 +x_0=2000000 +y_0=500000 +ellps=GRS80 
+units=m +no_defs\' -t_srs epsg:4326 \n' + str(x) + ' ' + str(y) )

I tried it using '\n', but that doesn't work.

1
  • Is there any reason why you dont call osr.CoordinateTransformation() from within Python? Commented Apr 9, 2013 at 6:30

2 Answers 2

3

My guess is that you run gdaltransform by pressing Enter and the coordinates are read by the program itself from its stdin, not the shell:

from subprocess import Popen, PIPE

p = Popen(['gdaltransform', '-s_srs', ('+proj=lcc ' 
    '+lat_1=34.03333333333333 ' 
    '+lat_2=35.46666666666667 '
    '+lat_0=33.5 '
    '+lon_0=-118 +x_0=2000000 +y_0=500000 +ellps=GRS80 '
    '+units=m +no_defs'), '-t_srs', 'epsg:4326'],
    stdin=PIPE, stdout=PIPE, universal_newlines=True) # run the program
output = p.communicate("%s %s\n" % (x, y))[0] # pass coordinates
Sign up to request clarification or add additional context in comments.

Comments

1
from subprocess import *

c = 'command 1 && command 2 && command 3'
# for instance: c = 'dir && cd C:\\ && dir'

handle = Popen(c, stdin=PIPE, stderr=PIPE, stdout=PIPE, shell=True)
print handle.stdout.read()
handle.flush()

If i'm not mistaken, the commands will be executed over a "session" and thus keeping whatever niformation you need in between the commands.

More correctly, using shell=True (from what i've been tought) is that it's supposed to be used if given a string of commands rather than a list. If you'd like to use a list suggestions are to do as follows:

import shlex
c = shlex.split("program -w ith -a 'quoted argument'")

handle = Popen(c, stdout=PIPE, stderr=PIPE, stdin=PIPE)
print handle.stdout.read()

And then catch the output, Or you could work with a open stream and use handle.stdin.write() but it's a bit tricky.

Unless you only want to execute, read and die, .communicate() is perfect, or just .check_output(<cmd>)

Good information n how Popen works can be found here (altho different topic): python subprocess stdin.write a string error 22 invalid argument




Solution

Anyway, this should work (you have to redirect STDIN and STDOUT):

from subprocess import *

c = 'gdaltransform -s_srs \'+proj=lcc +lat_1=34.03333333333333 +lat_2=35.46666666666667 +lat_0=33.5 +lon_0=-118 +x_0=2000000 +y_0=500000 +ellps=GRS80 +units=m +no_defs\' -t_srs epsg:4326 \n' + str(x) + ' ' + str(y) + '\n'

handle = Popen(c, stdin=PIPE, stderr=PIPE, stdout=PIPE, shell=True)
print handle.stdout.read()
handle.flush()

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.