Commit 19f03595 authored by Jean-Marie Lepioufle's avatar Jean-Marie Lepioufle
Browse files

first

parents
^.*\.Rproj$
^\.Rproj\.user$
.Rproj.user
.Rhistory
.RData
Package: osmosm
Type: Package
Version: 0.0.1
Authors@R: c(person("Jean-Marie", "Lepioufle", , "jml@nilu.no", role=c("aut","cre")))
Title: Osmosm Enables To Get And Manipulate Osm Data
Description: Quickly get osm data related to a specific place with key features.
Manipulate the osm data as subset and augment them by simple modification (rotation, flip,...).
The creation of plot of the augmented subset will come soon.
Depends:
R (>= 3.6.3)
Imports:
osmdata,
sf,
lwgeom,
units,
dplyr,
grDevices,
graphics
Suggests:
knitr
VignetteBuilder: knitr
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: false
RoxygenNote: 7.1.1
The MIT License (MIT)
Copyright © 2021 NILU
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# Generated by roxygen2: do not edit by hand
export(ft_init)
export(fto_checkosm)
export(fto_getosm)
export(fto_osmtype)
export(fto_subset_augm)
export(fto_transform)
export(get_osmdata_sf)
importFrom(dplyr,"%>%")
importFrom(graphics,plot)
features <- function(place= NULL,bbox=NULL,workdir=tempdir()){
#####################
# private variables #
#####################
r_version <- paste0(version$major,".",version$minor)
if (r_version >= "1.4.0" & r_version <="3.5.0") {
version_r <- 2
} else if (r_version > "3.5.0") {
version_r <- 3
} else stop(r_version)
ft_ <- ft_init(place = place, bbox = bbox, osmdir = NULL, workdir = normalizePath(workdir,mustWork=TRUE),version_r=version_r)
place_name_ <- gsub("[[:punct:][:blank:]]","_",place)
place_name_ <- gsub('([[:punct:]])\\1+', '\\1', place_name_)
#####################
# public variables #
#####################
object <- local({
get_osmdata <- function(tags,osmdir){
ft_$osmdata <<- fto_getosm(ft_osmdata=ft_$osmdata,place,bbox,tags=tags,osmdir=osmdir,place_name=place_name_,version_r= version_r)
ft_$osmdata <<- fto_osmtype(ft_osmdata=ft_$osmdata,osmdir=osmdir)
invisible()
}
load_osmdata <- function(tags,osmdir){
ft_$osmdir <<- normalizePath(osmdir,mustWork=TRUE)
ft_$osmdata <<- fto_checkosm(ft_osmdata=ft_$osmdata,tags=tags,osmdir=osmdir,place_name=place_name_)
ft_$osmdata <<- fto_osmtype(ft_osmdata=ft_$osmdata,osmdir=osmdir)
invisible()
}
transform <- function(refsys){
fto_transform(ft_osmdata=ft_$osmdata,refsys=refsys,osmdir=ft_$osmdir,workdir=ft_$workdir,version_r=ft_$version_r)
invisible()
}
subset_augm <- function(subset,augm=NULL){
fto_subset_augm(ft_osmdata=ft_$osmdata,subset=subset, augm=augm,osmdir=ft_$osmdir,workdir=ft_$workdir,version_r=ft_$version_r)
invisible()
}
environment()
})
lockEnvironment(object, TRUE)
structure(object, class=c("features_obj", class(object)))
}
#' ft_init
#' ft_init
#' @param place place
#' @param bbox bbox
#' @param osmdir osmdir
#' @param workdir workdir
#' @param version_r version_r
#' @keywords osmosm
#' @export
#' @examples
#' \dontrun{
#' ft_init()
#' }
ft_init <- function(place=NULL,bbox=NULL, osmdir = NULL,workdir=tempdir(), version_r=3){
if (is.null(place) && is.null(bbox)) stop("need either 'place' or 'bbox'")
if (!is.null(place) && !is.null(bbox)) stop("need either 'place' or 'bbox'")
res <- list(
place = place,
bbox = bbox,
osmdir = osmdir,
workdir = workdir,
version_r = version_r,
osmdata = list(
highway = list(
trunk = list(file = NULL, tags ="roads" , osm_type = NULL, augmentation = NULL, plot = list(col = "black" , lw = 0.7, lty=1, cex=0.5)),
trunk_link = list(file = NULL, tags ="roads" , osm_type = NULL, augmentation = NULL, plot = list(col = "black" , lw = 0.7, lty=1, cex=0.5)),
motorway = list(file = NULL, tags ="roads" , osm_type = NULL, augmentation = NULL, plot = list(col = "black" , lw = 0.6, lty=1, cex=0.5)),
motorway_link = list(file = NULL, tags ="roads" , osm_type = NULL, augmentation = NULL, plot = list(col = "black" , lw = 0.6, lty=1, cex=0.5)),
primary = list(file = NULL, tags ="roads" , osm_type = NULL, augmentation = NULL, plot = list(col = "black" , lw = 0.5, lty=1, cex=0.5)),
primary_link = list(file = NULL, tags ="roads" , osm_type = NULL, augmentation = NULL, plot = list(col = "black" , lw = 0.5, lty=1, cex=0.5)),
secondary = list(file = NULL, tags ="roads" , osm_type = NULL, augmentation = NULL, plot = list(col = "black" , lw = 0.4, lty=1, cex=0.5)),
secondary_link = list(file = NULL, tags ="roads" , osm_type = NULL, augmentation = NULL, plot = list(col = "black" , lw = 0.4, lty=1, cex=0.5)),
tertiary = list(file = NULL, tags ="roads" , osm_type = NULL, augmentation = NULL, plot = list(col = "black" , lw = 0.3, lty=1, cex=0.5)),
tertiary_link = list(file = NULL, tags ="roads" , osm_type = NULL, augmentation = NULL, plot = list(col = "black" , lw = 0.3, lty=1, cex=0.5)),
living_street = list(file = NULL, tags ="street", osm_type = NULL, augmentation = NULL, plot = list(col = "orange", lw = 0.4, lty=2, cex=0.5)),
service = list(file = NULL, tags ="street", osm_type = NULL, augmentation = NULL, plot = list(col = "orange", lw = 0.4, lty=3, cex=0.5)),
unclassified = list(file = NULL, tags ="street", osm_type = NULL, augmentation = NULL, plot = list(col = "orange", lw = 0.4, lty=4, cex=0.5)),
pedestrian = list(file = NULL, tags ="street", osm_type = NULL, augmentation = NULL, plot = list(col = "orange", lw = 0.4, lty=5, cex=0.5)),
footway = list(file = NULL, tags ="street", osm_type = NULL, augmentation = NULL, plot = list(col = "orange", lw = 0.4, lty=6, cex=0.5)),
track = list(file = NULL, tags ="street", osm_type = NULL, augmentation = NULL, plot = list(col = "orange", lw = 0.2, lty=1, cex=0.5)),
path = list(file = NULL, tags ="street", osm_type = NULL, augmentation = NULL, plot = list(col = "orange", lw = 0.2, lty=1, cex=0.5)),
residential = list(file = NULL, tags ="street", osm_type = NULL, augmentation = NULL, plot = list(col = "orange", lw = 0.4, lty=1, cex=0.5))
),
place = list(
island = list(file = NULL, tags ="island", osm_type = NULL, augmentation = NULL, plot = list(col="white",border = 'blue', lw = 0.7, lty = 1, cex=0.5)),
islet = list(file = NULL, tags ="island", osm_type = NULL, augmentation = NULL, plot = list(col="white",border = 'blue', lw = 0.7, lty = 1, cex=0.5))
),
natural = list(
wood = list(file = NULL, tags ="forest", osm_type = NULL, augmentation = NULL, plot = list(col="lightgreen", border="lightgreen", lw = 0.7, lty = 1, cex=0.5)),
coastline = list(file = NULL, tags = "water", osm_type = NULL, augmentation = NULL, plot = list(col="lightblue",border="lightblue", lw = 0.7, lty = 1, cex=0.5))
),
boundary = list(
national_park = list(file = NULL, tags ="forest", osm_type = NULL, augmentation = NULL, plot = list(col="lightgreen", border="lightgreen", lw = 0.7, lty = 1, cex=0.5))
),
landuse = list(
forest = list(file = NULL, tags ="forest" , osm_type = NULL, augmentation = NULL, plot = list(col="lightgreen", border="lightgreen", lw = 0.7, lty = 1, cex=0.5)),
meadow = list(file = NULL, tags ="meadow" , osm_type = NULL, augmentation = NULL, plot = list(col="yellow" , border="" , lw = 0.7, lty = 1, cex=0.5)),
farmyard = list(file = NULL, tags ="meadow" , osm_type = NULL, augmentation = NULL, plot = list(col="yellow" , border="" , lw = 0.7, lty = 1, cex=0.5)),
farmland = list(file = NULL, tags ="meadow" , osm_type = NULL, augmentation = NULL, plot = list(col="yellow" , border="" , lw = 0.7, lty = 1, cex=0.5)),
grass = list(file = NULL, tags ="grass" , osm_type = NULL, augmentation = NULL, plot = list(col="green" , border="green" , lw = 0.7, lty = 1, cex=0.5)),
cemetery = list(file = NULL, tags ="grass" , osm_type = NULL, augmentation = NULL, plot = list(col="green" , border="green" , lw = 0.7, lty = 1, cex=0.5)),
residential = list(file = NULL, tags ="residential", osm_type = NULL, augmentation = NULL, plot = list(col="light steel blue",border="white", lw = 0.7, lty = 1, cex=0.5)),
commercial = list(file = NULL, tags ="commercial" , osm_type = NULL, augmentation = NULL, plot = list(col="darkolivegreen1", border="darkolivegreen1", lw = 0.1, lty = 1, cex=0.5)),
retail = list(file = NULL, tags ="commercial" , osm_type = NULL, augmentation = NULL, plot = list(col="darkolivegreen1", border="darkolivegreen1", lw = 0.1, lty = 1, cex=0.5)),
industrial = list(file = NULL, tags ="industrial" , osm_type = NULL, augmentation = NULL, plot = list(col="dim grey",border="dim grey", lw = 0.1, lty = 1, cex=0.5)),
railway = list(file = NULL, tags ="depot" , osm_type = NULL, augmentation = NULL, plot = list(col="pink" ,border="pink", lw = 0.3, lty = 1, cex=0.3)),
depot = list(file = NULL, tags ="depot" , osm_type = NULL, augmentation = NULL, plot = list(col="pink" ,border="pink", lw = 0.2, lty = 1, cex=0.2)),
brownfield = list(file = NULL, tags ="depot" , osm_type = NULL, augmentation = NULL, plot = list(col="pink" ,border="pink", lw = 0.1, lty = 1, cex=0.1))
),
building = list(
residential = list(file = NULL, tags ="residential", osm_type = NULL, augmentation = NULL, plot = list(col="light steel blue",border="white", lw = 0.7, lty = 1, cex=0.5)),
apartments = list(file = NULL, tags ="residential", osm_type = NULL, augmentation = NULL, plot = list(col="light steel blue",border="white", lw = 0.7, lty = 1, cex=0.5)),
house = list(file = NULL, tags ="residential", osm_type = NULL, augmentation = NULL, plot = list(col="light steel blue",border="white", lw = 0.7, lty = 1, cex=0.5)),
commercial = list(file = NULL, tags ="commercial" , osm_type = NULL, augmentation = NULL, plot = list(col="darkolivegreen1", border="darkolivegreen1", lw = 0.7, lty = 1, cex=0.5)),
office = list(file = NULL, tags ="office" , osm_type = NULL, augmentation = NULL, plot = list(col="darkseagreen2", border="darkseagreen2", lw = 0.7, lty = 1, cex=0.5)),
industrial = list(file = NULL, tags ="industrial" , osm_type = NULL, augmentation = NULL, plot = list(col="dim grey",border="dim grey", lw = 0.1, lty = 1, cex=0.5))
),
amenity = list(
parking = list(file = NULL, tags = "parking", osm_type = NULL, augmentation = NULL, plot = list(col="yellow",border="darkgoldenrod3", lw = 0.1, lty = 1, cex=0.5)),
college = list(file = NULL, tags = "school" , osm_type = NULL, augmentation = NULL, plot = list(col="blue" ,border="blue", lw = 0.1, lty = 1, cex=0.5)),
school = list(file = NULL, tags = "school" , osm_type = NULL, augmentation = NULL, plot = list(col="blue" ,border="blue", lw = 0.1, lty = 1, cex=0.5)),
kindergarten = list(file = NULL, tags = "school" , osm_type = NULL, augmentation = NULL, plot = list(col="blue" ,border="blue", lw = 0.1, lty = 1, cex=0.5))
),
waterway = list(
river = list(file = NULL, tags = "water", osm_type = NULL, augmentation = NULL, plot = list(col="lightblue", lw = 0.7, lty = 1, cex=0.5)),
water = list(file = NULL, tags = "water", osm_type = NULL, augmentation = NULL, plot = list(col="lightblue",border="lightblue", lw = 0.7, lty = 1, cex=0.5))
)
)
)
class(res) <- c("ft_object",class(res))
return(res)
}
#' fto_checkosm
#' fto_checkosm
#' @param ft_osmdata ft_osmdata
#' @param tags tags
#' @param osmdir path of osm files
#' @param place_name place_name
#' @keywords osmosm
#' @export
#' @examples
#' \dontrun{
#' fto_checkosm()
#' }
fto_checkosm <- function(ft_osmdata=NULL,
tags=c("roads"),
osmdir,
place_name
){
ft_osmdata_ <- ft_osmdata
res <- lapply(1:length(ft_osmdata),function(x){
lapply(1:length(ft_osmdata[[x]]),function(y){
if (ft_osmdata[[x]][[y]]$tags %in% tags) {
name <- paste0(place_name,"_",names(ft_osmdata)[x] , "_", names(ft_osmdata[[x]])[y],"_sf")
cat("\n ** Targeting ",name, "...\n")
filename <- paste0(name,".rda")
if (file.exists(normalizePath(file.path(osmdir,filename),mustWork = FALSE))) {
cat("\n -> File targeted \n")
ft_osmdata_[[x]][[y]]$file <<- filename
} else {cat("\n -> File does not exist \n")}
}
})
})
return(ft_osmdata_)
}
#' fto_getosm
#' fto_getosm
#' @param ft_osmdata ft_osmdata
#' @param place place
#' @param bbox bbox
#' @param tags tags
#' @param osmdir destination path of osm files
#' @param place_name place_name
#' @param version_r version_r
#' @keywords osmosm
#' @export
#' @examples
#' \dontrun{
#' fto_getosm()
#' }
fto_getosm <- function(ft_osmdata=NULL,
place,
bbox,
tags=c("roads"),
osmdir,
place_name,
version_r=3){
ft_osmdata_ <- ft_osmdata
res <- lapply(1:length(ft_osmdata),function(x){
lapply(1:length(ft_osmdata[[x]]),function(y){
if (ft_osmdata[[x]][[y]]$tags %in% tags) {
name <- paste0(place_name,"_",names(ft_osmdata)[x] , "_", names(ft_osmdata[[x]])[y],"_sf")
expr <- paste0("get_osmdata_sf(place='", place,"', key='", names(ft_osmdata)[x],"' ,value='",names(ft_osmdata[[x]])[y],"')")
cat("\n ** getting ",name, "...\n")
res <- tryCatch({
osm_features <- eval(parse(text=expr))
filename <- paste0(name,".rda")
save(osm_features,file=normalizePath(file.path(osmdir,filename),mustWork = FALSE),version=version_r)
cat("\n -> File saved \n")
ft_osmdata_[[x]][[y]]$file <<- filename
}, error = function(err) {
cat("\n -> Error while getting the file \n")
})
}
})
})
return(ft_osmdata_)
}
#' fto_osmtype
#' fto_osmtype
#' @param ft_osmdata ft_osmdata
#' @param osmdir path of osm files
#' @keywords osmosm
#' @export
#' @examples
#' \dontrun{
#' fto_osmtype()
#' }
fto_osmtype <- function(ft_osmdata=NULL,osmdir){
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()
load(normalizePath(file.path(osmdir,ft_osmdata[[x]][[y]]$file),mustWork = TRUE),env)
tmp <- sapply(c("osm_points", "osm_lines","osm_polygons","osm_multilines","osm_multipolygons"),function(x){
!is.null(get("osm_features",env)[[x]])
})
ft_osmdata_[[x]][[y]]$osm_type <<- names(tmp)[which(tmp)]
rm(env)
}
})
})
return(ft_osmdata_)
}
#' 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)
}
#' fto_transform
#' fto_transform
#' @param ft_osmdata ft_osmdata
#' @param refsys refsysc
#' @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_transform()
#' }
fto_transform <- function(ft_osmdata=NULL,refsys,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]])) {
subset_osm[[z]] <- sf::st_transform(osm_features[[z]],refsys)
}
})
osm_features <- subset_osm
save(osm_features,file=normalizePath(file.path(workdir,filename),mustWork = FALSE),version=version_r)
}
})
})
return(TRUE)
}
#' get_osmdata_sf
#' get_osmdata_sf
#' @param place name of the place
#' @param key name of the key
#' @param value name of the value
#' @keywords osmosm
#' @export
#' @examples
#' \dontrun{
#' get_osmdata_sf()
#' }
get_osmdata_sf <- function(place,key,value){
res <- osmdata::osmdata_sf(osmdata::add_osm_feature(osmdata::opq(bbox = place), key = key,value=value))
return(res)
}
st_subset_coords_modif <- function(x,sb,what,arg){
sb_inverse <- st_coords_modif(sb,what,arg=arg,INVERSE=TRUE)
tmp_ft <- sf::st_intersection(x,sb_inverse,sparse=FALSE)
res <- st_coords_modif(tmp_ft,what=what,arg=arg)
return(res)
}
st_coords_modif <- function(x,what,arg,INVERSE=FALSE){
geom <- sf::st_geometry(x)
geom <- sf::st_set_crs(what(geom = geom, arg = arg, INVERSE = INVERSE), st_crs(x))
if (inherits(x, "sf")) {
res <- sf::st_set_geometry(x, geom)
} else {
res <- geom
}
return(res)
}
rotation <- function(geom,arg,INVERSE=FALSE) {
if (INVERSE) {
angle <- - arg$angle
} else if (!INVERSE) {
angle <- arg$angle
}
rot <- matrix(c(cos(angle), sin(angle), -sin(angle), cos(angle)), 2, 2)
res <- (geom - arg$centroid) * rot + arg$centroid
return(res)
}
reflection <- function(geom,arg,INVERSE=FALSE) {
if (INVERSE) {
angle <- - arg$angle
} else if (!INVERSE) {
angle <- arg$angle
}
lx <- 1*cos(angle)
ly <- 1*sin(angle)
ref <- matrix(c((lx^2-ly^2), (2*lx*ly), (2*lx*ly), (ly^2-lx^2)), 2, 2)
res <- (geom - arg$centroid) * ref + arg$centroid
return(res)
}
#' @importFrom dplyr %>%
NULL
#' @importFrom graphics plot
NULL
.onLoad <- function(libname, pkgname) {
base::require("sf")
invisible()
}
# osmosm
## Description
Quickly get osm data related to a specific place with key features.
- get main features from osm.
- osm augmentation
- easy plot (soon)
## Installation
```R
setwd("~")
download.file("https://git.nilu.no/rgis/osmosm/-/archive/0.0.1/osmosm-0.0.1.tar.gz",destfile="osmosm-0.0.1.tar.gz")
untar("osmosm-0.0.1.tar.gz")
install.packages(file.path("~","osmosm-0.0.1.tar.gz"), repos=NULL, type='source')
unlink(file.path("~/osmosm-0.0.1"),recursive=TRUE)
file.remove(file.path("~/osmosm-0.0.1.tar.gz"))
```
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/ft_init.R
\name{ft_init}
\alias{ft_init}
\title{ft_init
ft_init}
\usage{
ft_init(
place = NULL,
bbox = NULL,
osmdir = NULL,
workdir = tempdir(),
version_r = 3
)
}
\arguments{
\item{place}{place}