Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I want to get all features in the current extent of my PageLayoutControl's ActiveView. I got it working for a MapControl as described here.

With my PageLayoutControl it seems that I don't get the correct extent. When I fill the found features it looks like this:

Here is how I get the features:

Dictionary<String, IFeatureCursor> featureCursors = new Dictionary<String, IFeatureCursor>();
IMap map = this._pageLayoutControl.ActiveView.FocusMap;

// Get ActiveView from MapFrame
IActiveView tmpActiveView = null;
IGraphicsContainer graphicsContainer = (IGraphicsContainer)_pageLayoutControl.PageLayout;
graphicsContainer.Reset();
IElement element = graphicsContainer.Next();
while (element != null)
{
  if (element is IMapFrame)
  {
    IMapFrame mapFrame = (IMapFrame)element;
    tmpActiveView = (IActiveView)mapFrame.Map;
    break;
  }
  element = graphicsContainer.Next();
}
if (tmpActiveView == null)
  return featureCursors;

IEnvelope envelope = this._pageLayoutControl.ActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds; // FittedBounds;
// PageLayout has no SpatialReference. So use the View of the MapFrame 
IPoint pointMax = tmpActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint((System.Int32)envelope.XMax, (System.Int32)envelope.YMax);
IPoint pointMin = tmpActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint((System.Int32)envelope.XMin, (System.Int32)envelope.YMin);
envelope.XMax = pointMax.X;
envelope.YMax = pointMax.Y;
envelope.XMin = pointMin.X;
envelope.YMin = pointMin.Y;

// Get Features in all layers
for (int iFeatLayer = 0; iFeatLayer < _pageLayoutControl.ActiveView.FocusMap.LayerCount; iFeatLayer++)
{ 
  ILayer thisLayer = _pageLayoutControl.ActiveView.FocusMap.Layer[iFeatLayer];
  IGeoFeatureLayer featLayer = thisLayer as IGeoFeatureLayer;
  if (featLayer != null)
  {
    IFeatureClass featureClass = featLayer.FeatureClass;
    System.String shapeFieldName = featureClass.ShapeFieldName;

    // Create a new spatial filter and use the new envelope as the geometry
    ISpatialFilter spatialFilter = new SpatialFilterClass();
    spatialFilter.Geometry = envelope;
    spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelEnvelopeIntersects;
    spatialFilter.set_OutputSpatialReference(shapeFieldName, map.SpatialReference);
    spatialFilter.GeometryField = shapeFieldName;

    // Do the search
    IFeatureCursor featureCursor = featureClass.Search(spatialFilter, false);
    featureCursors.Add(thisLayer.Name, featureCursor);
  }
}
return featureCursors;

What did I miss? :)

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Finally figured it out: The IEnvelope I got was the wrong one. The Extent of the MapFrame's ActiveView is the one I needed. So that part should look like this:

IEnvelope envelope = tmpActiveView.Extent;

The IEnvelope already has the spatial reference, so the 'translation' via ToMapPoint is no longer needed.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.