@@ -106,7 +106,7 @@ rasterOptions()
106106# '
107107# ' Save while creating
108108# # ----eval=F--------------------------------------------------------------
109- # # dem=merge(dem1,dem2,filename=file.path(datadir,"dem.tif"))
109+ # # dem=merge(dem1,dem2,filename=file.path(datadir,"dem.tif"),overwrite=T )
110110
111111# '
112112# ' Or after
@@ -132,20 +132,25 @@ rasterOptions()
132132# '
133133# ' `rgdal` package does even more...
134134# '
135- # ' ### Crop to Bangladesh
135+ # ' ### Crop to Coastal area of Bangladesh
136136# '
137137# # ------------------------------------------------------------------------
138- dem = crop(dem ,bgd ,filename = file.path(datadir ," dem_bgd.tif" ),overwrite = T )
138+ # Crop using border polygon
139+ # dem=crop(dem,bgd,filename=file.path(datadir,"dem_bgd.tif"),overwrite=T)
140+
141+ # Or crop to a lat-lon box
142+ dem = crop(dem ,extent(89 ,91.5 ,21.5 ,24 ),filename = file.path(datadir ," dem_bgd.tif" ),overwrite = T )
143+
139144plot(dem ); plot(bgd ,add = T )
140145
141146# '
142- # '
147+ # ' # Use ggplot
143148# # ----warning=F-----------------------------------------------------------
144149gplot(dem ,max = 1e5 )+ geom_tile(aes(fill = value ))+
145150 scale_fill_gradientn(
146151 colours = c(" red" ," yellow" ," grey30" ," grey20" ," grey10" ),
147152 trans = " log1p" ,breaks = log_breaks(n = 5 , base = 10 )(c(1 , 1e3 )))+
148- coord_equal(ylim = c(21 , 25 ))+
153+ coord_equal(ylim = c(21.5 , 24 ))+
149154 geom_path(data = fortify(bgd ),
150155 aes(x = long ,y = lat ,order = order ,group = group ),size = .5 )
151156
@@ -283,21 +288,6 @@ flowdir=terrain(reg1,opt="flowdir")
283288plot(flowdir )
284289
285290# ' Much more powerful hydrologic modeling in [GRASS GIS](https://grass.osgeo.org)
286- # '
287- # ' ## Reclassification
288- # '
289- # ' Another useful function for raster processing is `reclass()`.
290- # '
291- # # ------------------------------------------------------------------------
292- rcl = matrix (c(- Inf ,2 ,1 ,
293- 2 ,5 ,2 ,
294- 5 ,10 ,3 ),byrow = T ,nrow = 3 )
295- rcl
296- regclass = reclassify(dem ,rcl )
297- gplot(regclass ,max = 1e6 )+ geom_tile(aes(fill = value ))+
298- scale_fill_gradient2(low = " blue" ,high = " red" ,midpoint = 0 )+
299- coord_equal()
300-
301291# '
302292# ' # Sea Level Rise
303293# '
@@ -359,6 +349,34 @@ plot(area)
359349# ' </div>
360350# ' </div>
361351# '
352+ # ' ## Reclassification
353+ # '
354+ # ' Another useful function for raster processing is `reclass()`.
355+ # '
356+ # # ------------------------------------------------------------------------
357+ rcl = matrix (c(- Inf ,2.76 ,1 ,
358+ 2.76 ,10.97 ,2 ,
359+ 10.97 ,Inf ,3 ),byrow = T ,ncol = 3 )
360+ rcl
361+ regclass = reclassify(dem ,rcl )
362+
363+ gplot(regclass ,max = 1e5 )+
364+ geom_tile(aes(fill = as.factor(value )))+
365+ scale_fill_manual(values = c(" red" ," orange" ," blue" ),
366+ name = " Flood Class" )+
367+ coord_equal()
368+
369+ # '
370+ # '
371+ # ' Or, do reclassification 'on the fly in the plotting function
372+ # '
373+ # # ------------------------------------------------------------------------
374+ gplot(dem ,max = 1e5 )+
375+ geom_tile(aes(fill = cut(value ,c(- Inf ,2.76 ,10.97 ,Inf ))))+
376+ scale_fill_manual(values = c(" red" ," orange" ," blue" ),
377+ name = " Flood Class" )+
378+ coord_equal()
379+
362380# '
363381# '
364382# '
@@ -392,14 +410,14 @@ plot(area)
392410# ' Use `raster()` to load a raster from disk.
393411# '
394412# # ------------------------------------------------------------------------
395- pop = raster(file.path(datadir ," gpw-v4-population-density-2015/gpw-v4-population-density_2015.tif" ))
396- plot(pop )
413+ pop_global = raster(file.path(datadir ," gpw-v4-population-density-2015/gpw-v4-population-density_2015.tif" ))
414+ plot(pop_global )
397415
398416# '
399417# '
400418# ' A nicer plot...
401419# # ------------------------------------------------------------------------
402- gplot(pop ,max = 1e6 )+ geom_tile(aes(fill = value ))+
420+ gplot(pop_global ,max = 1e5 )+ geom_tile(aes(fill = value ))+
403421 scale_fill_gradientn(
404422 colours = c(" grey90" ," grey60" ," darkblue" ," blue" ," red" ),
405423 trans = " log1p" ,breaks = log_breaks(n = 5 , base = 10 )(c(1 , 1e5 )))+
@@ -411,10 +429,10 @@ gplot(pop,max=1e6)+geom_tile(aes(fill=value))+
411429# ' ### Crop to region with the `dem` object
412430# '
413431# # ------------------------------------------------------------------------
414- pop2 = pop %> %
432+ pop = pop_global %> %
415433 crop(dem )
416434
417- gplot(pop2 ,max = 1e6 )+ geom_tile(aes(fill = value ))+
435+ gplot(pop ,max = 1e5 )+ geom_tile(aes(fill = value ))+
418436 scale_fill_gradientn(colours = c(" grey90" ," grey60" ," darkblue" ," blue" ," red" ),
419437 trans = " log1p" ,breaks = log_breaks(n = 5 , base = 10 )(c(1 , 1e5 )))+
420438 coord_equal()
@@ -424,28 +442,37 @@ gplot(pop2,max=1e6)+geom_tile(aes(fill=value))+
424442# '
425443# ' ### Resample to DEM
426444# '
445+ # ' Compare the resolution and origin of `pop2` and `dem`.
446+ # '
447+ # # ------------------------------------------------------------------------
448+ pop
449+ dem
450+
451+ res(pop )
452+ res(dem )
453+
454+ origin(pop )
455+ origin(dem )
456+
457+ # Look at average cell area in km^2
458+ cellStats(raster :: area(pop )," mean" )
459+ cellStats(raster :: area(dem )," mean" )
460+
461+ # '
462+ # ' So to work with these rasters (population and elevation), it is easiest to "adjust" them to have the same resolution. But there is no good way to do this. Do you aggregate the finer raster or resample the coarser one?
463+ # '
427464# ' Assume equal density within each grid cell and resample
428465# # ---- warning=F----------------------------------------------------------
429- pop3 = pop2 %> %
466+ pop_fine = pop %> %
430467 resample(dem ,method = " bilinear" )
431468
432- gplot(pop3 ,max = 1e6 )+ geom_tile(aes(fill = value ))+
433- scale_fill_gradientn(colours = c(" grey90" ," grey60" ," darkblue" ," blue" ," red" ),
434- trans = " log1p" ,breaks = log_breaks(n = 5 , base = 10 )(c(1 , 1e5 )))+
469+ gplot(pop_fine ,max = 1e5 )+ geom_tile(aes(fill = value ))+
470+ scale_fill_gradientn(
471+ colours = c(" grey90" ," grey60" ," darkblue" ," blue" ," red" ),
472+ trans = " log1p" ,breaks = log_breaks(n = 5 , base = 10 )(c(1 , 1e5 )))+
435473 coord_equal()
436474
437475
438- # '
439- # '
440- # '
441- # ' Or resample elevation to resolution of population:
442- # '
443- # # ----eval=F--------------------------------------------------------------
444- # # res(pop2)/res(dem)
445- # # demc=dem%>%
446- # # aggregate(fact=50,fun=min,expand=T)%>%
447- # # resample(pop2,method="bilinear")
448-
449476# '
450477# '
451478# ' <div class="well">
@@ -462,22 +489,35 @@ gplot(pop3,max=1e6)+geom_tile(aes(fill=value))+
462489# ' <button data-toggle="collapse" class="btn btn-primary btn-sm round" data-target="#demo3">Show Solution</button>
463490# ' <div id="demo3" class="collapse">
464491# '
492+ # ' For the fine resolution population data
493+ # '
465494# '
466495# ' Number of potentially affected people across the region.
467496# '
468497# '
469498# ' </div>
470499# ' </div>
471500# '
501+ # ' Or resample elevation to resolution of population:
502+ # ' 1. First aggregate to approximate spatial resolution
503+ # ' 2. Resample to align grids perfectly
504+ # '
505+ # # ------------------------------------------------------------------------
506+ res(pop )/ res(dem )
507+ dem_coarse = dem %> %
508+ aggregate(fact = 50 ,fun = min ,expand = T )%> %
509+ resample(pop ,method = " bilinear" )
510+
472511# '
512+ # ' For the coarse resolution data
473513# '
474514# '
475515# ' ## Raster Distances
476516# '
477517# ' `distance()` calculates distances for all cells that are NA to the nearest cell that is not NA.
478518# '
479519# # ------------------------------------------------------------------------
480- popcenter = pop2 > 5000
520+ popcenter = pop > 5000
481521popcenter = mask(popcenter ,popcenter ,maskvalue = 0 )
482522plot(popcenter ,col = " red" ,legend = F )
483523
0 commit comments