0

I have create a grid using this code, the grid it is created over the port map:

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


plot(porto)
plot(portoWithin, add = T)

Now I want to insert SpatialPonits in the grid, so that I can discretize the data. The result would be to see in each grid cell how many points there are, to work with cells instead of with points.

The dataSet of points are SpatialPoints which correspond to GPS coordinates:

      [,1]     [,2]
[1,] -8.574678 41.15195
[2,] -8.574705 41.15194
[3,] -8.574696 41.15193
[4,] -8.574660 41.15196
[5,] -8.574723 41.15193
[6,] -8.574714 41.15192
zx8754
  • 52,746
  • 12
  • 114
  • 209
  • Please mention what libraries you are using. – zx8754 Jan 15 '20 at 12:37
  • I think you just want to merge? I would use `sf` package maybe here instead `sp` (can convert between the two of them). Something like `new_grid <- grid_df %>% st_join(datapoints_df) %>% dplyr::group_by(grids) %>% summarise(etc...` See [here](https://stackoverflow.com/questions/43456524/how-to-find-which-polygon-a-point-belong-to-via-sf) and [here](https://ryanpeek.github.io/mapping-in-R-workshop/vig_spatial_joins.html) – user63230 Jan 15 '20 at 12:55
  • I am working with sp and raster – Ander Jarauta Álvarez Jan 16 '20 at 10:04

0 Answers0