Select to view content in your preferred language

SelectLayerByLocation with Python Cursor

761
5
10-26-2010 09:47 AM
CharltonLewis
Deactivated User
I'm really interested if anyone has done this successfully.

In Python 2.5, I need to iterate through a point feature layer (searchcursor) and select the underlying record in a polygon feature layer. This just isn't working for me. From what I've come across online, "<cursor>.shape" should be used as the select layer parameter. There could be multiple points in one polygon, so I need to identify and create a polygon for each point.

It seems straightforward, but I'm not getting any selection. If I print the value of the cursor.shape, this is what I get:

<geoprocessing describe geometry object object at 0x009DA5D8>


Heres a sample of the code:

while pointCurRow:
print pointCurRow.X_COORD
print pointCurRow.shape
print
rows = gp.selectLayerByLocation(polyFeatLayer, "INTERSECT", pointCurRow.shape)
row = rows.Next()
print "row count = " + str(row.GetCount)


The error that results is:

Traceback (most recent call last):
File "C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py" , line 310, in RunScript
exec codeObject in __main__.__dict__
File "C:\ArcGIS\Python Scripts\test_program.py", line 61, in <module>
row = rows.Next()
AttributeError: 'str' object has no attribute 'Next'


Thanks in advance for any help.
0 Kudos
5 Replies
StevePeaslee
Deactivated User
Charlton,

I think you've got a couple of problems here. The error message indicates to me that your cursor object was not opened correctly. You don't show that part of the code, so I don't know for sure.

According to the documentation The gp.SelectLayerByLocation requires a 'Feature Layer' datatype for the 'select features' parameter. I don't think you can use a shape from a cursor, though that would be really nice.

One option would be to set up a loop using the number of points (GetCount).

Within the loop, create a point feature layer using a query against the point featureclass on FID or OBJECTID. The point featurelayer containing a single point could then be used in your SelectLayerByLocation. Delete the featurelayer immediately after the SelectLayerByLocation and then do whatever you want with the selected polygon(s) in the other featurelayer.

GetCount works on a featurelayer or featureclass, not on a cursor record.

It's not clear to me what your ultimate goal is, but there are other geoprocessing options that would allow you to select all polygons that intersect with the points in one operation rather than iterating through each point.


-Steve
0 Kudos
CharltonLewis
Deactivated User
Steve, thanks much for your reply. While searching for the correct way to do this, I came across a reference to "SelectLayerByLocation" with the "JOIN_ONE_TO_MANY" option.

This does exactly what I need which is to select and save multiple polygons for multiple points that fall within a polygon feature.

Thanks again.
0 Kudos
ChrisSnyder
Honored Contributor
Wouldn't the standard "Identity" tool do what you need?
0 Kudos
NiklasNorrthon
Frequent Contributor
Wouldn't the standard "Identity" tool do what you need?


Identity requires ArcInfo, which unfortunatly isn't part of every installation.

Fortunatly a mix of python, patience and time can be used to create tools similar to many of those requiring the ArcInfo license.
0 Kudos
CharltonLewis
Deactivated User
I do have an ArcInfo license. There may be more than one way to accomplish this, but my current solution is getting it done for me. I haven't looked at the Identity option.

I do appreciate the responses.

Charlton
0 Kudos