有一个R库可以在地图上创建网格?

时间:2019-11-12 08:48:47

标签: r

我正在准备创建地图(特别是波尔图城市地图),我有一些用纬度和经度表示的点。但是这些点非常准确,我想创建一个网格以将相似点和闭合点放在一起。 通过这种方式,我可以获取更多有关集中更多点的区域的信息。

########### Exploratory Visualization of the station locations ###########

# Create a data frame showing the **Latitude/Longitude**
# They are more than 5000 points (these they are only an example)
station <- data.frame(lat = c(41.141412, 41.140359, 41.151951, 41.18049),
                      long = c(-8.618643, -8.612964, -8.574678, -8.645994),
                      station = 1:4)

# Convert to SpatialPointsDataFrame
coordinates(station) <- ~long + lat

# 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)


########### Determine the origin NOT RUN ###########

# Set the origin
ori <- SpatialPoints(cbind(41.141412, -8.618643), 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)
coordinates(ori_t)

########### Determine the extent of the grid ###########

# Transformed with https://epsg.io/
y_ori <- 5029715.97
x_ori <- -966577.67

# Define how many cells for x and y axis
x_cell <- 16
y_cell <- 12

# 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)) 
ext

# 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)


########### Determine the extent of the grid ###########

# Save the raster layer
writeRaster(ras, filename = "ras.tif", format="GTiff") 

# Convert to spatial pixel
st_grid <- rasterToPoints(ras, spatial = TRUE)
gridded(st_grid) <- TRUE
st_grid <- as(st_grid, "SpatialPixels")

此代码的结果是在波尔图地图上的4个点和那里的一个网格。我要附近的点属于同一网格。 谢谢!

0 个答案:

没有答案