How to run OGR in ArcGIS Scripts?

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 "", line 26, in _ogr = swig_import_helper() File "", 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 :


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', '' and I get the same error. If I use this method in standalone script it works.

I've gotten this working on my 10.1 system, at least somewhat (I can runimport ogrfrom 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)ArcGISDesktop10.1Python27ArcGIS10.1, not C:Python27ArcGIS10.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:

  • GDAL Core
  • FileGDB Plugin (You probably want this)
  • Python 2.7 Bindings

I then setup the following environment variables:

  • PATH: Added;C:Program Files (x86)GDALto the end. This resolved the DLL load error for me.
  • GDAL_DRIVER_PATH: set toC:Program Files (x86)GDALgdalplugins(to get the FileGDB stuff to work)
  • GDAL_DATA: set toC:Program Files (x86)GDALgdal-data(to get reprojection and other projection related stuff working)

Watch the video: Automate Backups of an ArcGIS Online Hosted Feature Layer using Python