Title: | Thematic Map Tools |
---|---|
Description: | Set of tools for reading and processing spatial data. The aim is to supply the workflow to create thematic maps. This package also facilitates 'tmap', the package for visualizing thematic maps. |
Authors: | Martijn Tennekes [aut, cre] |
Maintainer: | Martijn Tennekes <[email protected]> |
License: | GPL-3 |
Version: | 3.1-1 |
Built: | 2024-11-11 05:16:16 UTC |
Source: | https://github.com/r-tmap/tmaptools |
This package offers a set of handy tool functions for reading and processing spatial data. The aim of these functions is to supply the workflow to create thematic maps, e.g. read shape files, set map projections, append data, calculate areas and distances, and query OpenStreetMap. The visualization of thematic maps can be done with the tmap package.
This page provides a brief overview of all package functions.
approx_areas
|
Approximate area sizes of polygons |
approx_distances
|
Approximate distances |
bb
|
Create, extract or modify a bounding box |
bb_poly
|
Convert bounding box to a polygon |
get_asp_ratio
|
Get the aspect ratio of a shape object |
--------------------------- | --------------------------------------------------------------------------------------------------- |
get_brewer_pal
|
Get and plot a (modified) Color Brewer palette |
map_coloring
|
Find different colors for adjacent polygons |
palette_explorer
|
Explore Color Brewer palettes |
--------------------------- | --------------------------------------------------------------------------------------------------- |
crop_shape
|
Crop shape objects |
simplify_shape
|
Simplify a shape |
--------------------------- | --------------------------------------------------------------------------------------------------- |
geocode_OSM
|
Get a location from an address description |
read_GPX
|
Read a GPX file |
read_osm
|
Read Open Street Map data |
rev_geocode_OSM
|
Get an address description from a location |
--------------------------- | --------------------------------------------------------------------------------------------------- |
Martijn Tennekes [email protected]
The pipe operator from magrittr, %>%
, can also be used in functions from tmaptools
.
lhs |
Left-hand side |
rhs |
Right-hand side |
Approximate the area sizes of the polygons in real-world area units (such as sq km or sq mi), proportional numbers, or normalized numbers. Also, the areas can be calibrated to a prespecified area total. This function is a convenient wrapper around st_area
.
approx_areas(shp, target = "metric", total.area = NULL)
approx_areas(shp, target = "metric", total.area = NULL)
shp |
shape object, i.e., an |
target |
target unit, one of
These units are the output units. See |
total.area |
total area size of |
Note that the method of determining areas is an approximation, since it depends on the used projection and the level of detail of the shape object. Projections with equal-area property are highly recommended. See https://en.wikipedia.org/wiki/List_of_map_projections for equal area world map projections.
Numeric vector of area sizes (class units
).
if (require(tmap) && packageVersion("tmap") >= "2.0") { data(NLD_muni) NLD_muni$area <- approx_areas(NLD_muni, total.area = 33893) tm_shape(NLD_muni) + tm_bubbles(size="area", title.size=expression("Area in " * km^2)) # function that returns min, max, mean and sum of area values summary_areas <- function(x) { list(min_area=min(x), max_area=max(x), mean_area=mean(x), sum_area=sum(x)) } # area of the polygons approx_areas(NLD_muni) %>% summary_areas() # area of the polygons, adjusted corrected for a specified total area size approx_areas(NLD_muni, total.area=33893) %>% summary_areas() # proportional area of the polygons approx_areas(NLD_muni, target = "prop") %>% summary_areas() # area in squared miles approx_areas(NLD_muni, target = "mi mi") %>% summary_areas() # area of the polygons when unprojected approx_areas(NLD_muni %>% sf::st_transform(crs = 4326)) %>% summary_areas() }
if (require(tmap) && packageVersion("tmap") >= "2.0") { data(NLD_muni) NLD_muni$area <- approx_areas(NLD_muni, total.area = 33893) tm_shape(NLD_muni) + tm_bubbles(size="area", title.size=expression("Area in " * km^2)) # function that returns min, max, mean and sum of area values summary_areas <- function(x) { list(min_area=min(x), max_area=max(x), mean_area=mean(x), sum_area=sum(x)) } # area of the polygons approx_areas(NLD_muni) %>% summary_areas() # area of the polygons, adjusted corrected for a specified total area size approx_areas(NLD_muni, total.area=33893) %>% summary_areas() # proportional area of the polygons approx_areas(NLD_muni, target = "prop") %>% summary_areas() # area in squared miles approx_areas(NLD_muni, target = "mi mi") %>% summary_areas() # area of the polygons when unprojected approx_areas(NLD_muni %>% sf::st_transform(crs = 4326)) %>% summary_areas() }
Approximate distances between two points or across the horizontal and vertical centerlines of a bounding box.
approx_distances(x, y = NULL, projection = NULL, target = NULL)
approx_distances(x, y = NULL, projection = NULL, target = NULL)
x |
object that can be coerced to a bounding box with |
y |
a pair of coordintes, vector of two. Only required when |
projection |
projection code, needed in case |
target |
target unit, one of: |
If y
is specifyed, a list of two: unit and dist. Else, a list of three: unit, hdist (horizontal distance) and vdist (vertical distance).
## Not run: if (require(tmap)) { data(NLD_prov) # North-South and East-West distances of the Netherlands approx_distances(NLD_prov) # Distance between Maastricht and Groningen p_maastricht <- geocode_OSM("Maastricht")$coords p_groningen <- geocode_OSM("Groningen")$coords approx_distances(p_maastricht, p_groningen, projection = 4326, target = "km") # Check distances in several projections sapply(c(3035, 28992, 4326), function(projection) { p_maastricht <- geocode_OSM("Maastricht", projection = projection)$coords p_groningen <- geocode_OSM("Groningen", projection = projection)$coords approx_distances(p_maastricht, p_groningen, projection = projection) }) } ## End(Not run)
## Not run: if (require(tmap)) { data(NLD_prov) # North-South and East-West distances of the Netherlands approx_distances(NLD_prov) # Distance between Maastricht and Groningen p_maastricht <- geocode_OSM("Maastricht")$coords p_groningen <- geocode_OSM("Groningen")$coords approx_distances(p_maastricht, p_groningen, projection = 4326, target = "km") # Check distances in several projections sapply(c(3035, 28992, 4326), function(projection) { p_maastricht <- geocode_OSM("Maastricht", projection = projection)$coords p_groningen <- geocode_OSM("Groningen", projection = projection)$coords approx_distances(p_maastricht, p_groningen, projection = projection) }) } ## End(Not run)
Swiss army knife for bounding boxes. Modify an existing bounding box or create a new bounding box from scratch. See details.
bb( x = NA, ext = NULL, cx = NULL, cy = NULL, width = NULL, height = NULL, xlim = NULL, ylim = NULL, relative = FALSE, asp.limit = NULL, current.projection = NULL, projection = NULL, output = c("bbox", "matrix", "extent") )
bb( x = NA, ext = NULL, cx = NULL, cy = NULL, width = NULL, height = NULL, xlim = NULL, ylim = NULL, relative = FALSE, asp.limit = NULL, current.projection = NULL, projection = NULL, output = c("bbox", "matrix", "extent") )
x |
One of the following:
If |
ext |
Extension factor of the bounding box. If 1, the bounding box is unchanged. Values smaller than 1 reduces the bounding box, and values larger than 1 enlarges the bounding box. This argument is a shortcut for both |
cx |
center x coordinate |
cy |
center y coordinate |
width |
width of the bounding box. These are either absolute or relative (depending on the argument |
height |
height of the bounding box. These are either absolute or relative (depending on the argument |
xlim |
limits of the x-axis. These are either absolute or relative (depending on the argument |
ylim |
limits of the y-axis. See |
relative |
boolean that determines whether relative values are used for |
asp.limit |
maximum aspect ratio, which is width/height. Number greater than or equal to 1. For landscape bounding boxes, |
current.projection |
projection that corresponds to the bounding box specified by |
projection |
projection to transform the bounding box to. |
output |
output format of the bounding box, one of:
|
An existing bounding box (defined by x
) can be modified as follows:
Using the extension factor ext
.
Changing the width and height with width
and height
. The argument relavitve
determines whether relative or absolute values are used.
Setting the x and y limits. The argument relavitve
determines whether relative or absolute values are used.
A new bounding box can be created from scratch as follows:
Using the extension factor ext
.
Setting the center coorinates cx
and cy
, together with the width
and height
.
Setting the x and y limits xlim
and ylim
bounding box (see argument output
)
if (require(tmap) && packageVersion("tmap") >= "2.0") { ## load shapes data(NLD_muni) data(World) ## get bounding box (similar to sp's function bbox) bb(NLD_muni) ## extent it by factor 1.10 bb(NLD_muni, ext=1.10) ## convert to longlat bb(NLD_muni, projection=4326) ## change existing bounding box bb(NLD_muni, ext=1.5) bb(NLD_muni, width=2, relative = TRUE) bb(NLD_muni, xlim=c(.25, .75), ylim=c(.25, .75), relative = TRUE) } ## Not run: if (require(tmap)) { bb("Limburg", projection = "rd") bb_italy <- bb("Italy", projection = "eck4") tm_shape(World, bbox=bb_italy) + tm_polygons() # shorter alternative: tm_shape(World, bbox="Italy") + tm_polygons() } ## End(Not run)
if (require(tmap) && packageVersion("tmap") >= "2.0") { ## load shapes data(NLD_muni) data(World) ## get bounding box (similar to sp's function bbox) bb(NLD_muni) ## extent it by factor 1.10 bb(NLD_muni, ext=1.10) ## convert to longlat bb(NLD_muni, projection=4326) ## change existing bounding box bb(NLD_muni, ext=1.5) bb(NLD_muni, width=2, relative = TRUE) bb(NLD_muni, xlim=c(.25, .75), ylim=c(.25, .75), relative = TRUE) } ## Not run: if (require(tmap)) { bb("Limburg", projection = "rd") bb_italy <- bb("Italy", projection = "eck4") tm_shape(World, bbox=bb_italy) + tm_polygons() # shorter alternative: tm_shape(World, bbox="Italy") + tm_polygons() } ## End(Not run)
Convert bounding box to a spatial (sfc
) object . Useful for plotting (see example). The function bb_earth
returns a spatial polygon of the 'boundaries' of the earth, which can also be done in other projections (if a feasible solution exists).
bb_poly(x, steps = 100, stepsize = NA, projection = NULL) bb_earth( projection = NULL, stepsize = 1, earth.datum = 4326, bbx = c(-180, -90, 180, 90), buffer = 1e-06 )
bb_poly(x, steps = 100, stepsize = NA, projection = NULL) bb_earth( projection = NULL, stepsize = 1, earth.datum = 4326, bbx = c(-180, -90, 180, 90), buffer = 1e-06 )
x |
object that can be coerced to a bounding box with |
steps |
number of intermediate points along the shortest edge of the bounding box. The number of intermediate points along the longest edge scales with the aspect ratio. These intermediate points are needed if the bounding box is plotted in another projection. |
stepsize |
stepsize in terms of coordinates (usually meters when the shape is projected and degrees of longlat coordinates are used). If specified, it overrules |
projection |
projection in which the coordinates of |
earth.datum |
Geodetic datum to determine the earth boundary. By default EPSG 4326. |
bbx |
boundig box of the earth in a vector of 4 values: min longitude, max longitude, min latitude, max latitude. By default |
buffer |
In order to determine feasible earth bounding boxes in other projections, a buffer is used to decrease the bounding box by a small margin (default |
sfc
object
if (require(tmap) && packageVersion("tmap") >= "2.0") { data(NLD_muni) current.mode <- tmap_mode("view") qtm(bb_poly(NLD_muni)) # restore mode tmap_mode(current.mode) }
if (require(tmap) && packageVersion("tmap") >= "2.0") { data(NLD_muni) current.mode <- tmap_mode("view") qtm(bb_poly(NLD_muni)) # restore mode tmap_mode(current.mode) }
Transpose quantitative variables to densitiy variables, which are often needed for choroplets. For example, the colors of a population density map should correspond population density counts rather than absolute population numbers.
calc_densities( shp, var, target = "metric", total.area = NULL, suffix = NA, drop = TRUE )
calc_densities( shp, var, target = "metric", total.area = NULL, suffix = NA, drop = TRUE )
shp |
a shape object, i.e., an |
var |
name(s) of a qualtity variable name contained in the |
target |
the target unit, see |
total.area |
total area size of |
suffix |
character that is appended to the variable names. The resulting names are used as column names of the returned data.frame. By default, |
drop |
boolean that determines whether an one-column data-frame should be returned as a vector |
Vector or data.frame (depending on whether length(var)==1
with density values.
if (require(tmap) && packageVersion("tmap") >= "2.0") { data(NLD_muni) NLD_muni_pop_per_km2 <- calc_densities(NLD_muni, target = "km km", var = c("pop_men", "pop_women")) NLD_muni <- sf::st_sf(data.frame(NLD_muni, NLD_muni_pop_per_km2)) tm_shape(NLD_muni) + tm_polygons(c("pop_men_km.2", "pop_women_km.2"), title=expression("Population per " * km^2), style="quantile") + tm_facets(free.scales = FALSE) + tm_layout(panel.show = TRUE, panel.labels=c("Men", "Women")) }
if (require(tmap) && packageVersion("tmap") >= "2.0") { data(NLD_muni) NLD_muni_pop_per_km2 <- calc_densities(NLD_muni, target = "km km", var = c("pop_men", "pop_women")) NLD_muni <- sf::st_sf(data.frame(NLD_muni, NLD_muni_pop_per_km2)) tm_shape(NLD_muni) + tm_polygons(c("pop_men_km.2", "pop_women_km.2"), title=expression("Population per " * km^2), style="quantile") + tm_facets(free.scales = FALSE) + tm_layout(panel.show = TRUE, panel.labels=c("Men", "Women")) }
Crop a shape object (from class sf
, stars
, sp
, or raster
). A shape file x
is cropped, either by the bounding box of another shape y
, or by y
itself if it is a SpatialPolygons object and polygon = TRUE
.
crop_shape(x, y, polygon = FALSE, ...)
crop_shape(x, y, polygon = FALSE, ...)
x |
shape object, i.e. an object from class |
y |
bounding box, an |
polygon |
should |
... |
not used anymore |
This function is similar to crop
from the raster
package. The main difference is that crop_shape
also allows to crop using a polygon instead of a rectangle.
cropped shape, in the same class as x
if (require(tmap) && packageVersion("tmap") >= "2.0") { data(World, NLD_muni, land, metro) #land_NLD <- crop_shape(land, NLD_muni) #qtm(land_NLD, raster="trees", style="natural") metro_Europe <- crop_shape(metro, World[World$continent == "Europe", ], polygon = TRUE) qtm(World) + tm_shape(metro_Europe) + tm_bubbles("pop2010", col="red", title.size="European cities") + tm_legend(frame=TRUE) }
if (require(tmap) && packageVersion("tmap") >= "2.0") { data(World, NLD_muni, land, metro) #land_NLD <- crop_shape(land, NLD_muni) #qtm(land_NLD, raster="trees", style="natural") metro_Europe <- crop_shape(metro, World[World$continent == "Europe", ], polygon = TRUE) qtm(World) + tm_shape(metro_Europe) + tm_bubbles("pop2010", col="red", title.size="European cities") + tm_legend(frame=TRUE) }
Geocodes a location (based on a search query) to coordinates and a bounding box. Similar to geocode from the ggmap package. It uses OpenStreetMap Nominatim. For processing large amount of queries, please read the usage policy (https://operations.osmfoundation.org/policies/nominatim/).
geocode_OSM( q, projection = NULL, return.first.only = TRUE, keep.unfound = FALSE, details = FALSE, as.data.frame = NA, as.sf = FALSE, geometry = c("point", "bbox"), server = "https://nominatim.openstreetmap.org" )
geocode_OSM( q, projection = NULL, return.first.only = TRUE, keep.unfound = FALSE, details = FALSE, as.data.frame = NA, as.sf = FALSE, geometry = c("point", "bbox"), server = "https://nominatim.openstreetmap.org" )
q |
a character (vector) that specifies a search query. For instance |
projection |
projection in which the coordinates and bounding box are returned. See |
return.first.only |
Only return the first result |
keep.unfound |
Keep list items / data.frame rows with |
details |
provide output details, other than the point coordinates and bounding box |
as.data.frame |
Return the output as a |
as.sf |
Return the output as |
geometry |
When |
server |
OpenStreetMap Nominatim server name. Could also be a local OSM Nominatim server. |
If as.sf
then a sf
object is returned. Else, if as.data.frame
, then a data.frame
is returned, else a list.
## Not run: if (require(tmap)) { geocode_OSM("India") geocode_OSM("CBS Weg 1, Heerlen") geocode_OSM("CBS Weg 1, Heerlen", projection = 28992) data(metro) # sample 5 cities from the metro dataset five_cities <- metro[sample(length(metro), 5), ] # obtain geocode locations from their long names five_cities_geocode <- geocode_OSM(five_cities$name_long, as.sf = TRUE) # change to interactive mode current.mode <- tmap_mode("view") # plot metro coordinates in red and geocode coordinates in blue # zoom in to see the differences tm_shape(five_cities) + tm_dots(col = "blue") + tm_shape(five_cities_geocode) + tm_dots(col = "red") # restore current mode tmap_mode(current.mode) } ## End(Not run)
## Not run: if (require(tmap)) { geocode_OSM("India") geocode_OSM("CBS Weg 1, Heerlen") geocode_OSM("CBS Weg 1, Heerlen", projection = 28992) data(metro) # sample 5 cities from the metro dataset five_cities <- metro[sample(length(metro), 5), ] # obtain geocode locations from their long names five_cities_geocode <- geocode_OSM(five_cities$name_long, as.sf = TRUE) # change to interactive mode current.mode <- tmap_mode("view") # plot metro coordinates in red and geocode coordinates in blue # zoom in to see the differences tm_shape(five_cities) + tm_dots(col = "blue") + tm_shape(five_cities_geocode) + tm_dots(col = "red") # restore current mode tmap_mode(current.mode) } ## End(Not run)
Get the aspect ratio of a shape object, a tmap
object, or a bounding box
get_asp_ratio(x, is.projected = NA, width = 700, height = 700, res = 100)
get_asp_ratio(x, is.projected = NA, width = 700, height = 700, res = 100)
x |
A shape from class |
is.projected |
Logical that determined wether the coordinates of |
width |
See details; only applicable if |
height |
See details; only applicable if |
res |
See details; only applicable if |
The arguments width
, height
, and res
are passed on to png
. If x
is a tmap object, a temporarily png image is created to calculate the aspect ratio of a tmap object. The default size of this image is 700 by 700 pixels at 100 dpi.
aspect ratio
if (require(tmap) && packageVersion("tmap") >= "2.0") { data(World) get_asp_ratio(World) get_asp_ratio(bb(World)) tm <- qtm(World) get_asp_ratio(tm) } ## Not run: get_asp_ratio("Germany") #note: bb("Germany") uses geocode_OSM("Germany") ## End(Not run)
if (require(tmap) && packageVersion("tmap") >= "2.0") { data(World) get_asp_ratio(World) get_asp_ratio(bb(World)) tm <- qtm(World) get_asp_ratio(tm) } ## Not run: get_asp_ratio("Germany") #note: bb("Germany") uses geocode_OSM("Germany") ## End(Not run)
Get and plot a (modified) palette from Color Brewer. In addition to the base function brewer.pal
, a palette can be created for any number of classes. The contrast of the palette can be adjusted for sequential and diverging palettes. For categorical palettes, intermediate colors can be generated. An interactive tool that uses this function is palette_explorer
.
get_brewer_pal(palette, n = 5, contrast = NA, stretch = TRUE, plot = TRUE)
get_brewer_pal(palette, n = 5, contrast = NA, stretch = TRUE, plot = TRUE)
palette |
name of the color brewer palette. Run |
n |
number of colors |
contrast |
a vector of two numbers between 0 and 1 that defines the contrast range of the palette. Applicable to sequential and diverging palettes. For sequential palettes, 0 stands for the leftmost color and 1 the rightmost color. For instance, when |
stretch |
logical that determines whether intermediate colors are used for a categorical palette when |
plot |
should the palette be plot, or only returned? If |
The default contrast of the palette depends on the number of colors, n
, in the following way. The default contrast is maximal, so (0, 1)
, when n = 9
for sequential palettes and n = 11
for diverging palettes. The default contrast values for smaller values of n
can be extracted with some R magic: sapply(1:9, tmaptools:::default_contrast_seq)
for sequential palettes and sapply(1:11, tmaptools:::default_contrast_div)
for diverging palettes.
vector of color values. It is silently returned when plot=TRUE
.
get_brewer_pal("Blues") get_brewer_pal("Blues", contrast=c(.4, .8)) get_brewer_pal("Blues", contrast=c(0, 1)) get_brewer_pal("Blues", n=15, contrast=c(0, 1)) get_brewer_pal("RdYlGn") get_brewer_pal("RdYlGn", n=11) get_brewer_pal("RdYlGn", n=11, contrast=c(0, .4)) get_brewer_pal("RdYlGn", n=11, contrast=c(.4, 1)) get_brewer_pal("Set2", n = 12) get_brewer_pal("Set2", n = 12, stretch = FALSE)
get_brewer_pal("Blues") get_brewer_pal("Blues", contrast=c(.4, .8)) get_brewer_pal("Blues", contrast=c(0, 1)) get_brewer_pal("Blues", n=15, contrast=c(0, 1)) get_brewer_pal("RdYlGn") get_brewer_pal("RdYlGn", n=11) get_brewer_pal("RdYlGn", n=11, contrast=c(0, .4)) get_brewer_pal("RdYlGn", n=11, contrast=c(.4, 1)) get_brewer_pal("Set2", n = 12) get_brewer_pal("Set2", n = 12, stretch = FALSE)
Get neighbours list from spatial objects. The output is similar to the function poly2nb
of the spdep
package, but uses sf
instead of sp
.
get_neighbours(x)
get_neighbours(x)
x |
a shape object, i.e., a |
A list where the items correspond to the features. Each item is a vector of neighbours.
Color the polygons of a map such that adjacent polygons have different colors
map_coloring( x, algorithm = "greedy", ncols = NA, minimize = FALSE, palette = NULL, contrast = 1 )
map_coloring( x, algorithm = "greedy", ncols = NA, minimize = FALSE, palette = NULL, contrast = 1 )
x |
Either a shape (i.e. a |
algorithm |
currently, only "greedy" is implemented. |
ncols |
number of colors. By default it is 8 when |
minimize |
logical that determines whether |
palette |
color palette. |
contrast |
vector of two numbers that determine the range that is used for sequential and diverging palettes (applicable when |
If palette
is defined, a vector of colors is returned, otherwise a vector of color indices.
if (require(tmap) && packageVersion("tmap") >= "2.0") { data(World, metro) World$color <- map_coloring(World, palette="Pastel2") qtm(World, fill = "color") # map_coloring used indirectly: qtm(World, fill = "MAP_COLORS") data(NLD_prov, NLD_muni) tm_shape(NLD_prov) + tm_fill("name", legend.show = FALSE) + tm_shape(NLD_muni) + tm_polygons("MAP_COLORS", palette="Greys", alpha = .25) + tm_shape(NLD_prov) + tm_borders(lwd=2) + tm_text("name", shadow=TRUE) + tm_format("NLD", title="Dutch provinces and\nmunicipalities", bg.color="white") }
if (require(tmap) && packageVersion("tmap") >= "2.0") { data(World, metro) World$color <- map_coloring(World, palette="Pastel2") qtm(World, fill = "color") # map_coloring used indirectly: qtm(World, fill = "MAP_COLORS") data(NLD_prov, NLD_muni) tm_shape(NLD_prov) + tm_fill("name", legend.show = FALSE) + tm_shape(NLD_muni) + tm_polygons("MAP_COLORS", palette="Greys", alpha = .25) + tm_shape(NLD_prov) + tm_borders(lwd=2) + tm_text("name", shadow=TRUE) + tm_format("NLD", title="Dutch provinces and\nmunicipalities", bg.color="white") }
palette_explorer()
starts an interactive tool shows all Color Brewer and viridis palettes, where the number of colors can be adjusted as well as the constrast range. Categorical (qualitative) palettes can be stretched when the number of colors exceeds the number of palette colors. Output code needed to get the desired color values is generated. Finally, all colors can be tested for color blindness. The data.frame tmap.pal.info
is similar to brewer.pal.info
, but extended with the color palettes from viridis.
palette_explorer() tmap.pal.info
palette_explorer() tmap.pal.info
An object of class data.frame
with 40 rows and 4 columns.
https://www.color-blindness.com/types-of-color-blindness/
get_brewer_pal
, dichromat
, RColorBrewer
## Not run: if (require(shiny) && require(shinyjs)) { palette_explorer() } ## End(Not run)
## Not run: if (require(shiny) && require(shinyjs)) { palette_explorer() } ## End(Not run)
Read a GPX file. By default, it reads all possible GPX layers, and only returns shapes for layers that have any features.
read_GPX( file, layers = c("waypoints", "routes", "tracks", "route_points", "track_points"), remove.empty.layers = TRUE, as.sf = TRUE )
read_GPX( file, layers = c("waypoints", "routes", "tracks", "route_points", "track_points"), remove.empty.layers = TRUE, as.sf = TRUE )
file |
a GPX filename (including directory) |
layers |
vector of GPX layers. Possible options are |
remove.empty.layers |
should empty layers (i.e. with 0 features) be removed from the list? |
as.sf |
not used anymore |
Note that this function returns sf
objects, but still uses methods from sp and rgdal internally.
a list of sf objects, one for each layer
Read Open Street Map data. OSM tiles are read and returned as a spatial raster. Vectorized OSM data is not supported anymore (see details).
read_osm( x, zoom = NULL, type = "osm", minNumTiles = NULL, mergeTiles = NULL, use.colortable = FALSE, ... )
read_osm( x, zoom = NULL, type = "osm", minNumTiles = NULL, mergeTiles = NULL, use.colortable = FALSE, ... )
x |
object that can be coerced to a bounding box with |
zoom |
passed on to |
type |
tile provider, by default |
minNumTiles |
passed on to |
mergeTiles |
passed on to |
use.colortable |
should the colors of the returned raster object be stored in a |
... |
arguments passed on to |
As of version 2.0, read_osm
cannot be used to read vectorized OSM data anymore. The reason is that the package that was used under the hood, osmar
, has some limitations and is not actively maintained anymore. Therefore, we recommend the package osmdata
. Since this package is very user-friendly, there was no reason to use read_osm
as a wrapper for reading vectorized OSM data.
The output of read_osm
is a raster
object.
## Not run: if (require(tmap)) { #### Choropleth with OSM background # load Netherlands shape data(NLD_muni) # read OSM raster data osm_NLD <- read_osm(NLD_muni, ext=1.1) # plot with regular tmap functions tm_shape(osm_NLD) + tm_rgb() + tm_shape(NLD_muni) + tm_polygons("population", convert2density=TRUE, style="kmeans", alpha=.7, palette="Purples") #### A close look at the building of Statistics Netherlands in Heerlen # create a bounding box around the CBS (Statistics Netherlands) building CBS_bb <- bb("CBS Weg 11, Heerlen", width=.003, height=.002) # read Microsoft Bing satellite and OpenCycleMap OSM layers CBS_osm1 <- read_osm(CBS_bb, type="bing") CBS_osm2 <- read_osm(CBS_bb, type="opencyclemap") # plot OSM raster data qtm(CBS_osm1) qtm(CBS_osm2) } ## End(Not run)
## Not run: if (require(tmap)) { #### Choropleth with OSM background # load Netherlands shape data(NLD_muni) # read OSM raster data osm_NLD <- read_osm(NLD_muni, ext=1.1) # plot with regular tmap functions tm_shape(osm_NLD) + tm_rgb() + tm_shape(NLD_muni) + tm_polygons("population", convert2density=TRUE, style="kmeans", alpha=.7, palette="Purples") #### A close look at the building of Statistics Netherlands in Heerlen # create a bounding box around the CBS (Statistics Netherlands) building CBS_bb <- bb("CBS Weg 11, Heerlen", width=.003, height=.002) # read Microsoft Bing satellite and OpenCycleMap OSM layers CBS_osm1 <- read_osm(CBS_bb, type="bing") CBS_osm2 <- read_osm(CBS_bb, type="opencyclemap") # plot OSM raster data qtm(CBS_osm1) qtm(CBS_osm2) } ## End(Not run)
Reverse geocodes a location (based on spatial coordinates) to an address. It uses OpenStreetMap Nominatim. For processing large amount of queries, please read the usage policy (https://operations.osmfoundation.org/policies/nominatim/).
rev_geocode_OSM( x, y = NULL, zoom = NULL, projection = 4326, as.data.frame = NA, server = "https://nominatim.openstreetmap.org" )
rev_geocode_OSM( x, y = NULL, zoom = NULL, projection = 4326, as.data.frame = NA, server = "https://nominatim.openstreetmap.org" )
x |
x coordinate(s), or a spatial points object ( |
y |
y coordinate(s) |
zoom |
zoom level |
projection |
projection in which the coordinates |
as.data.frame |
return as data.frame ( |
server |
OpenStreetMap Nominatim server name. Could also be a local OSM Nominatim server. |
A data frame or a list with all attributes that are contained in the search result
## Not run: if (require(tmap)) { data(metro) # sample five cities from metro dataset set.seed(1234) five_cities <- metro[sample(length(metro), 5), ] # obtain reverse geocode address information addresses <- rev_geocode_OSM(five_cities, zoom = 6) five_cities <- sf::st_sf(data.frame(five_cities, addresses)) # change to interactive mode current.mode <- tmap_mode("view") tm_shape(five_cities) + tm_markers(text="name") # restore current mode tmap_mode(current.mode) } ## End(Not run)
## Not run: if (require(tmap)) { data(metro) # sample five cities from metro dataset set.seed(1234) five_cities <- metro[sample(length(metro), 5), ] # obtain reverse geocode address information addresses <- rev_geocode_OSM(five_cities, zoom = 6) five_cities <- sf::st_sf(data.frame(five_cities, addresses)) # change to interactive mode current.mode <- tmap_mode("view") tm_shape(five_cities) + tm_markers(text="name") # restore current mode tmap_mode(current.mode) } ## End(Not run)
Simplify a shape consisting of polygons or lines. This can be useful for shapes that are too detailed for visualization, especially along natural borders such as coastlines and rivers. The number of coordinates is reduced.
simplify_shape(shp, fact = 0.1, keep.units = FALSE, keep.subunits = FALSE, ...)
simplify_shape(shp, fact = 0.1, keep.units = FALSE, keep.subunits = FALSE, ...)
shp |
|
fact |
simplification factor, number between 0 and 1 (default is 0.1) |
keep.units |
prevent small polygon features from disappearing at high simplification (default FALSE) |
keep.subunits |
should multipart polygons be converted to singlepart polygons? This prevents small shapes from disappearing during simplification if keep.units = TRUE. Default FALSE |
... |
other arguments passed on to the underlying function |
This function is a wrapper of ms_simplify
. In addition, the data is preserved. Also sf
objects are supported.
sf
object
## Not run: if (require(tmap)) { data(World) # show different simplification factors tm1 <- qtm(World %>% simplify_shape(fact = 0.05), title="Simplify 0.05") tm2 <- qtm(World %>% simplify_shape(fact = 0.1), title="Simplify 0.1") tm3 <- qtm(World %>% simplify_shape(fact = 0.2), title="Simplify 0.2") tm4 <- qtm(World %>% simplify_shape(fact = 0.5), title="Simplify 0.5") tmap_arrange(tm1, tm2, tm3, tm4) # show different options for keeping smaller (sub)units tm5 <- qtm(World %>% simplify_shape(keep.units = TRUE, keep.subunits = TRUE), title="Keep units and subunits") tm6 <- qtm(World %>% simplify_shape(keep.units = TRUE, keep.subunits = FALSE), title="Keep units, ignore small subunits") tm7 <- qtm(World %>% simplify_shape(keep.units = FALSE), title="Ignore small units and subunits") tmap_arrange(tm5, tm6, tm7) } ## End(Not run)
## Not run: if (require(tmap)) { data(World) # show different simplification factors tm1 <- qtm(World %>% simplify_shape(fact = 0.05), title="Simplify 0.05") tm2 <- qtm(World %>% simplify_shape(fact = 0.1), title="Simplify 0.1") tm3 <- qtm(World %>% simplify_shape(fact = 0.2), title="Simplify 0.2") tm4 <- qtm(World %>% simplify_shape(fact = 0.5), title="Simplify 0.5") tmap_arrange(tm1, tm2, tm3, tm4) # show different options for keeping smaller (sub)units tm5 <- qtm(World %>% simplify_shape(keep.units = TRUE, keep.subunits = TRUE), title="Keep units and subunits") tm6 <- qtm(World %>% simplify_shape(keep.units = TRUE, keep.subunits = FALSE), title="Keep units, ignore small subunits") tm7 <- qtm(World %>% simplify_shape(keep.units = FALSE), title="Ignore small units and subunits") tmap_arrange(tm5, tm6, tm7) } ## End(Not run)