Wednesday 25 January 2017

Selecting ArcSDE polygon by point in ArcGIS Desktop using ArcPy?


I keep thinking that I must be missing something, but there does not seem to be a tool in ArcGIS 10 to select features (in particular polygons) from a layer at a point (X,Y) location via ArcPy. The parameters for such a tool would just be a layer name and an XY location.



At the moment I workaround this by creating a point featureclass containing the point and performing a SelectLayerByLocation on it. However, when the polygon feature class is in Oracle (accessed via ArcSDE 9.x) and contains 3.5 million polygons the time taken to make the selection can be more than 5 mins when I think a second or two (with less code) would be more appropriate. The feature class has a spatial index and I've tried using arcpy.env.extent (which SelectLayerByLocation appears to ignore) to restrict the geographic area accessed but the performance remains very poor.


Is there a quicker way to do this using ArcGIS Desktop 10 and ArcPy?



Answer



Another approach to this would be to use the Spatial Join tool. Use the point as your input feature layer as above and the polygon layer as your identity features.
Unlike SelectLayerByLocation, SpatialJoin does honor the extent environment.


targetlayer = layername
joinlayer=arcpy.PointGeometry(arcpy.Point(x, y))
fieldmappings = arcpy.FieldMappings()
fieldmappings.addTable(targetlayer)
arcpy.SpatialJoin_analysis(targetlayer, joinlayer, outputlayer, "JOIN_ONE_TO_MANY", "KEEP_COMMON", fieldmappings)


JOIN_ONE_TO_MANY might seem counter-intuitive, but since you only have one join feature, the main function of this option is to turn off aggregationand merge rules. KEEP_COMMON will make sure that your output is restricted only to the polygon that intersects your point. The Fieldmappings will restrict the output attributes to the shape and attributes of the polygon layer only; the default would include the point layer's attributes too.


The rest of the defaults will work fine, so you can leave off the remaining arguments.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...