Friday, December 30, 2011

Code for Zoom to GIS Layer

Team,

Here is the code to Zoom to a specific GIS Layer.

This code scans through all features and zooms to the extent of all.

Particularly this line of code "envelopeCls.Union(featureExtent);" collects the envelope extents of each record.

Have fun coding!

Cheers
Anand




private bool ZoomtoLayer(string m_zoomtoLayerName)
{
bool togoBool = false;
for (int layerIndex = 0; layerIndex < ArcMap.Document.ActiveView.FocusMap.LayerCount; layerIndex++)
{
ILayer2 eachLayer = ArcMap.Document.ActiveView.FocusMap.get_Layer(layerIndex) as ILayer2;
if (eachLayer is IFeatureLayer)
{
IFeatureLayer eachFeatureLayer = eachLayer as IFeatureLayer;
if (eachFeatureLayer.Name.Trim().ToUpper() == m_zoomtoLayerName.Trim().ToUpper())
{
IFeatureLayerDefinition featureLayerDef = eachFeatureLayer as IFeatureLayerDefinition;
QueryFilterClass queryFilter = new QueryFilterClass();
queryFilter.WhereClause = featureLayerDef.DefinitionExpression;
ESRI.ArcGIS.Geodatabase.IFeatureCursor featureCursor = eachFeatureLayer.FeatureClass.Search(queryFilter, true);
ESRI.ArcGIS.Geometry.IEnvelope envelopeCls = new EnvelopeClass();
envelopeCls.SetEmpty();
ESRI.ArcGIS.Geodatabase.IFeature feature;
while ((feature = featureCursor.NextFeature()) != null)
{
ESRI.ArcGIS.Geometry.IGeometry geometry = feature.Shape;
ESRI.ArcGIS.Geometry.IEnvelope featureExtent = geometry.Envelope;
envelopeCls.Union(featureExtent);
}
envelopeCls.Expand(m_zoomFactor, m_zoomFactor, true);
ArcMap.Document.ActiveView.Extent = envelopeCls.Envelope;
ArcMap.Document.ActiveView.Refresh();
return true;
} // if (eachFeatureLayer.Name.Trim().ToUpper() == gisLayerName.Trim().ToUpper())
} // if (eachLayer is IFeatureLayer)
} // for
return togoBool;
}

All Blogs so far ...