How to serve GeoJSON features using Google Endpoints API within App Engine (GAE)

90 views
Skip to first unread message

Jamie Vleeshouwer

unread,
Nov 20, 2014, 1:49:35 AM11/20/14
to google-a...@googlegroups.com

We are interested in serving out geospatial features in a standardised data exchange format using the Google Endpoints API. We have used the API to be able to serve out Point features (code below), and overlay the points on to a map using the Google Maps API. Now we would like to serve out polygons and are having difficulty constructing the required response using the Endpoints API.

Example Of GeoJSON Point:

{ "type": "FeatureCollection",
   
"features": [
     
{ "type": "Feature",
       
"geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
       
"properties": {"name": "my point"}
     
}
     
]
   
}


To do this we defined the following Endpoints Message Classes:

class Geometry(messages.Message):
    type
= messages.StringField(1)
    coordinates
= messages.FloatField(2, repeated=True)

class Properties(messages.Message):
    name
= messages.StringField(1)

class Feature(messages.Message):
    type
= messages.StringField(1)
    geometry
= messages.MessageField(Geometry, 2)
    properties
= messages.MessageField(Properties, 3)

class FeatureCollection(messages.Message):
    type
= messages.StringField(1)
    features
= messages.MessageField(Feature, 2, repeated = True)


A GeoJSON Point feature collection could then be constructed using the following Endpoint code:

@endpoints.method(message_types.VoidMessage, FeatureCollection,
                  path
='user/mus', http_method='GET',
                  name
='user.mus.getManagementUnits')

def user_endpoint_mu_list(self, unused_request):

    feats
= []

    coords
= [102.0, 0.5]
    geom
= Geometry(type='Point', coordinates=coords)
    prop
= Properties(name='my point')

    feat
= Feature(type='Feature', geometry=geom, properties=prop)
    feats
.append(feat)

    ret
= FeatureCollection(type='FeatureCollection', features=feats)

   
return ret


The point could then be overlayed on to google maps by invoking the Endpoint using the following javascript code:

gapi.client.irrisat.user.mus.getManagementUnits().execute(
     
function (result) {
          map
.data.addGeoJson(result);
     
});


Now we would like to serve out polygon feature types using the Endpoints API (example response below). This is similar to the point response however the type is now "Polygon", and the coordinates for the polygon are described by an array of coordinate point arrays:

{ "type": "FeatureCollection",
   
"features": [
     
{ "type": "Feature",
       
"geometry": {"type": "Polygon", "coordinates": [[102.0, 0.5], [103.0, 0.8], [105.0, 0.2]]},
       
"properties": {"name": "my polygon"}
     
}
     
]
   
}


We are unsure how to construct the "coordinates" field for a polygon response. The Endpoints API specified the following types can be used in an Endpoints response:

class BooleanField()
class BytesField()
class EnumField()
class FloatField()
class IntegerField()
class MessageField()
class StringField()
class DateTimeField()


However the type required for the coordinates field for a polygon is an Array of Arrays so to speak. Is there a way to properly construct a GeoJSON polygon response according to the GeoJSON specifications?

Jamie

Reply all
Reply to author
Forward
0 new messages