1

Is there an open source (gdal, pyshp, shapely etc.) programmatic option to the select command in ArcGIS. I want to select polygons within a shapefile based on an attribute value. This command is here:

Select (Analysis)

Extracts features from an input feature class or input feature layer, typically using a select or Structured Query Language (SQL) expression and stores them in an output feature class.

I want a python based solution.

1
  • 1
    GDAL command line example is ogr2ogr -f "ESRI Shapefile" -sql "SELECT * from input_data WHERE attribute='value'" output_data.shp input_data.shp. You can do the same with GDAL Python bindings.
    – user30184
    Mar 13, 2016 at 12:12

1 Answer 1

4

Yes, there is. This functionality is essentially a SQL SELECT ... WHERE ... clause. There are lots of ways to achieve this, essentially depending on how performant you want it to be, what you want to install, and what you want to learn about. It is certainly not unique to Esri.

Personally, I'd take the shapefile, import it into PostgreSQL/PostGIS (and be very loath to go back), and then use SQLAlchemy/GeoAlchemy if I wanted to work in Python, but that's probably overkill. Look at SQLite/SpatiaLite. Both of these options will be fast with appropriate attribute indexing, and you can also do selection based on spatial attributes.

If you just want to work with existing shapefiles and not worry about a database, there is the pyshp that offers read and write support for shapefiles in Python. There are various tutorials online for pyshp. You will essentially want to iterate through features and check if each feature's attributes match a Boolean condition (e.g. population > 5000).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.