GeoJSON Renderer¶
Papyrus provides a GeoJSON renderer, based on Sean Gillies’ geojson package.
To use it the GeoJSON renderer factory must be added to the application configuration.
For that you can either pass the factory to the Configurator
constructor:
from pyramid.mako_templating import renderer_factory as mako_renderer_factory
from papyrus.renderers import GeoJSON
config = Configurator(
renderers=(('.mako', mako_renderer_factory),
('geojson', GeoJSON()))
)
Or you can apply the add_renderer
method to the Configurator
instance:
from papyrus.renderers import GeoJSON
config.add_renderer('geojson', GeoJSON())
Make sure that add_renderer
is called before any add_view
call that
names geojson
as an argument.
To use the GeoJSON renderer in a view set renderer
to geojson
in the
view config. Here is a simple example:
@view_config(renderer='geojson')
def hello_world(request):
return {
'type': 'Feature',
'id': 1,
'geometry': {'type': 'Point', 'coordinates': [53, -4]},
'properties': {'title': 'Dict 1'},
}
Views configured with the geojson
renderer must return objects that
implement the Python Geo Interface.
Here’s another example where the returned object is an SQLAlchemy (or GeoAlchemy) mapped object:
@view_config(renderer='geojson')
def features(request):
return Session().query(Spot).all()
In the above example the Spot
objects returned by the query
call must
implement the Python Geo Interface.
Notes:
The GeoJSON renderer supports JSONP. The renderer indeed checks if there’s a
callback
parameter in the query string, and if there’s one it wraps the response in a JavaScript call and sets the response content type totext/javascript
.The application developer can also specify the name of the JSONP callback parameter, using this:
from papyrus.renderers import GeoJSON config.add_renderer('geojson', GeoJSON(jsonp_param_name='cb'))
With this, if there’s a parameter named
cb
in the query string, the renderer will return a JSONP response.By default, lists and tuples passed to the renderer will be rendered as FeatureCollection. You can change this using the
collection_type
argument:from papyrus.renderers import GeoJSON config.add_renderer('geojson', GeoJSON(collection_type='GeometryCollection'))
API Reference¶
-
class
papyrus.renderers.
GeoJSON
(jsonp_param_name='callback', collection_type=<class 'geojson.feature.FeatureCollection'>)[source]¶ GeoJSON renderer.
This class is actually a renderer factory helper, implemented in the same way as Pyramid’s JSONP renderer.
Configure a GeoJSON renderer using the
add_renderer
method on the Configurator object:from papyrus.renderers import GeoJSON config.add_renderer('geojson', GeoJSON())
Once this renderer has been registered as above , you can use
geojson
as therenderer
parameter to@view_config
or to theadd_view
method on the Configurator object:@view_config(renderer='geojson') def myview(request): return Feature(id=1, geometry=Point(1, 2), properties=dict(foo='bar'))
The GeoJSON renderer supports JSONP:
- If there is a parameter in the request’s HTTP query string that matches
the
jsonp_param_name
of the registered JSONP renderer (by default,callback
), the renderer will return a JSONP response. - If there is no callback parameter in the request’s query string, the renderer will return a ‘plain’ JSON response.
By default the renderer treats lists and tuples as feature collections. If you want lists and tuples to be treated as geometry collections, set
collection_type
to'GeometryCollection'
:config.add_renderer( 'geojson', GeoJSON(collection_type='GeometryCollection')
- If there is a parameter in the request’s HTTP query string that matches
the