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

How to add altitude coordinates in Feature Collection then serialize to GeoJson #152

Open
Shaaruddin opened this issue Nov 12, 2020 · 0 comments

Comments

@Shaaruddin
Copy link

Shaaruddin commented Nov 12, 2020

I greatly appreciate for any help, I deserialized GeoJson file into feature collection using GeoJson.Net. Then I get the feature from the feature collection. Inside the feature I can loop and get all the type of geometry. From there I can loop each of the type and get the coordinate. However now I am stuck on how to edit to add Altitude equal to 0 for each coordinate then serialize back to geojson.

This is my sample test.geojson file:

{
  "type": "FeatureCollection",
  "name": "MYS_adm2",
  "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
  "features": [
{
    "type": "Feature",
    "id": 0,    
    "properties": {"party": "Republican","count": "2500"},
    "geometry": {
        "type": "Polygon",
        "coordinates": [[
            [-104.05, 48.99],
            [-97.22,  48.98],
            [-96.58,  45.94],
            [-104.03, 45.94],
            [-104.05, 48.99]
        ]]
    }
},
{
    "type": "Feature",
    "id": 1,
    "properties": {"party": "Democrat","count": "1300"},
    "geometry": {
        "type": "MultiPolygon",
        "coordinates": [[[
            [-109.05, 41.00],
            [-102.06, 40.99],
            [-102.03, 36.99],
            [-109.04, 36.99],
            [-109.05, 41.00]
        ]]]
    }
}
  ]
}

This is code how I deserialized:

string filePath = @"D:\test.geojson";
            FeatureCollection collection = JsonConvert.DeserializeObject<FeatureCollection>(File.ReadAllText(filePath)); //1 min 21 s
            var feature = collection.Features;
            foreach (var featureItem in feature)
            {
                if (featureItem.Geometry.Type.Equals(GeoJSONObjectType.Point))
                {
                    Point point = featureItem.Geometry as Point;

                }
                else if (featureItem.Geometry.Type.Equals(GeoJSONObjectType.MultiPoint))
                {

                }
                else if (featureItem.Geometry.Type.Equals(GeoJSONObjectType.Polygon))
                {
                    Polygon polygon = featureItem.Geometry as Polygon;
                    foreach (var Item in polygon.Coordinates)
                    {
                        foreach (var coordinates in Item.Coordinates)
                        {
                            //Here I want to add value 0 altitude coordinate
                            

                        }
                    }
                }
                else if (featureItem.Geometry.Type.Equals(GeoJSONObjectType.MultiPolygon))
                {
                    MultiPolygon multiPolygon = featureItem.Geometry as MultiPolygon;
                    foreach (var Item in multiPolygon.Coordinates)
                    {
                        foreach (var item1 in Item.Coordinates)
                        {
                            foreach (var item2 in item1.Coordinates)
                            {
                                //Here I want to add value 0 altitude coordinate
                            }
                        }
                    }
                }
            }

            //here I want to serialize my FeatureCollection after edit
            File.WriteAllText(@"D:\test_Edit.geojson", JsonConvert.SerializeObject(collection));

My expected output is to add Altitude equal to 0 like below:

{
  "type": "FeatureCollection",
  "name": "MYS_adm2",
  "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
  "features": [
{
    "type": "Feature",
    "id": 0,    
    "properties": {"party": "Republican","count": "2500"},
    "geometry": {
        "type": "Polygon",
        "coordinates": [[
            [-104.05, 48.99, 0],
            [-97.22,  48.98, 0],
            [-96.58,  45.94, 0],
            [-104.03, 45.94, 0],
            [-104.05, 48.99, 0]
        ]]
    }
},
{
    "type": "Feature",
    "id": 1,
    "properties": {"party": "Democrat","count": "1300"},
    "geometry": {
        "type": "MultiPolygon",
        "coordinates": [[[
            [-109.05, 41.00, 0],
            [-102.06, 40.99, 0],
            [-102.03, 36.99, 0],
            [-109.04, 36.99, 0],
            [-109.05, 41.00, 0]
        ]]]
    }
}
  ]
}
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

1 participant