

plyst_grid_H_all_obfD <- function(area,input_data,mdata,lvl,k1,k2,nr,t,N,mapflag,H){
  start_time <- Sys.time()
  alpha1 <- 0.25
  pnt <- input_data
  lpnt <- nrow(pnt)
  ##############################################
  coordinates(pnt) <- ~ cnt_x + cnt_y
  crs(pnt) <- CRS("+init=epsg:2157")
  
  res = 2^ lvl
  d <- res *res
  trsh <- 40
  #trsh_area <- 5000000
 # trsh_area1 <- 3000000
  
  trsh_area <- 4000000
  trsh_area1 <- 2000000

  pnt <- st_as_sf(pnt)
  area <- st_as_sf(area)
  area <- st_transform(area,crs =  st_crs(pnt))
  ##########################################
  obf_all <- data.frame(plotUID = integer(lpnt),
                        obf_x = double(lpnt),
                        obf_y = double(lpnt))
  coordinates(obf_all) <- ~ obf_x +obf_y
  crs(obf_all) <- CRS("+init=epsg:2157")
  obf_all <- st_as_sf(obf_all)

  nrt <- nr + t-1
  mdata$dt_hrand_x <- 0
  mdata$dt_hrand_y <- 0
  pnt$dt_hrand_x <- 0
  pnt$dt_hrand_y <- 0
  totalp <- 0
  total_ply <- c(numpoly = 0,areapoly = area)
  tag1 <- TRUE
 # if (mapflag)
  #  plot(area$geometry )
  
  for (center_p in nr:nrt)
  {
    print(center_p)
    # plot(pnt$geometry[center_p],col='red', add = TRUE, cex = 1,pch = '*')
    visited_cell <- c()
    visited_ngh <<- c()
    k <- k1
    org_shape <- readWKB( pnt$plot_shape[center_p])
    org_shape <- SpatialPolygons(list(Polygons(list(org_shape@polygons[[1]]@Polygons[[1]]),ID=1)))
    org_shape <- st_as_sf(org_shape)
    st_crs(org_shape) <- st_crs(pnt)
   
    area_bufk <-drop_units(st_area(org_shape)) * k
   
    center_lonlat <- pnt$geometry[center_p]
    lx <- (H$x@extent@xmax - H$x@extent@xmin) / res
    ly <- (H$x@extent@ymax - H$x@extent@ymin) / res
    xres <- getcoord(res,lx,mdata$cnt_x[center_p],H$x@extent@xmin)
    yres <- getcoord(res,ly,mdata$cnt_y[center_p],H$x@extent@ymin) -1
    next_ind <- (xres + yres* H$x@ncols)
    i <- 1
    totalk <- 0
    num_poly <- 0
    tag <<- FALSE
    tag_obf_all <-TRUE
    tag2 <- FALSE
    totalk <- H$df.Num[next_ind]
    visited_cell[i] <- H$df.OId[next_ind]
    total_ply <- ply_numpolyg_obfpoly(area,H,pnt,total_ply,tag,visited_cell[i],
                                      lx,ly,visited_cell,visited_ngh,res,obf_all,center_p,mapflag)
   
    l <- length(visited_ngh)
    i <- i +1
    if(l > 0 ){
      il <- i+l-1
      visited_cell[i:il] <- visited_ngh[1:l]
      i <- i+l
    }
    tag2 <- TRUE
    j <- 0
    
    while (total_ply$numpoly <= k & drop_units(st_area(total_ply$areapoly)) < trsh_area1 & H$df.OId[next_ind] < d){
      j <- j + 1
      if ((next_ind -1) %% res != 0) 
       if (!H$df.OId[next_ind -1]%in% visited_cell & H$df.Hvalue[next_ind -1] != H$df.Hvalue[next_ind]+1  )
        if (total_ply$numpoly <= k  & drop_units(st_area(total_ply$areapoly)) < trsh_area1){
          totalk <- totalk +  H$df.Num[next_ind -1]
          visited_cell[i] <- H$df.OId[next_ind-1]
          total_ply <- ply_numpolyg_obfpoly(area,H,pnt,total_ply,tag,visited_cell[i],
                                            lx,ly,visited_cell,visited_ngh,res,obf_all,center_p,mapflag)
          
          lnext <- length(visited_ngh)
          i <- i+1
          if(l != lnext ){
            ln <- lnext-l
            il <- i +lnext-l-1
            visited_cell[i: il] <- visited_ngh[l:ln]
            i <- i+lnext -l
            l <- lnext
          }
        }
      if ( (next_ind +1) %% res != 1)
       if (!H$df.OId[next_ind +1] %in% visited_cell & H$df.Hvalue[next_ind +1] != H$df.Hvalue[next_ind]+1  )
        if ((total_ply$numpoly <= k & drop_units(st_area(total_ply$areapoly)) < trsh_area1 ) 
            | drop_units(st_area(st_buffer(total_ply$areapoly ,-pnt$maxd_edge[center_p]))) < trsh_area){
          totalk <- totalk +  H$df.Num[next_ind + 1]
          visited_cell[i] <- H$df.OId[next_ind +1]
          total_ply <- ply_numpolyg_obfpoly(area,H,pnt,total_ply,tag,visited_cell[i],
                                            lx,ly,visited_cell,visited_ngh,res,obf_all,center_p,mapflag)
          
          lnext <- length(visited_ngh)
          i <- i +1
          if(l != lnext ){
            ln <- lnext-l
            il <- i +lnext-l-1
            visited_cell[i: il] <- visited_ngh[l:ln]
            i <- i+lnext-l
            l <- lnext
          }
          
        }
      if (next_ind > res)
       if (!H$df.OId[next_ind -res] %in% visited_cell & H$df.Hvalue[next_ind -res ] != H$df.Hvalue[next_ind]+1 )
        ###//& H$df.Num[next_ind -res] > 0)
        if ((total_ply$numpoly <= k & drop_units(st_area(total_ply$areapoly)) < trsh_area1 ) 
            | drop_units(st_area(st_buffer(total_ply$areapoly ,-pnt$maxd_edge[center_p]))) < trsh_area){
          totalk <- totalk +  H$df.Num[next_ind - res]
          visited_cell[i] <- H$df.OId[next_ind- res]
          total_ply <- ply_numpolyg_obfpoly(area,H,pnt,total_ply,tag,visited_cell[i],
                                            lx,ly,visited_cell,visited_ngh,res,obf_all,center_p,mapflag)
          
          lnext <- length(visited_ngh)
          i <- i +1
          if(l != lnext ){
            ln <- lnext-l
            il <- i +lnext-l-1
            visited_cell[i: il] <- visited_ngh[l+1:ln]
            i <- i+lnext-l
            l <- lnext
          }
          
        }
      if (next_ind + res < d)
       if (!H$df.OId[next_ind + res] %in% visited_cell & H$df.Hvalue[next_ind +res ] != H$df.Hvalue[next_ind]+1  )
        ###// & H$df.Num[next_ind +res ] > 0)
        if (total_ply$numpoly <= k & drop_units(st_area(total_ply$areapoly)) < trsh_area1) {
          totalk <- totalk +  H$df.Num[next_ind + res]
          visited_cell[i] <- H$df.OId[next_ind + res]
          total_ply <- ply_numpolyg_obfpoly(area,H,pnt,total_ply,tag,visited_cell[i],
                                            lx,ly,visited_cell,visited_ngh,res,obf_all,center_p,mapflag)
          
          lnext <- length(visited_ngh)
          i <- i +1
          if(l != lnext ){
            ln <- lnext-l
            il <- i +lnext-l-1
            visited_cell[i: il] <- visited_ngh[l+1:ln]
            i <- i+lnext-l
            l <- lnext
          }
        }
      next_Hv <- H$df.Hvalue[next_ind]+1 
      next_ind <- which(H$df.Hvalue == next_Hv)
      if (!H$df.OId[next_ind] %in% visited_cell )
        if (total_ply$numpoly <= k  & drop_units(st_area(total_ply$areapoly)) < trsh_area1){
          totalk <- totalk +  H$df.Num[next_ind]
          visited_cell[i] <- H$df.OId[next_ind]
          total_ply <- ply_numpolyg_obfpoly(area,H,pnt,total_ply,tag,visited_cell[i],
                                            lx,ly,visited_cell,visited_ngh,res,obf_all,center_p,mapflag)
          
          lnext <- length(visited_ngh)
          i <- i +1
          if(l != lnext ){
            ln <- lnext-l
            il <- i +lnext-l-1
            visited_cell[i: il] <- visited_ngh[l+1:ln]
            i <- i+lnext-l
            l <- lnext
          }
        }
      if (!tag1 & total_ply$numpoly != num_poly){
        ##   plot(obf_all, add = TRUE, col = 'blue')
        ###total_ply$areapoly <- st_difference(total_ply$areapoly,obf_all)
        num_poly <- total_ply$numpoly
      }
    }
    
    ##################################################################
    inter_total_ply <- total_ply$areapoly
    k <- k2
    area_bufk <-drop_units(st_area(org_shape)) * k
    # diff_total_ply <- total_ply$areapoly
    diff_area <- 0
    ###############################################
    while ( (diff_area < trsh_area) &
            (total_ply$numpoly <= k | diff_area <= area_bufk) 
            & H$df.OId[next_ind] < d) {
      j <- j + 1
      if ((next_ind -1) %% res != 0)
       if (!H$df.OId[next_ind -1]%in% visited_cell & H$df.Hvalue[next_ind -1] != H$df.Hvalue[next_ind]+1  )
        if (total_ply$numpoly <= k | diff_area < trsh_area){
          totalk <- totalk +  H$df.Num[next_ind -1]
          visited_cell[i] <- H$df.OId[next_ind-1]
          total_ply <- ply_numpolyg_obfpoly(area,H,pnt,total_ply,tag,visited_cell[i],
                                            lx,ly,visited_cell,visited_ngh,res,obf_all,center_p,mapflag)
          if (drop_units(st_area(total_ply$areapoly))- drop_units(st_area(inter_total_ply)) > 0){
            diff_total_ply <- st_difference(total_ply$areapoly,inter_total_ply)
            diff_area <- drop_units(st_area(st_buffer(diff_total_ply ,-(pnt$maxd_edge[center_p] +alpha1))))
          }
          lnext <- length(visited_ngh)
          i <- i+1
          if(l != lnext ){
            ln <- lnext-l
            il <- i +lnext-l-1
            visited_cell[i: il] <- visited_ngh[l:ln]
            i <- i+lnext -l
            l <- lnext
          }
        }
      if ((next_ind +1) %% res != 1)
       if (!H$df.OId[next_ind +1] %in% visited_cell & H$df.Hvalue[next_ind +1] != H$df.Hvalue[next_ind]+1  )
        ###// & H$df.Num[next_ind +1] > 0)
        if ((total_ply$numpoly <= k & diff_area <= area_bufk) 
            | diff_area < trsh_area){
          totalk <- totalk +  H$df.Num[next_ind + 1]
          visited_cell[i] <- H$df.OId[next_ind +1]
          total_ply <- ply_numpolyg_obfpoly(area,H,pnt,total_ply,tag,visited_cell[i],
                                            lx,ly,visited_cell,visited_ngh,res,obf_all,center_p,mapflag)
          if (drop_units(st_area(total_ply$areapoly))- drop_units(st_area(inter_total_ply)) > 0){
            diff_total_ply <- st_difference(total_ply$areapoly,inter_total_ply)
            diff_area <- drop_units(st_area(st_buffer(diff_total_ply ,-(pnt$maxd_edge[center_p] +alpha1))))
          }
        
          lnext <- length(visited_ngh)
          i <- i +1
          if(l != lnext ){
            ln <- lnext-l
            il <- i +lnext-l-1
            visited_cell[i: il] <- visited_ngh[l:ln]
            i <- i+lnext-l
            l <- lnext
          }
          
        }
      if (next_ind > res)
       if (!H$df.OId[next_ind -res] %in% visited_cell & H$df.Hvalue[next_ind -res ] != H$df.Hvalue[next_ind]+1 )
        ###//& H$df.Num[next_ind -res] > 0)
        if ((total_ply$numpoly <= k & diff_area <= area_bufk)|diff_area < trsh_area){
          totalk <- totalk +  H$df.Num[next_ind - res]
          visited_cell[i] <- H$df.OId[next_ind- res]
          total_ply <- ply_numpolyg_obfpoly(area,H,pnt,total_ply,tag,visited_cell[i],
                                            lx,ly,visited_cell,visited_ngh,res,obf_all,center_p,mapflag)
          if (drop_units(st_area(total_ply$areapoly))- drop_units(st_area(inter_total_ply)) > 0){
            diff_total_ply <- st_difference(total_ply$areapoly,inter_total_ply)
            diff_area <- drop_units(st_area(st_buffer(diff_total_ply ,-(pnt$maxd_edge[center_p] +alpha1))))
          }
          lnext <- length(visited_ngh)
          i <- i +1
          if(l != lnext ){
            ln <- lnext-l
            il <- i +lnext-l-1
            visited_cell[i: il] <- visited_ngh[l+1:ln]
            i <- i+lnext-l
            l <- lnext
          }
          
        }
      if (next_ind + res < d)
       if (!H$df.OId[next_ind + res] %in% visited_cell & H$df.Hvalue[next_ind +res ] != H$df.Hvalue[next_ind]+1  )
        ###// & H$df.Num[next_ind +res ] > 0)
        if ((total_ply$numpoly <= k & diff_area <= area_bufk) | diff_area < trsh_area){
          totalk <- totalk +  H$df.Num[next_ind + res]
          visited_cell[i] <- H$df.OId[next_ind + res]
          total_ply <- ply_numpolyg_obfpoly(area,H,pnt,total_ply,tag,visited_cell[i],
                                            lx,ly,visited_cell,visited_ngh,res,obf_all,center_p,mapflag)
          if (drop_units(st_area(total_ply$areapoly))- drop_units(st_area(inter_total_ply)) > 0){
            diff_total_ply <- st_difference(total_ply$areapoly,inter_total_ply)
            diff_area <- drop_units(st_area(st_buffer(diff_total_ply ,-(pnt$maxd_edge[center_p] +alpha1))))
          }
          lnext <- length(visited_ngh)
          i <- i +1
          if(l != lnext ){
            ln <- lnext-l
            il <- i +lnext-l-1
            visited_cell[i: il] <- visited_ngh[l+1:ln]
            i <- i+lnext-l
            l <- lnext
          }
        }
      next_Hv <- H$df.Hvalue[next_ind]+1 
      next_ind <- which(H$df.Hvalue == next_Hv)
      if (!H$df.OId[next_ind] %in% visited_cell )
        
        ###// & H$df.Num[next_ind] > 0)
        if ((total_ply$numpoly <= k & diff_area <= area_bufk)| diff_area < trsh_area){
          totalk <- totalk +  H$df.Num[next_ind]
          visited_cell[i] <- H$df.OId[next_ind]
          total_ply <- ply_numpolyg_obfpoly(area,H,pnt,total_ply,tag,visited_cell[i],
                                            lx,ly,visited_cell,visited_ngh,res,obf_all,center_p,mapflag)
          if (drop_units(st_area(total_ply$areapoly))- drop_units(st_area(inter_total_ply)) > 0){
            diff_total_ply <- st_difference(total_ply$areapoly,inter_total_ply)
            diff_area <- drop_units(st_area(st_buffer(diff_total_ply ,-(pnt$maxd_edge[center_p] +alpha1))))
          }
          lnext <- length(visited_ngh)
          i <- i +1
          if(l != lnext ){
            ln <- lnext-l
            il <- i +lnext-l-1
            visited_cell[i: il] <- visited_ngh[l+1:ln]
            i <- i+lnext-l
            l <- lnext
          }
        }
      if (!tag1 & total_ply$numpoly != num_poly){
        num_poly <- total_ply$numpoly
      }
    }
    ####################################################################################
    #plot(total_ply$areapoly$geometry,col= 'yellow')
    if (drop_units(st_area(total_ply$areapoly))- drop_units(st_area(inter_total_ply)) > 0)
      total_ply$areapoly <- st_difference(total_ply$areapoly,inter_total_ply)
    else{
      print(paste('equal',drop_units(st_area(total_ply$areapoly)), drop_units(st_area(inter_total_ply )) ))
    }
    total_ply$areapoly  <- st_buffer(total_ply$areapoly ,-(pnt$maxd_edge[center_p] +alpha1))
    ###if (mapflag)
      ###  plot(total_ply$areapoly$geometry,col= 'purple', add = TRUE)
    #print(total_ply$numpoly)
    #plot(total_ply$areapoly$geometry,col= 'purple', add = TRUE)
    rand_point <- st_sample(total_ply$areapoly, N)
    t <- st_geometry(rand_point)
    ixm <- 1
    # ixm <-whichmedian(st_distance(rand_point,center_lonlat))
    mdata$dt_hrand_x[center_p] <- rand_point[[ixm]][1]
    mdata$dt_hrand_y[center_p] <- rand_point[[ixm]][2]
    
    pnt$dt_hrand_x[center_p] <- rand_point[[ixm]][1]
    pnt$dt_hrand_y[center_p] <- rand_point[[ixm]][2]
    
    
    obf_all$geometry[[center_p]][1] <- rand_point[[ixm]][1]
    obf_all$geometry[[center_p]][2] <- rand_point[[ixm]][2]
    obf_all$plotUID[center_p] <- pnt$plotUID[center_p]
    # r <- input_data$maxd_edge[center_p] + alpha
    tmp_poly <- st_buffer(obf_all$geometry[center_p],input_data$maxd_edge[center_p])
    ## print(paste('area of tmp_poly', st_area(tmp_poly)))
    if (tag1){
      tag1 <- FALSE
    }
   if (mapflag){
      plot(total_ply$areapoly$geometry,col= 'purple', add = TRUE)
      plot(tmp_poly, col = 'green', add = TRUE)
      plot(pnt$geometry[center_p],col='red', add = TRUE, cex = 1,pch = '*')
    #plot(rand_point,add =TRUE,col= 'black',cex = 1,pch = '*')
   }
  }
  print(Sys.time() - start_time)
  return(mdata)
}

##  example to plot obfuscated area 
htest <- plyst_grid_H_all_obfD(Corkt,orgmock_k1_5_k2_12_tmp,orgmock_k1_5_k2_12_tmp,8,5,12,1,5,5,TRUE,H8t)

orgmock_k1_5_k2_12_tmp <- plyst_grid_H_all_obfD(Corkt,orgmock_k1_5_k2_12_tmp,orgmock_k1_5_k2_12_tmp,8,5,12,1,nrow(orgmock_k1_5_k2_12_tmp),1,FALSE,H8t) 
