Find a polygon for a single point in Python script

1834
6
05-30-2018 07:11 AM
MarcWouters
New Contributor III

Hi all,

I am writing a Python script in which I build a geodatabase, based on several inputs.

At one point, I am looping over a number of points (X,Y) which need to be treated individually using several criteria. 
One criterium is the polygon in which this point is located. This polygon comes from a feature table with a number of other fields.

There are several tools (Intersect, SelectByLocation, SpatialJoin, ....) which might help doing this, but they all are based on input and output tables.

How can I extract this one polygon, and linked fields, from that table, without having to make temporary input and output tables ?

In an ideal world, it could look like this :

for i in range (0, numberOfPoints):

      inputPoint = arcpy.Point(X, Y)

      findFeature(tableWithPolygons, inputPoint, outputFeature)

      myValue = outputFeature.value

      # based on this value, I can do further processing

Thanks for pointing me in the right direction

0 Kudos
6 Replies
JoshuaBixby
MVP Esteemed Contributor

Does some part of this involve the ArcGIS API for Python or are you just using ArcPy, or aren't you sure?  I am sharing with Python‌ since this seems to be ArcPy related.

0 Kudos
MarcWouters
New Contributor III

I don't clearly understand the difference between ArcPy and ArcGIS API for Python.

Aren't they both using Python functions to get things automated which you can do manually in ArcGIS Pro ?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

With the information provided, I can only provide a rough workflow that may or may not meet your needs:

SR = arcpy.Describe(tableWithPolygons).spatialReference
layerPolygon = arcpy.MakeFeatureLayer_management(tableWithPolygons, "layerPolygon")

for i in range(0, numberofPoints):
    inputPointGeometry = arcpy.PointGeometry(arcpy.Point(X,Y), SR)
    arcpy.SelectLayerByLocation_management(
        layerPolygon,
        "INTERSECT",
        inputPointGeometry,
        "",
        "NEW_SELECTION"
    )
    with arcpy.da.SearchCursor(layerPolygon, "*") as cur:
        for row in cur:
            myValue = # retrieve value(s) 
            # based on this value, I can do further processing

A few comments:

  • Tools like Intersect, Select By Location, Spatial Join work on geometries, so you will have to create a Point Geometry from your Point.
    • To create a Point Geometry, you will have to (or really should) have a spatial reference.  My code above assumes the Point Geometry has the same spatial reference as the polygon layer.
  • If you are going to be repeatedly executing spatial queries against a feature class, it is best to make a feature layer outside of the loop.
  • The code above assumes there may be more than one record returned from the spatial query.  If only one record is returned, at most, the code can be changed up a bit.
0 Kudos
MarcWouters
New Contributor III

Hello Joshua,

Sorry for the late reply (I'm leaving my current position, and have lots of things to wrap up).

Thanks for the code snippet. 

I've implemented your proposal (with a few modifications), and it works OK. 
Unfortunately I cannot avoid temporary layers.

It's quite time consuming (a query for every single point), but I cannot do it with the complete table, because the Intersect tool doesn't keep my features in the same order, and this is something I MUST have.

Thanks again.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

If the polygon data set is already loaded into ArcMap as a layer, you don't need to create another layer, you can just use the already created one.  If the script is run outside of ArcMap, creating a feature layer in memory for processing isn't really an issue because the layer will go away when the script finishes.

0 Kudos