fto_subset.R 1.82 KB
Newer Older
Jean-Marie Lepioufle's avatar
first  
Jean-Marie Lepioufle committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#' fto_subset

#' fto_subset
#' @param ft_osmdata ft_osmdata
#' @param subset a subset in a flat refsys
#' @param augm description of the augmentation
#' @param osmdir path of osm files
#' @param workdir path of processed osm files
#' @param version_r version of save R
#' @keywords osmosm
#' @export
#' @examples
#' \dontrun{
#' fto_subset()
#' }
fto_subset_augm <- function(ft_osmdata=NULL,subset, augm=NULL, osmdir, workdir,version_r=3){

    ft_osmdata_ <- ft_osmdata


    tmp <- lapply(1:length(ft_osmdata),function(x){
     lapply(1:length(ft_osmdata[[x]]),function(y){

         if (!is.null(ft_osmdata[[x]][[y]]$file)) {

           env <- new.env()
           filename <- ft_osmdata[[x]][[y]]$file
           load(normalizePath(file.path(osmdir,filename),mustWork = TRUE),env)
           osm_features <- get("osm_features",env)

           subset_osm <- list(osm_points=NULL, osm_lines=NULL, osm_polygons=NULL, osm_multilines=NULL, osm_multipolygons=NULL)

           tmp <- lapply(c("osm_points", "osm_lines","osm_polygons","osm_multilines","osm_multipolygons"),function(z){
             if(!is.null(osm_features[[z]])) {

               osm_features[[z]][,"geometry"] <- sf::st_transform(osm_features[[z]][,"geometry"],st_crs(subset))

              print(paste0(x," ",y," ",z ))
               if (is.null(augm)){
                 subset_osm[[z]] <- sf::st_intersection(sf::st_make_valid(osm_features[[z]][,"geometry"]),subset,sparse=FALSE)
               } else if (!is.null(augm)) {
                 subset_osm[[z]] <- st_subset_coords_modif(osm_features[[z]][,"geometry"],subset,augm$fun,arg=augm$arg)
               }
             }
           })
           osm_features <- subset_osm

           save(osm_features,file=normalizePath(file.path(workdir,filename),mustWork = FALSE),version=version_r)

         }
     })
    })
    return(TRUE)

}