5

I have a series of points in an area whose 'footprint' shape is highly irregular:

LE82

I'd like to determine all of the coordinates within the footprint's vertices. The end goal is to determine which data points lay outside this footprint.

Does anyone have an efficient way to go about doing this??


My best idea to approaching this is to draw a polygon based on the green area's vertices and then use said polygon's coordinates to determine 'outlier' points' (though, I'm not sure how to do that yet -- one step at a time!).

However, when I try creating a convex hull, it obviously creates problems because of the irregular shape of my green space. [Anyone know of a way to create CONCAVE hulls?]

Alternatively, is there a way to draw polygons manually using a 'click the graph' type method?


...Again, if you have a better solution to my problem than using polygons, please by all means suggest that solution!

Community
  • 1
  • 1
theforestecologist
  • 4,667
  • 5
  • 54
  • 91
  • My interpretation of your question: find all black dots that fall "inside" of the green dot area. A potential solution to this is to break the green dot area into 3 separate convex areas (rectangles more or less). Cut at the x axis at ~650 and ~1150. The first cut can go all the way, while the second would stop around y=200. Now, use something like `chull` or related functions for each of the 3 areas. – lmo Sep 19 '16 at 19:33
  • @lmo yes initially my goal is to determine coordinates within the green dot area, but only so I can determine when a black dot falls *outside* that area. – theforestecologist Sep 19 '16 at 19:35
  • @lmo: I had considered splitting the area up into multiple convex areas, but because the areas have lines not parallel with the axes, I would have to determine each of the vertices by hand to 'draw' each area. I thought this would be kind of time consuming. Do you know of a way to do so quickly? – theforestecologist Sep 19 '16 at 19:39
  • How about fitting a model to separate two types of points? If you use a over-fit model, [it could generate a good enough boundary between footprint and other points](https://leonardoaraujosantos.gitbooks.io/artificial-inteligence/content/Images/overfitting_examples.jpg). Since you are not really predicting, over-fit is not a problem. – dracodoc Sep 19 '16 at 19:42
  • 3
    `alphahull::ahull()` _may_ just help out here. – hrbrmstr Sep 19 '16 at 19:42
  • 4
    Have a look at the [**alphahull** package](https://cran.r-project.org/web/packages/alphahull/index.html). For an example application, see Barry Rowlingson's nice RPubs doc [here](https://rpubs.com/geospacedman/alphasimple). – Josh O'Brien Sep 19 '16 at 19:43
  • 1
    if you want to go the manual route, the very simplest way is with `?locator` ... only needs base R. – Ben Bolker Sep 19 '16 at 21:45
  • Just a note: alphahull did not initially work for me because required package spatstat could not be installed properly. Turns out, spatstat [requires the newest version of R](http://stackoverflow.com/questions/39226685/spatstat-not-available-for-r-3-2-3) to even show up in the install packages list. – theforestecologist Sep 20 '16 at 12:33
  • @theforestecologist I'm curious: did you try some other approaches, and if so, mind if you added them (to your post or as an answer)? – lukeA Sep 20 '16 at 16:14

1 Answers1

4

Alternatively, is there a way to draw polygons manually using a 'click the graph' type method?

Here's one idea. First, some random points:

library(manipulate)
library(sp)
set.seed(1)
par(pch = 19, cex=.5)
x <- runif(1000)
y <- runif(1000)

Now, draw and capture the polygon:

coords <- data.frame()
manipulate({
  plot(y~x)
  res <- manipulatorMouseClick()
  coords <<- rbind(coords, data.frame(x=res$userX, y=res$userY))
  if (length(coords)) lines(coords)
})

enter image description here

And determine which points are inside/outside of it (see ?point.in.polygon):

res <- point.in.polygon(x, y, coords$x, coords$y)!=0 

plot(y~x, col = res + 1L)
lines(coords)

enter image description here

lukeA
  • 53,097
  • 5
  • 97
  • 100
  • 1
    Interesting, "The manipulate package must be run from within RStudio." Time to download RStudio :p. – theforestecologist Sep 19 '16 at 21:01
  • uhm.. good question :-D I just let it be in the background (you can continue your work in the console or source window). I guess if everything fails, you can select _Remove Plot..._ from RStudio's _Plots_ menu. – lukeA Sep 19 '16 at 22:05