How can I access the four extent values of a layer shown in the below image using C# in ArcObjects?
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– Michael Stimson2018年05月29日 03:28:10 +00:00Commented May 29, 2018 at 3:28
-
@MichaelStimson, (PolygonLayer.FeatureClass as IGeoDataset).Extent returns null.jay– jay2018年05月29日 17:20:05 +00:00Commented May 29, 2018 at 17:20
-
Does the featureclass have any features in it?Kirk Kuykendall– Kirk Kuykendall2018年05月30日 00:30:28 +00:00Commented May 30, 2018 at 0:30
1 Answer 1
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
-
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– Michael Stimson2018年05月29日 21:58:00 +00:00Commented May 29, 2018 at 21:58
-
@MichaelStimson, the feature type is "Simple" and geometry type is "Polygon"jay– jay2018年05月30日 00:38:58 +00:00Commented 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?danielm– danielm2018年05月30日 17:39:29 +00:00Commented 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 withIEnvelope.Expand
, then setILayer2.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 theILayer2.AreaOfInterest
automatically. When I right click and choose "Zoom to layer" it zoomed to thefeatureclass
'sIGeoDataset.Extent
- not to theAreaOfInterest
.Kirk Kuykendall– Kirk Kuykendall2018年06月02日 17:49:19 +00:00Commented Jun 2, 2018 at 17:49 -
I didn't check to confirm that
IGeoDataset.Extent
for theFeatureLayer
returns the same thing asIGeoDataset.Extent
for theFeatureclass
.Kirk Kuykendall– Kirk Kuykendall2018年06月02日 17:49:31 +00:00Commented Jun 2, 2018 at 17:49
lang-cs