When using ArcPy with the ArcGIS 10.x architecture there is a simple coding pattern that I find I use frequently:
import arcpymxd = arcpy.mapping.MapDocument("CURRENT")df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]lyr = arcpy.mapping.ListLayers(mxd,"ne_10m_admin_0_countries",df)[0]lyr.definitionQuery = '"ADMIN" = ' + "'Chile'"df.extent = lyr.getSelectedExtent()arcpy.RefreshActiveView()To see it in action:
import arcpyaprx = arcpy.mp.ArcGISProject("CURRENT")mapx = aprx.listMaps("Map")[0]lyr = mapx.listLayers("ne_10m_admin_0_countries")[0]lyr.definitionQuery = '"ADMIN" = ' + "'Chile'"Is there a simple way to choose and zoom to features using an SQL query in ArcPy with ArcGIS Pro?
As a workaround I've been investigating how to perhaps incorporate Layout and MapFrame classes into my coding pattern and, although the latter has a zoomToAllLayers method that looks more hopeful, I have not yet been able to find a way to do this.
أكثر...
import arcpymxd = arcpy.mapping.MapDocument("CURRENT")df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]lyr = arcpy.mapping.ListLayers(mxd,"ne_10m_admin_0_countries",df)[0]lyr.definitionQuery = '"ADMIN" = ' + "'Chile'"df.extent = lyr.getSelectedExtent()arcpy.RefreshActiveView()To see it in action:
- Start ArcMap with a Blank map
- Add a layer using a shapefile like ne_10m_admin_0_countries.shp from Natural Earth
- Copy/paste the code above into the Python window and you should see the country of Chile zoomed to
- Start ArcGIS Pro
- Choose Map.aptx to open a map
- Add a layer using a shapefile like ne_10m_admin_0_countries.shp from Natural Earth
- Copy/paste code like below into the Python pane
import arcpyaprx = arcpy.mp.ArcGISProject("CURRENT")mapx = aprx.listMaps("Map")[0]lyr = mapx.listLayers("ne_10m_admin_0_countries")[0]lyr.definitionQuery = '"ADMIN" = ' + "'Chile'"Is there a simple way to choose and zoom to features using an SQL query in ArcPy with ArcGIS Pro?
As a workaround I've been investigating how to perhaps incorporate Layout and MapFrame classes into my coding pattern and, although the latter has a zoomToAllLayers method that looks more hopeful, I have not yet been able to find a way to do this.
أكثر...