Skip to content

Commit

Permalink
make usage of landuse. FIRST: you need to fetch osm data whrer landus…
Browse files Browse the repository at this point in the history
…e is not null!
  • Loading branch information
Robert John committed Aug 16, 2023
1 parent 3b50f4f commit 35b4d8a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
26 changes: 26 additions & 0 deletions ding0/config/landuse_table_and_sql_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# landuse has to be fetched and stored in DB as selct * from osm data where osmdata.landuse is not null;

class Landuse(Base):
"""Landuse Model"""

__tablename__ = "landuse"

osm_id = Column(Integer, primary_key=True)
landuse = Column(String(50))
natural = Column(String(50))
geometry = Column(Geometry('POLYGON'))
area = Column(Float)



def get_osm_landuse(geo_area, session_osm):
""" load ways from db for given polygon as geo_area_wkt """

landuse = session_osm.query(Landuse).filter(
func.st_intersects(func.ST_GeomFromText(geo_area, srid), Landuse.geometry))

landuse_sql_df = pd.read_sql(
landuse.statement,
con=session_osm.bind)
# con=engine_osm both ways are working. select the easier/ more appropriate one
return landuse_sql_df
9 changes: 8 additions & 1 deletion ding0/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,11 +839,17 @@ def import_lv_load_areas_and_build_new_lv_districts(

# Get n_clusters based on capacity of the load area.
n_cluster = get_cluster_numbers(buildings_w_loads_df, simp_graph)

# fetch landuse like oam ways
# todo: check if wokring. landuse has to be stored in DB before !
landuse_sql_df = db_io.get_osm_landuse(self.orm, session, buffer_poly_list[-1].wkt)
landuse_gdf['geometry'] = landuse_gdf.geometry.apply(to_shape)
landuse_gdf = gpd.GeoDataFrame(landuse_gdf, geometry="geometry", crs=3035)

# Cluster graph and locate lv stations based on load center per cluster.
clustering_successfully, cluster_graph, mvlv_subst_list, nodes_w_labels = \
distance_restricted_clustering(
simp_graph, n_cluster, street_loads_df, mv_grid_district, id_db
simp_graph, n_cluster, street_loads_df, mv_grid_district, id_db, landuse_sql_df
)
if not clustering_successfully:
return f"Clustering not successful for " \
Expand Down Expand Up @@ -920,6 +926,7 @@ def import_lv_load_areas_and_build_new_lv_districts(

# Get convex hull for ding0 objects.
points = get_points_in_load_area(cluster_geo_list)

if create_lvgd_geo_method == 'convex_hull':
polygon = get_convex_hull_from_points(points)
elif create_lvgd_geo_method == 'bounding_box':
Expand Down
4 changes: 2 additions & 2 deletions ding0/grid/lv_grid/clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def apply_AgglomerativeClustering(G, k, round_decimals=True):
else: return np.array([0])


def distance_restricted_clustering(simp_graph, n_cluster, street_loads_df, mv_grid_district, id_db):
def distance_restricted_clustering(simp_graph, n_cluster, street_loads_df, mv_grid_district, id_db, landuse):
"""
Apply ward hierarchical AgglomerativeClustering with connectivity constraints for underlying graph
https://scikit-learn.org/stable/modules/clustering.html#hierarchical-clustering
Expand Down Expand Up @@ -101,7 +101,7 @@ def distance_restricted_clustering(simp_graph, n_cluster, street_loads_df, mv_gr
# locate stations for districts
mvlv_subst_list, valid_cluster_distance = get_mvlv_subst_loc_list(cluster_graph, nodes_w_labels, \
street_loads_df, labels, n_cluster, \
check_distance_criterion)
check_distance_criterion, landuse)

if valid_cluster_distance:

Expand Down
38 changes: 37 additions & 1 deletion ding0/grid/lv_grid/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,25 @@ def loads_in_ons_dist_threshold(dm_cluster, cluster_nodes, osmid):
return True


def get_mvlv_subst_loc_list(cluster_graph, nodes, street_loads_df, labels, n_cluster, check_distance_criterion=True):
def _get_landuse_in_area(landuse_gdf, area_geo):
landuse_gdf.loc[:, "intersecting_cluster"] = landuse_gdf.intersects(area_geo)
landuse_gdf = landuse_gdf.loc[landuse_gdf.intersecting_cluster]
landuse_intersecting_dict = {}
for landuse, grp in landuse_gdf.groupby("landuse"):
areasin = grp.intersection(area_geo.buffer(0))
areasqm = 0
if len(areasin) > 1:
for ain in areasin:
areasqm += ain.area
else:
areasqm = areasin.area.item()
landuse_intersecting_dict[landuse] = areasqm

return landuse_intersecting_dict



def get_mvlv_subst_loc_list(cluster_graph, nodes, street_loads_df, labels, n_cluster, check_distance_criterion=True, landuse_gdf=None):
"""
identify position of station at street load center
get list of location of mvlv substations for load areal
Expand Down Expand Up @@ -154,9 +172,27 @@ def get_mvlv_subst_loc_list(cluster_graph, nodes, street_loads_df, labels, n_clu
# due to distance threshold to ons is trespassed
valid_cluster_distance = False
return mvlv_subst_list, valid_cluster_distance

area_geo = df_cluster.geometry.to_list() # looks like duple somewhere in further coding, maybe just keep from here instead same processing later
if len(area_geo) == 1:
area_geo = area_geo[0]
area_geo_area = 100
elif len(area_geo) == 2:
area_geo = LineString(area_geo)
area_geo_area = area_geo.length
else:
area_geo = Polygon(area_geo)
area_geo_area = area_geo.area

if landuse_gdf is not None:
landuse_gdf.loc[:, "intersecting_cluster"] = False # init False and set True where intersecting
landuse_dict = _get_landuse_in_area(landuse_gdf, area_geo)
else:
landuse_dict = {}

mvlv_subst_loc = cluster_graph.nodes[osmid]
mvlv_subst_loc['osmid'] = osmid
mvlv_subst_loc['landuse'] = landuse_dict # todo: reagarding this dict: how to choose if village, rural, city or whatever ?
mvlv_subst_loc['graph_district'] = cluster_subgraph
mvlv_subst_list.append(mvlv_subst_loc)

Expand Down

0 comments on commit 35b4d8a

Please sign in to comment.