The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

Ushant Suman (23) [Avatar] Offline
#1
Hi chris,
First of all I congratulate you for writing and sharing your real time experience in the form of this great book.I have just a small query I am trying to design a web interface which accepts the zipped shape files(.shp,.dbf,.shx and .prj) as a zip file and the user should be able to see all the attribute data is gridular format in a webpage.Iam using php and passing the file path as command line arguments to my python script.My python code is like this :
import os
import sys
from osgeo import ogr
import ospybook as pb
import zipfile
f1=sys.argv[1]--command line from user input
f2=sys.argv[2]
f3=sys.argv[3]
f4=f2.replace('\\','\\\\')
f6=f1.replace('\\','\\\\')
print f6
with zipfile.ZipFile(f6, "r") as z:
z.extractall(f4)
f7=os.path.splitext(f3)[0]
f5=ogr.Open(f4,0)

print f7
sql = '''SELECT * from f7'''
lyr = f5.ExecuteSQL(sql)
pb.print_attributes(lyr,1)
I get the error on f7 is there any way for replacing f7 with the user uploaded file name so that sql query error is removed.

I know the question might go beyond the scope of book but please help me.

Regards,
Ushant Suman
Dan Hamill (8) [Avatar] Offline
#2
What are the command line arguments f1,f2,f3?
Ushant Suman (23) [Avatar] Offline
#3
Yes f1 is the full path of file such c:\users\somefile.zip
f2 s the c:\users\somefile
f3 is the name of file somefile.zip

All three are passed as command line from php and executed as
exec("C:\Python27\python.exe pythonfilename.py $n", $output);

and all the output needs to be send in the php array $output
Dan Hamill (8) [Avatar] Offline
#4
Yes f1 is the full path of file such c:\users\somefile.zip
f2 s the c:\users\somefile
f3 is the name of file somefile.zip

All three are passed as command line from php and executed as
exec("C:\Python27\python.exe pythonfilename.py $n", $output);

and all the output needs to be send in the php array $output


Ok. First off, I would recommend passing file paths with '\\' to your python script. For example
c:\\users\\somefile.zip 
I don't think there is a way to tell python to interpret a command line arguement as a raw string. I think that would take care of the f4 and f6 variables

Second, I am not sure you need to pass multiple command line arguments to your python script. I think you can parse out everything using split()

f2 = f1.split('.')[0] #root directory
f3 = f1.split('\\')[-1] #file name


Finally, are you assuming the shapefile layer name is the same as the shapefile name? I would suggest generalizing it:

import os 
import sys 
from osgeo import ogr 
import ospybook as pb 
import zipfile 

f1 = "C:\\workspace\\Merged_SS\\window_analysis\\shapefiles\\shapefiles.zip"

f2 = f1.split('.')[0] #root directory
f3 = f1.split('\\')[-1] #file name

with zipfile.ZipFile(f1, "r") as z: 
    z.extractall(f2) 
    
ds=ogr.Open(f2 +os.sep + f3.split('.')[0]+'.shp',0) 
lyr = ds.GetLayer(0)
sql = '''SELECT * from %s''' %(lyr.GetName(),)
query = ds.ExecuteSQL(sql)
pb.print_attributes(query)
del ds



That script assumes the shapefile has the same name as the input zip file. If it is different, you could use glob.glob to find the shapefile in the extracted shapefile name. Also you might want to delete the extraced shapefiles once your are done with them.
Ushant Suman (23) [Avatar] Offline
#5
Thanks you very much Dan for your guidance its working now I have to dig out some way for grid display of attributes in UI.

Regards,
Ushant Suman
chris.garrard (26) [Avatar] Offline
#6
Thanks, Dan, for helping Ushant out before I even saw the question!