def update_or_create(): try: m = Area.objects.get(id=code) except Area.DoesNotExist: m = Area( id = code, name = name, type = area_code, country = country, 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': lang }, { 'name': v }) m.codes.update_or_create({ 'type': 'n5000' }, { 'code': code_str }) m.codes.update_or_create({ 'type': 'osm' }, { 'code': int(kml_data.data[name]['osm']) }) save_polygons({ code : (m, poly) })
def handle_label(self, filename, **options): 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!" short_filename = filename.split("/")[-1] filename_prefix = short_filename[:4] filename_suffix = short_filename.split(".")[0][-3:] # check shapefile type - we handle both LSOA and MSOA if filename_prefix=="LSOA": feat_name = 'LSOA04NM' feat_code = 'LSOA04CD' if filename_suffix=='BGC': area_type = 'OLG' else: area_type = 'OLF' elif filename_prefix=="MSOA": feat_name = 'MSOA04NM' feat_code = 'MSOA04CD' if filename_suffix=='BGC': area_type = 'OMG' else: area_type = 'OMF' else: raise Exception, "Sorry, this script only handles LSOA/MSOA shapefiles!" ds = DataSource(filename) layer = ds[0] for feat in layer: # retrieve name and code, and set country name = feat[feat_name].value lsoa_code = feat[feat_code].value country = lsoa_code[0] # check if the LOA already exists in db, create it if not try: m = Area.objects.get(codes__type=area_type, codes__code=lsoa_code) except Area.DoesNotExist: m = Area( type = area_type, country = country, generation_low = new_generation, generation_high = new_generation, ) # check the generation if m.generation_high and m.generation_high.id < current_generation: raise Exception, "Area %s found, but not in current generation %s" % (m, current_generation) m.generation_high = new_generation m.save() poly = [ feat.geom ] # TODO: check, is this type correct? m.names.update_or_create({ 'type': 'S' }, { 'name': name }) self.lsoa_code_to_shape[lsoa_code] = (m, poly) m.codes.update_or_create({ 'type': 'ons' }, { 'code': lsoa_code }) # save all the polygons once done self.save_polygons(self.lsoa_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!" 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='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 try: m = Area.objects.get(codes__type='gss', codes__code=ons_code) except Area.DoesNotExist: m = Area( type = area_code, country = 'S', 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': 'O' }, { 'name': name }) if ons_code: self.ons_code_to_shape[ons_code] = (m, poly) if options['commit']: m.codes.update_or_create({ 'type': 'gss' }, { 'code': ons_code }) if options['commit']: save_polygons(self.ons_code_to_shape)
def handle_label(self, filename, **options): print filename generation = Generation.objects.current() short_filename = filename.split("/")[-1] filename_prefix = short_filename[:4] filename_suffix = short_filename.split(".")[0][-3:] # check shapefile type - we handle both LSOA and MSOA if filename_prefix=="LSOA": feat_name = 'LSOA04NM' feat_code = 'LSOA04CD' if filename_suffix=='BGC': area_type = 'OLG' else: area_type = 'OLF' elif filename_prefix=="MSOA": feat_name = 'MSOA04NM' feat_code = 'MSOA04CD' if filename_suffix=='BGC': area_type = 'OMG' else: area_type = 'OMF' else: raise Exception, "Sorry, this script only handles LSOA/MSOA shapefiles!" ds = DataSource(filename) layer = ds[0] for feat in layer: # retrieve name and code, and set country name = feat[feat_name].value lsoa_code = feat[feat_code].value country = lsoa_code[0] # skip if the SOA already exists in db (SOAs don't change) if Area.objects.filter(type=area_type, codes__code=lsoa_code).count(): continue print "Adding %s (%s) %s" % (name, lsoa_code, feat.geom.geom_name) m = Area( type = area_type, country = country, generation_low = generation, generation_high = generation, ) m.save() m.names.update_or_create({ 'type': 'S' }, { 'name': name }) m.codes.update_or_create({ 'type': 'ons' }, { 'code': lsoa_code }) p = feat.geom if p.geom_name == 'POLYGON': shapes = [ p ] else: shapes = p for g in shapes: m.polygons.create(polygon=g.wkt)
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!" 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' country = 'O' try: m = Area.objects.get(codes__type='n5000', codes__code=code_str) except Area.DoesNotExist: m = Area( id = code, 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 g = feat.geom.transform(4326, clone=True) poly = [ g ] if options['commit']: m.save() m.names.update_or_create({ 'type': 'M' }, { 'name': name }) m.codes.update_or_create({ 'type': 'n5000' }, { 'code': code_str }) save_polygons({ code : (m, poly) })
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)