The Problem
I am trying to plot the kernel density estimate for conflict data in Nigeria. I am using GISTools
, rgdal
, and sp
loaded into R.
I want to generate the KDE, plot it out, and then mask the plot so that all excess space is clipped from the plot.
I am able to create the kernel density without problem. It's generating the mask that seems to be giving me the problem. I get the following error:
Error in `proj4string<-`(`*tmp*`, value = CRS(proj4string(input.poly))) :
Geographical CRS given to non-conformant data: -997.36173977 1014.70658626
-995.75375669 1013.91618459
In addition: Warning message:
In RGEOSBinTopoFunc(spgeom1, spgeom2, byid, id, drop_lower_td, unaryUnion_if_byid_false, :
spgeom1 and spgeom2 have different proj4 strings
Code
Here is what I have done:
I import the shapefile for the administrative districts 2
nks_gis2 <- readOGR(dsn = "Map/Data/nks_admin2.shp")
nks_gis1 <- readOGR(dsn = "Map/Data/nks_admin1.shp")
nks_gis0 <- readOGR(dsn = "Map/Data/nks_admin0.shp")
I then check their CRS
proj4string(nks_gis2)
[1] "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
proj4string(nks_gis1)
[1] "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
proj4string(nks_gis0)
[1] "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
I import the incident data in as a spatial data frame
nks_longlat <- cbind(nks$longitude, nks$latitude)
nks_points <- SpatialPointsDataFrame(nks_longlat, nks, proj4string = CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))
I check the CRS for the points
proj4string(nks_points)
[1] "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
I now generate the kernel density estimate
nks_dens <- kde.points(subset(nks_points, country %in% "Nigeria"),
lims = subset(nks_gis2, NAME_0 %in% "Nigeria"))
And once again, I check the CRS
proj4string(nks_dens)
[1] "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
I plot the density using level.plot
level.plot(nks_dens)
Now, here is where the problem occurs. I try to create a 'masker' so that it clips the plot to fit my Nigeria shapefile (I'll use the admin2 file):
nga_masker <- poly.outer(nks_dens, subset(nks_gis2, NAME_0 %in% "Nigeria"), extend = 1000)
I receive this error:
Error in
proj4string<-
(*tmp*
, value = CRS(proj4string(input.poly))) :Geographical CRS given to non-conformant data: -997.36173977 1014.70658626 -995.75375669 1013.91618459
In addition: Warning message:
In RGEOSBinTopoFunc(spgeom1, spgeom2, byid, id, drop_lower_td, unaryUnion_if_byid_false, : spgeom1 and spgeom2 have different proj4 strings
I have checked both my latitude and longitude, but do not see any long/lat that is even close to -997 or 1014.