Tuesday, 14 June 2016

arcpy - How to run OGR in ArcGIS Scripts?


When I run my script outside ArcGIS it works because during execution script uses a GDAL and OGR modules from osgeo. When I use script as a tool in ArcGIS it does not work. In PySripter I can import all modules and it work. In Python Window in ArcGIS I cannot import because I get this error:




Parsing error SyntaxError: invalid syntax (line 2) import ogr Runtime error Traceback (most recent call last): File "", line 1, in File "C:\Python27\ArcGIS10.1\lib\site-packages\osgeo\ogr.py", line 26, in _ogr = swig_import_helper() File "C:\Python27\ArcGIS10.1\lib\site-packages\osgeo\ogr.py", line 22, in swig_import_helper _mod = imp.load_module('_ogr', fp, pathname, description) ImportError: DLL load failed:



In the case when I import gdal in ArcGIS Python Window I do not get this error. How to force arcgis to import ogr and use it in ModelBuilder or Script?? I check a lot of solutions includin reinstallation, changing and adding environment variables. I checked :



  1. http://pythongisandstuff.wordpress.com/2011/07/07/installing-gdal-and-ogr-for-python-on-windows/

  2. http://www.wildsong.biz/index.php/Running_GDAL_scripts_in_ESRI_Model_Builder


and I still do not have a good solution. Any idea?


UPDATE: I also try use direct importing in my script: import imp ogr = imp.load_source('ogr', 'c:\Python27\Lib\site-packages\osgeo\ogr.py' and I get the same error. If I use this method in standalone script it works.



Answer




I've gotten this working on my 10.1 system, at least somewhat (I can run import ogr from the ArcGIS python window and it appears to be working).


First I attempted to get my system as clean as possible. I uninstalled previous versions of GDAL (from OSGeo4W). I do still have PostgreSQL+PostGIS which has it's own version of GDAL, but I can't uninstall that (I need it).


Secondly, it looks like ArcGIS's python is located at a different folder on my system (it's in C:\Program Files (x86)\ArcGIS\Desktop10.1\Python27\ArcGIS10.1, not C:\Python27\ArcGIS10.1). This is likely due to my 10.1 environment being a fresh install vs. an upgrade or something.


I used the GDAL installers from this page. Specifically:



I then setup the following environment variables:



  • PATH: Added ;C:\Program Files (x86)\GDAL to the end. This resolved the DLL load error for me.

  • GDAL_DRIVER_PATH: set to C:\Program Files (x86)\GDAL\gdalplugins (to get the FileGDB stuff to work)

  • GDAL_DATA: set to C:\Program Files (x86)\GDAL\gdal-data (to get reprojection and other projection related stuff working)



OGR in ArcGIS 10.1


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...