Package madrona :: Package heatmap :: Module views
[hide private]

Source Code for Module madrona.heatmap.views

  1  # Create your views here. 
  2  from madrona.heatmap.models import create_heatmap 
  3  from madrona.shapes.views import ShpResponder 
  4  from madrona.common import default_mimetypes as mimetypes 
  5  from django.http import HttpResponse, HttpResponseRedirect, HttpResponseBadRequest, HttpResponseServerError, HttpResponseForbidden, Http404 
  6  from madrona.features import get_feature_by_uid, get_collection_models 
  7  from madrona.features.models import PolygonFeature 
  8  import os 
  9   
10 -def overlap_geotiff(collection_uids, user=None):
11 collections = [get_feature_by_uid(x) for x in collection_uids.split(',')] 12 collections = [x for x in collections if x.__class__ in get_collection_models()] 13 if len(collections) < 1: 14 raise Http404 15 16 filenames = [] 17 for collection in collections: 18 viewable, response = collection.is_viewable(user) 19 if user and not viewable: 20 return response 21 22 fs = collection.feature_set(recurse=True) 23 poly_fs = [x for x in fs if isinstance(x,PolygonFeature)] 24 unique_types = list(set([x.__class__ for x in poly_fs])) 25 for model in unique_types: 26 responder = ShpResponder(model.objects.filter(pk__in=[x.pk for x in poly_fs if x.__class__ == model])) 27 responder.geo_field = 'geometry_final' 28 fn = responder('return_file_not_response') 29 filenames.append(fn) 30 31 temp_geotiff = create_heatmap(filenames) 32 return temp_geotiff
33
34 -def overlap_geotiff_response(request, collection_uids):
35 import cStringIO 36 buff = cStringIO.StringIO() 37 temp_geotiff = overlap_geotiff(collection_uids, request.user) 38 if isinstance(temp_geotiff, HttpResponse): 39 return temp_geotiff 40 rfile = open(temp_geotiff,'rb') 41 buff.write(rfile.read()) 42 buff.flush() 43 stream = buff.getvalue() 44 response = HttpResponse() 45 response['Content-Disposition'] = 'attachment; filename=%s' % os.path.basename(temp_geotiff) 46 response['Content-length'] = str(len(stream)) 47 response['Content-Type'] = 'image/geotiffint16' 48 response.write(stream) 49 return response
50
51 -def overlap_kmz_response(request, collection_uids):
52 import cStringIO 53 import shutil 54 import tempfile 55 56 buff = cStringIO.StringIO() 57 temp_geotiff = overlap_geotiff(collection_uids, request.user) 58 if isinstance(temp_geotiff, HttpResponse): 59 return response 60 61 tempdir = os.path.join(tempfile.gettempdir(),str(collection_uids.__hash__())) 62 if os.path.exists(tempdir): 63 shutil.rmtree(tempdir) 64 os.mkdir(tempdir) 65 outrgb = os.path.join(tempdir, "rgb.png") 66 outvrt = os.path.join(tempdir, "rgb.vrt") 67 outkmldir = os.path.join(tempdir, "kmlfiles") 68 if os.path.exists(outkmldir): 69 shutil.rmtree(outkmldir) 70 os.mkdir(outkmldir) 71 outkmz = os.path.join(tempdir, "array_overlap.kmz") 72 73 # Convert to RGB 74 cmd = "pct2rgb.py -of PNG %s %s" % (temp_geotiff, outrgb) 75 print cmd 76 print os.popen(cmd).read() 77 78 # Assign a nodata value 79 cmd = "gdal_translate -of vrt -a_nodata 0 %s %s" % (outrgb, outvrt) 80 print cmd 81 print os.popen(cmd).read() 82 83 # Split into wgs84 tiles 84 cmd = "gdal2tiles.py -k -z 6-10 -p geodetic %s %s" % (outvrt, outkmldir) 85 print cmd 86 print os.popen(cmd).read() 87 88 # Zip up directory into a kmz 89 print "Creating", outkmz 90 from madrona.common.utils import KMZUtil 91 zu = KMZUtil() 92 filename = outkmz 93 os.chdir(outkmldir) 94 directory = "" 95 zu.toZip(directory, filename) 96 97 if not os.path.exists(outkmz): 98 return HttpResponse("KMZ creation error", status=500) 99 rfile = open(outkmz,'rb') 100 buff.write(rfile.read()) 101 buff.flush() 102 stream = buff.getvalue() 103 response = HttpResponse() 104 response['Content-Disposition'] = 'attachment; filename=%s' % os.path.basename(outkmz) 105 response['Content-length'] = str(len(stream)) 106 response['Content-Type'] = mimetypes.KMZ 107 response.write(stream) 108 return response
109