1

I'm trying to figure out GDAL equivalencies to the following arcpy functions:

  • arcpy.env.overwriteOutput()
  • arcpy.MakeFeatureLayer_management()
  • arcpy.SpatialJoin_analysis()
  • arcpy.da.SearchCursor()
  • arcpy.Delete_management()
  • arcpy.AddMessage()
  • arcpy.AddError()

I did not see anything in the Python Script examples thread to help me translate the arcpy commands over to GDAL. If I'm missing something obvious to an experienced GDAL / arcpy user would you be kind enough to point it out to me?

George
  • 521
  • 4
  • 15
  • 1
    Not every geoprocessing tool or Python function has a third-party equivalent, or there could be multiple options. – Vince Apr 06 '16 at 20:37
  • https://pcjericks.github.io/py-gdalogr-cookbook/index.html – klewis Apr 06 '16 at 21:11
  • 3
    There isn't a one-to-one mapping that will allow you to just do a find and replace. Some of these are simply file operations (e.g. delete, overwrite), some use strange concepts that kind of don't exist elsewhere (search cursor), some are routine DB operations (spatial join), some aren't necessary outside of Esri (make feature layer), add message is a log message, and add error is a Python exception. You should put energy into learning Python generally. – alphabetasoup Apr 06 '16 at 21:21
  • @Vince, I've never done anything with GIS so all of this is new to me. – George Apr 11 '16 at 16:58
  • @klewis, Thanks for the cookbook link. Do you have a site / link you'd recommend for GIS noobs? – George Apr 11 '16 at 16:59
  • @Richard Law, understood. My problem is that I'm also trying to wrap my head around GIS vocabulary / concepts while trying to find alternatives to things I do not quite understand at the moment. – George Apr 11 '16 at 17:01
  • ESRI has a bunch of free Videos to learn about GIS. http://video.arcgis.com – klewis Apr 11 '16 at 18:45
  • I've closed this as too broad because it is in effect asking multiple questions i.e. the same question for at least seven functions. I suspect some of these have already been answered in in existing Q&As here, and I think the remainder should be asked as separate questions, if they are important to know. – PolyGeo Apr 16 '16 at 02:39

2 Answers2

5

Just my $0.02, there may be better ways.

arcpy.env.overwriteOutput()

Handle this yourself, ogr.Driver.DeleteDataSource() and ogr.DataSource.DeleteLayer() can handle this. You can use OGR_TRUNCATE, but this appears to be at the layer level

arcpy.MakeFeatureLayer_management()

If you have writable ogr.DataSource, then ogr.DataSource.CreateLayer(...)

arcpy.SpatialJoin_analysis()

I guess it depends on what exactly you are trying to do here. It looks like you can pretty much handle any situation in SpatialJoin with sqlite dialect (with spatialite support) and ogr.DataSource.ExecuteSQL(...). See the docs on OGR SQL and the SQLite Dialect. All of spatialite's functions are available with sqlite and spatialite support enabled in the build.

arcpy.da.SearchCursor()

The cursor in arcpy, if I remember correctly, is a generic cursor for iterating over a feature set. The equivalent in OGR is to iterate over features in a layer. To subset the features in a layer, use the ExecuteSQL(...) function above and then iterate:

...
lyr = ds.ExecuteSQL("SELECT * FROM myLayer WHERE id < 10")
for feat in lyr:
    print(feat.GetFID())
...

or similar. OGR doesn't care if the underlying data is a db or a shapefile (that is the point of GDAL/OGR).

arcpy.Delete_management()

ogr.DataSource.DeleteLayer(...) for a layer, ogr.Driver.DeleteDataSource(...) for an entire datasource.

arcpy.AddMessage()

Just use print()

arcpy.AddError()

I can't remember what that does, sorry.

No code tested, and help(ogr.Class.Func) should give you more info. @gene is right on the API not being what you'd expect. It closely follows the C API of GDAL/OGR, which is documented pretty well.

  • os.path.exists and os.remove will not replace arcpy.env.overwriteOutput. Many (most?) spatial datasets are not represented on the file system by a single file. The easiest example: a shapefile is not just filename.shp, but a group of files with the same name. The workaround that I've used in the past is to create a temporary directory, move the shapefile to it, do your business, then move it back. You can easily put these steps into a decorator. Geodatabases are a different story altogether. – Tom Apr 06 '16 at 22:00
  • ogr.Driver.DeleteDataSource() will work, updated above. –  Apr 07 '16 at 18:58
2

I think that the direct equivalencies are impossible unless you have a person who knows ArcPy and GDAL.

Moreover the GDAL/OGR Python bindings (osgeo (GDAL/OGR) are not very "Pythonic" and difficult. It exists other easier alternatives ( Fiona, Pyshp (shapefile), GeoPandas,shapely, rasterio, ...). The last module is compared to ArcPy in Comparing Map Algebra Implementations for Python: Rasterio and ArcPy (pdf) for example.

You know ArcPy and I know Python and not ArcPy. The result is that I do not know what these commands do.

For example:

  • I suppose that ArcPy use cursors (arcpy.da.SearchCursor()) as the SQL database cursors to process the results returned by database queries. They are used by the Python databases modules (x_Oracle (Oracle), Psycopg2 (PostgreSQL/PostGIS), MySQLdb (MySQL), sqlite3 (SQLite)...). But none of the Python geospatial solutions use cursors (they use standard lists, dictionaries or numpy arrays). Therefore arcpy.da.SearchCursor() has no equivalent unless the command is used to search in a list, a tuple or a dictionary (result of the cursor)
  • I suppose also that arcpy.SpatialJoin_analysis() computes spatial joins. There are many examples in GIS SE that show alternatives as in More Efficient Spatial join in Python without QGIS, ArcGIS, PostGIS, etc ( with Fiona and Shapely, with GeoPandas, with Rtree)

The best way is to search for equivalencies in GIS SE, the Python GDAL/OGR Cookbook, and others

gene
  • 54,868
  • 3
  • 110
  • 187
  • Many thanks! I'm trying to wrap my head around the source code I've been asked to consider doing via GDAL. I understand your point on SearchCursor() and how context of what I'm achieving will dictate an "equivalence" as there are no actual equivalencies. I will have to get smarter on GIS-type stuff before I think I can be comfortable to proceed with whatever I may need to do. – George Apr 11 '16 at 17:10