def update_or_create(): try: m = Area.objects.get(id=code) except Area.DoesNotExist: m = Area( id = code, name = name, type = Type.objects.get(code=area_code), country = Country.objects.get(code='O'), parent_area = parent_area, generation_low = new_generation, generation_high = new_generation, ) if m.generation_high and current_generation and m.generation_high.id < current_generation.id: raise Exception, "Area %s found, but not in current generation %s" % (m, current_generation) m.generation_high = new_generation g = feat.geom.transform(4326, clone=True) poly = [ g ] if options['commit']: m.save() for k, v in kml_data.data[name].items(): if k in ('name:smi', 'name:fi'): lang = 'N' + k[5:] m.names.update_or_create({ 'type': NameType.objects.get(code=lang) }, { 'name': v }) m.codes.update_or_create({ 'type': code_type_n5000 }, { 'code': code_str }) m.codes.update_or_create({ 'type': code_type_osm }, { 'code': int(kml_data.data[name]['osm']) }) save_polygons({ code : (m, poly) })
def update_or_create(): try: m = Area.objects.get(id=int(regionid)) print "Updating area %s with id %d" % (regionname, int(regionid)) except Area.DoesNotExist: print "Creating new area %s with id %d" % ( regionname, int(regionid)) m = Area( id=int(regionid), name=regionname, type=Type.objects.get(code=area_type), country=Country.objects.get(code='O'), generation_low=new_generation, generation_high=new_generation, ) if m.generation_high and current_generation \ and m.generation_high.id < current_generation.id: raise Exception, "Area %s found, but not in current generation %s" % ( m, current_generation) m.generation_high = new_generation poly = [GEOSGeometry(unionoutline).ogr] if options['commit']: m.save() save_polygons({regionid: (m, poly)})
def handle_label(self, filename, **options): code_version = CodeType.objects.get(code='gss') name_type = NameType.objects.get(code='O') for feat in DataSource(filename)[0]: name = unicode(feat['NAME'].value, 'iso-8859-1') name = re.sub('\s*\(DET( NO \d+|)\)\s*(?i)', '', name) name = re.sub('\s+', ' ', name) ons_code = feat['CODE'].value area_code = feat['AREA_CODE'].value country = ons_code[0] if ons_code in ('E07000100', 'E07000104', 'S12000009', 'S12000043'): assert Area.objects.filter(codes__type=code_version, codes__code=ons_code).count() == 0 print(ons_code, area_code, country, name) m = Area( type=Type.objects.get(code=area_code), country=Country.objects.get(code=country), generation_low=Generation.objects.get(id=1), generation_high=Generation.objects.get(id=14), ) if options['commit']: m.save() m.names.update_or_create(type=name_type, defaults={'name': name}) m.codes.update_or_create(type=code_version, defaults={'code': ons_code}) save_polygons({ons_code: (m, [feat.geom])})
def handle_label(self, filename, **options): code_version = CodeType.objects.get(code='gss') name_type = NameType.objects.get(code='O') for feat in DataSource(filename)[0]: name = feat['NAME'].value if not isinstance(name, six.text_type): name = name.decode('iso-8859-1') name = re.sub(r'\s*\(DET( NO \d+|)\)\s*(?i)', '', name) name = re.sub(r'\s+', ' ', name) ons_code = feat['CODE'].value area_code = feat['AREA_CODE'].value country = ons_code[0] if ons_code in ('E07000100', 'E07000104', 'S12000009', 'S12000043'): assert Area.objects.filter(codes__type=code_version, codes__code=ons_code).count() == 0 print(ons_code, area_code, country, name) m = Area( type=Type.objects.get(code=area_code), country=Country.objects.get(code=country), generation_low=Generation.objects.get(id=1), generation_high=Generation.objects.get(id=14), ) if options['commit']: m.save() m.names.update_or_create(type=name_type, defaults={'name': name}) m.codes.update_or_create(type=code_version, defaults={'code': ons_code}) save_polygons({ons_code: (m, [feat.geom])})
def handle_label(self, filename, **options): print filename new_generation = Generation.objects.new() if not new_generation: raise Exception, "No new generation to be used for import!" name_type = NameType.objects.get(code='O') code_type = CodeType.objects.get(code='gss') ds = DataSource(filename) layer = ds[0] for feat in layer: name = unicode(feat['NAME'].value, 'iso-8859-1') print " ", name name = re.sub('\s*\(DET( NO \d+|)\)\s*(?i)', '', name) name = re.sub('\s+', ' ', name) if "P Const" in name: area_code = 'SPC' elif "PER" in name: area_code = 'SPE' else: raise Exception, "Unknown type of area %s" % name ons_code = name_to_code[name] if ons_code in self.ons_code_to_shape: m, poly = self.ons_code_to_shape[ons_code] if options['commit']: m_name = m.names.get(type=name_type).name if name != m_name: raise Exception, "ONS code %s is used for %s and %s" % (ons_code, name, m_name) # Otherwise, combine the two shapes for one area print " Adding subsequent shape to ONS code %s" % ons_code poly.append(feat.geom) continue try: m = Area.objects.get(codes__type=code_type, codes__code=ons_code) except Area.DoesNotExist: m = Area( type = Type.objects.get(code=area_code), country = Country.objects.get(name='Scotland'), generation_low = new_generation, generation_high = new_generation, ) if options['commit']: m.save() poly = [ feat.geom ] if options['commit']: m.names.update_or_create({ 'type': name_type }, { 'name': name }) if ons_code: self.ons_code_to_shape[ons_code] = (m, poly) if options['commit']: m.codes.update_or_create({ 'type': code_type }, { 'code': ons_code }) if options['commit']: save_polygons(self.ons_code_to_shape)
def update_or_create(): if osm_code: m = osm_code.area else: m = Area( name = name, type = Type.objects.get(code=area_code), country = Country.objects.get(code='G'), parent_area = parent_area, generation_low = new_generation, generation_high = new_generation, ) if m.generation_high and current_generation and m.generation_high.id < current_generation.id: raise Exception, "Area %s found, but not in current generation %s" % (m, current_generation) m.generation_high = new_generation g = feat.geom.transform(4326, clone=True) # In generating the data we should have # excluded any "polygons" with less than four # points (the final one being the same as the # first), but just in case: for polygon in g: if g.num_points < 4: return poly = [ g ] if options['commit']: m.save() if name not in kml_data.data: print json.dumps(kml_data.data, sort_keys=True, indent=4) raise Exception, u"Will fail to find '%s' in the dictionary" % (name,) for k, v in kml_data.data[name].items(): language_name = None if k == 'name': lang = 'default' language_name = "OSM Default" else: name_match = re.search(r'^name:(.+)$', k) if name_match: lang = name_match.group(1) if lang in language_code_to_name: language_name = language_code_to_name[lang] if not language_name: continue # Otherwise, make sure that a NameType for this language exists: NameType.objects.update_or_create({'code': lang}, {'code': lang, 'description': language_name}) name_type = NameType.objects.get(code=lang) m.names.update_or_create({ 'type': name_type }, { 'name': v }) m.codes.update_or_create({ 'type': code_type_osm }, { 'code': osm_id }) save_polygons({ code : (m, poly) })
def handle_label(self, filename, **options): code_version = CodeType.objects.get(code="gss") for feat in DataSource(filename)[0]: name = unicode(feat["NAME"].value, "iso-8859-1") ons_code = feat["CODE"].value if ons_code in ("E04008782", "E05004419"): m = Area.objects.get(codes__type=code_version, codes__code=ons_code) if options["commit"]: print "Updating %s" % name save_polygons({ons_code: (m, [feat.geom])})
def handle_label(self, filename, **options): code_version = CodeType.objects.get(code='gss') for feat in DataSource(filename)[0]: name = unicode(feat['NAME'].value, 'iso-8859-1') ons_code = feat['CODE'].value if ons_code in ('E04008782', 'E05004419'): m = Area.objects.get(codes__type=code_version, codes__code=ons_code) if options['commit']: print 'Updating %s' % name save_polygons({ ons_code: (m, [ feat.geom ]) })
def handle_label(self, filename, **options): code_version = CodeType.objects.get(code='gss') for feat in DataSource(filename)[0]: name = unicode(feat['NAME'].value, 'iso-8859-1') ons_code = feat['CODE'].value if ons_code in ('E04008782', 'E05004419'): m = Area.objects.get(codes__type=code_version, codes__code=ons_code) if options['commit']: print('Updating %s' % name) save_polygons({ons_code: (m, [feat.geom])})
def handle_label(self, filename, **options): current_generation = Generation.objects.current() new_generation = Generation.objects.new() if not new_generation: raise Exception, "No new generation to be used for import!" code_type = CodeType.objects.get(code='n5000') name_type = NameType.objects.get(code='M') ds = DataSource(filename) layer = ds[0] for feat in layer: name = unicode(feat['NAVN'].value, 'iso-8859-1') name = re.sub('\s+', ' ', name) print " ", name code = feat['KOMM'].value code_str = '%04d' % code area_code = 'NKO' try: m = Area.objects.get(codes__type=code_type, codes__code=code_str) except Area.DoesNotExist: m = Area( id=code, type=Type.objects.get(code=area_code), country=Country.objects.get(code='O'), generation_low=new_generation, generation_high=new_generation, ) if m.generation_high and current_generation and m.generation_high.id < current_generation.id: raise Exception, "Area %s found, but not in current generation %s" % ( m, current_generation) m.generation_high = new_generation g = feat.geom.transform(4326, clone=True) poly = [g] if options['commit']: m.save() m.names.update_or_create({'type': name_type}, {'name': name}) m.codes.update_or_create({'type': code_type}, {'code': code_str}) save_polygons({code: (m, poly)})
def handle_label(self, filename, **options): current_generation = Generation.objects.current() new_generation = Generation.objects.new() if not new_generation: raise Exception, "No new generation to be used for import!" code_type = CodeType.objects.get(code='n5000') name_type = NameType.objects.get(code='M') ds = DataSource(filename) layer = ds[0] for feat in layer: name = unicode(feat['NAVN'].value, 'iso-8859-1') name = re.sub('\s+', ' ', name) print " ", name code = feat['KOMM'].value code_str = '%04d' % code area_code = 'NKO' try: m = Area.objects.get(codes__type=code_type, codes__code=code_str) except Area.DoesNotExist: m = Area( id = code, type = Type.objects.get(code=area_code), country = Country.objects.get(code='O'), generation_low = new_generation, generation_high = new_generation, ) if m.generation_high and current_generation and m.generation_high.id < current_generation.id: raise Exception, "Area %s found, but not in current generation %s" % (m, current_generation) m.generation_high = new_generation g = feat.geom.transform(4326, clone=True) poly = [ g ] if options['commit']: m.save() m.names.update_or_create({ 'type': name_type }, { 'name': name }) m.codes.update_or_create({ 'type': code_type }, { 'code': code_str }) save_polygons({ code : (m, poly) })
def update_or_create(): try: m = Area.objects.get(id=code) except Area.DoesNotExist: m = Area( id=code, name=name, type=Type.objects.get(code=area_code), country=Country.objects.get(code='O'), parent_area=parent_area, generation_low=new_generation, generation_high=new_generation, ) if m.generation_high and current_generation and m.generation_high.id < current_generation.id: raise Exception, "Area %s found, but not in current generation %s" % ( m, current_generation) m.generation_high = new_generation g = feat.geom.transform(4326, clone=True) poly = [g] if options['commit']: m.save() for k, v in kml_data.data[name].items(): if k in ('name:smi', 'name:fi'): lang = 'N' + k[5:] m.names.update_or_create( {'type': NameType.objects.get(code=lang)}, {'name': v}) m.codes.update_or_create({'type': code_type_n5000}, {'code': code_str}) m.codes.update_or_create( {'type': code_type_osm}, {'code': int(kml_data.data[name]['osm'])}) save_polygons({code: (m, poly)})
def update_or_create(): try: m = Area.objects.get(id=int(regionid)) print "Updating area %s with id %d" % (regionname, int(regionid)) except Area.DoesNotExist: print "Creating new area %s with id %d" % (regionname, int(regionid)) m = Area( id = int(regionid), name = regionname, type = Type.objects.get(code=area_type), country = Country.objects.get(code='O'), generation_low = new_generation, generation_high = new_generation, ) if m.generation_high and current_generation \ and m.generation_high.id < current_generation.id: raise Exception, "Area %s found, but not in current generation %s" % (m, current_generation) m.generation_high = new_generation poly = [ GEOSGeometry(unionoutline).ogr ] if options['commit']: m.save() save_polygons({ regionid : (m, poly) })
def handle_label(self, filename, **options): country = Country.objects.get(code='N') oa_type = Type.objects.get(code='OUA') soa_type = Type.objects.get(code='OLF') name_type = NameType.objects.get(code='S') code_type = CodeType.objects.get(code='ons') current_generation = Generation.objects.current() new_generation = Generation.objects.new() if not new_generation: raise Exception, "No new generation to be used for import!" # Compile an alphabetical list of NI councils and their wards, OA codes # are assigned alphabetically. if not self.councils: self.councils = Area.objects.filter(type=Type.objects.get(code='LGD')).order_by('name').values() for lgd in self.councils: lges = Area.objects.filter(parent_area=lgd['id']) areas = [] for lge in lges: lgws = Area.objects.filter(parent_area=lge).values() areas += lgws lgd['wards'] = sorted(areas, key=lambda x: x['name']) ds = DataSource(filename) layer = ds[0] layer_name = str(layer) for feat in layer: if layer_name == 'soa': area_type = soa_type ons_code = feat['SOA_CODE'].value name = feat['SOA_LABEL'].value.replace('_', ' ') elif layer_name == 'OA_ni': area_type = oa_type ons_code = feat['OA_CODE'].value name = 'Output Area %s' % ons_code else: raise Exception, 'Bad data passed in' council = ord(ons_code[2:3]) - 65 ward = int(ons_code[4:6]) - 1 if ward == 98: # SOA covers two wards, set parent to council, best we can do parent = self.councils[council]['id'] else: parent = self.councils[council]['wards'][ward]['id'] try: m = Area.objects.get(codes__type=code_type, codes__code=ons_code) if int(options['verbosity']) > 1: print " Area matched, %s" % (m, ) except Area.DoesNotExist: print " New area: %s" % (ons_code) m = Area( name = name, # If committing, this will be overwritten by the m.names.update_or_create type = area_type, country = country, parent_area_id = parent, generation_low = new_generation, generation_high = new_generation, ) if m.generation_high and current_generation and m.generation_high.id < current_generation.id: raise Exception, "Area %s found, but not in current generation %s" % (m, current_generation) m.generation_high = new_generation m.parent_area_id = parent if options['commit']: m.save() f = feat.geom f.srid = 29902 poly = [ f ] if options['commit']: m.names.update_or_create({ 'type': name_type }, { 'name': name }) if ons_code: self.ons_code_to_shape[ons_code] = (m, poly) if options['commit']: m.codes.update_or_create({ 'type': code_type }, { 'code': ons_code }) if options['commit']: save_polygons(self.ons_code_to_shape)
def handle_label(self, filename, **options): if not options['control']: raise Exception, "You must specify a control file" __import__(options['control']) control = sys.modules[options['control']] print filename current_generation = Generation.objects.current() new_generation = Generation.objects.new() if not new_generation: raise Exception, "No new generation to be used for import!" ds = DataSource(filename) layer = ds[0] for feat in layer: name = unicode(feat['NAME'].value, 'iso-8859-1') print " ", name name = re.sub('\s*\(DET( NO \d+|)\)\s*(?i)', '', name) name = re.sub('\s+', ' ', name) ons_code = feat['CODE'].value if feat['CODE'].value not in ('999999', '999999999') else None unit_id = str(feat['UNIT_ID'].value) area_code = feat['AREA_CODE'].value if self.patch_boundary_line(ons_code, area_code): ons_code = None if area_code == 'NCP': continue # Ignore Non Parished Areas if ons_code in self.ons_code_to_shape: m, poly = self.ons_code_to_shape[ons_code] m_name = m.names.get(type='O').name if name != m_name: raise Exception, "ONS code %s is used for %s and %s" % (ons_code, name, m_name) # Otherwise, combine the two shapes for one area print " Adding subsequent shape to ONS code %s" % ons_code poly.append(feat.geom) continue if unit_id in self.unit_id_to_shape: m, poly = self.unit_id_to_shape[unit_id] m_name = m.names.get(type='O').name if name != m_name: raise Exception, "Unit ID code %s is used for %s and %s" % (unit_id, name, m_name) # Otherwise, combine the two shapes for one area print " Adding subsequent shape to unit ID %s" % unit_id poly.append(feat.geom) continue if control.code_version() == 'gss' and ons_code: country = ons_code[0] # Hooray! elif area_code in ('CED', 'CTY', 'DIW', 'DIS', 'MTW', 'MTD', 'LBW', 'LBO', 'LAC', 'GLA'): country = 'E' elif control.code_version() == 'gss': raise Exception, area_code elif (area_code == 'EUR' and 'Scotland' in name) or area_code in ('SPC', 'SPE') or (ons_code and ons_code[0:3] in ('00Q', '00R')): country = 'S' elif (area_code == 'EUR' and 'Wales' in name) or area_code in ('WAC', 'WAE') or (ons_code and ons_code[0:3] in ('00N', '00P')): country = 'W' elif area_code in ('EUR', 'UTA', 'UTE', 'UTW', 'CPC'): country = 'E' else: # WMC # Make sure WMC are loaded after all wards... area_within = Area.objects.filter(type__in=('UTW','UTE','MTW','COP','LBW','DIW'), polygons__polygon__contains=feat.geom.geos.point_on_surface)[0] country = area_within.country # Can't do the above ons_code checks with new GSS codes, will have to do more PinP checks # Do parents in separate P-in-P code after this is done. try: if control.check(name, area_code, country, feat.geom): raise Area.DoesNotExist if ons_code: m = Area.objects.get(codes__type=control.code_version(), codes__code=ons_code) elif unit_id: m = Area.objects.get(codes__type='unit_id', codes__code=unit_id) m_name = m.names.get(type='O').name if name != m_name: raise Exception, "Unit ID code %s is %s in DB but %s in SHP file" % (unit_id, m_name, name) else: raise Exception, 'Area "%s" (%s) has neither ONS code nor unit ID' % (name, area_code) except Area.DoesNotExist: m = Area( type = area_code, country = country, generation_low = new_generation, generation_high = new_generation, ) if m.generation_high and current_generation and m.generation_high.id < current_generation.id: raise Exception, "Area %s found, but not in current generation %s" % (m, current_generation) m.generation_high = new_generation if options['commit']: m.save() poly = [ feat.geom ] if options['commit']: m.names.update_or_create({ 'type': 'O' }, { 'name': name }) if ons_code: self.ons_code_to_shape[ons_code] = (m, poly) if options['commit']: m.codes.update_or_create({ 'type': control.code_version() }, { 'code': ons_code }) if unit_id: self.unit_id_to_shape[unit_id] = (m, poly) if options['commit']: m.codes.update_or_create({ 'type': 'unit_id' }, { 'code': unit_id }) if options['commit']: save_polygons(self.unit_id_to_shape) save_polygons(self.ons_code_to_shape)
def handle_label(self, filename, **options): if not options['control']: raise Exception, "You must specify a control file" __import__(options['control']) control = sys.modules[options['control']] code_version = CodeType.objects.get(code=control.code_version()) name_type = NameType.objects.get(code='O') code_type_os = CodeType.objects.get(code='unit_id') print filename current_generation = Generation.objects.current() new_generation = Generation.objects.new() if not new_generation: raise Exception, "No new generation to be used for import!" ds = DataSource(filename) layer = ds[0] for feat in layer: name = unicode(feat['NAME'].value, 'iso-8859-1') name = re.sub('\s*\(DET( NO \d+|)\)\s*(?i)', '', name) name = re.sub('\s+', ' ', name) ons_code = feat['CODE'].value if feat['CODE'].value not in ( '999999', '999999999') else None unit_id = str(feat['UNIT_ID'].value) area_code = feat['AREA_CODE'].value patch = self.patch_boundary_line(ons_code, area_code) if patch == True: ons_code = None elif patch: ons_code = patch if area_code == 'NCP': continue # Ignore Non Parished Areas if ons_code in self.ons_code_to_shape: m, poly = self.ons_code_to_shape[ons_code] try: m_name = m.names.get(type=name_type).name except Name.DoesNotExist: m_name = m.name # If running without commit for dry run, so nothing being stored in db if name != m_name: raise Exception, "ONS code %s is used for %s and %s" % ( ons_code, name, m_name) # Otherwise, combine the two shapes for one area poly.append(feat.geom) continue if unit_id in self.unit_id_to_shape: m, poly = self.unit_id_to_shape[unit_id] m_name = m.names.get(type=name_type).name if name != m_name: raise Exception, "Unit ID code %s is used for %s and %s" % ( unit_id, name, m_name) # Otherwise, combine the two shapes for one area poly.append(feat.geom) continue if code_version.code == 'gss' and ons_code: country = ons_code[0] # Hooray! elif area_code in ('CED', 'CTY', 'DIW', 'DIS', 'MTW', 'MTD', 'LBW', 'LBO', 'LAC', 'GLA'): country = 'E' elif code_version.code == 'gss': raise Exception, area_code elif (area_code == 'EUR' and 'Scotland' in name) or area_code in ( 'SPC', 'SPE') or (ons_code and ons_code[0:3] in ('00Q', '00R')): country = 'S' elif (area_code == 'EUR' and 'Wales' in name) or area_code in ( 'WAC', 'WAE') or (ons_code and ons_code[0:3] in ('00N', '00P')): country = 'W' elif area_code in ('EUR', 'UTA', 'UTE', 'UTW', 'CPC'): country = 'E' else: # WMC # Make sure WMC are loaded after all wards... area_within = Area.objects.filter( type__code__in=('UTW', 'UTE', 'MTW', 'COP', 'LBW', 'DIW'), polygons__polygon__contains=feat.geom.geos.point_on_surface )[0] country = area_within.country.code # Can't do the above ons_code checks with new GSS codes, will have to do more PinP checks # Do parents in separate P-in-P code after this is done. try: check = control.check(name, area_code, country, feat.geom) if check == True: raise Area.DoesNotExist if isinstance(check, Area): m = check ons_code = m.codes.get(type=code_version) elif ons_code: m = Area.objects.get(codes__type=code_version, codes__code=ons_code) elif unit_id: m = Area.objects.get(codes__type=code_type_os, codes__code=unit_id) m_name = m.names.get(type=name_type).name if name != m_name: raise Exception, "Unit ID code %s is %s in DB but %s in SHP file" % ( unit_id, m_name, name) else: raise Exception, 'Area "%s" (%s) has neither ONS code nor unit ID' % ( name, area_code) except Area.DoesNotExist: print "New area: %s %s %s %s" % (area_code, ons_code, unit_id, name) m = Area( name= name, # If committing, this will be overwritten by the m.names.update_or_create type=Type.objects.get(code=area_code), country=Country.objects.get(code=country), generation_low=new_generation, generation_high=new_generation, ) if m.generation_high and current_generation and m.generation_high.id < current_generation.id: raise Exception, "Area %s found, but not in current generation %s" % ( m, current_generation) m.generation_high = new_generation if options['commit']: m.save() poly = [feat.geom] if options['commit']: m.names.update_or_create({'type': name_type}, {'name': name}) if ons_code: self.ons_code_to_shape[ons_code] = (m, poly) if options['commit']: m.codes.update_or_create({'type': code_version}, {'code': ons_code}) if unit_id: self.unit_id_to_shape[unit_id] = (m, poly) if options['commit']: m.codes.update_or_create({'type': code_type_os}, {'code': unit_id}) if options['commit']: save_polygons(self.unit_id_to_shape) save_polygons(self.ons_code_to_shape)
def handle_label(self, filename, **options): print filename new_generation = Generation.objects.new() if not new_generation: raise Exception, "No new generation to be used for import!" name_type = NameType.objects.get(code='O') code_type = CodeType.objects.get(code='gss') ds = DataSource(filename) layer = ds[0] for feat in layer: name = unicode(feat['NAME'].value, 'iso-8859-1') print " ", name name = re.sub('\s*\(DET( NO \d+|)\)\s*(?i)', '', name) name = re.sub('\s+', ' ', name) if "P Const" in name: area_code = 'SPC' elif "PER" in name: area_code = 'SPE' else: raise Exception, "Unknown type of area %s" % name ons_code = name_to_code[name] if ons_code in self.ons_code_to_shape: m, poly = self.ons_code_to_shape[ons_code] if options['commit']: m_name = m.names.get(type=name_type).name if name != m_name: raise Exception, "ONS code %s is used for %s and %s" % ( ons_code, name, m_name) # Otherwise, combine the two shapes for one area print " Adding subsequent shape to ONS code %s" % ons_code poly.append(feat.geom) continue try: m = Area.objects.get(codes__type=code_type, codes__code=ons_code) except Area.DoesNotExist: m = Area( type=Type.objects.get(code=area_code), country=Country.objects.get(name='Scotland'), generation_low=new_generation, generation_high=new_generation, ) if options['commit']: m.save() poly = [feat.geom] if options['commit']: m.names.update_or_create({'type': name_type}, {'name': name}) if ons_code: self.ons_code_to_shape[ons_code] = (m, poly) if options['commit']: m.codes.update_or_create({'type': code_type}, {'code': ons_code}) if options['commit']: save_polygons(self.ons_code_to_shape)
def handle_label(self, filename, **options): for k in ['generation_id','area_type_code','name_type_code','country_code']: if options[k]: continue raise Exception("Missing argument '--%s'" % k) generation_id = options['generation_id'] area_type_code = options['area_type_code'] name_type_code = options['name_type_code'] country_code = options['country_code'] area_type = Type.objects.get(code=area_type_code) name_type = NameType.objects.get(code=name_type_code) country = Country.objects.get(code=country_code) print "Importing from %s" % filename if not options['commit']: print '(will not save to db as --commit not specified)' current_generation = Generation.objects.current() new_generation = Generation.objects.get( id=generation_id ) # Need to parse the KML manually to get the ExtendedData kml_data = KML() xml.sax.parse(filename, kml_data) ds = DataSource(filename) layer = ds[0] for feat in layer: name = feat['Name'].value.decode('utf-8') name = re.sub('\s+', ' ', name) if not name: raise Exception( "Could not find a name to use for area" ) print " looking at '%s'" % name.encode('utf-8') try: m = Area.objects.get(name=name, type=area_type) except Area.DoesNotExist: m = Area( name = name, type = area_type, country = country, # parent_area = parent_area, generation_low = new_generation, generation_high = new_generation, ) # check that we are not about to skip a generation if m.generation_high and current_generation and m.generation_high.id < current_generation.id: raise Exception, "Area %s found, but not in current generation %s" % (m, current_generation) m.generation_high = new_generation g = feat.geom.transform(4326, clone=True) poly = [ g ] if options['commit']: m.save() m.names.update_or_create({ 'type': name_type }, { 'name': name }) save_polygons({ m.id : (m, poly) })