def area_polygon(request, srid='', area_id='', format='kml'): if not srid and hasattr(countries, 'area_code_lookup'): resp = countries.area_code_lookup(request, area_id, format) if resp: return resp if not re.match('\d+$', area_id): raise ViewException(format, 'Bad area ID specified', 400) if not srid: srid = 4326 if format in ('kml', 'json', 'geojson') else settings.MAPIT_AREA_SRID srid = int(srid) area = get_object_or_404(Area, id=area_id) try: simplify_tolerance = float(request.GET.get('simplify_tolerance', 0)) except ValueError: raise ViewException(format, 'Badly specified tolerance', 400) try: output, content_type = area.export(srid, format, simplify_tolerance=simplify_tolerance) if output is None: return output_json({'error': 'No polygons found'}, code=404) except SimplifiedAway: return output_json({'error': 'Simplifying removed all the polygons'}, code=404) return HttpResponse(output, content_type='%s; charset=utf-8' % content_type)
def area_polygon(request, srid='', area_id='', format='kml'): if not srid and hasattr(countries, 'area_code_lookup'): resp = countries.area_code_lookup(request, area_id, format) if resp: return resp if not re.match('\d+$', area_id): raise ViewException(format, 'Bad area ID specified', 400) if not srid: srid = 4326 if format in ('kml', 'json', 'geojson') else settings.MAPIT_AREA_SRID srid = int(srid) area = get_object_or_404(Area, id=area_id) try: simplify_tolerance = float(request.GET.get('simplify_tolerance', 0)) except ValueError: raise ViewException(format, 'Badly specified tolerance', 400) try: output, content_type = area.export(srid, format, simplify_tolerance=simplify_tolerance) if output is None: return output_json({'error': 'No polygons found'}, code=404) except TransformError as e: return output_json({'error': e.args[0]}, code=400) response = HttpResponse(content_type='%s; charset=utf-8' % content_type) response['Access-Control-Allow-Origin'] = '*' response['Cache-Control'] = 'max-age=2419200' # 4 weeks response.write(output) return response
def area_polygon(request, srid="", area_id="", format="kml"): if not srid and hasattr(countries, "area_code_lookup"): resp = countries.area_code_lookup(request, area_id, format) if resp: return resp if not re.match("\d+$", area_id): raise ViewException(format, "Bad area ID specified", 400) if not srid: srid = 4326 if format in ("kml", "json", "geojson") else settings.MAPIT_AREA_SRID srid = int(srid) area = get_object_or_404(Area, id=area_id) try: simplify_tolerance = float(request.GET.get("simplify_tolerance", 0)) except ValueError: raise ViewException(format, "Badly specified tolerance", 400) try: output, content_type = area.export(srid, format, simplify_tolerance=simplify_tolerance) if output is None: return output_json({"error": "No polygons found"}, code=404) except SimplifiedAway: return output_json({"error": "Simplifying removed all the polygons"}, code=404) return HttpResponse(output, content_type="%s; charset=utf-8" % content_type)
def area_polygon(request, srid='', area_id='', format='kml'): if not srid and hasattr(countries, 'area_code_lookup'): resp = countries.area_code_lookup(area_id, format) if resp: return resp if not re.match('\d+$', area_id): return output_error(format, 'Bad area ID specified', 400) if not srid: srid = 4326 if format in ('kml', 'json', 'geojson') else settings.MAPIT_AREA_SRID srid = int(srid) area = get_object_or_404(Area, id=area_id) if isinstance(area, HttpResponse): return area all_areas = area.polygons.all() if len(all_areas) > 1: all_areas = all_areas.collect() elif len(all_areas) == 1: all_areas = all_areas[0].polygon else: return output_json({ 'error': 'No polygons found' }, code=404) if srid != settings.MAPIT_AREA_SRID: all_areas.transform(srid) try: simplify_tolerance = float(request.GET.get('simplify_tolerance', 0)) except: return output_error(format, 'Badly specified tolerance', 400) if simplify_tolerance: all_areas = all_areas.simplify(simplify_tolerance) if format=='kml': out = '''<?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://www.opengis.net/kml/2.2"> <Style id="transBluePoly"> <LineStyle> <color>70ff0000</color> <width>2</width> </LineStyle> <PolyStyle> <color>3dff5500</color> </PolyStyle> </Style> <Placemark> <styleUrl>#transBluePoly</styleUrl> <name>%s</name> %s </Placemark> </kml>''' % (escape(area.name), all_areas.kml) content_type = 'application/vnd.google-earth.kml+xml' elif format in ('json', 'geojson'): out = all_areas.json content_type = 'application/json' elif format=='wkt': out = all_areas.wkt content_type = 'text/plain' return HttpResponse(out, content_type='%s; charset=utf-8' % content_type)
def area_polygon(request, srid='', area_id='', format='kml'): if not srid and hasattr(countries, 'area_code_lookup'): resp = countries.area_code_lookup(area_id, format) if resp: return resp if not re.match('\d+$', area_id): raise ViewException(format, 'Bad area ID specified', 400) if not srid: srid = 4326 if format in ('kml', 'json', 'geojson') else settings.MAPIT_AREA_SRID srid = int(srid) area = get_object_or_404(Area, id=area_id) all_areas = area.polygons.all() if len(all_areas) > 1: all_areas = all_areas.collect() elif len(all_areas) == 1: all_areas = all_areas[0].polygon else: return output_json({ 'error': 'No polygons found' }, code=404) if srid != settings.MAPIT_AREA_SRID: all_areas.transform(srid) try: simplify_tolerance = float(request.GET.get('simplify_tolerance', 0)) except: raise ViewException(format, 'Badly specified tolerance', 400) if simplify_tolerance: all_areas = all_areas.simplify(simplify_tolerance) if format=='kml': out = '''<?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://www.opengis.net/kml/2.2"> <Style id="transBluePoly"> <LineStyle> <color>70ff0000</color> <width>2</width> </LineStyle> <PolyStyle> <color>3dff5500</color> </PolyStyle> </Style> <Placemark> <styleUrl>#transBluePoly</styleUrl> <name>%s</name> %s </Placemark> </kml>''' % (escape(area.name), all_areas.kml) content_type = 'application/vnd.google-earth.kml+xml' elif format in ('json', 'geojson'): out = all_areas.json content_type = 'application/json' elif format=='wkt': out = all_areas.wkt content_type = 'text/plain' return HttpResponse(out, content_type='%s; charset=utf-8' % content_type)
def area(request, area_id, format='json'): if hasattr(countries, 'area_code_lookup'): resp = countries.area_code_lookup(area_id, format) if resp: return resp if not re.match('\d+$', area_id): return output_error(format, 'Bad area ID specified', 400) area = get_object_or_404(Area, format=format, id=area_id) if isinstance(area, HttpResponse): return area if format == 'html': return render(request, 'mapit/area.html', { 'area': area, 'show_geometry': (area.type.code not in ('EUR', 'SPE', 'WAE')) }) return output_json( area.as_dict() )
def area_polygon(request, srid='', area_id='', format='kml'): if not srid and hasattr(countries, 'area_code_lookup'): resp = countries.area_code_lookup(request, area_id, format) if resp: return resp args = query_args_polygon(request, format, srid, [area_id]) area = get_object_or_404(Area, id=area_id) try: output, content_type = area.export(args['srid'], format, simplify_tolerance=args['simplify_tolerance']) if output is None: return output_json({'error': _('No polygons found')}, code=404) except TransformError as e: return output_json({'error': e.args[0]}, code=400) return output_polygon(content_type, output)
def area(request, area_id, format='json'): if hasattr(countries, 'area_code_lookup'): resp = countries.area_code_lookup(area_id, format) if resp: return resp if not re.match('\d+$', area_id): return output_error(format, 'Bad area ID specified', 400) area = get_object_or_404(Area, format=format, id=area_id) if isinstance(area, HttpResponse): return area if format == 'html': return render( request, 'mapit/area.html', { 'area': area, 'show_geometry': (area.type.code not in ('EUR', 'SPE', 'WAE')) }) return output_json(area.as_dict())
def area(request, area_id, format=''): if hasattr(countries, 'area_code_lookup'): resp = countries.area_code_lookup(request, area_id, format) if resp: return resp if not re.match(r'\d+$', area_id): raise ViewException(format, _('Bad area ID specified'), 400) area = get_object_or_404(Area, format=format, id=area_id) codes = [] for code_type, code in sorted(area.all_codes.items()): code_link = None if code_type in ('osm', 'osm_rel'): code_link = 'http://www.openstreetmap.org/browse/relation/' + code elif code_type == 'osm_way': code_link = 'http://www.openstreetmap.org/browse/way/' + code codes.append((code_type, code, code_link)) # Sort any alternative names by the description of the name (the # English name of the language for global MapIt) and exclude the # default OSM name, since if that exists, it'll already be # displayed as the page title. names = Name.objects.filter(area=area).select_related() alternative_names = sorted((n.type.description, n.name) for n in names if n.type.code != "default") geotype = {} if hasattr(countries, 'restrict_geo_html'): geotype = countries.restrict_geo_html(area) if format == 'html': return render( request, 'mapit/area.html', { 'area': area, 'codes': codes, 'alternative_names': alternative_names, 'geotype': geotype, }) return output_json(area.as_dict(names))
def area(request, area_id, format='json'): if hasattr(countries, 'area_code_lookup'): resp = countries.area_code_lookup(request, area_id, format) if resp: return resp if not re.match('\d+$', area_id): raise ViewException(format, 'Bad area ID specified', 400) area = get_object_or_404(Area, format=format, id=area_id) codes = [] for code_type, code in sorted(area.all_codes.items()): code_link = None if code_type in ('osm', 'osm_rel'): code_link = 'http://www.openstreetmap.org/browse/relation/' + code elif code_type == 'osm_way': code_link = 'http://www.openstreetmap.org/browse/way/' + code codes.append((code_type, code, code_link)) # Sort any alternative names by the description of the name (the # English name of the language for global MapIt) and exclude the # default OSM name, since if that exists, it'll already be # displayed as the page title. names = Name.objects.filter(area=area).select_related() alternative_names = sorted((n.type.description, n.name) for n in names if n.type.code != "default") geotype = {} if hasattr(countries, 'restrict_geo_html'): geotype = countries.restrict_geo_html(area) if format == 'html': return render(request, 'mapit/area.html', { 'area': area, 'codes': codes, 'alternative_names': alternative_names, 'geotype': geotype, }) return output_json( area.as_dict(names) )
def area(request, area_id, format="json"): if hasattr(countries, "area_code_lookup"): resp = countries.area_code_lookup(request, area_id, format) if resp: return resp if not re.match("\d+$", area_id): raise ViewException(format, "Bad area ID specified", 400) area = get_object_or_404(Area, format=format, id=area_id) codes = [] for code_type, code in sorted(area.all_codes.items()): code_link = None if code_type in ("osm", "osm_rel"): code_link = "http://www.openstreetmap.org/browse/relation/" + code elif code_type == "osm_way": code_link = "http://www.openstreetmap.org/browse/way/" + code codes.append((code_type, code, code_link)) # Sort any alternative names by the description of the name (the # English name of the language for global MapIt) and exclude the # default OSM name, since if that exists, it'll already be # displayed as the page title. names = Name.objects.filter(area=area).select_related() alternative_names = sorted((n.type.description, n.name) for n in names if n.type.code != "default") geotype = {} if hasattr(countries, "restrict_geo_html"): geotype = countries.restrict_geo_html(area) if format == "html": return render( request, "mapit/area.html", {"area": area, "codes": codes, "alternative_names": alternative_names, "geotype": geotype}, ) return output_json(area.as_dict(names))