I am writing a script that takes an input KML file, created in google earth, and plots a grid of coordinate points inside of the polygon.
So far, I have the polygon input and a grid of points for the bounding box of the polygon, but I want to have only points INSIDE the polygon.
I tried to do this using the over()
function, but it is not working. Suggestions?
You can download my test KML file HERE.
library(rgdal)
library(sp)
library(maptools)
# ogrInfo() to find layer name... not as labelled in Google Earth?!
my.poly = readOGR(ds = "PolyNYC.kml", layer = "PolyNYC")
proj4string(my.poly) <- "+proj=longlat +datum=WGS84 +no_defs"
# Creating grid of points
grdpts <- makegrid(my.poly)
# Converting from df to spdf
coords = cbind(grdpts$x1, grdpts$x2)
sp = SpatialPoints(coords)
spdf = SpatialPointsDataFrame(coords, grdpts, proj4string = CRS(proj4string(my.poly)))
# Using over() to select only those points in the polygon
inPoly = over(spdf, my.poly)
# This is not working
# Plotting the polygon with the points overlaid.
plot(my.poly)
points(spdf, pch = 3, col = "red")
#kmlPoints(obj = spdf, kmlfile = "BBoxFromPoly.kml", kmlname = "Testing123")