Calling Python ogr2ogr from PHP doesn't work as expected?

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I have a HTML page that takes and uploaded zip file and then calls a PHP page as a form action. That PHP then calls two Python scripts in my Apache cgi-bin. The first Python script runs with no issue and simply unzips the uploaded file within a sub directory. The second Python script includes a subprocess call to a command line tool to perform some action on the unzipped files. However, I am finding that using the exec() in PHP for the second Python script with the subprocess does not work. If I run the Python script from the command line it works fine. But, if I call it from the PHP exec function it does nothing.

The snippet of PHP:

$tmp1 = exec("C:\\ms4w\\Python\\python C:\\ms4w\\Apache\\cgi-bin\\unzip_shp.py $without_extension");
echo $tmp1;
echo "
"
; $tmp2 = exec("C:\\ms4w\\Python\\python C:\\ms4w\\Apache\\cgi-bin\\convert_to_geojson.py $without_extension", $output); echo $output

The snippet of Python:

command = "C:\\ms4w\\tools\\gdal-ogr\\ogr2ogr.exe -f \"GeoJSON\" "+sys.argv[1]+".geojson "+sys.argv[1]+".shp" #f="https://forum.tuteehub.com/tag/os">os.system(command)
subprocess.check_call(command)
f="https://forum.tuteehub.com/tag/print">print(command)

The f="https://forum.tuteehub.com/tag/print">print command in the Python f="https://forum.tuteehub.com/tag/returns">returns this to the PHP:

C:\ms4w\tools\gdal-ogr\ogr2ogr.exe -f "GeoJSON" C:\ms4w\Apache\htdocs\uploads\parks_escambia.geojson C:\ms4w\Apache\htdocs\uploads\parks_escambia.shp

Also, I am running this all on a Windows OS as a localhf="https://forum.tuteehub.com/tag/os">ost development server. Using Python version 2.7.

profilepic.png
manpreet 2 years ago

You may need to set some of GDAL's environmental variables when you run subprocess, such as GDAL_DATA.

You can see which are being set in the MS4W setenv.bat script.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.