Search code examples
c#.netgeojsonienumerable

Format GeoJson based on IEnumerable<Users>


I have a basic API running, I can store data and get the data as usual. However, I am having issues with formatting my content. I can get a list of all my users with HTTPGet, and it looks like this...

[
    {
        "userId": "1                           ",
        "geoHash": "123456789",
        "latitude": 1.234,
        "longitude": 5.689,
        "locationDate": "2019-07-01T00:00:00"

    },
    {
        "userId": "2                           ",
        "geoHash": "123456789",
        "latitude": 1.234,
        "longitude": 5.689,
    "locationDate": "2019-07-01T00:00:00"

    },
    {
        "userId": "3                           ",
        "geoHash": "123456789",
        "latitude": 1.234,
        "longitude": 5.689,
    "locationDate": "2019-07-01T00:00:00"

    }
]

However, I have no clue to how I can turn this into a GeoJson (https://geojson.org/) which looks like this...

{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [125.6, 10.1]
  },
  "properties": {
    "name": "Dinagat Islands"
  }
}

And this is the basic of how I get my user list

        [HttpGet]
        public IEnumerable<Users> GetUsers()
        {
            return _context.Users;
        }

I've been searching for hours and the content around this subject seems to be somewhat confusing for me, I would really appreciate some help here.


Solution

  • Edit: although the question contains the user data as Json, comments now indicate it is contained in an IEnumerable<Users>. The code below assumes the property names of those values are the same as shown in the Json.

    You need to 'project' the user data as GeoJson, the easiest way is to use an anonymous type then serialize it: this creates valid GeoJson according to this linter.

        var users = GetUsers();
    
        var userGeo = users.Select(u => new
            {
                type = "Feature",
                geometry = new
                {
                    type = "Point",
                    coordinates = new double[] { u.longitude, u.latitude }
                },
                properties = new {
                    name = "User " + u.userId.Trim()
                }
            }
        );
    
        var featureCollection = new {
            type = "FeatureCollection",
            features = userGeo
        };
    
        var geoJson = JsonConvert.SerializeObject(featureCollection, Formatting.Indented);
    

    Output:

    {
      "type": "FeatureCollection",
      "features": [
        {
          "type": "Feature",
          "geometry": {
            "type": "Point",
            "coordinates": [
              5.689,
              1.234
            ]
          },
          "properties": {
            "name": "User 1"
          }
        },
        {
          "type": "Feature",
          "geometry": {
            "type": "Point",
            "coordinates": [
              5.689,
              1.234
            ]
          },
          "properties": {
            "name": "User 2"
          }
        },
        {
          "type": "Feature",
          "geometry": {
            "type": "Point",
            "coordinates": [
              5.689,
              1.234
            ]
          },
          "properties": {
            "name": "User 3"
          }
        }
      ]
    }