def postcode(request, postcode, format=None): if hasattr(countries, 'canonical_postcode'): canon_postcode = countries.canonical_postcode(postcode) postcode = canon_postcode #if (postcode != canon_postcode and format is None) or format == 'json': # return redirect('mapit.views.postcodes.postcode', postcode=canon_postcode) if format is None: format = 'json' if not is_valid_postcode(postcode): raise ViewException(format, "Postcode '%s' is not valid." % postcode, 400) postcode = get_object_or_404(Postcode, format=format, postcode=postcode) try: generation = int(request.REQUEST['generation']) except: generation = Generation.objects.current() if not hasattr(countries, 'is_special_postcode') or not countries.is_special_postcode( postcode.postcode): areas = add_codes(Area.objects.by_postcode(postcode, generation)) else: areas = [] # Shortcuts shortcuts = {} for area in areas: if area.type.code in ('COP', 'LBW', 'LGE', 'MTW', 'UTE', 'UTW'): shortcuts['ward'] = area.id shortcuts['council'] = area.parent_area_id elif area.type.code == 'CED': shortcuts.setdefault('ward', {})['county'] = area.id shortcuts.setdefault('council', {})['county'] = area.parent_area_id elif area.type.code == 'DIW': shortcuts.setdefault('ward', {})['district'] = area.id shortcuts.setdefault('council', {})['district'] = area.parent_area_id elif area.type.code in ( 'WMC' ): # XXX Also maybe 'EUR', 'NIE', 'SPC', 'SPE', 'WAC', 'WAE', 'OLF', 'OLG', 'OMF', 'OMG'): shortcuts[area.type.code] = area.id # Add manual enclosing areas. extra = [] for area in areas: if area.type.code in enclosing_areas.keys(): extra.extend(enclosing_areas[area.type.code]) areas = itertools.chain(areas, Area.objects.filter(id__in=extra)) if format == 'html': return render( request, 'mapit/postcode.html', { 'postcode': postcode.as_dict(), 'areas': areas, 'json_view': 'mapit.views.postcodes.postcode', }) out = postcode.as_dict() out['areas'] = dict((area.id, area.as_dict()) for area in areas) if shortcuts: out['shortcuts'] = shortcuts return output_json(out)
def nearest(request, srid, x, y, format='json'): location = Point(float(x), float(y), srid=int(srid)) set_timeout(format) try: postcode = Postcode.objects.filter( location__distance_gte=(location, D(mi=0))).distance(location).order_by('distance')[0] except QueryCanceledError: raise ViewException(format, 'That query was taking too long to compute.', 500) except DatabaseError as e: if 'canceling statement due to statement timeout' not in e.args[0]: raise raise ViewException(format, 'That query was taking too long to compute.', 500) except: raise ViewException(format, 'No postcode found near %s,%s (%s)' % (x, y, srid), 404) if format == 'html': return render(request, 'mapit/postcode.html', { 'postcode': postcode.as_dict(), 'json_view': 'mapit.views.postcodes.postcode', }) pc = postcode.as_dict() pc['distance'] = round(postcode.distance.m) return output_json({ 'postcode': pc, })
def partial_postcode(request, postcode, format='json'): postcode = re.sub('\s+', '', postcode.upper()) if is_valid_postcode(postcode): postcode = re.sub('\d[A-Z]{2}$', '', postcode) if not is_valid_partial_postcode(postcode): raise ViewException(format, "Partial postcode '%s' is not valid." % postcode, 400) try: postcode = Postcode( postcode=postcode, location=Postcode.objects.filter( postcode__startswith=postcode).extra( where=['length(postcode) = %d' % (len(postcode) + 3)]).collect().centroid) except: raise ViewException(format, 'Postcode not found', 404) if format == 'html': return render( request, 'mapit/postcode.html', { 'postcode': postcode.as_dict(), 'json_view': 'mapit.views.postcodes.partial_postcode', }) return output_json(postcode.as_dict())
def postcode(request, postcode, format=None): if hasattr(countries, 'canonical_postcode'): canon_postcode = countries.canonical_postcode(postcode) postcode = canon_postcode # if (postcode != canon_postcode and format is None) or format == 'json': # return redirect('mapit.views.postcodes.postcode', postcode=canon_postcode) if format is None: format = 'json' if not is_valid_postcode(postcode): raise ViewException(format, "Postcode '%s' is not valid." % postcode, 400) postcode = get_object_or_404(Postcode, format=format, postcode=postcode) try: generation = int(request.REQUEST['generation']) except: generation = Generation.objects.current() if not hasattr(countries, 'is_special_postcode') or not countries.is_special_postcode(postcode.postcode): areas = add_codes(Area.objects.by_postcode(postcode, generation)) else: areas = [] # Shortcuts shortcuts = {} for area in areas: if area.type.code in ('COP', 'LBW', 'LGE', 'MTW', 'UTE', 'UTW'): shortcuts['ward'] = area.id shortcuts['council'] = area.parent_area_id elif area.type.code == 'CED': shortcuts.setdefault('ward', {})['county'] = area.id shortcuts.setdefault('council', {})['county'] = area.parent_area_id elif area.type.code == 'DIW': shortcuts.setdefault('ward', {})['district'] = area.id shortcuts.setdefault('council', {})['district'] = area.parent_area_id elif area.type.code in ('WMC',): # XXX Also maybe 'EUR', 'NIE', 'SPC', 'SPE', 'WAC', 'WAE', 'OLF', 'OLG', 'OMF', 'OMG'): shortcuts[area.type.code] = area.id # Add manual enclosing areas. extra = [] for area in areas: if area.type.code in enclosing_areas.keys(): extra.extend(enclosing_areas[area.type.code]) areas = itertools.chain(areas, Area.objects.filter(id__in=extra)) if format == 'html': return render(request, 'mapit/postcode.html', { 'postcode': postcode.as_dict(), 'areas': areas, 'json_view': 'mapit.views.postcodes.postcode', }) out = postcode.as_dict() out['areas'] = dict((area.id, area.as_dict()) for area in areas) if shortcuts: out['shortcuts'] = shortcuts return output_json(out)
def area(request, area_id, format='json'): if re.match('\d\d([A-Z]{2}|[A-Z]{4}|[A-Z]{2}\d\d\d|[A-Z]|[A-Z]\d\d)$', area_id): area = get_object_or_404(Area, format=format, codes__type='ons', codes__code=area_id) elif re.match('[ENSW]\d{8}$', area_id): area = get_object_or_404(Area, format=format, codes__type='gss', codes__code=area_id) elif not re.match('\d+$', area_id): return output_error(format, 'Bad area ID specified', 400) else: area = get_object_or_404(Area, format=format, id=area_id) if isinstance(area, HttpResponse): return area if format == 'html': return render(request, 'area.html', { 'area': area, 'show_geometry': (area.type not in ('EUR', 'SPE', 'WAE')) }) return output_json( area.as_dict() )
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='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='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))
def example_postcode_for_area(request, area_id, format='json'): area = get_object_or_404(Area, format=format, id=area_id) try: pc = Postcode.objects.filter(areas=area).order_by()[0] except: set_timeout(format) try: pc = Postcode.objects.filter_by_area(area).order_by()[0] except QueryCanceledError: raise ViewException(format, 'That query was taking too long to compute.', 500) except DatabaseError as e: if 'canceling statement due to statement timeout' not in e.args[0]: raise raise ViewException(format, 'That query was taking too long to compute.', 500) except: pc = None if pc: pc = pc.get_postcode_display() if format == 'html': return render(request, 'mapit/example-postcode.html', {'area': area, 'postcode': pc}) return output_json(pc)
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 partial_postcode(request, postcode, format='json'): postcode = re.sub('\s+', '', postcode.upper()) if is_valid_postcode(postcode): postcode = re.sub('\d[A-Z]{2}$', '', postcode) if not is_valid_partial_postcode(postcode): raise ViewException(format, "Partial postcode '%s' is not valid." % postcode, 400) try: postcode = Postcode( postcode = postcode, location = Postcode.objects.filter(postcode__startswith=postcode).extra( where = [ 'length(postcode) = %d' % (len(postcode)+3) ] ).collect().centroid ) except: raise ViewException(format, 'Postcode not found', 404) if format == 'html': return render(request, 'mapit/postcode.html', { 'postcode': postcode.as_dict(), 'json_view': 'mapit.views.postcodes.partial_postcode', }) return output_json(postcode.as_dict())
try: pc = Postcode.objects.filter(areas=area).order_by()[0] except: set_timeout(format) try: pc = Postcode.objects.filter_by_area(area).order_by()[0] except QueryCanceledError: raise ViewException(format, 'That query was taking too long to compute.', 500) except DatabaseError, e: if 'canceling statement due to statement timeout' not in e.args[0]: raise raise ViewException(format, 'That query was taking too long to compute.', 500) except: pc = None if pc: pc = pc.get_postcode_display() if format == 'html': return render(request, 'mapit/example-postcode.html', { 'area': area, 'postcode': pc }) return output_json(pc) def form_submitted(request): pc = request.POST.get('pc', None) if not request.method == 'POST' or not pc: return redirect('/') return redirect('mapit.views.postcodes.postcode', postcode=pc, format='html') @ratelimit(minutes=3, requests=100) def nearest(request, srid, x, y, format='json'): location = Point(float(x), float(y), srid=int(srid)) set_timeout(format) try: postcode = Postcode.objects.filter(location__distance_gte=( location, D(mi=0) )).distance(location).order_by('distance')[0] except QueryCanceledError:
except QueryCanceledError: raise ViewException(format, 'That query was taking too long to compute.', 500) except DatabaseError, e: if 'canceling statement due to statement timeout' not in e.args[0]: raise raise ViewException(format, 'That query was taking too long to compute.', 500) except: pc = None if pc: pc = pc.get_postcode_display() if format == 'html': return render(request, 'mapit/example-postcode.html', { 'area': area, 'postcode': pc }) return output_json(pc) def form_submitted(request): pc = request.POST.get('pc', None) if not request.method == 'POST' or not pc: return redirect('/') return redirect('mapit.views.postcodes.postcode', postcode=pc, format='html') @ratelimit(minutes=3, requests=100) def nearest(request, srid, x, y, format='json'):