Add a Scale Bar to 'OpenStreetMap' Plots

Functionality to handle and project lat-long coordinates, easily download background maps and add a correct scale bar to 'OpenStreetMap' plots in any map projection.


OSMscale is an R package to easily handle and project lat-long coordinates, download background maps and add a correct scale bar to 'OpenStreetMap' plots in any map projection. There are some other spatially related miscellaneous functions as well.

installation

OSMscale is available on CRAN: CRAN_Status_Badge downloads Rdoc

It relies on OpenStreetMap to do the actual work, thus rgdal and rjava must be available.

  • On Windows: Check if Java is available. There should be no errors when running install.packages("rJava") ; library(rJava) in R. If necessary, install Java in the same bit-version as R (eg 64bit). The Java binary file must be on the search path, which will normally happen automatically.

  • On Linux: open a terminal (CTRL+ALT+T) and paste (CTRL+SHIFT+V) the following line by line to install gdal and rJava:

sudo apt update
sudo apt install libgdal-dev libproj-dev
sudo apt-get install r-cran-rjava
R
install.packages("rgdal")
library("rgdal"); library("rJava") # should not return errors
q("no") # to quit R
  • Now actually install OSMscale from within R:
install.packages("OSMscale") 
library(OSMscale)
?OSMscale
 
# To update to the most recent development version:
berryFunctions::instGit("brry/berryFunctions")
berryFunctions::instGit("brry/OSMscale")

basic usage

Assuming a data.frame with lat-long coordinates:

d <- read.table(sep=",", header=TRUE, text=
"lat, long # could e.g. be copied from googleMaps, rightclick on What's here?
55.685143, 12.580008
52.514464, 13.350137
50.106452, 14.419989
48.847003, 2.337213
51.505364, -0.164752")
png("ExampleMap.png", width=4, height=3, units="in", res=150)
 
map <- pointsMap(lat, long, data=d, type="maptoolkit-topo", proj=putm(d$long), scale=FALSE)
scaleBar(map, abslen=500, y=0.8, cex=0.8)
lines(projectPoints(d$lat, d$long), col="blue", lwd=3)
points(projectPoints(52.386609, 4.877008, to=putm(zone=32)), cex=3, lwd=2, col="purple")
 
dev.off()

ExampleMap

trouble

If direct installation doesn't work, your R version might be too old. In that case, an update is really recommendable: r-project.org. If you can't update R, try installing from source (github) via instGit as mentioned above. If that's not possible either, you might be able to source some functions from the package zip folder

Vectorize(source)(dir("path/you/unzipped/to/OSMscale-master/R", full=T))

This creates all R functions as objects in your globalenv workspace (and overwrites existing objects of the same name!).

News

package OSMscale: project points and add a scalebar to OpenStreetMap plots. Feedback, ideas, code suggestions welcome at [email protected] or via github.com/brry/OSMscale



Version 0.5.0 (2017-04-12)



Version 0.4.1 (2017-01-19)



Version 0.3.5 (2016-09-21)

  • do.call(scaleBar, ...) computing time in pointsMap removed
  • API unified to function(lat, long, data, ...)
  • scaleBar: automatic abslen and ndiv selection greatly improved
  • tracing of errors is simplified through berryFunctions::traceCall()
  • pointsMap can now handle a single coordinate location
  • projectPoints can now return the spTransform output
  • documentation and a few defaults have been improved


Version 0.2.9 (2016-08-21) first release on CRAN Detailed changes at https://github.com/brry/OSMscale/commits/master

  • documentation (examples + github readme) expanded + improved

  • scaleBar automatic length selection improved and abslen unit bug corected

  • projectPoints can now handle datasets with NAs

  • pointsMap: argument scale can now be a list of arguments

  • new functions: degree, earthDist, checkLL

  • new projection functions: putm, posm, pll

  • moved here from berryFunctions: equidistPoints, randomPoints, triangleArea



Version 0.2.1 (2016-07-27) second stable release

  • suppress warning from openmap
  • scalebar type="bar" added
  • argument rearranged and names changed sometimes
  • now prints progress messages


Version 0.1.7 (2016-06-21) first stable release with 3 functions: pointsMap, projectPoints, scaleBar Can download openstreetmap background map for a data.frame with points, project those (and the map itself) to UTM (or any other projection), draws accurate scalebars even in the default OSM mercator projection (currently minimalistic in design)

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("OSMscale")

0.5.1 by Berry Boessenkool, 2 years ago


https://github.com/brry/OSMscale


Browse source code at https://github.com/cran/OSMscale


Authors: Berry Boessenkool


Documentation:   PDF Manual  


GPL (>= 2) license


Imports OpenStreetMap, berryFunctions, sp

Suggests testthat


Suggested by rdwd.


See at CRAN