[GIS] Get all points from multiline in shapefile using C# and GDAL

cgdal

I'm trying to read a shapefile and draw it using DirectX and GDAL library. So first I will read the shapefile and get a list of all the points in the shapefile. It works with linestring using GetPoint().

var polylines = new ConcurrentDictionary<string, List<Point3D>>();
Feature feat;
while ((feat = layer.GetNextFeature()) != null) {
    var geom = feat.GetGeometryRef();
    var points = new List<Point3D>();
    for (var i = 0; i < geom.GetPointCount(); i++)
        {   
            double[] geopoint = {0, 0, 0};
            geom.GetPoint(i, geopoint);
            points.Add(new Point3D(geopoint[0], geopoint[1], geopoint[2]));
        }

    polylines.TryAdd(feat.GetFID().ToString(), points);
    feat.Dispose();
}

But when the geometry is MultiLineString then GetPoint(), it always returns 0. Does anyone know if there is a way to unpack MultilLineString to LineString or read MultiLineString using GDAL?

Best Answer

So, after digging around. I finally found a way to unpack multilinestring.

var polylines = new ConcurrentDictionary<string, List<Point3D>>();
Feature feat;
while ((feat = layer.GetNextFeature()) != null) {
    var geom = feat.GetGeometryRef();
    var points = new List<Point3D>();
    if (geom.GetGeometryCount() > 0) //Check to see if the geometry is multigeometry
    {
        for (var i = 0; i < geom.GetGeometryCount(); i++)
        {
           var line = geom.GetGeometryRef(i);

           //Get points using the code I post before
         }
    }

    polylines.TryAdd(feat.GetFID().ToString(), points);
    feat.Dispose();
}

This should also work with multipolygons.

Related Question