本文整理汇总了Python中mapit.shortcuts.output_json函数的典型用法代码示例。如果您正苦于以下问题:Python output_json函数的具体用法?Python output_json怎么用?Python output_json使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了output_json函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: area_polygon
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
示例2: postcode
def postcode(request, postcode, legacy=False, format='json'):
postcode = check_postcode(postcode)
if isinstance(postcode, HttpResponse): return postcode
try:
generation = int(request.REQUEST['generation'])
except:
generation = Generation.objects.current()
areas = Area.objects.by_postcode(postcode, generation)
# Add manual enclosing areas.
extra = []
for area in areas:
if area.type in enclosing_areas.keys():
extra.extend(enclosing_areas[area.type])
areas = itertools.chain(areas, Area.objects.filter(id__in=extra))
if legacy:
areas = dict( (area.type, area.id) for area in areas )
return output_json(areas)
if format == 'html':
return render_to_response('postcode.html', {
'postcode': postcode.as_dict(),
'areas': areas,
})
out = postcode.as_dict()
out['areas'] = dict( ( area.id, area.as_dict() ) for area in areas )
return output_json(out)
示例3: area_polygon
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)
示例4: area_polygon
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)
示例5: area
def area(request, area_id, legacy=False):
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, codes__type='ons', codes__code=area_id)
elif not re.match('\d+$', area_id):
return output_json({ 'error': 'Bad area ID specified' }, code=400)
else:
area = get_object_or_404(Area, id=area_id)
if isinstance(area, HttpResponse): return area
return output_json( area.as_dict() )
示例6: area_children
def area_children(request, area_id, legacy=False, format='json'):
area = get_object_or_404(Area, id=area_id)
if isinstance(area, HttpResponse): return area
generation = Generation.objects.current()
children = add_codes(area.children.filter(
generation_low__lte=generation, generation_high__gte=generation
))
if legacy: return output_json( [ child.id for child in children ] )
if format == 'html': return output_html( children )
return output_json( dict( (child.id, child.as_dict() ) for child in children ) )
示例7: area_touches
def area_touches(request, area_id, format='json'):
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)
areas = Area.objects.filter(polygons__polygon__touches=all_areas, type=area.type)
if format == 'html': return output_html( areas )
return output_json( dict( (a.id, a.as_dict() ) for a in areas ) )
示例8: deal_with_POST
def deal_with_POST(request, call="areas"):
url = request.POST.get("URL", "")
if not url:
return output_json({"error": "No content specified"}, code=400)
view, args, kwargs = resolve("/%s/%s" % (call, url))
kwargs["request"] = request
return view(*args, **kwargs)
示例9: _area_geometry
def _area_geometry(area_id):
area = get_object_or_404(Area, id=area_id)
all_areas = area.polygons.all().collect()
if not all_areas:
return output_json({"error": "No polygons found"}, code=404)
out = {"parts": all_areas.num_geom}
if settings.MAPIT_AREA_SRID != 4326:
out["srid_en"] = settings.MAPIT_AREA_SRID
out["area"] = all_areas.area
out["min_e"], out["min_n"], out["max_e"], out["max_n"] = all_areas.extent
out["centre_e"], out["centre_n"] = all_areas.centroid
all_areas.transform(4326)
out["min_lon"], out["min_lat"], out["max_lon"], out["max_lat"] = all_areas.extent
out["centre_lon"], out["centre_lat"] = all_areas.centroid
else:
out["min_lon"], out["min_lat"], out["max_lon"], out["max_lat"] = all_areas.extent
out["centre_lon"], out["centre_lat"] = all_areas.centroid
if hasattr(countries, "area_geometry_srid"):
srid = countries.area_geometry_srid
all_areas.transform(srid)
out["srid_en"] = srid
out["area"] = all_areas.area
out["min_e"], out["min_n"], out["max_e"], out["max_n"] = all_areas.extent
out["centre_e"], out["centre_n"] = all_areas.centroid
return out
示例10: deal_with_POST
def deal_with_POST(request, call='areas'):
url = request.POST.get('URL', '')
if not url:
return output_json({ 'error': 'No content specified' }, code=400)
view, args, kwargs = resolve('/%s/%s' % (call, url))
kwargs['request'] = request
return view(*args, **kwargs)
示例11: _area_geometry
def _area_geometry(area_id):
area = get_object_or_404(Area, id=area_id)
if isinstance(area, HttpResponse): return area
all_areas = area.polygons.all().collect()
if not all_areas:
return output_json({ 'error': 'No polygons found' }, code=404)
out = {
'parts': all_areas.num_geom,
}
if settings.MAPIT_AREA_SRID != 4326:
out['srid_en'] = settings.MAPIT_AREA_SRID
out['area'] = all_areas.area
out['min_e'], out['min_n'], out['max_e'], out['max_n'] = all_areas.extent
out['centre_e'], out['centre_n'] = all_areas.centroid
all_areas.transform(4326)
out['min_lon'], out['min_lat'], out['max_lon'], out['max_lat'] = all_areas.extent
out['centre_lon'], out['centre_lat'] = all_areas.centroid
else:
out['min_lon'], out['min_lat'], out['max_lon'], out['max_lat'] = all_areas.extent
out['centre_lon'], out['centre_lat'] = all_areas.centroid
if hasattr(countries, 'area_geometry_srid'):
srid = countries.area_geometry_srid
all_areas.transform(srid)
out['srid_en'] = srid
out['area'] = all_areas.area
out['min_e'], out['min_n'], out['max_e'], out['max_n'] = all_areas.extent
out['centre_e'], out['centre_n'] = all_areas.centroid
return out
示例12: get_voting_areas_info
def get_voting_areas_info(request, area_ids):
area_ids = area_ids.split(',')
out = {}
for id in area_ids:
area = _get_voting_area_info(id)
out[id] = {} if isinstance(area, HttpResponse) else area
return output_json(out)
示例13: areas_by_name
def areas_by_name(request, name, legacy=False, format='json'):
generation = Generation.objects.current()
try:
min_generation = int(request.REQUEST['min_generation'])
except:
min_generation = generation
type = request.REQUEST.get('type', '')
args = {
'name__istartswith': name,
'generation_low__lte': generation,
'generation_high__gte': min_generation,
}
if ',' in type:
args['type__in'] = type.split(',')
elif type:
args['type'] = type
areas = add_codes(Area.objects.filter(**args))
if legacy:
out = dict( (area.id, {
'area_id': area.id,
'name': area.name,
'type': area.type,
'parent_area_id': area.parent_area_id,
}) for area in areas )
else:
out = dict( ( area.id, area.as_dict() ) for area in areas )
if format == 'html': return output_html(request, 'Areas starting with %s' % name, areas)
return output_json(out)
示例14: _area_geometry
def _area_geometry(area_id):
area = get_object_or_404(Area, id=area_id)
if isinstance(area, HttpResponse): return area
all_areas = area.polygons.all().collect()
if not all_areas:
return output_json({ 'error': 'No polygons found' }, code=404)
out = {
'parts': all_areas.num_geom,
}
if int(mysociety.config.get('AREA_SRID')) != 4326:
out['srid_en'] = mysociety.config.get('AREA_SRID')
out['area'] = all_areas.area
out['min_e'], out['min_n'], out['max_e'], out['max_n'] = all_areas.extent
out['centre_e'], out['centre_n'] = all_areas.centroid
all_areas.transform(4326)
out['min_lon'], out['min_lat'], out['max_lon'], out['max_lat'] = all_areas.extent
out['centre_lon'], out['centre_lat'] = all_areas.centroid
elif mysociety.config.get('COUNTRY') == 'NO':
out['min_lon'], out['min_lat'], out['max_lon'], out['max_lat'] = all_areas.extent
out['centre_lon'], out['centre_lat'] = all_areas.centroid
all_areas.transform(32633)
out['srid_en'] = 32633
out['area'] = all_areas.area
out['min_e'], out['min_n'], out['max_e'], out['max_n'] = all_areas.extent
out['centre_e'], out['centre_n'] = all_areas.centroid
return out
示例15: nearest
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,
})