Добавить дополнительные свойства в JSON для создания GeoJSON? - PullRequest
0 голосов
/ 21 октября 2019

С помощью SO я смог создать класс, который приближает меня к созданию объекта GeoJSON:

Это класс:

var envelope = new
{
    type = "FeatureCollection",
    features = result.Tables[0]
};

string callback = JsonConvert.SerializeObject(envelope);
byte[] resultBytes = Encoding.UTF8.GetBytes(callback);
return new System.IO.MemoryStream(resultBytes);

, который возвращает этот JSON:

{
    "type" : "FeatureCollection",
    "features" : [
        {
            "Name " : "Joe",
            "Status" : "Dev",
            "imageUrl" : "markers/Dev.svg",
            "lat" : 34.310100,
            "Lon" : -77.215500
        },
        {
            "Name " : "Joe",
            "Status" : "Dev",
            "imageUrl" : "markers/Dev.svg",
            "lat" : 34.310100,
            "Lon" : -77.215500
        },
        {
            "Name " : "Mary",
            "Status" : "Dev",
            "imageUrl" : "markers/Dev.svg",
            "lat" : 34.310100,
            "Lon" : -77.215500
        }
    ]
}

Мне нужно добавить еще один набор свойств для этого объекта JSON, но я не уверен, как изменить класс для этой цели.

Новый JSON должен выглядеть следующим образомэто;это по сути GeoJSON. Перед каждой «записью» будет еще один набор свойств "type" : "Feature", "properties" : {. После каждой строки будет также другое подмножество "geometry" ::

{
    "type" : "FeatureCollection",
    "features" : [
        {
            "type" : "Feature",
                "properties" : {
                "Name " : "Joe",
                "Status" : "Dev",
                "imageUrl" : "markers/Dev.svg",
                "lat" : 34.21092,
                "lon" : -77.59384
            },
            "geometry" : {
                "type" : "Point",
                "coordinates" : [ -77.59384, 34.21092 ]
            }
        },
        {
            "type" : "Feature",
                "properties" : {
                "Name " : "Mary",
                "Status" : "Dev",
                "imageUrl" : "markers/Dev.svg",
                "lat" : 32.49323,
                "lon" : -78.43144
            },
            "geometry" : {
                "type" : "Point",
                "coordinates" : [ -78.43144, 32.49323 ]
            }
        }
    ]
}

Это дата:

Name       Status       imageUrl          lat       lon
Joe        Dev          markers/Dev.svg   34.21092  -77.59384
Mary       Dev          markers/Dev.svg   32.49323  -78.43144

1 Ответ

0 голосов
/ 21 октября 2019

Это намного проще, если вы реализуете некоторые классы

public abstract class TypedClass
{
    public string type { get; set; }
}

public class Envelope : TypedClass
{
    public List<Feature> features { get; set; }
    public GeoJSONObject ToGeoJSON()
    {
        return new GeoJSONObject()
        {
            type = type,
            features = new List<GeoJSONFeature>(features.Select(x => x.ToGeoJSONFeature()))
        };
    }
}

public class Feature
{
    [JsonProperty("Name ")]
    public string Name { get; set; }
    public string Status { get; set; }
    public string imageUrl { get; set; }
    public double lat { get; set; }
    public double Lon { get; set; }
    public GeoJSONFeature ToGeoJSONFeature()
    {
        return new GeoJSONFeature()
        {
            type = "Feature",
            properties = this,
            geometry = new Geometry()
            {
                type = "Point",
                coordinates = new List<double>() { Lon, lat }
            }
        };
    }
}

public class GeoJSONObject : TypedClass
{
    public List<GeoJSONFeature> features { get; set; }
}

public class GeoJSONFeature : TypedClass
{
    public Feature properties { get; set; }
    public Geometry geometry { get; set; }
}

public class Geometry : TypedClass
{
    public List<double> coordinates { get; set; }
}

Использование:

Envelope envelope = JsonConvert.DeserializeObject<Envelope>(json);
GeoJSONObject gjo = envelope.ToGeoJSON();
string geo_json = JsonConvert.SerializeObject(gjo);
...