def mpa_all_uba(): u = gp.read_file('data/mex_ALL_mpa_uba.geojson') u.set_index('name', inplace=True) u.index.name = 'urban' u.crs = None assign_crs(u, 4326) return u
def metropolitans_16(): m = gp.read_file('data/mex_16_metropolitans.geojson') m.set_index('name', inplace=True) m.index.name = 'metropolitan' m.crs = None assign_crs(m, 4326) return m
def urban_areas_cvh_16(): u = gp.read_file('data/mex_16_munic_urban_merge_cvh.geojson') u.set_index('name', inplace=True) u.index.name = 'urban' u.crs = None assign_crs(u, 4326) return u
def grids(rkind, side, redo=False): """ build grids and save it using gzip, gp.to_json; read grids if it exists unless redo=True :param rkind: which kind of regions is used, now only cities :param side: the side of grid in meter :param redo: if True, build grids regardless the existing file :return: """ import gzip rgns = regions(rkind) rname = rgns.index.name grid_path = f'data/mex_grid_{rkind}_{side}m.geojson.gz' if not redo and os.path.exists(grid_path): print('reading existing grids') g = gp.read_file(f'gzip://{grid_path}') assign_crs(g, 4326) return g print('building grids') g = gp_polys_to_grids(rgns, side, cur_crs=4326, eqdc_crs=EQDC_CRS, pname=rname) g['grid'] = g.index print('writing grids as gzip') assign_crs(g, 4326) with gzip.open(grid_path, 'wt') as fout: fout.write(g.to_json()) return g
def cities(): """ It is actually metropolitan areas. """ c = gp.read_file('data/cities_mexico.geojson') c.set_index('cname', inplace=True) c.index.name = 'city' c.crs = None assign_crs(c, 4326) return c
def tower2loc(loc_buffer): t2loc_path = f'data/mex_tower/tower2loc-{loc_buffer}.geojson.gz' if os.path.exists(t2loc_path): print('reading existing t2loc file') t2loc = gp.read_file(f'gzip://{t2loc_path}') t2loc = t2loc.set_index('id') t2loc.index = t2loc.index.astype(int) t2loc.index.name = None gis.assign_crs(t2loc, 4326) return t2loc # ============= # distribute tower's users count to intersections with localidad by population # ============= print('load tower vor') tvor = mex.tower_vor() print('load localidads') localidad = mex.localidad(loc_buffer, to_crs=4326) loc_with_pop = localidad[localidad.Pop > 0] print('intersect tower and loc') # compute the intersection area between tower and localidad t2loc = gis.polys2polys(tvor, loc_with_pop, pname1='tower', pname2='localidad', cur_crs=4326, area_crs=mex.AREA_CRS, intersection_only=False) t2loc = t2loc.merge(loc_with_pop[['Pop']], left_on='localidad', right_index=True) print('compute weight') # localidad area is the sum area covered by towers # because Localidads' polgyons are note exactly the same as the official map # also, the points are bufferred, which adds fake areas. loc_area = t2loc.groupby('localidad').iarea.sum() loc_area.name = 'loclidad_area' t2loc = t2loc.drop(['localidad_area', 'weight'], axis=1).merge(loc_area.to_frame(), left_on='localidad', right_index=True) # iPop is the population of the intersected area between a tower and a localidad # within a localidad, the population is assumed to be distributed evenly over space # therefore the population is divided proportionally to the intersection area t2loc['iPop'] = t2loc.Pop * t2loc.iarea / t2loc.loclidad_area # the total population covered by a tower is the sum of iPop tower_cover_pop = t2loc.groupby('tower').iPop.sum() tower_cover_pop.name = 'tower_pop' t2loc = t2loc.merge(tower_cover_pop.to_frame(), left_on='tower', right_index=True) # the weight to distribute tower's users count t2loc['weight'] = t2loc.iPop / t2loc.tower_pop print('saving result') with gzip.open(t2loc_path, 'wt') as fout: fout.write(t2loc.to_json()) return t2loc
def tower2loc(loc_buffer): t2loc_path = f'data/mex_tower/tower2loc-{loc_buffer}.geojson.gz' if not os.path.exists(t2loc_path): raise FileNotFoundError('please run the scripts in mex_prep/ first') print('loading t2loc with geometry') t2loc = gp.read_file(f'gzip://{t2loc_path}') t2loc = t2loc.set_index('id') t2loc.index = t2loc.index.astype(int) t2loc.index.name = None assign_crs(t2loc, 4326) return t2loc
def tower_vor(rkind=None, intersection_only=False, in_country=True): """ :param rkind: region kind :param intersection_only: works with rkind only :param in_country: whether to cut vor by country boarder or not :return: tower vor """ in_or_not = 'in_country' if in_country else 'raw_vor' path = f'data/mex_tower/mex_tvor_{in_or_not}.geojson' if os.path.exists(path): tvor = gp.read_file(path) tvor.set_index('gtid', inplace=True) assign_crs(tvor, 4326, ignore_gpdf_crs=True) print(f'loading existing tvor file: {path}') else: t = tower() # voronoi polygons across mexico tvor = lonlats2vor_gp(t.lonlat.tolist(), dataframe=True) tvor['gtid'] = t.gtid if in_country: print('clipping tvor outside mexico country boarder') country_poly = country().geometry.values[0] tvor['geometry'] = tvor.geometry.apply( lambda x: clip_if_not_within(x, country_poly)) assign_crs(tvor, 4326) print(f'saving tvor file: {path}') tvor.to_file(path, driver='GeoJSON') tvor.set_index('gtid', inplace=True) if rkind is None: return tvor else: rgns = regions(rkind) return polys2polys(tvor, rgns, tvor.index.name, rgns.index.name, cur_crs=4326, area_crs=AREA_CRS, intersection_only=intersection_only)
def pts(to_4326=False): tower_info_path = mex_root + mex_tower_fn towers = pd.read_csv(tower_info_path, header=None, sep='|') towers['lonlat'] = towers.apply(lambda x: '%.6f' % (x[2]) + ',' + '%.6f' % (x[3]), axis=1) # group towers with same location towers_gp = towers.groupby('lonlat')[0].apply(list).to_frame() towers_gp['gtid'] = towers_gp[0].apply(lambda x: '-'.join(x)) towers_shp = towers_gp.reset_index() towers_shp['lonlat'] = towers_shp.lonlat.apply(lambda x: eval(x)) towers_shp['geometry'] = towers_shp.lonlat.apply(lambda x: Point(x)) towers_shp = gp.GeoDataFrame(towers_shp) gis.assign_crs(towers_shp, 4326) if not to_4326: towers_shp = towers_shp.to_crs(mex.crs) return towers_shp
def tower(): tower_info_path = mex_root + mex_tower_fn towers = pd.read_csv(tower_info_path, header=None, sep='|') towers['lonlat'] = towers.apply(lambda x: '%.6f' % (x[2]) + ',' + '%.6f' % (x[3]), axis=1) # group towers with same location towers_gp = towers.groupby('lonlat')[0].apply(list).to_frame() towers_gp['gtid'] = towers_gp[0].apply(lambda x: '-'.join(x)) # get group id # gt2loc = {row['gtid']: loc.split(',') for loc, row in towers_gp.iterrows()} # t2gt = {} # for _, row in towers_gp.iterrows(): # for tid in row[0]: # t2gt[tid] = row['gtid'] towers_shp = towers_gp.reset_index() towers_shp['lonlat'] = towers_shp.lonlat.apply(lambda x: eval(x)) towers_shp['geometry'] = towers_shp.lonlat.apply(lambda x: Point(x)) towers_shp = gp.GeoDataFrame(towers_shp) assign_crs(towers_shp, 4326) return towers_shp
def country(): c = gp.read_file('data/mex_country.geojson') c.crs = None assign_crs(c, 4326) return c