GeoJSON Web API

This example shows how to return GeoJSON data using the ASP.NET Web API methodology.

The following shows some of the code for generating GeoJSON data using the ASP.NET Web API methodology:
  • Here are the classes to support GeoJSON formatting
        Public Enum FeatureItemType
            Feature
        End Enum

        Public Enum FeatureGeometryType
            Point
            Polygon
            Polyline
            MultiPolygon
        End Enum

        ' Defines the FeatureCollection class used by the GeoJSON output format.
        Public Class FeatureCollection
            Public type As String = "FeatureCollection"
            Public features As New List(Of FeatureItem)
        End Class

        ' Defines a single feature item for the features collection.
        Public Class FeatureItem
            Public id As String
            Public type As String
            Public properties As New Dictionary(Of String, String)
            Public geometry As New FeatureGeometry
        End Class

        ' Defines the geometry for a feature.
        Public Class FeatureGeometry
            Public type As String
            Public coordinates As Object
        End Class
      
  • Here is the PointsOfInterestController method for generating the FeatureCollection result.
            ' Find points by category and keyword
            ' GET api/pointsofinterest/find/{category}/{keyword}
            <Route("find/{category}/{keyword}")>
            <System.Web.Http.HttpGet>
            Public Function FindPoints(ByVal category As String, ByVal keyword As String) As IHttpActionResult
                ' Return result as GeoJSON format
                Dim geojson As New FeatureCollection
                geojson.type = "FeatureCollection"

                ' Retrieve POI data from source database
                Dim data As DataTable = GetPOIData()

                ' Unsanitize category name since it has special character not allowable by Web API
                category = category.Replace("-and-", "&")
                keyword = keyword.Replace("*", "%").Replace("(any)", "%")

                ' Perform data filtering
                Dim filterExpression As String = String.Format("CATEGORY = '{0}' AND NAME LIKE '%{1}%'", category.Trim(), keyword.Trim())
                Dim matchRows As DataRow() = data.Select(filterExpression)

                ' Add features to FeatureCollection GeoJSON object
                Dim rowCount As Integer = 0

                For Each row As DataRow In matchRows
                    Dim feature As New FeatureItem
                    feature.id = row("ID")
                    feature.type = FeatureItemType.Feature.ToString
                    feature.properties.Add("NAME", row("NAME"))
                    feature.properties.Add("CATEGORY", row("CATEGORY"))
                    feature.properties.Add("COLOR", row("COLOR"))
                    feature.geometry.type = FeatureGeometryType.Point.ToString
                    feature.geometry.coordinates = {row("X"), row("Y")}

                    geojson.features.Add(feature)
                Next

                Return Ok(geojson)
            End Function
        
The end result is the service returns data in GeoJSON format ready to be consumed by OpenLayers!
            {
              "type": "FeatureCollection",
              "features": [{
                "id": "1167",
                "type": "Feature",
                "properties": {
                  "NAME": "Cafe",
                  "CATEGORY": "Eating&Drinking",
                  "COLOR": "#58FA58"
                },
                "geometry": {
                  "type": "Point",
                  "coordinates": [-105.2471939,
                  40.0149442]
                }
              },
              {
                "id": "1203",
                "type": "Feature",
                "properties": {
                  "NAME": "Cafe",
                  "CATEGORY": "Eating&Drinking",
                  "COLOR": "#58FA58"
                },
                "geometry": {
                  "type": "Point",
                  "coordinates": [-105.11558,
                  40.5730241]
                }
              }]
            }