Select to view content in your preferred language

Zoom To Layer (VBA)

1686
7
05-04-2012 09:08 AM
JoelMadero
Emerging Contributor
Hi All,

Let me start by saying I have never coded in GIS programs before, I know basic, some C/C++, and a few others I've dabbled in.

What I'm looking for is how would I go about automating a query on a layer, zoom to the layer, change a number that is on the map (MAP # which is based on a field that is located in the zoomed to layer), then print it. For the last couple years I've had to manually print out hundreds of maps for this project and I think it can be automated relatively easily.


Thanks in advance 🙂

EDIT:
I think doing this one step at a time would be best. So how do I just zoom to a layer, I have this:

Sub Multiple_Polls()
Dim pDoc As IMxDocument

Set pDoc = ThisDocument

Dim pMap As IMap

Set pMap = pDoc.FocusMap



Dim pLayer As IFeatureLayer

Dim pFSel As IFeatureSelection

Set pLayer = pMap.Layer(3)

Set pFSel = pLayer



'Get the selected features

Dim pSelSet As ISelectionSet

Set pSelSet = pFSel.SelectionSet



Dim pEnumGeom As IEnumGeometry

Dim pEnumGeomBind As IEnumGeometryBind



Set pEnumGeom = New EnumFeatureGeometry

Set pEnumGeomBind = pEnumGeom

pEnumGeomBind.BindGeometrySource Nothing, pSelSet



Dim pGeomFactory As IGeometryFactory

Set pGeomFactory = New GeometryEnvironment



Dim pGeom As IGeometry

Set pGeom = pGeomFactory.CreateGeometryFromEnumerator(pEnumGeom)



pDoc.ActiveView.Extent = pGeom.Envelope

pDoc.ActiveView.Refresh
End Sub




But how do I set the correct layer, I changed this line:

Set pLayer = pMap.Layer(3)


to different numbers but all just seem to refresh the map, not actually zoom to layer. Thanks in advance
Tags (2)
0 Kudos
7 Replies
waleedelmarimi
Deactivated User
Hi,
It seems that what would do the trick for you is using the Data Driven Pages:
1- Activate the Data Driven Pages toolbar
2- Click the Data Driven Page setup button on the very left toolbar.
3- Choose the feature layer you'll use
4- Control the zoom (or scale) under the second tab "Data Driven Scale"
[ATTACH=CONFIG]14105[/ATTACH]

5- Use Dynamic text to display map specific info such as Map#, page#

As for the query part, it's not clear to me what you need to do, so I'll wait for an example of what you need to accomplish.

Hope this helps you.
0 Kudos
JoelMadero
Emerging Contributor
Thanks for the reply, I'll give a bit more detail:

What we have are consolidation #'s that are polygons. Currently the manual method of printing is to query a consolidation #, right click on the layer, zoom to the layer (which then zooms to the queried polygon only), then we manually change the Consolidation # that is on the page (just straight text), then we print.

Furthermore each map is actually two consolidation #'s. So I actually query both consolidations, zoom to them, then I print two identical maps just with the consolidation # changed in the upper right corner of the map.
0 Kudos
JeffBarrette
Esri Regular Contributor
Have you considered using arcpy.mapping?  It is a great substitute for many common tasks and can accomplish what you are trying to do.

You can change query definitions on a layer using the layer object:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Layer/00s300000008000000/


For good resources on how to get started, check out:
http://blogs.esri.com/esri/arcgis/2011/09/30/new-resources-available-for-getting-started-with-python...

Jeff
0 Kudos
JoelMadero
Emerging Contributor
Have you considered using arcpy.mapping?  It is a great substitute for many common tasks and can accomplish what you are trying to do.

You can change query definitions on a layer using the layer object:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Layer/00s300000008000000/


For good resources on how to get started, check out:
http://blogs.esri.com/esri/arcgis/2011/09/30/new-resources-available-for-getting-started-with-python...

Jeff


I work for the gov't and because of that it's virtually impossible to convince IT that I need additional components installed and if I do it'll take months of hassle. Does arcpy.mapping require installation of something python related or is it defaulted in the ARCMap install?
0 Kudos
JakeSkinner
Esri Esteemed Contributor
I work for the gov't and because of that it's virtually impossible to convince IT that I need additional components installed and if I do it'll take months of hassle. Does arcpy.mapping require installation of something python related or is it defaulted in the ARCMap install?


Python and the arcpy module are installed by default with ArcGIS Desktop.
0 Kudos
JoelMadero
Emerging Contributor
Python and the arcpy module are installed by default with ArcGIS Desktop.


Thank you much! So where I am now, I have figured out the zoom to feature but I have only figured out zoom to selected, not zoom to queried. I can probably work with this BUT if possible I'd like to at least know if it's possible to zoom to queried. This is the same as right click on layer -> zoom to layer. This automatically zooms to queried layer. Thanks again, making progress 🙂
0 Kudos
JeffBarrette
Esri Regular Contributor
You can do this two ways.

mxd = arcpy.mapping.MapDocument("current")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "myLayer")[0]

#Zoom to all selected features in data frame
df.zoomToSelectedFeatures()

#Zoom to selected features for a specific layer.
df.extent = lyr.getSelectedExtent()

arcpy.RefreshActiveView()


Jeff
0 Kudos