def calc_surrounding_area(zone_gdf, buffer_m): geometry_without_holes = zone_gdf.convex_hull geometry_without_holes_gdf = Gdf(geometry=geometry_without_holes.values) geometry_without_holes_gdf["one_class"] = "buildings" geometry_merged = geometry_without_holes_gdf.dissolve(by='one_class', aggfunc='sum') geometry_merged_final = Gdf(geometry=geometry_merged.convex_hull) new_buffer = Gdf(geometry=geometry_merged_final.buffer(buffer_m)) area = overlay(geometry_merged_final, new_buffer, how='symmetric_difference') # THIS IS ANOTHER METHOD, NOT FUNCTIONAL THOUGH # from shapely.ops import Point # # new GeoDataFrame with same columns # points = [] # # Extraction of the polygon nodes and attributes values from polys and integration into the new GeoDataFrame # for index, row in zone_gdf.iterrows(): # for j in list(row['geometry'].exterior.coords): # points.append(Point(j)) # # concave_hull, edge_points = alpha_shape(points, alpha=0.1) # simple_polygons = [x for x in concave_hull] # geometry_merged_final = Gdf(geometry=simple_polygons) # geometry_merged_final.plot() # plt.show() # new_buffer = Gdf(geometry=geometry_merged_final.buffer(buffer_m)) # area = overlay(geometry_merged_final, new_buffer, how='symmetric_difference') # area.plot() return area, geometry_merged_final
def geom_buffer(df:gpd.GeoDataFrame, buffer_dis=100, att='buffer_', crs_wgs=4326, crs_prj=900913): df.loc[:, att] = df.to_crs(epsg=crs_prj).buffer(buffer_dis).to_crs(epsg=crs_wgs) df.set_geometry(att, inplace=True) whole_geom = df.dissolve().iloc[0][att] return df, whole_geom
def match_shapes(self, shapes, buffer=0.01): """ checks if shapes are completely within each others buffers, aggregates routes for these :return: """ # buffer for spatial self join first_points = shapes["geometry"].apply(lambda x: Point(x.coords[0])) last_points = shapes["geometry"].apply(lambda x: Point(x.coords[-1])) points = pd.concat([first_points, last_points]) point_df = points.to_frame(name='geometry') #point_df = point_df.set_geometry(point_df["geometry"], crs=self.crs_eurefin) point_df = point_df.set_geometry(point_df["geometry"], crs=self.crs_wgs) #buffer = point_df.buffer(30) #buffer = GeoDataFrame(crs=self.crs_eurefin, geometry=point_df.buffer(buffer)) buffer = GeoDataFrame(crs=self.crs_wgs, geometry=point_df.buffer(buffer)) buffer["everything"] = 1 gdf_poly = buffer.dissolve(by="everything") polygons = None for geoms in gdf_poly["geometry"]: polygons = [polygon for polygon in geoms] #single_parts = GeoDataFrame(crs=self.crs_eurefin, geometry=polygons) single_parts = GeoDataFrame(crs=self.crs_wgs, geometry=polygons) single_parts['new_stop_I'] = single_parts.index gdf_joined = sjoin(shapes, single_parts, how="left", op='within') return gdf_joined
def dissolve_touching(gdf: gpd.GeoDataFrame): dg = 'dissolve_group' overlap_matrix = gdf.geometry.apply( lambda x: gdf.geometry.touches(x)).values.astype(int) n, ids = connected_components(overlap_matrix) gdf[dg] = ids dissolved = gdf.dissolve(by=dg) return dissolved
def flatten_geometry(gdf : gpd.GeoDataFrame) -> gpd.GeoDataFrame: """ flattens/merges geometric shapes of import kml and return new geodataframe Args: gdf (gpd.GeoDataFrame): raw df from import KML Returns: [type]: [description] """ gdf['new_col'] = 0 gdf_merged = gdf.dissolve(by='new_col') return gdf_merged
def plot_cluster(gdf: geopandas.GeoDataFrame, fig_location: str = None, show_figure: bool = False): """ Vykresleni grafu s lokalitou vsech nehod v jihomoravskem kraji shlukovanych do 9 clusteru """ gdf = gdf[gdf["region"] == 'JHM'] plt.figure(figsize=(20, 15)) ax = plt.gca() gdf = gdf.set_geometry(gdf.centroid) gdf = gdf.to_crs("epsg:3857") # vyplotovani vsech nehod do grafu gdf.plot(ax=ax, color="grey", markersize=0.5) # ypocet kmeans algoritmu pro 9 clusteru s max 300 iteracemi coords = np.dstack([gdf.geometry.x, gdf.geometry.y]).reshape(-1, 2) kmeans = sklearn.cluster.KMeans(n_clusters=9).fit(coords) clusters = geopandas.GeoDataFrame(geometry=geopandas.points_from_xy( kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1])) # pridani sloupce s clustery do dataframu gdf["cluster"] = kmeans.labels_ gdf = gdf.dissolve(by="cluster", aggfunc={"p1": "count"}) gdf = gdf.merge(clusters, left_on="cluster", right_index=True).set_geometry("geometry_y") # plotovani clusteru a omezeni os gdf.plot(ax=ax, markersize=gdf["p1"] * 0.8, column="p1", legend=True, alpha=0.5) ax.set_title("Nehody v Jihomoravskem kraji") ax.axis("off") ax.set_ylim([6220000, 6380000]) ax.set_xlim([1735000, 1970000]) # mapovy podklad ctx.add_basemap(ax, crs="epsg:3857", source=ctx.providers.Stamen.TonerLite, zoom=10, alpha=0.9) # ukladani/zobrazeni grafu if fig_location is not None: plt.savefig(fig_location) if show_figure: plt.show()
def dissolve_un_m49_regions( self, world: geopandas.GeoDataFrame) -> geopandas.GeoDataFrame: codes_mapping = self._unm49_data[[ 'Region Code', 'Region Name', 'Sub-region Code', 'Sub-region Name', 'Intermediate Region Code', 'Intermediate Region Name', 'ISO-alpha3 Code', 'Country or Area', 'M49 Code' ]] world = world.merge(codes_mapping, left_on='iso_a3', right_on='ISO-alpha3 Code', how='left') world_regions = [] for level in ['Region', 'Sub-region', 'Intermediate Region']: code_label = f'{level} Code' name_label = f'{level} Name' regions_group = world.dissolve(by=code_label)[[ 'geometry', name_label ]].rename(columns={name_label: 'name'}) regions_group.index.name = 'id' regions_group.index = regions_group.index.astype('int64') world_regions.append(regions_group) antartica = world[world['ISO-alpha3 Code'] == 'ATA'][[ 'geometry', 'M49 Code', 'Country or Area' ]] antartica = antartica.rename(columns={ 'M49 Code': 'id', 'Country or Area': 'name' }).astype({ 'id': 'int64' }).set_index('id') world_regions.append(antartica) world = pd.concat(world_regions) world['un_m49_numeric'] = world.index.astype('int64') world.index = world.index.astype('str') return world
def aggregate_raw_PV_polygons_to_raw_PV_installations( self, raw_PV_polygons_gdf: gpd.GeoDataFrame) -> gpd.GeoDataFrame: """ Aggregate raw PV polygons belonging to the same PV installation. Raw refers to the fact that the PV area is not corrected by the tilt angle. For each PV installation, we compute its raw area and a unique identifier. Parameters ---------- raw_PV_polygons_gdf : GeoPandas.GeoDataFrame GeoDataFrame which contains all the raw PV polygons which have been detected during the previous pipeline step. Returns ------- GeoPandas.GeoDataFrame GeoDataFrame with dissolved PV polygon geometries. """ # Buffer polygons, i.e. overwrite the original polygons with their buffered versions # Based on our experience, the buffer value should be within [1e-6, 1e-8] degrees raw_PV_polygons_gdf["geometry"] = raw_PV_polygons_gdf[ "geometry"].buffer(1e-6) # Dissolve, i.e. aggregate, all PV polygons into one Multipolygon raw_PV_polygons_gdf = raw_PV_polygons_gdf.dissolve(by="class") # Explode multi-part geometries into multiple single geometries raw_PV_installations_gdf = ( raw_PV_polygons_gdf.explode().reset_index().drop( columns=["level_1"])) # Compute the raw area for each pv installation raw_PV_installations_gdf["raw_area"] = ( raw_PV_installations_gdf["geometry"].to_crs(epsg=5243).area) # Create a unique identifier for each pv installation raw_PV_installations_gdf[ "identifier"] = raw_PV_installations_gdf.index.map( lambda id: "polygon_" + str(id)) return raw_PV_installations_gdf
def get_polygons_of_loccode(geo_df: gpd.GeoDataFrame, dissolveby='OA11CD', search=None) -> gpd.GeoDataFrame: """ Gets the polygon for a place based on it name, LSOA code or OA code Parameters: geo_df: (gpd.Datafame): loc_code = LSOA11CD, OA11CD or LSOA11NM search = search terms to find in the LSOA11NM column. Only needed if intending to dissolve on a name in the LSOA11NM column Returns: (gpd.DataFrame) agregated multipolygons, agregated on LSOA, OA code, or a search in the LSOA11NM column """ if dissolveby in ['LSOA11CD', 'OA11CD']: polygon_df = geo_df.dissolve(by=dissolveby) else: filtered_df = geo_df[geo_df[f'{dissolveby}'].str.contains(search)] filtered_df.insert(0, 'place_name', search) polygon_df = filtered_df.dissolve(by='place_name') polygon_df = gpd.GeoDataFrame(polygon_df.pop('geometry')) return polygon_df