Project

General

Profile

« Previous | Next » 

Revision 961af42e

Added by Benoit Parmentier about 8 years ago

introducing function to make raster for tiles in the region

View differences:

climate/research/oregon/interpolation/global_product_assessment_part0.R
380 380
  
381 381
  r <- raster(infile_mask)
382 382
  plot(r)
383
  plot(shp1,add=T,border="blue",usePolypath = FALSE) #added usePolypath following error on brige and NEX
383
  plot(shps_tiles[[26]],add=T,border="blue",usePolypath = FALSE) #added usePolypath following error on brige and NEX
384 384

  
385 385
  ## find overlap
386 386
  #http://gis.stackexchange.com/questions/156660/loop-to-check-multiple-polygons-overlap-in-r
387 387
  
388 388
  matrix_overlap <- matrix(data=NA,nrow=length(shps_tiles),ncol=length(shps_tiles))
389 389
  for(i in 1:length(shps_tiles)){
390
     for(j in 2:length(shps_tiles)){
390
     for(j in 1:length(shps_tiles)){
391 391
      overlap_val <- as.numeric(over(shps_tiles[[i]],shps_tiles[[j]]))
392 392
      matrix_overlap[i,j]<- overlap_val
393 393
    }
394 394
    #
395 395
  }
396 396
  
397
  names(shps_tiles) <- list_names_tile_coord
398
  r_ref <- raster(list_lf_raster_tif_tiles[[1]][1])
399
  tile_spdf <- shps_tiles[[1]]
400
  tile_coord <- basename(in_dir_reg[1])
401
  date_val <- df_missing$date[1]
402
  
403
  mclapply(1:length(shps_tiles),
404
           FUN=rasterize_tile_day,
405
           list_spdf=shps_tiles,
406
           df_missing=df_missing,
407
           r_ref=list_r
408
           )
409
  rasterize_tile_day <- function(i,list_spdf,df_missing,list_r_ref,date_val){
410
    #
411
    tile_spdf <- list_spdf[[i]]
412
    tile_coord <- names(tile_spdf)[i]
413
    r_ref <- list_r_ref[[i]]
414
    
415
    df_tmp <- subset(df_missing,date==date_val,select=tile_coord)
416
    #for each row (date)
417
    val <- df_tmp[[tile_coord]]
418
    if(val==1){
419
      val<-0 #missing then not predicted
420
    }else{
421
      val<-1
422
    }
423
  
424
    tile_spdf$predicted <- val
425
    tile_spdf$tile_coord <- tile_coord
426
    tile_spdf$overlap <- 1
427
    
428
    r <- rasterize(tile_spdf,r_ref,"predicted")
429

  
430
    return(r)
431
  }
432
  
433
  
434
  ### use rasterize
435
  spdf_tiles <- do.call(bind, shps_tiles) #bind all tiles together in one shapefile
436
  spdf_tiles <- do.call(intersect, shps_tiles)
437
  
438
  ### Now use intersect to retain actual overlap
439
  
440
  for(i in 1:length(shps_tiles)){
441
    overlap_intersect <- intersect(shps_tiles[[1]],shps_tiles[[i]]))
442
  }
443
  
444
  overlap_intersect <- lapply(1:length(shps_tiles),FUN=function(i){intersect(shps_tiles[[1]],shps_tiles[[i]])})
445
  #test <- overlap_intersect <- intersect(shps_tiles[[1]],shps_tiles[[2]]))
446

  
447
  names(overlap_intersect) <- basename(in_dir_reg)
448
  shp_selected <- unlist(lapply(1:length(overlap_intersect),function(i){!is.null(overlap_intersect[[i]])}))
449
  test_list <- overlap_intersect[shp_selected]
450
  spdf_tiles_test <- do.call(bind, test_list) #combines all intersect!!
451
  #ll <- ll[ ! sapply(ll, is.null) ]
452
  test <- overlap_intersect[!lapply(overlap_intersect,is.null)]
453
  spdf_tiles_test <- do.call(bind, test) #combines all intersect!!
454
  #ll <- ll[ ! sapply(ll, is.null) ]
455
  spdf_tiles <- do.call(bind, overlap_intersect[1:4]) #combines all intersect!!
456
  spdf_tiles_test <- do.call(bind, test) #combines all intersect!!
457
  
458
  plot(spdf_tiles_test,add=T,border="green",usePolypath = FALSE) #added usePolypath following error on brige and NEX
459

  
397 460
  matrix_overlap%*%df_missing[1,1:26]
398 461
  
462
  
399 463
  ## For each day can do overalp matrix* prediction
400 464
  ## if prediction and overlap then 1 else 0, if no-overlap is then NA
401 465
  ## then for each tile compute the number of excepted predictions taken into account in a tile

Also available in: Unified diff