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)
DOQQImport = arcpy.mapping.ListLayers(mxd, "DOQQImport", df)
import arcpy mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd)[0] DOQQImport = arcpy.mapping.ListLayers(mxd, "DOQQImport", df) 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)
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) arcpy.AddMessage("{0} {1}".format(myPt.X, myPt.Y)) myPtGeometry = arcpy.PointGeometry(myPt) arcpy.AddMessage("{0} {1}".format(myPtGeometry.firstPoint.X, myPtGeometry.firstPoint.Y)) arcpy.SelectLayerByLocation_management ("DOQQImport", "COMPLETELY_CONTAINS", myPtGeometry)
I updated my code to include printing out x and y values of myPt and myPtGeometry with an AddMessage (I'm running the script from a toolbox). Both are correct, matching what I read on the screen with my cursor at the center of my data frame.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) arcpy.AddMessage("{0} {1}".format(myPt.X, myPt.Y)) myPtGeometry = arcpy.PointGeometry(myPt) arcpy.AddMessage("{0} {1}".format(myPtGeometry.firstPoint.X, myPtGeometry.firstPoint.Y)) arcpy.SelectLayerByLocation_management ("DOQQImport", "COMPLETELY_CONTAINS", myPtGeometry)
The layer I'm trying this with is in a group. I tried moving it outside the group, but still nothing gets selected.
import arcpy # need a 'handle' on all 3: 'current' mxd, 1st data frame, DOQQ layer mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd)[0] DOQQImport = arcpy.mapping.ListLayers(mxd, "DOQQImport", df) # your geometry object to select by looks fine myPt = arcpy.Point((df.extent.XMax+df.extent.XMin)/2, (df.extent.YMax+df.extent.YMin)/2) arcpy.AddMessage("{0} {1}".format(myPt.X, myPt.Y)) myPtGeometry = arcpy.PointGeometry(myPt) arcpy.AddMessage("{0} {1}".format(myPtGeometry.firstPoint.X, myPtGeometry.firstPoint.Y)) # execute using the fetched layer and defined geom: arcpy.SelectLayerByLocation_management (DOQQImport, "INTERSECT", myPtGeometry) arcpy.RefreshActiveView() arcpy.AddMessage("\nIs this your intended polygon selection result?") arcpy.AddMessage("(If there are overlapping polys at this location, the result should be a multiple selection.)") del mxd
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) ##arcpy.AddMessage("{0} {1}".format(myPt.X, myPt.Y)) ##myPtGeometry = arcpy.PointGeometry(myPt) ##arcpy.AddMessage("{0} {1}".format(myPtGeometry.firstPoint.X, myPtGeometry.firstPoint.Y)) ##arcpy.SelectLayerByLocation_management ("DOQQImport", "COMPLETELY_CONTAINS", myPtGeometry) arcpy.SelectLayerByLocation_management ("DOQQImport", "COMPLETELY_CONTAINS", "somePtLayer")
import arcpy mxd = arcpy.mapping.MapDocument('CURRENT') df = arcpy.mapping.ListDataFrames(mxd)[0] parcels = arcpy.mapping.ListLayers(mxd, '*parcel*', df)[0] sr_dataframe = df.spatialReference sr_featureclass = arcpy.Describe(parcels).spatialReference arcpy.env.outputCoordinateSystem = sr_dataframe myPt = arcpy.Point((df.extent.XMax + df.extent.XMin)/2.0, (df.extent.YMax + df.extent.YMin)/2.0) myPtGeometry = arcpy.PointGeometry(myPt) arcpy.AddMessage("\nThe fetched parcel layer from the TOC, {0}".format(parcels.name)) arcpy.AddMessage("\nThe data frame spatial reference name: {0}".format(sr_dataframe.name)) arcpy.AddMessage("\nThe feature class (to select by) spatial reference name: {0}".format(sr_featureclass.name)) arcpy.AddMessage("\nThe data frame centerpoint (myPt) - X: {0} Y: {1}".format(myPt.X, myPt.Y)) arcpy.AddMessage("\nThe geometry (myPtGeometry) - {0} {1}".format(myPtGeometry.firstPoint.X, myPtGeometry.firstPoint.Y)) arcpy.AddMessage("\n...now proceeding with the selection, please wait...\n") arcpy.SelectLayerByLocation_management(parcels, 'INTERSECT', myPtGeometry) arcpy.RefreshActiveView() del mxd