class CoverageController(BaseController): """Converts coverage area descriptions to GeoJSON documents so they can be visualized. """ def geojson_response(self, document): if isinstance(document, dict): document = json.dumps(document) headers = {"Content-Type": "application/geo+json"} return Response(document, 200, headers=headers) def lookup(self): coverage = flask.request.args.get('coverage') try: coverage = json.loads(coverage) except ValueError, e: pass places, unknown, ambiguous = AuthenticationDocument.parse_coverage( self._db, coverage) document = Place.to_geojson(self._db, *places) # Extend the GeoJSON with extra information about parts of the # coverage document we found ambiguous or couldn't associate # with a Place. if unknown: document['unknown'] = unknown if ambiguous: document['ambiguous'] = ambiguous return self.geojson_response(document)
def _geojson_for_service_area(self, service_type): """Serve a GeoJSON document describing some subset of the active library's service areas. """ areas = [ x.place for x in flask.request.library.service_areas if x.type == service_type ] return self.geojson_response(Place.to_geojson(self._db, *areas))