Skip to content

Commit 4767e5a

Browse files
committed
refactored to dbscan so array of ids could be included
1 parent 59ed39b commit 4767e5a

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

network_routing/gaps/segments/generate_islands.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def random_rgb(a: float = 1.0) -> str:
2323
def generate_islands(
2424
db: Database,
2525
tbl: str = "pedestriannetwork_lines",
26+
id_col: str = "objectid",
2627
islands: str = "islands",
2728
output_schema: str = "data_viz",
2829
):
@@ -45,13 +46,17 @@ def generate_islands(
4546
)
4647

4748
query = f"""
48-
SELECT
49-
ST_COLLECTIONEXTRACT(
50-
UNNEST(ST_CLUSTERINTERSECTING(geom)),
51-
2
52-
) AS geom
53-
FROM {tbl}
54-
"""
49+
SELECT ST_Collect(geom) AS geom,
50+
ARRAY_AGG({id_col}) AS id_agg
51+
FROM (
52+
SELECT *,
53+
ST_ClusterDBSCAN(geom, 0, 1) OVER() AS _clst
54+
FROM {tbl}
55+
) q
56+
GROUP BY
57+
_clst
58+
;
59+
"""
5560
db.gis_make_geotable_from_query(
5661
query, f"{output_schema}.{islands}", "MULTILINESTRING", 26918
5762
)
@@ -126,7 +131,10 @@ def generate_islands(
126131

127132

128133
if __name__ == "__main__":
129-
from network_routing import pg_db_connection
134+
# from network_routing import pg_db_connection
135+
db = Database.from_config("lts2", "localhost")
130136

131-
db = pg_db_connection()
132-
generate_islands(db)
137+
# db = pg_db_connection()
138+
# generate_islands(db)
139+
generate_islands(db, "sidewalk.ped_network",
140+
"objectid", "sw_islands", "sidewalk")

0 commit comments

Comments
 (0)