Package madrona :: Package manipulators :: Package management :: Package commands :: Module create_manipulator_geom
[hide private]

Source Code for Module madrona.manipulators.management.commands.create_manipulator_geom

 1  from django.core.management.base import BaseCommand, AppCommand 
 2  from optparse import make_option 
 3  from django.contrib.gis.utils import LayerMapping 
 4  from django.contrib.gis.gdal import DataSource 
 5  from madrona.common.utils import get_class 
 6   
 7  #omm_manipulator_list = ['EastOfTerritorialSeaLine', 'TerrestrialAndEstuaries', 'Terrestrial', 'Estuaries'] 
 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 manipulator, 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 \n\tmanage.py create_manipulator_geom <path to shape> <module name>.models.<manipulator model name>""" 16 args = '[shapefile, manipulator]' 17
18 - def handle(self, shapefile, manipulator, *args, **options):
19 try: 20 manip_model = get_class(manipulator) 21 except: 22 raise Exception("The %s model could not be found. \nBe sure and provide the complete description: <module name>.models.<manipulator model name>" % manipulator) 23 24 ds = DataSource(shapefile) 25 if len(ds) != 1: 26 raise Exception("Data source should only contain a single layer. Aborting.") 27 28 layer = ds[0] 29 if len(layer) != 1: 30 raise Exception("Layer should containing ONLY a single feature") 31 32 if not 'polygon' in layer.geom_type.name.lower(): 33 print layer.geom_type.name 34 raise Exception("This geometry must be a polygon") 35 36 mapping = {'geometry': 'MULTIPOLYGON'} 37 38 lm = LayerMapping(manip_model, shapefile, mapping) 39 lm.save() 40 manip_geom = manip_model.objects.order_by('-creation_date')[0] 41 if options.get('region_name'): 42 manip_geom.name = options.get('region_name') 43 manip_geom.save() 44 else: 45 manip_geom.name = layer.name 46 manip_geom.save() 47 48 print "" 49 print "The manipulaotr geometry, %s, has been added to the %s model with primary key = %s" % (manip_geom.name, manipulator, manip_geom.pk) 50 51 print "To switch to this geometry, you will need to run 'manage.py change_manipulator_geom %s %s'" % (manip_geom.pk, manipulator) 52 print ""
53