Best way to determine the full path of a featurelayer, if it is a shapefile?

204
3
04-07-2020 07:37 AM
GyanendraGurung
New Contributor III

I am trying find the best way to find the full path of a shapefile, which is open in the active map. Or in other words, I want to find a way to know if the feature layer in the active map is a shapefile or not. I "think" I saw it somewhere in the SDK snippets github pages, but I cannot track it now. Anyway, here is what I did:

  1. string SelectedShapefileLayer = "Some_feature_in_map";  
  2. string fcLocation = "";  // Location of feature layer: either a FGDB or a folder //  
  3. string fcName = "";  // ACTUAL name of feature layer //    
  4. await QueuedTask.Run(() =>  
  5. {  
  6.     var fcLyr = (FeatureLayer)MapView.Active.Map.FindLayers(SelectedFeatureLayer).First();  
  7.     Uri fcURI = fcLyr.GetTable().GetDatastore().GetPath();  
  8.     fcLocation = fcURI.LocalPath;   
  9.     fcName = fcLyr.GetTable().GetDefinition().GetName(); // Displayed name may not be ACTUAL name //  
  10. });  
  11.   
  12. string fcPath = ""// Full path of feature layer //   
  13. if (Path.GetExtension(fcLocation) == "")  
  14. {  
  15.     // If feature layer is a shapefile //   
  16.     fcPath = Path.Combine(fcLocation, fcName + ".shp");  
  17. }  
  18. else  
  19. {  
  20.     // if feature layer is a FGDB feature class //  
  21.     fcPath = Path.Combine(fcLocation, fcName);  
  22. }  

I would be grateful if anyone could help me find a better way to do this. Surely, there must be a better way.

In the above code, I have assigned all feature layer WITHOUT an extension as a FGDB feature class, and others as a shapefile. I think that could run into trouble as well.

Many thanks in advance. 

Kenny

Tags (1)
0 Kudos
3 Replies
RichRuh
Esri Regular Contributor

Hi Kenny,

Your technique should work fine.

In the upcoming Pro 2.6 release (summer 2020), we will be adding a GetPath() method directly to the FeatureLayer class so that you don't have to go through this to get the path.

But if you're going through all of this just to find out if you are looking at a shape file, I would do something like this:

bool IsShapeFile(FeatureLayer featureLayer)
{
using (FeatureClass featureClass = featureLayer.GetFeatureClass())
using (Datastore datastore = featureClass.GetDatastore())
{
if (datastore is FileSystemDatastore)
{
return true;
}
else
{
return false;
}
}
}

 I hope this helps,

--Rich

GyanendraGurung
New Contributor III

Hi Rich, 

Thank you very much for your kind and prompt reply. It is very nice to know that such a feature will be available in the next release. Awesome !

Thank you for your script. I saw the snippet for opening a shapefile using "FileSystemDatastore" in the snippet section. But I could not find a way to use it to check for a shapefile. Thank you for sharing a simpler solution. This is really awesome! 

Many many thanks again ! 

Kenny

RichRuh
Esri Regular Contributor

With the release of ArcGIS Pro 2.6, the FeatureLayer.GetPath method is now available.