0

How can I access the four extent values of a layer shown in the below image using C# in ArcObjects?

enter image description here

Below is the code that i am using to achieve it but "AreaOfInterest" is null.

public ISpatialReference CreateSpatialRefGCS(ESRI.ArcGIS.Geometry.esriSRGeoCSType gcsType)
 {
 ISpatialReferenceFactory spatialRefFactory = new SpatialReferenceEnvironmentClass();
 IGeographicCoordinateSystem geoCS = spatialRefFactory.CreateGeographicCoordinateSystem((int)gcsType);
 return (ISpatialReference)geoCS;
 }
 public IEnvelope GetExtent(ESRI.ArcGIS.Carto.IFeatureLayer PolygonLayer)
 {
 try 
 {
 IEnvelope envelope = PolygonLayer.AreaOfInterest.Envelope;
 envelope.Project(CreateSpatialRefGCS(esriSRGeoCSType.esriSRGeoCS_WGS1984)); 
 }
 catch (Exception ex)
 {
 return null;
 }
 return PolygonLayer.AreaOfInterest.Envelope;
 }
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked May 28, 2018 at 19:18
3
  • That's not a good way to create a WGS84 projection as ISpatialReference, try ISpatialReferenceFactory3 pSRf3 = new SpatialReferenceEnvironmentClass(); ISpatialReference pDS_SR = pSRf3.CreateSpatialReference(4326); but you're not going to get those numbers from a geographic spatial reference; Geographic spatial references have the bounds -180,180 and -90,90. As for getting the extent try (PolygonLayer.FeatureClass as IGeoDataset).Extent resources.arcgis.com/en/help/arcobjects-net/componenthelp/… Commented May 29, 2018 at 3:28
  • @MichaelStimson, (PolygonLayer.FeatureClass as IGeoDataset).Extent returns null. Commented May 29, 2018 at 17:20
  • Does the featureclass have any features in it? Commented May 30, 2018 at 0:30

1 Answer 1

2

You can get this information from the source FeatureClass of the layer like so:

IFeatureClass source = (ILayer as IFeatureLayer).FeatureClass;
(source as IFeatureClassManage).UpdateExtent(); //per MichaelStimson's suggestion
IEnvelope extent = (source as IGeoDataset).Extent;
answered May 29, 2018 at 16:23
5
  • Yup, that's what I said, however this according to jay returns Null.. perhaps it would be a good idea to include a line with IFeatureClassManage.UpdateExtent resources.arcgis.com/en/help/arcobjects-net/componenthelp/… first.. if the result is still Null there's likely to be a bigger problem; what feature storage type is the polygon layer jay? Is it shapefile, geodatabase, sde, KML, GML etc.? To implement IFeatureClass the data storage should implement IGeoDataset fully but if one or more geometries are broken that could confuse the extent. Commented May 29, 2018 at 21:58
  • @MichaelStimson, the feature type is "Simple" and geometry type is "Polygon" Commented May 30, 2018 at 0:38
  • @jay have you tried running the Repair Geometry geoprocessing tool in ArcMap on your layer? Also could you specify how you are retrieving the ILayer object before you pass it into your GetExtent method? Commented May 30, 2018 at 17:39
  • I did a quick test with 10.4.1 ... I got the extent from layer's featureclass using IGeoDataset.Extent.Envelope, doubled it with IEnvelope.Expand, then set ILayer2.AreaOfInterest to the expanded envelope. I saved it to a lyr file, closed and reopened arcmap, then added the lyr file to an empty document. It zoomed to the ILayer2.AreaOfInterest automatically. When I right click and choose "Zoom to layer" it zoomed to the featureclass's IGeoDataset.Extent - not to the AreaOfInterest. Commented Jun 2, 2018 at 17:49
  • I didn't check to confirm that IGeoDataset.Extent for the FeatureLayer returns the same thing as IGeoDataset.Extent for the Featureclass. Commented Jun 2, 2018 at 17:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.