1

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:
https://stackoverflow.com/questions/19002744/spover-for-point-in-polygon-analysis
Checking if points fall within polygon Shapefile
R: Error in checking point inside polygon

Here is my code chunk

library(sp)
library(rgdal)
library(readr)

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"))

#Yeilds:
#  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

Vince
  • 20,017
  • 15
  • 45
  • 64
WannabeSmith
  • 121
  • 5
  • 1
    Answer 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 – WannabeSmith Mar 08 '20 at 04:24

1 Answers1

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!

WannabeSmith
  • 121
  • 5