Reputation: 422
So let's say two lists of longitude/latitude coordinates. I plot these coordinates on a map and connect each point from list 1 with straight lines and each point from list 2 with straight lines.
Here is a dataframe with sample longitudes and latitudes for these two lists:
long_1<- c(-87, -85.927, -83.8056)
long_2<- c(-87, -85.4729, -83.1944)
lat_1<- c(17.8,18.969, 20.31)
lat_2<- c(17.8, 18.23, 19.0881)
df<- data.frame(long_1,long_2,lat_1, lat_2)
leaflet() %>% addTiles() %>%
addPolylines(data = df, lng = ~long_1, lat = ~lat_1) %>%
addPolylines(data = df, lng = ~long_2, lat = ~lat_2) %>%
setView(-93.65, 42.0285, zoom = 4)
I am attempting to create a function that would be fed a point in longitude and latitude, and return true if the point falls inside the bounds of boundaries created by the connection of these lines, and false if the point does not.
I honestly have no idea how I would go about doing this. Any ideas would be greatly appreciated!
Upvotes: 1
Views: 311
Reputation: 9485
Maybe you can manage it easily with sp::point.in.polygon()
function. The idea is to test if the point(s) are in the polygon made by your coordinates.
library(sp)
# first you've to get one column for the long, and one column for the lat
new_df <- data.frame(long = c(long_1,long_2), lat = c(lat_1, lat_2))
# then you can put the points to test in a vector, like these, and use the function
point.in.polygon(c(-85.927,-84.2),c(18.7,18.5),new_df$long,new_df$lat)
[1] 1 0
One is in, one is out.
Upvotes: 1