1
votes

I have a spatial polygon object and a spatial points object. The latter was created from xy latlong data vectors (called latitude and longitude, respectively) in a dataframe, while the former was simply read into R directly using rgdal. My code is as follows:

boros <- readOGR(dsn = ".", "nybb")
rats <- read.csv("nycrats_missing_latlong_removed_4.2.14.csv", header = TRUE)
coordinates(rats) <- ~longitude + latitude

At this point neither spatial object is projected. If I project these objects as follows:

proj4string(boros) <- CRS("+proj=lcc")
proj4string(rats) <- CRS("+proj=lcc")

Both objects are now projected, and both will successfully map with the plot() function as follows:

plot(boros)
plot(rats)

However when I try to plot them together:

plot(boros)
plot(rats, add = TRUE)

I get the first plot only, without the rats object superimposed over boros. However, and this is the big problem, I get NO error message, so I have been unable to determine where the disconnection is between these two spatial objects being able to speak to each other. Both commands run smoothly without error or warning, yet I am left with just the single plot. And when I check the projections of each object with proj4string() I get the same projection returned for each object.

I have now spent many, many hours over several days trying various ways of creating two spatial objects whose CRS and projections match such that they can be mapped together using plot(). Incidentally, one approach I took was to create a shapefile in ArcGIS for the rats object, which worked fine to create the file. But I am still left with the same inability of the two spatial objects to work together in R. I have tried many different projections for both objects, spTransform on both objects, nothing seems to work. Any help would be most appreciated. I have also included a dropbox link with the 2 data files I have described above: https://www.dropbox.com/sh/x0urdo6guprnw8y/tQdfzSZ384

1
Perhaps more appropriate on gis stack exchange gis.stackexchange.comsckott
If you check extent(boros); extent(rats) you see that both extents do no overlap. I guess rats is indeed in lat-lon, but boros is not. Extent is in the raster library. Any idea which projection boros has?koekenbakker
proj4string()<- does not project, it is just declaring what the proj descriptor is. Are longitude and latitude actually in degrees or are they metres in LCC? bbox() and proj4string() without assignment on boros and rats will answer this.mdsumner
-1 please don't tell us what doesn't work without clearly reporting on what you did and saw, we can only guessmdsumner

1 Answers

3
votes

So, as some of the comments point out, the problem is that your data and your maps have different projections.

It looks like your map comes from the NYC Department of City Planning. The shapefile is definitely not in WGS84 (longlat), but the CRS is not included in the file (which is very disappointing by the way...). Nevertheless, there is a metadata file which indicates that the shapefile is projected as EPSG 2263.

In order to make use of this in R we need a projection string. The idiomatic way to get this in R is:

library(rgdal)
EPSG <- make_EPSG()
NY   <- with(EPSG,EPSG[grepl("New York",note) & code==2263,]$prj4)
NY
# [1] "+proj=lcc +lat_1=41.03333333333333 +lat_2=40.66666666666666 +lat_0=40.16666666666666 +lon_0=-74 +x_0=300000.0000000001 +y_0=0 +datum=NAD83 +units=us-ft +no_defs"

Now we can either take your map and reproject that into WGS84, or take your data and reproject that into the map CRS.

setwd("< directory with all your files >")
data   <- read.csv("nycrats_missing_latlong_removed_4.2.14.csv")

# First approach: reproject map into long-lat
wgs.84       <- "+proj=longlat +datum=WGS84"
map          <- readOGR(dsn=".",layer="nybb",p4s=NY)
map.wgs84    <- spTransform(map,CRS(wgs.84))
map.wgs84.df <- fortify(map.wgs84)
library(ggplot2)
ggplot(map.wgs84.df, aes(x=long,y=lat,group=group))+
  geom_path()+
  geom_point(data=data, aes(x=longitude,y=latitude, group=NULL),
             colour="red", alpha=0.2, size=1)+
  ggtitle("Projection: WGS84 longlat")+
  coord_fixed()

# Second approach: reproject data
map.df <- fortify(map)
rats <- SpatialPoints(data[,c("longitude","latitude")],proj4string=CRS(wgs.84))
rats <- spTransform(rats,CRS(NY))
rats.df <- data.frame(coordinates(rats))
ggplot(map.df, aes(x=long,y=lat,group=group))+
  geom_path()+
  geom_point(data=rats.df, aes(x=longitude,y=latitude, group=NULL),
             colour="red", alpha=0.2, size=1)+
  ggtitle("Projection: NAD83.2263")+
  coord_fixed()

No rats in Central Park?