Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid geometry type provided (Polygon). Expected: Multilinestring #32

Closed
txpppp opened this issue Jul 6, 2023 · 1 comment
Closed

Comments

@txpppp
Copy link

txpppp commented Jul 6, 2023

Hello, I have two datasets now: one is LineString data and the other is Polygon data. I added these two datasets to a FeatureCollection and tried to generate a shapefile using Shapefile.WriteAllFeatures. However, I encountered an error message saying "Invalid geometry type provided (Polygon). Expected: Multilinestring." I would like to know if this is because a FeatureCollection cannot contain different types of geometries. How can I solve this problem? Thank you.

code:

    var ntsService = new NtsGeometryServices(new PrecisionModel(), 4326);
    var wktReader = new WKTReader(ntsService);
    var featureCollection = new List<IFeature>();
    foreach (var item in wktArrays)
    {
        var geometry = wktReader.Read(item.Wkt);
        foreach (var name in item.Attributes.GetNames())
        {
            if (item.Attributes.GetType(name) == typeof(object))
            {
                item.Attributes.DeleteAttribute(name);
            }
        }
        var feature = new Feature { Geometry = geometry, Attributes = item.Attributes };
        featureCollection.Add(feature);
    }
    var now = DateTime.Now;
    var folderDate = now.ToString("yyyy") + now.ToString("MM") + now.ToString("dd") + now.ToString("HH")
        + now.ToString("mm") + now.ToString("ss");
    var shapeFilePath = Path.Combine(App.WebHostEnvironment.ContentRootPath, Options.ShapeFilePath, fileName, folderDate);
    if (!Directory.Exists(shapeFilePath))
    {
        Directory.CreateDirectory(shapeFilePath);
    }
    var savePath = Path.Combine(shapeFilePath, fileName);
    Shapefile.WriteAllFeatures(featureCollection, savePath);
@KubaSzostak
Copy link
Member

Thank you @txpppp for your feedback. The issue isn't caused by FeatureCollection, it's a Shapefile limitation. As per specification:

All the non-Null shapes in a shapefile are required to be of the same shape type.
...
Currently, shapefiles are restricted to contain the same type of shape as specified above. In the future, shapefiles may be allowed to contain more than one shape type. If mixed shape types are implemented, the shape type field in the header will flag the file as such.

The specification didn't changed since 1998.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants