2

I'm working with a subset of data from OpenStreetMap, where I have these island-like groups of line features. The groups are not connected to one another, but the lines are all interconnected within each group. I'd like to aggregate groups of line features that 1) are contiguous and 2) share a common attribute value, into single features, with non-grouping attributes aggregated.

This may sound like a dissolve operation, but a dissolve will typically aggregate all features in the dataset with a common value into a single multipart feature, irrespective of spatial relationship. Rather, I want only features that are connected to each other (directly or by intermediate features) to be aggregated into an output feature.

PostGIS's cluster functions offer exactly what I want, except for the database dependency. In PostGIS, I would do a query like this:

SELECT ST_Collect(the_geom) AS geom,
       min(osmid::integer) AS osmid,
       string_agg(distinct highway, ',') AS highway,
       string_agg(distinct ref, ',') AS ref
FROM
    (SELECT *,
            ST_ClusterDBSCAN(the_geom, 0, 1) OVER() AS _clst
     FROM mytable) q
GROUP BY _clst
ORDER BY osmid

Is it possible get this same result without PostGIS, using only Python libraries like Shapely, GeoPandas, or similar?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.