BrunoVilela / letsR

Package letsR
http://brunovilela.github.io/letsR/
9 stars 3 forks source link

Error in RGEOSBinPredFunc #4

Closed philiptsoares closed 2 years ago

philiptsoares commented 5 years ago

Hi Bruno.

I'm working with some IUCN data and would like to extract the information for cell grids. However, when I use the "lets.presab.grid" function it starts to run and after a while this error is returned:

resu <- lets.presab.grid(amphi_p, g , sample.unit = "ID") Error in RGEOSBinPredFunc (spgeom1, spgeom2, byid, "rgeos_overlaps"): rgeos_binpredfunc: comparison failed

How to proceed?

Follow used commands

Packages

library(rgdal) library(letsR)

Import shape - Grid Hexagonal

grid_hex <- readOGR("hex_grid_neo.shp") ## 2000 features plot(grid_hex) summary(grid_hex)

Import IUCN maps of Amphibian

amphi <- readOGR("AMPHIBIANS.shp") ## Shape amphibio Neotropical region summary(amphi)

Create grid - SpatialPolygonsDataFrame

Georref Grid Hexagonal

crs(grid_hex) <- CRS("+proj=longlat +datum=WGS84") grid_hex

Create a dataframe and display default rownames

( p.df <- data.frame( ID=1:length(grid_hex)) ) rownames(p.df)

Extract polygon ID's

( pid <- sapply(slot(grid_hex, "polygons"), function(x) slot(x, "ID")) )

Create dataframe with correct rownames

( p.df <- data.frame( ID=1:length(grid_hex), row.names = pid) )

Try coersion again and check class

g <- SpatialPolygonsDataFrame(grid_hex, p.df) class(g)

Create shape Anfíbios - SpatialPolygonsDataFrame

Georref Grid Hexagonal

crs(amphi) <- CRS("+proj=longlat +datum=WGS84") amphi

Create a dataframe and display default rownames

( p.df <- data.frame( ID=1:length(amphi)) ) rownames(p.df)

Extract polygon ID's

( pid <- sapply(slot(amphi, "polygons"), function(x) slot(x, "ID")) )

Create dataframe with correct rownames

( p.df <- data.frame( ID=1:length(amphi), row.names = pid) )

Try coersion again and check class

amphi_p <- SpatialPolygonsDataFrame(amphi, p.df) class(amphi_p)

Extract data from shape IUCN

resu <- lets.presab.grid(amphi_p, g , sample.unit = "ID")

Hug.

mmoroti commented 4 years ago

I have the same problem.

philiptsoares commented 4 years ago

Hello Moroti. In my analysis I ended up solving the problem by opening the species distribution shapes with the rgdalmaptools :: readShapePoly function. I did the test with other shapes and the data extraction worked correctly with rgdal :: readOGR. But the shape I used didn't work. Follow the codes to try to run your case there.

---Load packages

library(raster) library(maptools) library(rgdal) library(letsR) library(dplyr)

---Import IUCN maps of Amphibian

anura <- readShapePoly("ANURA.shp") ## Shape amphibia -IUCN head(anura)

anura@data <- anura@data %>% # # select specific data in shape of anura select("BINOMIAL", "PRESENCE", "ORIGIN", "SEASONAL") head(anura)

---Load our shapefile - Neotropical

shape <- readOGR("Neotropico.shp") plot(shape)

---Load our grid - Neotropical

sq_grid <- readOGR("sq_grid.shp") plot(sq_grid)

---Projection for shapes

projection(anura) <- projection(shape) anura

projection(sq_grid) <- projection(shape) sq_grid

---Extract data from shape IUCN

resu_sq <- lets.presab.grid(anura, sq_grid , sample.unit = "FID")

---Plot data composition anura Neotropical

rich_plus1 <- rowSums(resu_sq$PAM) + 1 colfunc <- colorRampPalette(c("#fff5f0", "#fb6a4a", "#67000d")) colors <- c("white", colfunc(max(rich_plus1))) plot(resu_sq$grid, border = "gray40", col = colors[rich_plus1])

---END

Good job! Hug.

rbfontana commented 1 year ago

I'm also having this problem. I tried opening just one of the files (species distributions) using the "readShapePoly" function, and after both files (species distribution and the map with grids), but in both attempts I had this error.