Skip to content
This repository has been archived by the owner on Jun 12, 2020. It is now read-only.

add argument to ukp_geo_chull to widen polygon #17

Open
njtierney opened this issue Nov 15, 2016 · 0 comments
Open

add argument to ukp_geo_chull to widen polygon #17

njtierney opened this issue Nov 15, 2016 · 0 comments
Milestone

Comments

@njtierney
Copy link
Owner

The case I think of here is that you might have long/lat points for a region, and if you want to find the crime that occurs within the long/lat polygon, you might want a bit of padding/space around the edges.

Extra bonus would be if we could specify this extra padding in metres or kilometres.

Here's a working example as a starting point, thanks @samclifford!

library(mvtnorm)
library(rgeos)
library(sp)
library(tidyverse)
xy <- rmvnorm(n = 100, mean=c(0,0), sigma = diag(c(1,1)))

hull.ind <- chull(xy)

hull <- xy[hull.ind, ]

plot(xy)
polygon(hull, asp=1)

centroid <- Polygon(hull) %>%
  SpatialPoints %>%
  gCentroid %>%
  data.frame

points(centroid, col="red")

z <- 1

newhull <- cbind(hull, centroid) %>%
  mutate(dx = `1` - x,
         dy = `2` - y) %>%
  # euclidean distance
  mutate(dr = sqrt(dx^2 + dy^2),
         dx = dx/dr,
         dy = dy/dr) %>%
  mutate(newx = `1` + z*dx,
         newy = `2` + z*dy)
#> Warning in data.frame(..., check.names = FALSE): row names were found from
#> a short variable and have been discarded

ggplot(data=newhull, aes(x=newx, y=newy)) +
  geom_polygon(fill="grey", alpha=0.5) +
  geom_polygon(fill="grey", alpha=0.5,
               aes(x=`1`, y=`2`)) +
  geom_point(data=as.data.frame(xy),
             aes(x=V1, y=V2)) +
  coord_equal()

# expand by mean/median distance across all points
@njtierney njtierney added this to the 0.2.0 milestone Feb 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant