将点插入R中的栅格

将点插入R中的栅格,r,R,我有一个波尔图的网格,我希望能够在网格中插入点来离散它们。通过这种方式,可以根据时间查看每个单元格中有多少个点 这是网格的代码: #latitudS and longitudS are coordinates x Y x = longitudS y = latitudS df = data.frame(x, y) # Converting to a Spatial Points Data Frame so you can keep the value that you will chang

我有一个波尔图的网格,我希望能够在网格中插入点来离散它们。通过这种方式,可以根据时间查看每个单元格中有多少个点

这是网格的代码:

#latitudS and longitudS are coordinates x Y
x = longitudS
y = latitudS

df = data.frame(x, y)


# Converting to a Spatial Points Data Frame so you can keep the value that you will change within the Grid
points = SpatialPointsDataFrame(coords = df[, 1:2], df)

# load some spatial data. Administrative Boundary
porto <- getData('GADM', country = 'Portugal', level = 2)
porto$NAME_1
porto <- porto[porto$NAME_2 == "Porto",]


# check the CRS to know which map units are used
proj4string(porto)
# "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"

# Create a grid of points within the bbox of the SpatialPolygonsDataFrame
# colorado with decimal degrees as map units
grid <- makegrid(porto, cellsize = 0.003) # cellsize in map units!

# grid is a data.frame. To change it to a spatial data set we have to
grid <- SpatialPoints(grid, proj4string = CRS(proj4string(porto)))

portoWithin <-
  SpatialPixels(grid, proj4string = CRS(proj4string(porto)))

#### Converting the GRID to a Raster
ras_portoWithin <- raster(portoWithin)

## Assigning temporary values to the raster
values(ras_portoWithin) <- 0

## Using the Spatial point file created above to replace the values of the raster
r1 <- ras_portoWithin

plot(porto)
plot(portoWithin, add = T)
#纬度和经度是坐标x和Y
x=纵向
y=纬度
df=数据帧(x,y)
#转换为空间点数据帧,以便可以将要更改的值保留在栅格中
点=空间点数据帧(坐标=df[,1:2],df)
#加载一些空间数据。行政边界
波尔图