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

Source Code for Module madrona.xyquery.views

 1  import struct 
 2  from osgeo import gdal 
 3  from django.shortcuts import render_to_response 
 4  from django.contrib.gis.geos import Point as GEOSPoint 
 5  from django.contrib.gis.gdal import SpatialReference 
 6  from models import Attribute, Feature, Raster 
 7   
 8   
9 -def query(request):
10 x = float(request.REQUEST['x']) 11 y = float(request.REQUEST['y']) 12 pnt = GEOSPoint(x,y,srid=4326) 13 14 # Query all the vector polygon layers 15 polygons = Feature.objects.filter(geom__bboverlaps=pnt).filter(geom__intersects=pnt) 16 results = {} 17 for poly in polygons: 18 attribs = Attribute.objects.filter(feature=poly.pk) 19 for attrib in attribs: 20 results['%s :: %s' % (poly.layer, attrib.key)] = attrib.value 21 22 # Query all the raster layers 23 rasters = Raster.objects.all() 24 for rast in rasters: 25 ds = gdal.Open(str(rast.filepath)) 26 numbands = ds.RasterCount 27 srs = SpatialReference(ds.GetProjection()) 28 pnt_proj = pnt.transform(srs,clone=True) 29 for band in range(1,numbands + 1): 30 val = getRasterValue(pnt_proj.x, pnt_proj.y, ds, band) 31 if val: 32 results["%s :: %s" % (rast.layer, "Band %s" % band)] = val 33 34 return render_to_response('query.html', {'x': x, 'y':y, 'results': results})
35
36 -def getRasterValue(x,y,ds,bandnum=1):
37 band = ds.GetRasterBand(bandnum) 38 gt = ds.GetGeoTransform() 39 nodata = band.GetNoDataValue() 40 try: 41 xoff = int((x - gt[0]) / gt[1]) 42 yoff = int((y - gt[3]) / gt[5]) 43 a = band.ReadAsArray(xoff, yoff, 1, 1) 44 #return float(Numeric.sum( Numeric.sum(a) ) / Numeric.size(a)) 45 val = a[0][0] 46 if val == nodata: 47 return None 48 else: 49 return val 50 except: 51 return None
52