【发布时间】:2019-09-11 14:59:37
【问题描述】:
我正在使用高斯过程模型进行预测,现在我需要根据数据中的坐标使用网格文件,但我没有,我也不知道如何创建它。
我关注了link 上的帖子,但它显示的是宾夕法尼亚州的网格,而不是我的数据坐标所在的芝加哥!
所以我很困惑,哪种方法是创建包含数据中其他列的网格文件的理想方式。
station <- data.frame(lat = c(41.997946, 41.960669, 41.960669, 41.960669,41.909269,41.931841,41.909269,41.910561,41.866129,41.866129), long = c(-87.654561, -87.747456, -87.67459, -87.646438,-87.747456,-87.67459,-87.67459,-87.619112,-87.747456,-87.691617),station = 1:10)
station
lat long station
1 41.99795 -87.65456 1
2 41.96067 -87.74746 2
3 41.96067 -87.67459 3
4 41.96067 -87.64644 4
5 41.90927 -87.74746 5
6 41.93184 -87.67459 6
7 41.90927 -87.67459 7
8 41.91056 -87.61911 8
9 41.86613 -87.74746 9
10 41.86613 -87.69162 10
数据包含更多列,例如 Hour、Day、Moths、Year、Speed,这些观察是针对 2 个月内的 10 个位置的,但我只是将坐标放在这里以了解如何创建网格。
这是我按照上面的链接创建网格的步骤:
# Set the projection. They were latitude and longitude, so use WGS84 long-lat projection
proj4string(station) <- CRS("+init=epsg:4326")
# View the station location using the mapview function
mapview(station)
#3. Determine the origin
# Set the origin
ori_t <- SpatialPoints(cbind(-87.67459, 41.99795), proj4string = CRS("+init=epsg:4326"))
# Convert the projection of ori
# Use EPSG: 3857 (Spherical Mercator)
ori_t <- spTransform(ori, CRSobj = CRS("+init=epsg:3857"))
coordinates(ori_t)
#ori_t <- spTransform(ori, CRSobj = CRS("+init=epsg:3857"))
#coordinates(ori_t)
# The origin has been rounded to the nearest 100
x_ori <- round(coordinates(ori_t)[1, 1]/100) * 100
y_ori <- round(coordinates(ori_t)[1, 2]/100) * 100
# Define how many cells for x and y axis
x_cell <- 250
y_cell <- 200
# Define the resolution to be 1000 meters
cell_size <- 1000
# Create the extent
ext <- extent(x_ori, x_ori + (x_cell * cell_size), y_ori, y_ori + (y_cell * cell_size))
# Initialize a raster layer
ras <- raster(ext)
# Set the resolution to be
res(ras) <- c(cell_size, cell_size)
ras[] <- 0
# Project the raster
projection(ras) <- CRS("+init=epsg:3857")
# Create interactive map
mapview(station) + mapview(ras)
但是当我查看地图时,网格位于宾夕法尼亚地区而不是芝加哥!你知道为什么吗?我选择了我的 lat : 41.99795 和我的 long:-87.67459 ,当我把它们放在谷歌地图上时,它显示了 Chicago area ,但在 R 上没有显示相同的区域!!
# Convert to spatial pixel
st_grid <- rasterToPoints(ras, spatial = TRUE)
gridded(st_grid) <- TRUE
st_grid <- as(st_grid, "SpatialPixels")
另外,当我保存网格文件时,如何使用网格坐标提示其他列?因为它只显示新的 long 和 lat 列
write.csv(st_grid, file = "st_grid.csv")
【问题讨论】:
标签: r grid coordinates geospatial spatial