Select features using geometry objects

9856
21
06-07-2013 12:44 PM
ScottOatley
New Contributor II
Greetings,

I'm writing some arcpy to select polygons that contain the centroid of my current data frame extent. Here's my code:
import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
myPt = arcpy.Point((df.extent.XMax+df.extent.XMin)/2, (df.extent.YMax+df.extent.YMin)/2)
myPtGeometry = arcpy.PointGeometry(myPt)

arcpy.SelectLayerByLocation_management ("DOQQImport", "COMPLETELY_CONTAINS", myPtGeometry)


The code runs without error, and the coordinates of myPt/myPtGeometry are correct. Yet, nothing gets selected.

Am I missing some coding step, or can you not use geometry this way?

Thanks,
Scott
Tags (2)
0 Kudos
21 Replies
ScottOatley
New Contributor II
OK,

The following code works ONLY IF coordinate system of my data frame and the target selection layer are the same. Note there is no explicit mention of spatial reference for env.outputCoordinateSystem or the instantiation of the PointGeometry; it's handled implicitly.

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]

myPt = arcpy.Point((df.extent.XMax + df.extent.XMin)/2.0, (df.extent.YMax + df.extent.YMin)/2.0)
myPtGeometry = arcpy.PointGeometry(myPt)

arcpy.SelectLayerByLocation_management('DOQQImport_prj', 'INTERSECT', myPtGeometry)

del df, mxd


The following code ALMOST works when the coordinate system of my data frame and the target selection layer are different.

If I zoom to layer on the target layer, it will select the correct polygons. If I THEN change the scale of the data frame, it still works. But if I pan to a different location it doesn't work.

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]

arcpy.env.outputCoordinateSystem = df.spatialReference

myPt = arcpy.Point((df.extent.XMax + df.extent.XMin)/2.0, (df.extent.YMax + df.extent.YMin)/2.0)
myPtGeometry = arcpy.PointGeometry(myPt, df.spatialReference)

arcpy.SelectLayerByLocation_management('DOQQImport_prj', 'INTERSECT', myPtGeometry)

del df, mxd


Scott
0 Kudos
DavidAllen
Occasional Contributor
Incidentally, my intention is to run this from an add-in toolbar which I tried today. It doesn't run there either.

Scott


Scott - test this both in data view and in layout view. My guess is that it'll work in one and not the other. Then look at the coordinates returned in layout view and see if they are page measurements or projected coordinates.

I like to add print statements periodically in my code to print out things like retrieved coordinates and messages. They'll appear in the Python window. When you know what the measurement system of the returned coordinates is, I think you'll see the answer.
0 Kudos