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;
        }
  • 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/… – Michael Stimson May 29 '18 at 3:28
  • @MichaelStimson, (PolygonLayer.FeatureClass as IGeoDataset).Extent returns null. – jay May 29 '18 at 17:20
  • Does the featureclass have any features in it? – Kirk Kuykendall May 30 '18 at 0:30
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;
  • 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. – Michael Stimson May 29 '18 at 21:58
  • @MichaelStimson, the feature type is "Simple" and geometry type is "Polygon" – jay May 30 '18 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? – danielm May 30 '18 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. – Kirk Kuykendall Jun 2 '18 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. – Kirk Kuykendall Jun 2 '18 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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