Skip to content

Instantly share code, notes, and snippets.

@hrbrmstr
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrbrmstr/e29cf9138e480db9e67d to your computer and use it in GitHub Desktop.
Save hrbrmstr/e29cf9138e480db9e67d to your computer and use it in GitHub Desktop.
tree crowns to polys
TREE POINT X Y
1 1 718434.8 4830761.3
1 2 718436.3 4830761.7
1 3 718437.6 4830761.6
1 4 718438.9 4830760.7
1 5 718439.6 4830759.4
1 6 718438.5 4830757.8
1 7 718436.4 4830758.4
1 8 718435.6 4830759
7 1 718427.7 4830761.5
7 2 718428.9 4830762.5
7 3 718429.6 4830761.7
7 4 718430.3 4830761.3
7 5 718430 4830760.8
7 6 718430.4 4830759.6
7 7 718429.3 4830759.4
7 8 718428.2 4830760
46 1 718427.2 4830769.4
46 2 718427.5 4830769.4
46 3 718428.2 4830770.1
46 4 718428.3 4830770.6
46 5 718427.3 4830771.1
46 6 718426.9 4830770.5
46 7 718426.6 4830769.7
46 8 718427 4830769.6
71 1 718421.1 4830771.2
71 2 718422 4830770.7
71 3 718423 4830770.5
71 4 718423.4 4830772
71 5 718422.8 4830772.7
71 6 718422.3 4830772.9
71 7 718421.9 4830772.8
71 8 718421.2 4830772.3
26 1 718426.9 4830767.1
26 2 718428.5 4830768.1
26 3 718430.1 4830767.6
26 4 718430.8 4830766.2
26 5 718429.8 4830764.4
26 6 718428.3 4830764.4
26 7 718427.1 4830764.9
26 8 718427.2 4830766.1
22 1 718423.2 4830758.9
22 2 718423.5 4830760
22 3 718424.3 4830758.8
22 4 718424.7 4830757.4
22 5 718423.9 4830754.8
22 6 718420.5 4830755.8
22 7 718420.1 4830758.3
22 8 718420.4 4830759.3
86 1 718414.7 4830767
86 2 718414 4830764.8
86 3 718412.8 4830764.9
86 4 718411.3 4830766.3
86 5 718411.4 4830767.6
86 6 718412 4830769.5
86 7 718412.1 4830770
86 8 718413.6 4830769.4

Just points

Filled, computed polys

library(ggplot2)
library(ggthemes)
library(sp)
#' Make smooth polys from points
#'
#' From: http://gis.stackexchange.com/a/24929/29544
#'
#' @param xy n x 2 matrix with n >= k
#' @param k # of vertices to expand points to
#' @param ... other args passed to \code{spline()}
spline.poly <- function(xy, vertices, k=3, ...) {
# Wrap k vertices around each end.
n <- dim(xy)[1]
if (k >= 1) {
data <- rbind(xy[(n-k+1):n,], xy, xy[1:k, ])
} else {
data <- xy
}
# Spline the x and y coordinates.
data.spline <- spline(1:(n+2*k), data[,1], n=vertices, ...)
x <- data.spline$x
x1 <- data.spline$y
x2 <- spline(1:(n+2*k), data[,2], n=vertices, ...)$y
# Retain only the middle part.
cbind(x1, x2)[k < x & x <= n+k, ]
}
trees <- read.csv("CrownsExample.csv")
trees <- trees[with(trees, order(TREE, POINT)),]
# plot just points colored by tree to make sure points look ok
ggplot(trees, aes(x=X, y=Y, color=factor(TREE))) + coord_equal() + geom_point()
# small function to avoid anonymous fnction in `by` make a polygon from our points
make_tree_poly <- function(tree) {
tree_pts <- spline.poly(as.matrix(tree[, c("X", "Y")]), 100)
Polygons(list(Polygon(tree_pts)), ID=unique(tree$TREE))
}
# for each tree, make a set of smoothed polygons then make the whole thing
# a SpatialPolygonsDataFrame so more spatial ops can be done on it (or more
# data stored with it)
tree_polys <- SpatialPolygonsDataFrame(
SpatialPolygons(unclass(by(trees, trees$TREE, make_tree_poly)),
proj4string=CRS("+proj=utm +zone=32 +ellps=GRS80 +units=m +no_defs ")),
data.frame(id=unique(trees$TREE), row.names=unique(trees$TREE)))
# plot the new polygon tree crowns colored by tree
tree_map <- fortify(tree_polys)
ggplot() +
geom_map(data=tree_map, map=tree_map,
aes(x=long, y=lat, fill=id, map_id=id, color=id)) +
geom_point(data=trees, aes(x=X, y=Y)) +
coord_equal() +
theme_map()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment