def initVectorViewer(request, shapefile_id): try: shapefile = Shapefile.objects.get(id=shapefile_id, created_by=request.user) nodes = shapefile.get_ancestors(include_self=True).exclude(level=0) features = Feature.objects.filter(shapefile=shapefile) geometryField = calcGeometryField(shapefile.geom_type) except Shapefile.DoesNotExist or Feature.DoesNotExist: raise Http404 bounds = list(features.extent(field_name=geometryField)) tmsURL = "http://" + request.get_host() + "/basqui/tms/" html = render_to_string("layers/vector/layers_vector_viewer.html", {'nodes': nodes, 'shapefile': shapefile },context_instance=RequestContext(request)) js = render_to_string("layers/vector/js/layers_vector_viewer.js", {'bounds': bounds, 'shapefile' : shapefile, 'tmsURL' : tmsURL},context_instance=RequestContext(request)) response = json.dumps({'html': html, 'js':js}) return HttpResponse(response, content_type="application/json")
def simplifyVectorLayer(request): post_dict = dict(request.POST.copy().iteritems()) layer_id = post_dict['id'] ratio = post_dict['ratio'] layer = Shapefile.objects.get(pk=layer_id) features = Feature.objects.filter(shapefile=layer) geometryField = calcGeometryField(layer.geom_type) for feat in features: geometry = getattr(feat, geometryField) simplifier = GDALSimplifier(geometry.ogr) VWGeometry = simplifier.from_ratio(float(ratio)) setattr(feat, geometryField, VWGeometry.wkt) feat.save() response = "Layer simplified" return HttpResponse(response, content_type="application/json")
def initLightVectorViewer(request, shapefile_id): try: post_dict = dict(request.POST.copy().iteritems()) x = int(post_dict['position[right]']) + 5 y = int(post_dict['position[top]']) id_relat = post_dict['id'] shapefile = Shapefile.objects.get(id=shapefile_id, created_by=request.user) feature = Feature.objects.get(shapefile=shapefile, id_relat=id_relat) geometryField = calcGeometryField(shapefile.geom_type) geometry = getattr(feature, geometryField) bounds = list(geometry.extent) except Shapefile.DoesNotExist or Feature.DoesNotExist: raise Http404 except AttributeError: bounds = [] response = json.dumps({'feat_id': feature.pk, 'x': x, 'y': y, 'bounds': bounds}) return HttpResponse(response, content_type="application/json")
def saveEdits(request): try: post_dict = dict(request.POST.copy().iteritems()) layer_id = post_dict['layer_id'] layer = Shapefile.objects.get(id=layer_id, created_by=request.user) geometryField = calcGeometryField(layer.geom_type) featureCollection = json.loads(post_dict['feature']) for feature in featureCollection['features']: try: feat_id = feature['properties']['feat_id'] feat = Feature.objects.get(id_relat=int(feat_id), shapefile=layer) except KeyError: feat = Feature.objects.create(shapefile=layer) setattr(feat, geometryField, json.dumps(feature['geometry'])) feat.save() return HttpResponse('') except: traceback.print_exc() return HttpResponse("")
def selectFeature(request): try: post_dict = dict(request.POST.copy().iteritems()) layer_id = int(post_dict['layer_id']) latitude = float(post_dict['latitude']) longitude = float(post_dict['longitude']) layer = Shapefile.objects.get(id=layer_id, created_by=request.user) query = findFeature(layer, longitude, latitude) if query: geometryField = calcGeometryField(layer.geom_type) SQLQuery = "SELECT row_to_json(f) As %s \ FROM (SELECT 'Feature' As type \ , ST_AsGeoJSON(%s)::json As geometry \ , row_to_json((SELECT l FROM (SELECT id_relat AS feat_id) As l)) As properties \ FROM layers_feature As l WHERE l.id = %s ) As f;" % (layer.geom_type, geometryField, query.pk) cursor = connection.cursor() cursor.execute(SQLQuery) row = cursor.fetchone()[0] response = json.dumps({'geojson':row, 'feat_id':query.pk}) return HttpResponse(response,content_type="application/json") else: return HttpResponse("") except: traceback.print_exc() return HttpResponse("")