'Lost data within a spatial polygon

Hopefully the following makes sense and apologies if not! I have a dataset of GPS locations (of various species footprints), and am measuring the distance from where each point was found to the boundary of a national park in R. Im doing this with a series of environmental factors, (roads/villages/lakes etc), and for all other enviro variables (and kml files) Ive had no problems, however when I run my park boundary data, (using a kml file read in as a spatial polygon of the national park), I keep getting negative values for all the GPS points that occur within the park boundary? (So anything within the polygon essentially). The results output I am getting shows the correct measurements from GPS points to the boundary that occur on the OUTSIDE of the boundary, (or polygon), but anything inside the park/polygon outputs as a zero value. Ive tried to reproject the polygon as just an outline and tried removing the 'fill' etc, along with a few other tricks Ive found, but no luck so far?

Am I correct in assuming the data is just not there and that I need to recreate the boarder of the park some other way? Or is it more an issue with how Im asking r to calculate the distance measurements?

Below is an example of the code I am using

KSNP_Poly <- readOGR("KSNPboundaryexport.kml")

Points <- read.csv("AllPoints.csv")

sptsPoints = SpatialPoints(Points)

plot(KSNP_Poly) plot(sptsPoints)

apply(gDistance(sptsPoints,KSNP_Poly,byid=TRUE),2,min)

KSNPResults <- apply(gDistance(sptsPoints,KSNP_Poly,byid=TRUE),2,min)

Hope this made sense and any advice greatly appreciated!

Thanks!

Kass



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source