简体   繁体   中英

How to serialize and deserialize geojson with different amount of nested arrays in the geometry in C#

I'm trying to deserialize GeoJson, manipulate the data and store it in a Db. However the deserialization of coordinates is failing because the coordinate have different nested array. For example, Polygon has 3 layers of nested array while Multipolygon has 4 layers of nested array. How can this be achieved dynamically instead of fixing the layer of nested array.

My sample geojson:

{
"type": "FeatureCollection",
"name": "MYS_adm2",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "id": 0, "properties": { "ID_0": 136 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 102.911849975585938, 1.763612031936702 ], [ 102.911430358886832, 1.763888001442069 ] ] ] } },
{ "type": "Feature", "id": 1, "properties": { "ID_0": 136 }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 103.556556701660156, 1.455448031425533 ], [ 103.555900573730582, 1.455950021743831 ] ] ] ] } },

My Class:

class Class1
{
    public class Properties
    {
        public string name { get; set; }
    }

    public class Crs
    {
        public string type { get; set; }
        public Properties properties { get; set; }
    }

    public class Properties2
    {
        public int ID_0 { get; set; }
    }

    public class Geometry
    {
        public string type { get; set; }
        public List<List<List<object>>> coordinates { get; set; }
    }

    public class Coordinates
    {
        public List<List<List<object>>> pCoordinates { get; set; }
        
    }

    public class Feature
    {
        public string type { get; set; }
        public int id { get; set; }
        public Properties2 properties { get; set; }
        public Geometry geometry { get; set; }
    }

    public class Root
    {
        public string type { get; set; }
        public string name { get; set; }
        public Crs crs { get; set; }
        public List<Feature> features { get; set; }
    }
}

My code:

string jsonFile = @"D:\test.geojson";
var myJsonResponse = File.ReadAllText(jsonFile);
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);

Rather than recreate a potentially large wheel (depending on what you're going to do with the data), check out https://github.com/GeoJSON-Net/GeoJSON.Net

var sample = JsonConvert.DeserializeObject<FeatureCollection>(yourGeoJson);
var firstFeature = sample.Features.First().Geometry as Polygon;
var secondFeature = sample.Features[1].Geometry as MultiPolygon;
var firstFeatureCoordinates = firstFeature.Coordinates;
// etc..

For manipulations, checking if a point is in a polygon, etc., check out GeoJSON.Net.Contrib.MsSqlSpatial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM