Package madrona :: Package studyregion :: Package management :: Package commands :: Module create_study_region
[hide private]

Source Code for Module madrona.studyregion.management.commands.create_study_region

 1  from django.core.management.base import BaseCommand, AppCommand 
 2  from optparse import make_option 
 3  from django.contrib.gis.gdal import DataSource 
 4  from django.contrib.gis.geos import GEOSGeometry  
 5  from django.contrib.gis import geos 
 6  from django.conf import settings 
 7  from madrona.studyregion.models import StudyRegion 
 8   
9 -class Command(BaseCommand):
10 option_list = AppCommand.option_list + ( 11 make_option('--name', action='store', dest='region_name', default=False, 12 help='Give a name to the study region, otherwise the name attribute from the shapefile will be used.'), 13 ) 14 help = "Creates a new study region from a shapefile containing a single multigeometry" 15 args = '[input shapefile or wkt string]' 16
17 - def handle_ogr(self, inshape, name):
18 ds = DataSource(inshape) 19 layer = ds[0] # assume first layer 20 feature = layer[0] #assume first feature 21 if not name: 22 try: 23 name = feature.name 24 except: 25 raise Exception("No `name` field or --name provided!") 26 27 g1 = feature.geom.geos 28 srid = g1.srid 29 if not srid: 30 # See if we can assume latlon 31 missing_srid = True 32 ext = list(g1.extent) 33 latlonmax = [180,90] * 2 34 latlonmin = [x * -1 for x in latlonmax] 35 overs = [a for a,b in zip(ext, latlonmax) if a > b] 36 unders = [a for a,b in zip(ext, latlonmin) if a < b] 37 if len(overs) == len(unders) == 0: 38 srid = 4326 39 g1.srid = 4326 40 else: 41 raise Exception("Unknown SRID. Try ewkt format; `SRID=4326;POLYGON((.....))`") 42 if g1 and isinstance(g1, geos.Polygon): 43 g1 = geos.MultiPolygon(g1) 44 g1.srid = srid 45 g1.transform(settings.GEOMETRY_DB_SRID) 46 47 region = StudyRegion.objects.create(geometry=g1, name=name, active=True) 48 region.save() 49 50 print "Study region created: %s, primary key = %s" % (region.name, region.pk)
51
52 - def handle_wkt(self, wkt, name):
53 g1 = GEOSGeometry(wkt) 54 srid = g1.srid 55 if not srid: 56 raise Exception("Unknown SRID. Try ewkt format; `SRID=4326;POLYGON((.....))`") 57 if g1 and isinstance(g1, geos.Polygon): 58 g1 = geos.MultiPolygon(g1) 59 g1.srid = srid 60 61 if not name: 62 raise Exception("No --name provided!") 63 64 g1.transform(settings.GEOMETRY_DB_SRID) 65 region = StudyRegion.objects.create(geometry=g1, name=name, active=True) 66 region.save() 67 68 print "Study region created: %s, primary key = %s" % (region.name, region.pk)
69
70 - def handle(self, inshape, *args, **options):
71 """ 72 `inshape` can be a wkt string or shapefile path 73 """ 74 htype = '' 75 name = options.get('region_name') 76 try: 77 ds = DataSource(inshape) 78 htype = 'ogr' 79 except: 80 try: 81 g1 = GEOSGeometry(inshape) 82 htype = 'wkt' 83 except: 84 pass 85 86 if htype == 'ogr': 87 self.handle_ogr(inshape, name) 88 elif htype == 'wkt': 89 self.handle_wkt(inshape, name) 90 else: 91 raise Exception("Your input shape is not recognized as a valid datasource or geometry string")
92