def test_get_indicators_path(self): shp_name = "estaciones" subcategory = "buffers" exp_path = os.path.join(pf.get_project_dir(), "indicadores/buffers/estaciones/estaciones") path = pf.get_indicators_shp_path(shp_name, subcategory) self.assertEqual(path, exp_path)
def test_get(self): path = pf.get("shp/transporte") exp_path = os.path.join(pf.get_project_dir(), "shp/transporte") self.assertEqual(path, exp_path) path = pf.get("/Users/abenassi/github/tod/shp/transporte") exp_path = "/Users/abenassi/github/tod/shp/transporte" self.assertEqual(path, exp_path)
def test_find_shp_path(self): directory = "shp/transporte/subte-estaciones" shp_path = pf.find_shp_path(directory) exp_shp_path = os.path.join( pf.get_project_dir(), "shp/transporte/subte-estaciones/estaciones_de_subte") self.assertEqual(shp_path, exp_shp_path)
def reproject_point(lat, lon, shp_path): point_wgs84 = Point(lat, lon) wgs84_prj = os.path.join(get_project_dir(), "shp", "4326.prj") fromcrs = pycrs.loader.from_file(wgs84_prj) fromcrs_proj4 = fromcrs.to_proj4() tocrs = pycrs.loader.from_file(shp_path + ".prj") tocrs_proj4 = tocrs.to_proj4() # print(fromcrs_proj4) project = partial(pyproj.transform, pyproj.Proj(fromcrs_proj4), pyproj.Proj(tocrs_proj4)) return transform(project, point_wgs84)
def reproject_point(lat, lon, shp_path): point_wgs84 = Point(lat, lon) wgs84_prj = os.path.join(get_project_dir(), "shp", "4326.prj") fromcrs = pycrs.loader.from_file(wgs84_prj) fromcrs_proj4 = fromcrs.to_proj4() tocrs = pycrs.loader.from_file(shp_path + ".prj") tocrs_proj4 = tocrs.to_proj4() # print(fromcrs_proj4) project = partial( pyproj.transform, pyproj.Proj(fromcrs_proj4), pyproj.Proj(tocrs_proj4)) return transform(project, point_wgs84)
def join_df_with_shp(shp_path, df, output_dir, create_pop_density=True): """Join data in a DataFrame with a shp from common ids. Args: shp_path (str): Path where shp is. df (pandas.DataFrame): A data frame with data to join by id. output_dir (str): Directory where new shp will be. Side effects: Create a copy of the shp with joined data in output_dir. Create population density indicator """ dir_input_shp = os.path.dirname(shp_path) shp_name = os.path.basename(shp_path) dir_output_shp = os.path.join(get_project_dir(), output_dir, shp_name) if os.path.isdir(dir_output_shp): # print("should delete") shutil.rmtree(dir_output_shp) shutil.copytree(dir_input_shp, dir_output_shp) sf = shapefile.Reader(shp_path) w = shapefile.Writer() # creating fields for field in sf.fields: w.field(*field) for indicator in df.columns: # print(indicator, str(indicator) in IDS_GCBA.keys(), IDS_GCBA.keys()) if str(indicator) in IDS_GCBA.values(): w.field(*[str(indicator), str("C"), 40, 0]) else: w.field(*[str(indicator), str("N"), 20, 18]) if create_pop_density: w.field(*[str("area_km2"), str("N"), 20, 18]) w.field(*[str("hab_km2"), str("N"), 20, 18]) # print(w.fields) # put df index in unicode df.index = [unicode(i) for i in df.index] # creating records for record_shape in sf.iterShapeRecords(): record = record_shape.record shape = record_shape.shape if unicode(record[0]) in df.index: for col in df.columns: record.append(df[col][unicode(record[0])]) if create_pop_density: area = calculate_area(shape) / 1000000 record.append(area) population = df[POPULATION][unicode(record[0])] record.append(population / area) else: for col in df.columns: record.append(0.0) if create_pop_density: record.append(0.0) record.append(0.0) # print("\n", record) w.record(*record) w.saveDbf(os.path.join(dir_output_shp, shp_name))