Package madrona :: Package analysistools :: Module examples
[hide private]

Source Code for Module madrona.analysistools.examples

  1  from models import Analysis 
  2  from django.contrib.gis.db import models 
  3  from django.conf import settings 
  4  from madrona.features.models import Feature, FeatureForm, get_absolute_media_url 
  5  from madrona.common.utils import get_class 
  6  from madrona.features import register 
  7  from django.contrib.gis.geos import GEOSGeometry  
  8  from madrona.common.utils import asKml 
  9  from django import forms 
 10  from widgets import SliderWidget 
 11  import os 
12 13 @register 14 -class BufferPoint(Analysis):
15 input_lat = models.FloatField(verbose_name='Latitude') 16 input_lon = models.FloatField(verbose_name='Longitude') 17 input_buffer_distance = models.FloatField(verbose_name="Buffer Distance (m)") 18 19 # All output fields should be allowed to be Null/Blank 20 output_area = models.FloatField(null=True,blank=True, 21 verbose_name="Buffer Area (meters)") 22 output_point_geom = models.PointField(srid=settings.GEOMETRY_DB_SRID, 23 null=True, blank=True, verbose_name="Point Geometry") 24 output_poly_geom = models.PolygonField(srid=settings.GEOMETRY_DB_SRID, 25 null=True, blank=True, verbose_name="Buffered Point Geometry") 26
27 - def run(self):
28 try: 29 g = GEOSGeometry('SRID=4326;POINT(%s %s)' % (self.input_lon, self.input_lat)) 30 g.transform(settings.GEOMETRY_DB_SRID) 31 self.output_point_geom = g 32 self.output_poly_geom = g.buffer(self.input_buffer_distance) 33 self.output_area = self.output_poly_geom.area 34 except: 35 return False 36 return True
37 38 @property
39 - def kml_done(self):
40 return """ 41 <Placemark id="%s"> 42 <visibility>0</visibility> 43 <name>%s</name> 44 <styleUrl>#%s-default</styleUrl> 45 <MultiGeometry> 46 %s 47 %s 48 </MultiGeometry> 49 </Placemark> 50 """ % (self.uid, self.name, self.model_uid(), 51 asKml(self.output_point_geom, uid=self.uid), 52 asKml(self.output_poly_geom, uid=self.uid))
53 54 @property
55 - def kml_working(self):
56 return """ 57 <Placemark id="%s"> 58 <visibility>0</visibility> 59 <name>%s (WORKING)</name> 60 <styleUrl>#%s-default</styleUrl> 61 <Point> 62 <coordinates>%s,%s</coordinates> 63 </Point> 64 </Placemark> 65 """ % (self.uid, self.name, self.model_uid(), 66 self.input_lon, self.input_lat)
67 68 @property
69 - def kml_style(self):
70 return """ 71 <Style id="%s-default"> 72 <IconStyle> 73 <color>ffffffff</color> 74 <colorMode>normal</colorMode> 75 <scale>0.9</scale> 76 <Icon> <href>http://maps.google.com/mapfiles/kml/paddle/wht-blank.png</href> </Icon> 77 </IconStyle> 78 <LabelStyle> 79 <color>ffffffff</color> 80 <scale>0.8</scale> 81 </LabelStyle> 82 <PolyStyle> 83 <color>778B1A55</color> 84 </PolyStyle> 85 </Style> 86 """ % (self.model_uid(),)
87
88 - class Options:
89 form = 'madrona.analysistools.models.BufferPointsForm' 90 show_template = 'analysis/show.html' 91 icon_url = 'analysistools/img/buffer-16x16.png'
92
93 #from django.forms.models import modelform_factory 94 #exclude_fieldnames = list(FeatureForm.Meta.exclude) 95 #for f in BufferPoint.output_fields(): 96 # exclude_fieldnames.append(f.attname) 97 #BufferPointsForm = modelform_factory(model=BufferPoint, 98 # form=FeatureForm, 99 # exclude=exclude_fieldnames) 100 101 -class BufferPointsForm(FeatureForm):
102 input_lat = forms.FloatField(max_value=90, min_value=-90, 103 widget=SliderWidget(min=-90,max=90,step=0.00001,image='analysistools/img/lat.gif'), 104 label="Latitude") 105 input_lon = forms.FloatField(max_value=180, min_value=-180, 106 widget=SliderWidget(min=-180,max=180,step=0.00001,image='analysistools/img/lon.gif'), 107 label="Longitude") 108 input_buffer_distance = forms.FloatField( 109 widget=SliderWidget(min=10, max=50000,step=1,image='analysistools/img/buffer.png'), 110 label="Buffer Distance (m)", 111 min_value=0.0001, max_value=50000) 112
113 - class Meta(FeatureForm.Meta):
114 # TODO put all this in AnalysisForm and inherit 115 # requires lots of Metaprogramming complexity 116 model = BufferPoint 117 exclude = list(FeatureForm.Meta.exclude) 118 for f in BufferPoint.output_fields(): 119 exclude.append(f.attname)
120