Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am unable to call shp2pgsql using python as I have to pass various options like (-s -a -i) and I am not getting the required output ..

Method I am using is

p1 = subprocess.Popen(["cmd", "/C", "shp2pgsql","-a","-s 4326","roads.shp" "roads", ">","roads.sql"],stdout=subprocess.PIPE)

>>>p1.communicate()[0]

as my intension is to add the features of the shapefile into the already existing table in the database

I am done with the environment settings.. roads.shp is the name of the shapefile roads is the table name and roads.sql is the sql file name..

share|improve this question
2  
Please consider accepting some answers to your previous questions. With a 0% accept rate, people might not be willing to put effort into answering your questions anymore. –  underdark Oct 13 '11 at 11:47
add comment

2 Answers

up vote 3 down vote accepted

This seems a bit convoluted. Why not use os.system instead? Just write out / manipulate the command as a string and feed it to os.system.

share|improve this answer
add comment

I suggest you can use the following codes:

p1 = subprocess.Popen(["cmd", "/C", "shp2pgsql","-a","-s 4326","roads.shp", "roads"], stdout=subprocess.PIPE)
stdout, stderr = p1.communicate()
with open('roads.sql', 'w') as f:
    f.writelines(stdout)

Another alternative method (if you want to import this into postgresql database):

p1 = subprocess.Popen(["cmd", "/C", "shp2pgsql","-a","-s 4326","roads.shp", "roads"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p2 = subprocess.Popen(['psql',  '-d db'], stdin=p1.stdout, 
    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p1.stdout.close()
stdout, stderr= p2.communicate()

References:

  1. http://docs.python.org/2/library/subprocess.html#subprocess.Popen.communicate
  2. http://docs.python.org/2/library/subprocess.html#replacing-shell-pipeline
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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