def _areas_polygon(request, format, areas, srid=None): args = query_args_polygon(request, format, srid) serialiser = GeometrySerialiser(list(areas), args['srid'], args['simplify_tolerance']) try: if format == 'kml': output, content_type = serialiser.kml('full') elif format == 'geojson': output, content_type = serialiser.geojson() except TransformError as e: return output_json({'error': e.args[0]}, code=400) return output_polygon(content_type, output)
def areas_polygon(request, area_ids, srid='', format='kml'): area_ids = area_ids.split(',') args = query_args_polygon(request, format, srid, area_ids) areas = list(Area.objects.filter(id__in=area_ids)) if not areas: return output_json({'error': _('No areas found')}, code=404) serialiser = GeometrySerialiser(areas, args['srid'], args['simplify_tolerance']) try: if format == 'kml': output, content_type = serialiser.kml('full') elif format == 'geojson': output, content_type = serialiser.geojson() except TransformError as e: return output_json({'error': e.args[0]}, code=400) return output_polygon(content_type, output)
def export(self, srid, export_format, simplify_tolerance=0, line_colour="70ff0000", fill_colour="3dff5500", kml_type="full"): """Generate a representation of the area in KML, GeoJSON or WKT This returns a tuple of (data, content_type), which are strings representing the data itself and its MIME type. If there are no polygons associated with this area (None, None) is returned. 'export_format' may be one of 'kml', 'wkt, 'json' and 'geojson', the last two being synonymous. The 'srid' parameter specifies the coordinate system that the polygons should be transformed into before being exported, if it is different from this MapIt. simplify_tolerance, if non-zero, is passed to django.contrib.gis.geos.GEOSGeometry.simplify for simplifying the polygon boundary before export. The line_colour and fill_colour parameters are only used if the export type is KML and kml_type is 'full'. The 'kml_type' parameter may be either 'full' (in which case a complete, valid KML file is returned) or 'polygon' (in which case just the <Polygon> element is returned). If the simplify_tolerance provided is large enough that all the polygons completely disappear under simplification, or something else goes wrong with the spatial transform, then a TransformError exception is raised. """ all_polygons = self.polygons.all() if len(all_polygons) == 0: return (None, None) serialiser = GeometrySerialiser(self, srid, simplify_tolerance) if export_format == 'kml': out, content_type = serialiser.kml(kml_type, line_colour, fill_colour) elif export_format in ('json', 'geojson'): out, content_type = serialiser.geojson() elif export_format == 'wkt': out, content_type = serialiser.wkt() return (out, content_type)