converting hierarchical clusters to polygons

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

converting hierarchical clusters to polygons

Bannar-Martin, Katherine
I have used hierarchical clustering to assign lats and longs (start and end positions of lines) to separate cluster IDs.
I now need to convert each cluster of points to a polygon.
My first attempt was with CHULL, with the aim to then convert each convex polygon to a polygon layer. However, I cannot get the hulls to plot by cluster and then save as an external object.
I think the easiest method would be to take the outside points of each cluster of points and connect them with a polyline and then convert it to a polygon.
I have 100s of clusters per year (13 years) so I do not want to have 1000s of separate polygon files, but instead one layer with all the polygons per year.

Dummy data to show approximate structure:
GISID     x             y             Attribute              Year       CLUSTER_ID
1             -129.24 52.994   Start                     2005      1
2             -131.26 52.73     Start                     2006      33
3             -126.02 49.297   End                       2005      5
...

Any advice, helpfiles, or vignettes would be greatly appreciated. I've read a variety of stackoverflow entries and r-sig-geo entries and have unsuccessfully adapted other solutions.
e.g. https://stackoverflow.com/questions/44928507/draw-polygons-around-clusters-from-hclust
https://www.nceas.ucsb.edu/scicomp/usecases/GenerateConvexHullAndROIForPoints
Thanks!

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: converting hierarchical clusters to polygons

Kent Johnson
On Mon, May 28, 2018 at 6:00 AM, <[hidden email]> wrote:

> From: "Bannar-Martin, Katherine"
>         <[hidden email]>
> To: "[hidden email]" <[hidden email]>
> Subject: [R-sig-Geo] converting hierarchical clusters to polygons
> Message-ID:
>         <[hidden email].
> dfo-mpo.ca>
>
> Content-Type: text/plain; charset="utf-8"
>
> I have used hierarchical clustering to assign lats and longs (start and
> end positions of lines) to separate cluster IDs.
> I now need to convert each cluster of points to a polygon.
>


> I have 100s of clusters per year (13 years) so I do not want to have 1000s
> of separate polygon files, but instead one layer with all the polygons per
> year.
>
> Dummy data to show approximate structure:
> GISID     x             y             Attribute              Year
>  CLUSTER_ID
> 1             -129.24 52.994   Start                     2005      1
> 2             -131.26 52.73     Start                     2006      33
> 3             -126.02 49.297   End                       2005      5
>

This uses tidyverse and sf to find the convex hull of points in each
cluster and create a simple features object with one row per polygon. It
retains the Year and CLUSTER_ID so you can select by year and show all
clusters or write one file per year.

library(tidyverse)
library(sf)

d = read_table2(col_names=TRUE,
file="GISID     x             y             Attribute              Year
   CLUSTER_ID
1             -129.24 52.994   Start                     2005      1
2             -131.26 52.73     Start                     2006      33
3             -126.02 49.297   End                       2005      5")

groups = d %>%
  select(-GISID, -Attribute) %>%
  nest(x, y) %>%
  mutate(poly = map(data, ~st_convex_hull(st_multipoint(as.matrix(.[,c('x',
'y')]))))) %>%
  select(-data) %>%
  st_sf()

# For example to plot all polygons for one year, colored y CLUSTER_ID:
groups %>% filter(Year==2006) %>% select(-Year) %>% plot()

HTH,
Kent Johnson

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo