1

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.

1 Answer 1

3

You have:

nga_masker <- poly.outer(nks_dens, subset(nks_gis2, NAME_0 %in% "Nigeria"), 
  extend = 1000)

and that extend=1000 is expanding your polygon by 1000 degrees:

extend: A buffer used to extend the mask if it is required to be
        larger than ‘exo.object’

that explains the output coordinates being around the 1000 mark, and not being valid lat-long degrees.

1
  • thank you so much! Yep, that does it ... boy do I feel silly. I really appreciate it.
    – Yasha
    Dec 28, 2018 at 19:41

Your Answer

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

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