计算栅格网格中的点数

时间:2019-10-31 19:46:04

标签: r raster

我正在尝试使栅格网格识别其中的点数并对其进行计数。我创建了一个栅格,并且有GPS点。我很确定网格和我拥有的点都在同一个CRS中,但是当我尝试将它们绘制在一起时,这些点就不存在了。这就是我所做的

我创建了一个栅格,并且有GPS点。我很确定网格和我拥有的点都在同一个CRS中,但是当我尝试将它们绘制在一起时,这些点就不存在了。这就是我所做的

### 1. Create Raster###
#set the origin of raster
ori <- SpatialPoints(cbind(113.58353,-25.80607), proj4string = CRS("+init=epsg:4326"))

#convert the projection of ori and use EPSG: 3857 (Spherical Mercator)
ori_t <- spTransform(ori, CRSobj = CRS("+init=epsg:32749"))

#Determine the extent of the grid
# 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 <- 13
y_cell <- 13

# Define the resolution to be 2000 meters
cell_size <- 2000

# 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:32749")

### 2. Plotting the coordinates in the same CRS###
GPS <- read.csv("wow.1994.2017.gps.csv", sep=";")
# example of what the data looks like
head(GPS)
  project       Date alliance letterCode longitude  latitude
1       1 15.05.1994        5        AJA  113.6651 -25.71582
2       1 26.08.1994        5        AJA  113.6058 -25.66950
3       1 09.09.1994        5        AJA  113.6858 -25.70700
4       1 04.08.1995        5        AJA  113.6126 -25.68989
5       1 04.08.1995        5        AJA  113.6070 -25.68986
6       1 12.08.1995        5        AJA  113.6345 -25.64736

xy_GPS = GPS[c("longitude", "latitude")]
coordinates(xy_GPS)=c("longitude","latitude")
GPS_sp<-SpatialPointsDataFrame(xy_GPS, GPS) 


### 3. Tried to visualize them together ###
tab <- table(cellFromXY(ras, GPS_sp))
ras[as.numeric(names(tab))] <- tab
plot(ras)
points(GPS_sp, pch=20)

1 个答案:

答案 0 :(得分:1)

您的栅格处于UTM中,extent(ras) = c(759000,785000,7143200,7169200),而您的GPS点位于latlong中。 R不会自动为您投影点。 您需要为GPS点提供CRS,然后将这些点投影到与ras相同的CRS中:

xy_GPS = GPS[,c("longitude", "latitude")]
coordinates(xy_GPS) = ~longitude+latitude
proj4string(xy_GPS) = CRS("+init=epsg:4326")
xy_GPS = spTransform(xy_GPS, crs(ras))

plot(ras)
points(xy_GPS)