How to access to layer properties with C# and ArcGIS Pro SDK?

2148
6
Jump to solution
03-03-2020 09:31 AM
Amadeus111
Occasional Contributor II

I created the layer but also I would like to retrieve layer properties such as geometry type, coord system etc. 

System.Uri shp_filepath = new System.Uri(@shp_path);
QueuedTask.Run(() =>
{
var shp_layer = LayerFactory.Instance.CreateLayer
(shp_filepath, MapView.Active.Map, indexNumber, shp_path.Substring(16));

//Get the active map view.
var mapView = MapView.Active;
var layerDescription = shp_layer.GetDefinition().Description; //This gives only the name but I need other properties too

if (mapView == null)
return Task.FromResult(false);
else
{
var selectedLayer = mapView.GetSelectedLayers()[0];

return mapView.ZoomToAsync(selectedLayer);
}
});

Thanks!

0 Kudos
1 Solution

Accepted Solutions
Amadeus111
Occasional Contributor II

Projection info can be accessed by 

var layer_projection = active_Layer.GetSpatialReference();
 projection_name = layer_projection.Name;

For the vector type, I just search through the Shape field in the attribute table. 

using (ArcGIS.Core.Data.Table shp_table = (active_Layer as FeatureLayer).GetTable())
{
   using (RowCursor rowCursor = shp_table.Search())
   {
      while (rowCursor.MoveNext())
      {
         using (ArcGIS.Core.Data.Row row = rowCursor.Current)
         {
            if (row["Shape"] != null && row["Shape"].ToString() != " ")
            {
               geometry_type = (string)row["Shape"].ToString();
            }
         }
      }
   }
}

This method still cannot help to distinguish between polygon and polygon ZM

View solution in original post

0 Kudos
6 Replies
UmaHarano
Esri Regular Contributor

Hi,

Cast the definition retrieved into CIMFeatureLayer. Like this:

var lyrDef = shp_layer.GetDefinition() as CIMFeatureLayer;
0 Kudos
Amadeus111
Occasional Contributor II

Ok, I did that. 

Now, How can I get coordination system and the geometry type as string? 

Thanks!

Oz

0 Kudos
Amadeus111
Occasional Contributor II

Projection info can be accessed by 

var layer_projection = active_Layer.GetSpatialReference();
 projection_name = layer_projection.Name;

For the vector type, I just search through the Shape field in the attribute table. 

using (ArcGIS.Core.Data.Table shp_table = (active_Layer as FeatureLayer).GetTable())
{
   using (RowCursor rowCursor = shp_table.Search())
   {
      while (rowCursor.MoveNext())
      {
         using (ArcGIS.Core.Data.Row row = rowCursor.Current)
         {
            if (row["Shape"] != null && row["Shape"].ToString() != " ")
            {
               geometry_type = (string)row["Shape"].ToString();
            }
         }
      }
   }
}

This method still cannot help to distinguish between polygon and polygon ZM

0 Kudos
BerndtNording
New Contributor III

Thanks for posting this solution, it was something I needed to do. Have you found any other methods for determining the geometry type of a feature layer? Your posted solution works, BUT it is excruciatingly sloooow - it took over 15 seconds to determine the geometry type for 2 layers...

0 Kudos
Amadeus111
Occasional Contributor II

Sorry, I just saw your message. Probably, there is a better solution for this but for a faster response, you can return after reading one row only. As we know,  shapefile geometry should be consistent for all the records on the attribute table. The solution above reads all rows before returning the geometry. 

0 Kudos
BerndtNording
New Contributor III

Yes, that is what I was doing, but still molasses slow. But I stumbled upon a solution that works much better: cast your featurelayer to a BasicFeatureLayer which has a ShapeType property. This little snippet creates a list of selected layers that match a specified geometry type. Fore some reason GeoNet doesn't give me the option for inserting "code".

 

 

dctLookup = new Dictionary<string, esriGeometryType>();
dctLookup.Add("POINT", esriGeometryType.esriGeometryPoint);
dctLookup.Add("LINE", esriGeometryType.esriGeometryPolyline);
dctLookup.Add("POLYGON", esriGeometryType.esriGeometryPolygon);

selLays = MapView.Active.GetSelectedLayers();
foreach (Layer lay in selLays)
if (lay.MapLayerType == MapLayerType.Operational)
{
pBFL = lay as BasicFeatureLayer;
if (pBFL != null)
{
for (int i = 0; i < lstTypes.Count; i++)
{
if (pBFL.ShapeType == dctLookup[lstTypes[i]])
{
lstLays[i] = lay;
}
}
}
}
return lstLays;