def upload(request): ctoken = {} ctoken.update(csrf(request)) if request.method == 'POST': form = UploadForm(request.POST, request.FILES) if form.is_valid(): shp = form.handle(request.FILES['file_obj']) ds = gdal.DataSource(shp) ct = CoordTransform(SpatialReference('EPSG:4326'), SpatialReference('EPSG:900913')) dados = [] for layer in ds[0]: aux = layer.geom aux.transform(ct) dados.append(aux) form = UploadForm() return render_to_response( 'googlev3_upload.html', RequestContext(request, { 'form': form, 'dados': dados, 'token': ctoken })) else: form = UploadForm() return render_to_response('googlev3_upload.html', RequestContext(request, {'form': form}))
def load_single_shp(request): """ GET returns a form to upload a zipped shp POST takes the zip, validates that it is a single-feature poly shp and returns KML """ user = request.user if user.is_anonymous() or not user.is_authenticated(): return HttpResponse('You must be logged in', status=401) form = UploadForm() if request.method == 'POST': form = UploadForm(request.POST, request.FILES) # Override the defulat form behavior and # only allow single-feature polygon shps form.multi_feature = False form.supported_geomtypes = ['Polygon'] if form.is_valid(): layer = form.handle(request.FILES['file_obj'], user) g = layer[0].geom if g.srs: g.transform_to(4326) geoms = [g] t = get_template('loadshp/loadshp.kml') kml = t.render(Context({ 'username': user.username, 'geoms': geoms })) json = simplejson.dumps({'input_kml': kml, 'status': 'success'}) # Jquery Form plugin requires that we wrap this in a textarea # otherwise it mangles the kml return HttpResponse('<textarea>' + json + '</textarea>', mimetype="text/html") else: json = simplejson.dumps({ 'error_html': form.errors['file_obj'][0], 'status': 'errors' }) return HttpResponse('<textarea>' + json + '</textarea>', mimetype="text/html") elif request.method == 'GET': return render_to_response( 'loadshp/upload.html', RequestContext(request, { 'form': form, 'action': request.path })) else: raise Exception("This URL does not support %s requests" % request.method)
def load_single_shp(request): """ GET returns a form to upload a zipped shp POST takes the zip, validates that it is a single-feature poly shp and returns KML """ user = request.user if user.is_anonymous() or not user.is_authenticated(): return HttpResponse('You must be logged in', status=401) form = UploadForm() if request.method == 'POST': form = UploadForm(request.POST, request.FILES) # Override the defulat form behavior and # only allow single-feature polygon shps form.multi_feature = False form.supported_geomtypes = ['Polygon'] if form.is_valid(): layer = form.handle(request.FILES['file_obj'],user) g = layer[0].geom if g.srs: g.transform_to(4326) geoms = [g] t = get_template('loadshp/loadshp.kml') kml = t.render(Context({'username': user.username, 'geoms': geoms})) json = simplejson.dumps({'input_kml': kml, 'status':'success'}) # Jquery Form plugin requires that we wrap this in a textarea # otherwise it mangles the kml return HttpResponse('<textarea>'+json+'</textarea>',mimetype="text/html") else: json = simplejson.dumps({'error_html': form.errors['file_obj'][0], 'status':'errors'}) return HttpResponse('<textarea>'+json+'</textarea>',mimetype="text/html") elif request.method == 'GET': return render_to_response('loadshp/upload.html', RequestContext(request,{'form': form,'action':request.path})) else: raise Exception("This URL does not support %s requests" % request.method)
def upload(request): ctoken = {} ctoken.update(csrf(request)) if request.method == 'POST': form = UploadForm(request.POST, request.FILES) if form.is_valid(): shp = form.handle(request.FILES['file_obj']) ds = gdal.DataSource(shp) ct = CoordTransform(SpatialReference('EPSG:4326'), SpatialReference('EPSG:900913')) dados=[] for layer in ds[0]: aux = layer.geom aux.transform(ct) dados.append(aux) form = UploadForm() return render_to_response('googlev3_upload.html', RequestContext(request,{'form': form, 'dados': dados, 'token':ctoken})) else: form = UploadForm() return render_to_response('googlev3_upload.html', RequestContext(request,{'form': form}))