geocode_OSM: Geocodes a location using OpenStreetMap Nominatim

Description Usage Arguments Value See Also Examples

View source: R/geocode_OSM.R

Description

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 (http://wiki.openstreetmap.org/wiki/Nominatim_usage_policy).

Usage

1
2
3
geocode_OSM(q, projection = NULL, return.first.only = TRUE,
  details = FALSE, as.data.frame = NA, as.sf = FALSE,
  server = "http://nominatim.openstreetmap.org")

Arguments

q

a character (vector) that specifies a search query. For instance "India" or "CBS Weg 11, Heerlen, Netherlands".

projection

projection in which the coordinates and bounding box are returned. Either a CRS object or a character value. If it is a character, it can either be a PROJ.4 character string or a shortcut. See get_proj4 for a list of shortcut values. By default latitude longitude coordinates.

return.first.only

Only return the first result

details

provide output details, other than the point coordinates and bounding box

as.data.frame

Return the output as a data.frame. If FALSE, a list is returned with at least two items: "coords", a vector containing the coordinates, and "bbox", the corresponding bounding box. By default false, unless q contains multiple queries

as.sf

Return the output as sf object. If TRUE, return.first.only will be set to TRUE.

server

OpenStreetMap Nominatim server name. Could also be a local OSM Nominatim server.

Value

If as.SPDF then a SpatialPointsDataFrame is returned. Else, if as.data.frame, then a data.frame is returned, else a list.

See Also

rev_geocode_OSM, bb

Examples

 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
## Not run: 
if (require(tmap)) {
    geocode_OSM("India")
    geocode_OSM("CBS Weg 1, Heerlen")
    geocode_OSM("CBS Weg 1, Heerlen", projection = "rd")

    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)
    sp::coordinates(five_cities_geocode) <- ~lon+lat

    # 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)

tmaptools documentation built on July 18, 2019, 9:05 a.m.