Skip to content

Commit

Permalink
add id code to nested grid.
Browse files Browse the repository at this point in the history
Example for 200m
  • Loading branch information
geocaruso committed Apr 10, 2024
1 parent c317ad8 commit c59a508
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 5 deletions.
Binary file modified .DS_Store
Binary file not shown.
9 changes: 4 additions & 5 deletions R/nest.grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@
#' @export
#'
#' @examples See nest.grid.lux.R
nest.grid<-function(ingrid,res=250){
nest.grid<-function(ingrid,res=250,idcol=1){

#get the 5 coords pairs for each input grid cell
dfcoords<-data.frame(sf::st_coordinates(ingrid))
#derive base resolution from difference in Y between first and 2nd pair
# of first square:
input_r<-abs(dfcoords[1,2]-dfcoords[2,2])
#derive base resolution from Y difference of first square:
input_r<-sf::st_bbox(ingrid[1,])$ymax-sf::st_bbox(ingrid[1,])$ymin
if ((input_r %% res) >0){stop("Remainder of dividing input resolution by target resolution not null")}
# split input into list so child output cell will know parent output cell
splitted<-split(dfcoords,dfcoords$L2) #dfcoords$L2 identifies polygons
#reuse input cells names with IN first to avoid numbers to start names and indicate it is input name
names(splitted)<-paste0("IN",ingrid$CELLCODE)
names(splitted)<-paste0("IN",sf::st_drop_geometry(ingrid)[,idcol])

#sq.polyg function gives the 5 coordinates pairs of the new (smaller) square polygons
#given the requested resolution r and list of base polygons origin x and origin y
Expand Down
Binary file modified data/.DS_Store
Binary file not shown.
Binary file modified data/EXT/.DS_Store
Binary file not shown.
Binary file modified data/Grid1km_LAU2_m2.gpkg
Binary file not shown.
Binary file added data/lunested200.gpkg
Binary file not shown.
10 changes: 10 additions & 0 deletions lunested200withCODE.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#lunested200withCODE.R

lukm<-readRDS("data/lukm3035.RDS")
nested200<-nest.grid(lukm,res=200) ###about 20min

nested200coords<-data.frame(sf::st_coordinates(nested200))
origincoords200<-nested200coords[match(unique(nested200coords[,"L2"]),nested200coords[,"L2"]),c(1,2)]
nested200$CELLCODE200m<-paste0("200mE",origincoords200[,1]/100,"N",origincoords200[,2]/100)

sf::st_write(nested200,"data/lunested200.gpkg",delete_dsn=TRUE) #export gpkg

0 comments on commit c59a508

Please sign in to comment.