def post(self, request, *args, **kwargs): feature = None tpi = request.META.get('HTTP_TEMP_POINT_ID', "none") aoi = request.POST.get('aoi') geometry = request.POST.get('geometry') geojson = json.loads(geometry) properties = geojson.get('properties') aoi = AOI.objects.get(id=aoi) job = getattr(aoi, 'job') project = getattr(job, 'project') template = properties.get('template') if properties else None # TODO: handle exceptions if template: template = FeatureType.objects.get(id=template) attrs = dict(aoi=aoi, job=job, project=project, analyst=request.user, template=template) geometry = geojson.get('geometry') geom_obj = GEOSGeometry(json.dumps(geometry)) attrs['the_geom'] = geom_obj county_list = Counties.objects.filter( poly__contains=geom_obj.centroid.wkt) county = None if len(county_list): county = str(county_list[0].name) try: feature = Feature(**attrs) feature.full_clean() if not feature.properties: feature.properties = {} if county: feature.properties['county'] = county feature.save() except ValidationError as e: response = HttpResponse(content=json.dumps( dict(errors=e.messages)), mimetype="application/json", status=400) response['Temp-Point-Id'] = tpi return response # This feels a bit ugly but it does get the GeoJSON into the response feature_json = serializers.serialize('json', [ feature, ]) feature_list = json.loads(feature_json) feature_list[0]['geojson'] = feature.geoJSON(True) response = HttpResponse(json.dumps(feature_list), mimetype="application/json") response['Temp-Point-Id'] = tpi return response
def post(self, request, *args, **kwargs): feature = None tpi = request.META.get('HTTP_TEMP_POINT_ID', "none") aoi = request.POST.get('aoi') geometry = request.POST.get('geometry') geojson = json.loads(geometry) properties = geojson.get('properties') aoi = AOI.objects.get(id=aoi) job = getattr(aoi, 'job') project = getattr(job, 'project') template = properties.get('template') if properties else None # TODO: handle exceptions if template: template = FeatureType.objects.get(id=template) attrs = dict(aoi=aoi, job=job, project=project, analyst=request.user, template=template) geometry = geojson.get('geometry') geom_obj = GEOSGeometry(json.dumps(geometry)) attrs['the_geom'] = geom_obj county_list = Counties.objects.filter(poly__contains=geom_obj.centroid.wkt) county = None if len(county_list): county = str(county_list[0].name) try: feature = Feature(**attrs) feature.full_clean() if not feature.properties: feature.properties = {} if county: feature.properties['county'] = county feature.save() except ValidationError as e: response = HttpResponse(content=json.dumps(dict(errors=e.messages)), mimetype="application/json", status=400) response['Temp-Point-Id'] = tpi return response # This feels a bit ugly but it does get the GeoJSON into the response feature_json = serializers.serialize('json', [feature,]) feature_list = json.loads(feature_json) feature_list[0]['geojson'] = feature.geoJSON(True) response = HttpResponse(json.dumps(feature_list), mimetype="application/json") response['Temp-Point-Id'] = tpi return response
def post(self, request, *args, **kwargs): feature = None aoi = request.POST.get('aoi') geometry = request.POST.get('geometry') geojson = json.loads(geometry) properties = geojson.get('properties') aoi = AOI.objects.get(id=aoi) job = getattr(aoi, 'job') project = getattr(job, 'project') template = properties.get('template') if properties else None # TODO: handle exceptions if template: template = FeatureType.objects.get(id=template) attrs = dict(aoi=aoi, job=job, project=project, analyst=request.user, template=template) geometry = geojson.get('geometry') attrs['the_geom'] = GEOSGeometry(json.dumps(geometry)) try: feature = Feature(**attrs) feature.full_clean() feature.save() except ValidationError as e: return HttpResponse(content=json.dumps(dict(errors=e.messages)), mimetype="application/json", status=400) # This feels a bit ugly but it does get the GeoJSON into the response feature_json = serializers.serialize('json', [ feature, ]) feature_list = json.loads(feature_json) feature_list[0]['geojson'] = feature.geoJSON(True) return HttpResponse(json.dumps(feature_list), mimetype="application/json")
def post(self, request, *args, **kwargs): feature = None aoi = request.POST.get('aoi') geometry = request.POST.get('geometry') geojson = json.loads(geometry) properties = geojson.get('properties') aoi = AOI.objects.get(id=aoi) job = getattr(aoi, 'job') project = getattr(job, 'project') template = properties.get('template') if properties else None # TODO: handle exceptions if template: template = FeatureType.objects.get(id=template) attrs = dict(aoi=aoi, job=job, project=project, analyst=request.user, template=template) geometry = geojson.get('geometry') attrs['the_geom'] = GEOSGeometry(json.dumps(geometry)) try: feature = Feature(**attrs) feature.full_clean() feature.save() except ValidationError as e: return HttpResponse(content=json.dumps(dict(errors=e.messages)), mimetype="application/json", status=400) # This feels a bit ugly but it does get the GeoJSON into the response feature_json = serializers.serialize('json', [feature,]) feature_list = json.loads(feature_json) feature_list[0]['geojson'] = feature.geoJSON(True) return HttpResponse(json.dumps(feature_list), mimetype="application/json")