def resolve_nuts(row): if not 'regions' in RES: RES['regions'] = load_region_hierarchy() if not 'geocoder' in RES: RES['geocoder'] = shapegeocode.geocoder( reference_data_path('nuts2-shapefile/data/NUTS_RG_10M_2006.shp'), filter=lambda r: r['STAT_LEVL_'] == 3) try: data = RES['geocoder'].geocode(float(row['lat']), float(row['lon'])) if data is None: return {} nuts3_code = data.get('NUTS_ID') nuts3 = RES['regions'].get(nuts3_code, {}) nuts2 = RES['regions'].get(nuts3_code[:4], {}) nuts1 = RES['regions'].get(nuts3_code[:3], {}) return { 'nuts3': nuts3_code, 'nuts3_label': nuts3.get('LABEL'), 'nuts2': nuts3_code[:4], 'nuts2_label': nuts2.get('LABEL'), 'nuts1': nuts3_code[:3], 'nuts1_label': nuts1.get('LABEL'), } except Exception, e: log.exception(e) return {}
def load_region_hierarchy(): with open(reference_data_path('NUTS_2006.csv'), 'rb') as fh: data = {} for row in csv.DictReader(fh): data[row['CODE']] = dict([(k, v.decode('utf-8')) \ for k, v in row.items()]) return data