简体   繁体   English

R 中的坐标值

[英]Coordinate values in R

I'm new to R and trying to learn how it's done for some species distribution modeling.我是 R 的新手,正在尝试了解它是如何为某些物种分布建模完成的。 I'm trying to create a mask of Italy using the wrld_simpl map included with the maptools package. When I run the code, I get "Error in.local(obj, ...): NA values in coordinates" when I'm trying to create spxy.我正在尝试使用 maptools package 中包含的 wrld_simpl map 创建意大利面具。当我运行代码时,我得到“Error in.local(obj, ...): NA values in coordinates”试图创建 spxy。 I suspect that this issue might be rooted in the rasterization step but I'm not sure... What am I doing wrong?我怀疑这个问题可能源于光栅化步骤,但我不确定......我做错了什么?

Apologies for the code dump为代码转储道歉

# Create library
library(raster)
library(dismo)
library(sf)
library(maptools)
library(rgdal)
library(sp)
library(rgeos)

# Call the cleaned nivale csv
nivale <- read.table('C:/Users/David/Documents/nival_1980_GeoDat.csv', header=T, sep = ',')
nivale <- nivale[,2:3]

# Create a simple map of Italy, plot nivale data points
data(wrld_simpl)
plot(wrld_simpl, xlim=c(0,20), ylim=c(40,50), axes=TRUE, col="light yellow")
box()
points(nivale$lon, nivale$lat, col='orange', pch=20, cex=0.75)
points(nivale$lon, nivale$lat, col='black', cex=0.75)

# set CRS of nivale equal to wrld_simpl
coordinates(nivale) <- ~lon+lat
crs(nivale) <- crs(wrld_simpl)
projection(nivale) <- CRS('+proj=longlat +datum=WGS84')
class(nivale)
class(wrld_simpl)

# Sampling Bias Assessment
r <- raster(nivale)
res(r) <- 1
r <- extend(r, extent(r)+1)
nisel <- gridSample(nivale, r, n=1)
p <- rasterToPolygons(r)
plot(p, border='blue')
points(nivale)
points(nisel, cex=1, col='red', pch='x')

# Pseudo-Absences
# Create shapefile from wrld_simpl
italy <- wrld_simpl[is.element(wrld_simpl$NAME, 'Italy'),]
set.seed(1963)
crsi = crs('+proj=longlat +datum=WGS84')
exti = extent(italy)
# Create template for rasterization
rst_temp <- raster(ncols = 1000, nrows = 1000, 
                   crs = crsi, 
                   ext = exti)
# Rasterize italy
rst_italy <- rasterize(italy, rst_temp)
# Random point generation
rand_point <- randomPoints(rst_italy, 250)

#Pseudo-Absence Points
x <- circles(nivale, d=50000, lonlat=TRUE)
pol <- polygons(x)
samp1 <- spsample(pol, 250, type='random', iter=25)
cells <- cellFromXY(rst_italy, samp1)
xy <- xyFromCell(rst_italy, cells)
plot(pol, axes=TRUE)
points(xy, cex=0.75, pch=20, col='blue')
#
# Error - NA values in coordinates
spxy <- SpatialPoints(xy, proj4string=CRS('+proj=longlat +datum=WGS84'))
o <- over(spxy, geometry(x))
xyInside <- xy[!is.na(o), ]
v <- extract(mask, x@polygons, cellnumbers=T)
v <- do.call(rbind, v)
v <- unique(v[,1])
head(v)
m <- italy
m[] <- NA
m[v] <- 1
plot(m, ext=extent(x@polygons)+1)
plot(x@polygons, add=T)

I don't have your nivale dataset, so I am not 100% sure, but I think the error arises after you are sampling points within polygons ( spsample(pol, 250, type='random', iter=25) ) created using circle() and not because of the rasterization, which looks fine to me.我没有你spsample(pol, 250, type='random', iter=25) nivale数据集,所以我不是 100% 确定,但我认为错误是在你使用circle()而不是因为光栅化,这对我来说很好。 My guess is that, after this, you sample random points in these polygons, which may fall outside the raster of Italy ( samp1 <- spsample(pol, 250, type='random', iter=25) ).我的猜测是,在此之后,您对这些多边形中的随机点进行采样,这些点可能落在意大利的栅格之外( samp1 <- spsample(pol, 250, type='random', iter=25) )。 Possibly, you can just remove such points by excluding NAs values in xy :可能,您可以通过排除xy中的 NAs 值来删除这些点:

xy <- xy[.is,na(xy[. 1]) &,is.na(xy[, 2])]

Try this and see if it works.试试这个,看看它是否有效。 If you need a certain number of points (eg xy needs to have 250 lines), you can put a while loop until you have what you want;如果你需要一定数量的点(例如xy需要有250条线),你可以放一个while循环直到你得到你想要的; this may take (a lot of) time, though.不过,这可能需要(很多)时间。

/Emilio /埃米利奥

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM