Skip to content

Commit

Permalink
split files
Browse files Browse the repository at this point in the history
  • Loading branch information
mem48 committed Dec 18, 2018
1 parent 06542f4 commit 3ba736c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 103 deletions.
61 changes: 0 additions & 61 deletions R/import_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ library(dplyr)
library(raster)
library(fasterize)
source("R/overline.R")
rasterOptions(maxmemory = 1e+11)
rasterOptions(tmpdir = "F:/RasterTmp/")



Expand All @@ -24,69 +22,10 @@ if(file.exists("securedata/rf_sf.Rds")){
saveRDS(rf_shape,"securedata/rf_sf.Rds")
}

#rf_test = rf_shape[1:10000,]

message(paste0(Sys.time()," Overlineing"))
overlined = overline_malcolm2(x = rf_shape,
attrib = c("bicycle","govtarget_slc","dutch_slc"), ncores = 6)

saveRDS(overlined,"schools_overlined.Rds")

message(paste0(Sys.time()," Buffering"))
overlined_buf = st_buffer(overlined, 10, nQuadSegs = 3)
overlined_buf = st_transform(overlined_buf, 4326)
message(paste0(Sys.time()," Rastering"))
rm(overlined, rf_shape)

types = c("bicycle","govtarget_slc","dutch_slc")
grid = st_make_grid(overlined_buf, n = c(2,2))
coltable = c("#FFFFFF","#9C9C9C","#FEFE73","#AFFE00","#00FEFE","#30B0FE","#2E5FFE","#0000FE","#FE00C5")


for(i in types){
message(paste0(Sys.time()," Doing ",i))
overlined_buf_tmp = overlined_buf[overlined_buf[[i]] > 0, ]
rast = raster::raster(overlined_buf, resolution = 0.0001)
raster::dataType(rast) <- "INT2U"
rast = fasterize::fasterize(overlined_buf_tmp, raster = rast, field = i, fun = "sum")
rast_col = raster::RGB(rast, breaks = c(1,10,50,100,250,500,1000,2000,9999999), col = coltable, alpha = F, colNA = NA, datatype = "INT1U")
raster::dataType(rast_col) <- "INT1U"
# Save Tiles for main raster
for(j in 1:length(grid)){
extent = st_bbox(grid[j])
extent = c(extent[1],extent[3],extent[2],extent[4])
rast_crop = raster::crop(rast_col,extent)
message(paste0(Sys.time()," saving Raster ",i,"-10m-",j))
raster::writeRaster(rast_crop,
filename = paste0("rasters/",i,"-10m-",j,".tif"),
format = "GTiff",
datatype = "INT1U",
overwrite = T)
rm(rast_crop, extent)
}
rm(rast_col)
# Save 50m raster
rast_50 = raster::aggregate(rast, fact=5, fun=max, na.rm=T)
rast_50 = raster::RGB(rast_50, breaks = c(1,10,50,100,250,500,1000,2000,9999999), col = coltable, alpha = F, colNA = NA, datatype = "INT1U")
raster::dataType(rast_50) <- "INT1U"
message(paste0(Sys.time()," saving Raster ",i,"-50m"))
raster::writeRaster(rast_50,
filename = paste0("rasters/",i,"-50m.tif"),
format = "GTiff",
datatype = "INT1U",
overwrite = T)
rm(rast_50)
# Save 100m raster
rast_100 = raster::aggregate(rast, fact=10, fun=max, na.rm=T)
rast_100 = raster::RGB(rast_100, breaks = c(1,10,50,100,250,500,1000,2000,9999999), col = coltable, alpha = F, colNA = NA, datatype = "INT1U")
raster::dataType(rast_100) <- "INT1U"
message(paste0(Sys.time()," saving Raster ",i,"-100m"))
raster::writeRaster(rast_100,
filename = paste0("rasters/",i,"-100m.tif"),
format = "GTiff",
datatype = "INT1U",
overwrite = T)
rm(rast_100,rast)

}
message(paste0(Sys.time()," Done"))
60 changes: 18 additions & 42 deletions R/import_data_old.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ library(dplyr)
library(raster)
library(fasterize)
source("R/overline.R")
rasterOptions(maxmemory = 1e+11)
rasterOptions(tmpdir = "F:/RasterTmp/")



if(file.exists("securedata/rf_sf.Rds")){
rf_shape = readRDS("securedata/rf_sf.Rds")
Expand All @@ -23,31 +27,29 @@ if(file.exists("securedata/rf_sf.Rds")){
#rf_test = rf_shape[1:10000,]

message(paste0(Sys.time()," Overlineing"))
# overlined1 = overline_malcolm(x = rf_test,
# attrib = c("bicycle","govtarget_slc","dutch_slc"), ncores = 1)
overlined = overline_malcolm2(x = rf_test,
attrib = c("bicycle","govtarget_slc","dutch_slc"), ncores = 4)
overlined = overline_malcolm2(x = rf_shape,
attrib = c("bicycle","govtarget_slc","dutch_slc"), ncores = 6)

saveRDS(overlined,"schools_overlined.Rds")

message(paste0(Sys.time()," Buffering"))
overlined_buf = st_buffer(overlined, 10)
overlined_buf = st_transform(overlined, 4326)
overlined_buf = st_buffer(overlined, dist = 10, nQuadSegs = 3)
overlined_buf = st_transform(overlined_buf, 4326)
message(paste0(Sys.time()," Rastering"))
rasterOptions(maxmemory = 1e+11)
rm(overlined, rf_shape)

types = c("bicycle","govtarget_slc","dutch_slc")
grid = st_make_grid(overlined_buf, n = c(2,2))
coltable = c("#000000","#9C9C9C","#FFFF73","#AFFF00","#00FFFF","#30B0FF","#2E5FFF","#0000FF","#FF00C5")
coltable = c("#FFFFFF","#9C9C9C","#FEFE73","#AFFE01","#00FEFE","#30B0FE","#2E5FFE","#0101FE","#FE01C5")
#inter = st_intersects(overlined_buf,grid)

for(i in types){
message(paste0(Sys.time()," Doing ",i))
overlined_buf_tmp = overlined_buf[overlined_buf[[i]] > 0, ]
rast = raster::raster(overlined_buf, resolution = 10)
rast = raster::raster(overlined_buf, resolution = 0.0001)
raster::dataType(rast) <- "INT2U"
rast = fasterize(overlined_buf_tmp, raster = rast, field = i)
rast = raster::RGB(rast, breaks = c(0,1,10,50,100,250,500,1000,2000), col = coltable, alpha = F)
rast = fasterize::fasterize(overlined_buf_tmp, raster = rast, field = i, fun = "sum")
rast = raster::RGB(rast, breaks = c(0,1,10,50,100,250,500,1000,2000), col = coltable, alpha = T, colNA = NA)
# Save Tiles for main raster
for(j in 1:length(grid)){
extent = st_bbox(grid[j])
Expand All @@ -57,6 +59,7 @@ for(i in types){
raster::writeRaster(rast_crop,
filename = paste0("rasters/",i,"-10m-",j,".tif"),
format = "GTiff",
datatype = "INT1U",
overwrite = T)
rm(rast_crop, extent)
}
Expand All @@ -65,45 +68,18 @@ for(i in types){
raster::writeRaster(rast_50,
filename = paste0("rasters/",i,"-50m.tif"),
format = "GTiff",
datatype = "INT1U",
overwrite = T)
rm(rast_50)
rast_100 = raster::aggregate(rast, fact=10, fun=max, na.rm=T)
message(paste0(Sys.time()," saving Raster ",i,"-100m"))
raster::writeRaster(rast_100,
filename = paste0("rasters/",i,"-100m.tif"),
format = "GTiff",
datatype = "INT1U",
overwrite = T)
rm(rast_100, rast)
rm(rast_100,rast,overlined_buf_tmp)

gc()
}
message(paste0(Sys.time()," Done"))


# for(i in types){
# message(paste0(Sys.time()," Doing ",i))
#
# for(j in 1:4){
# message(paste0(Sys.time()," Doing grid ",j))
# overlined_buf_tmp = overlined_buf[sapply(inter, function(y){j %in% y}), ]
# overlined_buf_tmp = overlined_buf_tmp[overlined_buf_tmp[[i]] > 0, ]
# rast = raster::raster(st_sf(data.frame(id = 1, geometry = grid[j])), resolution = 10)
# raster::dataType(rast) <- "INT2U"
# rast_tmp = fasterize(overlined_buf_tmp, raster = rast, field = i)
# message(paste0(Sys.time()," saving Raster ",i,"-",j))
# raster::writeRaster(rast_tmp,
# filename = paste0("rasters/",i,"-",j,"tif"),
# format = "ascii",
# datatype = "INT2U",
# overwrite = T)
# rm(rast_tmp)
# }
#
#
#
# }
# message(paste0(Sys.time()," Done"))

# point = st_centroid(overlined_fin[1,])
# point = st_buffer(point, 100000)
# qtm(overlined_fin[point,], lines.col = "dutch_slc", lines.lwd = 3)
# #qtm(rf_test, lines.col = "dutch_slc", lines.lwd = 3)
72 changes: 72 additions & 0 deletions R/rasterise.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
library(raster)
library(fasterize)
library(sf)
library(tmap)
tmap_mode("view")
rasterOptions(maxmemory = 1e+11)
rasterOptions(tmpdir = "F:/RasterTmp/")


overlined = readRDS("schools_overlined.Rds")

message(paste0(Sys.time()," Buffering"))
overlined_buf = st_buffer(overlined, 10, nQuadSegs = 3)
overlined_buf = st_transform(overlined_buf, 3857)
message(paste0(Sys.time()," Rastering"))
rm(overlined)

types = c("bicycle","govtarget_slc","dutch_slc")
grid = st_make_grid(overlined_buf, n = c(2,2))
coltable = c("#FFFFFF","#9C9C9C","#FEFE73","#AFFE00","#00FEFE","#30B0FE","#2E5FFE","#0000FE","#FE00C5")
gc()

for(i in types){
message(paste0(Sys.time()," Doing ",i))
overlined_buf_tmp = overlined_buf[overlined_buf[[i]] > 0, ]
overlined_buf_tmp = overlined_buf_tmp[,i]
rast = fasterize::raster(overlined_buf, resolution = 10)
raster::dataType(rast) <- "INT2U"
rast = fasterize::fasterize(overlined_buf_tmp, raster = rast, field = i, fun = "sum")
rast_col = raster::RGB(rast, breaks = c(1,10,50,100,250,500,1000,2000,9999999), col = coltable, alpha = F, colNA = NA, datatype = "INT1U")
raster::dataType(rast_col) <- "INT1U"
# Save Tiles for main raster
for(j in 1:length(grid)){
extent = st_bbox(grid[j])
extent = c(extent[1],extent[3],extent[2],extent[4])
rast_crop = raster::crop(rast_col,extent)
message(paste0(Sys.time()," saving Raster ",i,"-10m-",j))
raster::writeRaster(rast_crop,
filename = paste0("rasters/",i,"-10m-",j,".tif"),
format = "GTiff",
datatype = "INT1U",
overwrite = T)
rm(rast_crop, extent)
}
rm(rast_col)

# Save 50m raster
rast_50 = raster::aggregate(rast, fact=5, fun=max, na.rm=T)
rast_50 = raster::RGB(rast_50, breaks = c(1,10,50,100,250,500,1000,2000,9999999), col = coltable, alpha = F, colNA = NA, datatype = "INT1U")
raster::dataType(rast_50) <- "INT1U"
message(paste0(Sys.time()," saving Raster ",i,"-50m"))
raster::writeRaster(rast_50,
filename = paste0("rasters/",i,"-50m.tif"),
format = "GTiff",
datatype = "INT1U",
overwrite = T)
rm(rast_50)

# Save 100m raster
rast_100 = raster::aggregate(rast, fact=10, fun=max, na.rm=T)
rast_100 = raster::RGB(rast_100, breaks = c(1,10,50,100,250,500,1000,2000,9999999), col = coltable, alpha = F, colNA = NA, datatype = "INT1U")
raster::dataType(rast_100) <- "INT1U"
message(paste0(Sys.time()," saving Raster ",i,"-100m"))
raster::writeRaster(rast_100,
filename = paste0("rasters/",i,"-100m.tif"),
format = "GTiff",
datatype = "INT1U",
overwrite = T)
rm(rast_100,rast)

}
message(paste0(Sys.time()," Done"))

0 comments on commit 3ba736c

Please sign in to comment.