dggridR icon indicating copy to clipboard operation
dggridR copied to clipboard

Cell sequences in geographic objects

Open ldesousa opened this issue 7 years ago • 2 comments

I am aware of two different kinds of cartographic objects that can be obtained from a DGG with dggridR:

  1. Cell centres, obtained with something like:
dggs <- dgconstruct(res=3)
allSeqs <- seq(1, dgmaxcell(dggs))
cellCentres <- dgSEQNUM_to_GEO(dggs, allSeqs)
  1. Cell polygons, that can be saved to disk with:
dggs <- dgconstruct(res=3)
allSeqs <- seq(1, dgmaxcell(dggs))
dgcellstogrid(dggs, allSeqs, frame=FALSE, savegrid="./cells.shp")

In neither case are the cell sequences kept with the object. cellCentres is an array of two lists (one for longitude and another for latitude), the shapefile saved to disk does not have attributes.

It is possible to merge cell sequences and the centres' coordinates into a single data.frame object, however, there is no way of knowing if the order is the same across these objects: df.cellCentres <- data.frame(allSeqs, cellCentres[[1]], cellCentres[[2]])

Is there any formal way of obtaining the cells sequences for any of these objects? Can the data.frame example above be trusted?

ldesousa avatar Jan 27 '19 17:01 ldesousa

Maybe this will do

> dgquakes %>%
   dplyr::mutate(cell_id = dgGEO_to_SEQNUM(dggs, .$lon, .$lat)$seqnum) %>%
   bind_cols(dgSEQNUM_to_GEO(dggs, .$cell_id)) %>%
   as_tibble()
# A tibble: 19,914 x 7
   time                        lat    lon   mag cell_id lon_deg  lat_deg
   <fct>                     <dbl>  <dbl> <dbl>   <dbl>   <dbl>    <dbl>
 1 2015-12-31T23:39:28.940Z  -7.07  130.    4.6     231   129.  -1.20e+1
 2 2015-12-31T23:35:07.790Z  51.6  -174.    3.6     250  -169.   4.56e+1
 3 2015-12-31T23:25:03.900Z -15.0   167.    4.6     254   166.  -1.98e+1
 4 2015-12-31T23:15:15.690Z  -5.52  -11.5   4.6      62   -10.1 -7.43e+0
 5 2015-12-31T23:15:05.880Z  43.7  -105.    3.2      22   -96.8  4.75e+1
 6 2015-12-31T23:11:29.610Z  42.3   133.    4       122   131.   3.60e+1
 7 2015-12-31T22:32:23.580Z  -6.02  148.    4.1     241   147.   1.58e-9
 8 2015-12-31T22:23:12.000Z  -7.25   67.8   5        95    68.1 -1.38e-9
 9 2015-12-31T22:22:17.350Z  51.7  -173.    5       250  -169.   4.56e+1
10 2015-12-31T22:15:58.410Z  40.9    72.5   4.5     100    71.3  3.60e+1
# ... with 19,904 more rows

espinielli avatar Apr 10 '19 16:04 espinielli

I think in your cellCentres example, the allSeqs variable would already contain the cell sequence IDs and you can use cbind or @espinielli's example to simply merge the two.

For the dgcellstogrid example, I think the only use of being able to save directly to a shapefile is to visualize the grid. There is no ID information stored in the resulting shapefile, nor is there the ability to store any other gridded data into the shapefile. I don't think there would be much use to adding ID to the resulting shapefile unless some sort of other data could also be merged with it simultaneously.

My workflow where I need a shapefile is typically

  • bin data into grids

  • get grid polygons

  • merge the binned data and polygons

  • output to shape file

Code snippet below based on the quakecounts data from the vignette. I do think that the frame=FALSE option for dgcellstogrid should return a SpatialPolygonsDataFrame object. I can't think of a use case where a user would want a SpatialPolygon object instead.

#get grid cell boundaries
grid <- dgcellstogrid(dgg, quakecounts$cell, frame=FALSE)

#convert grid from SpatialPolygon to SpatialPolygonsDataFrame so that attribute data can be merged
grid.ID <- data.frame(ID=getSpPPolygonsIDSlots(grid), row.names=getSpPPolygonsIDSlots(grid))
grid <- SpatialPolygonsDataFrame(grid, grid.ID)


#append earthquake counts to each cell
grid <- merge(grid, quakecounts, by.x='ID', by.y='cell')

#output data to shapefile
writeOGR(grid, '_shapefile path_', '', 'ESRI Shapefile')` 

good-kiwi avatar Jun 27 '19 16:06 good-kiwi