NetTopologySuite / NetTopologySuite.IO.Esri

BSD 3-Clause "New" or "Revised" License
35 stars 18 forks source link

How do I get the Feature Id? #23

Closed mburbea closed 1 year ago

mburbea commented 1 year ago

I'm trying to upgrade a project that use to use the older NetTopologySuite.IO.ShapeFile. When I use to export a shape, I would get the esri generated Feature ID as the property "FeatureId". I've tried using GetOptionalId("FID") and GetOptionalId("FeatureID") and neither work.

KubaSzostak commented 1 year ago

The FeatureId it's not related to Shapefile specification. You won't be able to find this attribute neither in *.shp nor in *.dbf file.

This is specific to Esri software. The ObjectID is used by ArcGIS to do such things as scroll, display selection sets, and perform identify operations on features. To solve missing ObjectID in Shapefiles they used simple solution. They added virtual FID column where values start at zero. It's not real ID because if a record from a shapefile is deleted, the FIDs are renumbered so that they start from 0 and increase sequentially (there is no gap in numbering).

You can easily mimic the same:

var features = Shapefile.ReadAllFeatures(shpPath).ToList();
for (int featureId = 0; featureId < features.Count; featureId++)
{
    var feature = features[featureId];
    Console.WriteLine($"{"FID",10}: {featureId}");
    foreach (var attrName in feature.Attributes.GetNames())
    {
        Console.WriteLine($"{attrName,10}: {feature.Attributes[attrName]}");
    }
    Console.WriteLine($"     SHAPE: {feature.Geometry}");
    Console.WriteLine();
}
mburbea commented 1 year ago

Thank you for the explanation, I'll just use the array index as FID from now on.