I am not sure why i am keep getting NA whenever I run the Over function with Latitude and Longitude point on the polygon from shapefile. Please note that this is first time for me doing the spatial analysis, but I have done my research and replicated things, but didn't succeed. I need some points which are outside of the polygon to be NA, so I can focus on the real data.

I read these sources since these pertain to my cause but I can't work my problem out:
Checking if points fall within polygon Shapefile
R: Error in checking point inside polygon

Here is my code chunk


gainsville_df <- read_csv("311_Service_Requests__myGNV_.csv")
gnv <- readOGR("~\\Downloads\\GIS_cgbound", layer = "cgbound")

gnv_latlon <- spTransform(gnv, CRS("+proj=longlat +ellps=WGS84 +datum=WGS84"))

gnv_raw <- data.frame(Longitude= gainsville_df$Longitude, Latitude= gainsville_df$Latitude)

coordinates(gnv_raw) <- ~Longitude + Latitude
proj4string(gnv_raw) <- proj4string(gnv)
over(gnv_raw, as(gnv,"SpatialLinesDataFrame"))

#  FID_cgboun Id Perimeter Area Acres Hectares Shape_Leng
#1         NA NA        NA   NA    NA       NA         NA

# Desired Output:
# Whereas I should have seen which gainesville Latitudes and Longitude are within the shpaefile
# polygon so I can drop the outliers, that have the NA. According to this, none of my LatLon points 
# are inside the polygon.

The datafiles are here:
Shapefile: https://github.com/THsTestingGround/SO_readOGR_quest/tree/master/GIS_cgbound
reading csv file: https://github.com/THsTestingGround/SO_readOGR_quest/blob/master/311_Service_Requests__myGNV_.csv


1 Answer 1


A short summary: The latitude and longitude points in my data were an sf object since I had a column containing rows of characters points (which also needed to be changed using st_as_sf) such as [1] POINT (-82.34323174 29.67058748) [2] POINT (-82.3432356 29.67058998) and so on. Then next, my shapefile was a LINESTRING type, not Polygon so that also had to be an sf object (using st_transform function), so I can connect the data to the shapefile. Everything had to be done using functions of sf package.

Full answer to this was provided here to me: https://stackoverflow.com/questions/60583299/keep-getting-nas-when-i-run-over-function-on-latlon-points-on-shapefile-polygo/60584372#60584372

Thank you very much to user jazzurro!

  • 2
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review Mar 8, 2020 at 4:56
  • Thank you Kadir, I will edit this right away. Mar 8, 2020 at 4:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.