0

I'm learning how to create a GeoJson stream. I know how to convert a datatable to a JSON string and then serialize to JSON format. The problem is that I don't know how to include this JSON stream in a GeoJson object.

The following code generates a geojson object:

    public geoJson Leaflets_GeoJson()
    {

        var CorOrd = new LocalGeometry();
        CorOrd.coordinates = new List<double>(); // <=====
        CorOrd.coordinates.Add(34.2305);
        CorOrd.coordinates.Add(-86.2644);
        CorOrd.type = "Point";

        var myProps = new Properties();
        myProps.name = "John";
        myProps.id = "JN123";
        myProps.address = "Howard Street";


        var geoJson = new geoJson
        {
            type = "FeatureCollection",
            features = new List<LocalFeature>
            {
               new LocalFeature { type = "Feature", geometry = CorOrd, properties=myProps}
            }

        };

        return geoJson;

    }

Which returns the following:

{"features":[{"geometry":{"coordinates":[18.2305,-66.2644],"type":"Point"},"properties":{"address":"Howard Street","id":"JN123","name":"John"},"type":"Feature"}],"type":"FeatureCollection"}

This is the method that takes a datatable and returns a JSON stream:

public System.IO.Stream()
{
    string sqlQuery = string.Format("usp_GetNames");

    string connString = System.Configuration.ConfigurationManager.ConnectionStrings["connstring"].ToString();
    SqlDatabase sqlDatabase = new SqlDatabase(connString);
    DataSet result = sqlDatabase.ExecuteDataSet(CommandType.Text, sqlQuery);


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

and returns this JSON:

[
 {"name":"Joe","address":"Main Street","id":"Joe121", "Lon": 40.730610, "Lat": -73.935242}, 
 {"name":"Mary","address":"220 Avenue","id":"Mary625", "Lon": 42.730610, "Lat": -72.935242}
]

My question: How can I combine these two streams so that it returns a GeoJson object that looks like this?

{ "type": "FeatureCollection", "features": [ {"type": "Feature", "properties": 
   {"name":"Joe","address":"Main Street","id":"Joe121"},"geometry": {"type": "Point","coordinates": [-73.1232, 40.7306]}}, 
   {"type": "Feature", "properties": {"name":"Mary","address":"220 Avenue","id":"Mary625"},"geometry": {"type": "Point","coordinates": [-71.1232, 41.7306]}}] } 
fdkgfosfskjdlsjdlkfsf
  • 3,165
  • 2
  • 43
  • 110
  • Are you using [tag:nettopologysuite]? If so see maybe [Having trouble serializing NetTopologySuite FeaturesCollection to GeoJSON](https://stackoverflow.com/q/26763564/3744182) or [How to seed NetTopologySuite.Geometries.Point data from a Json file in ASP.Net core](https://stackoverflow.com/a/57227723/3744182). – dbc Oct 18 '19 at 19:52
  • I'm not using nettopologysuite. – fdkgfosfskjdlsjdlkfsf Oct 18 '19 at 20:27

1 Answers1

1

You need to wrap that array in an outer class that implements the two properties (type and features). You can just use a C# anonymous type for this:

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

string callback = JsonConvert.SerializeObject(envelope);
byte[] resultBytes = Encoding.UTF8.GetBytes(callback);
//...
Mark Waterman
  • 961
  • 7
  • 16
  • Thanks. I had tried that. The problem is that each has additional static data. So within `features` there's a another block of json. The coordinates are also in a separate block: `"features": [ {"type": "Feature", "properties": {"name":"Joe","address":"Main Street","id":"Joe121"},"geometry": {"type": "Point","coordinates": [-73.1232, 40.7306]}}` – fdkgfosfskjdlsjdlkfsf Oct 18 '19 at 22:16