# smartmap

    library(smartmap)

smartmap produces interactive maps from a variety of R objects with minimal coding required. This makes it great for previewing spatial data. It provides only three functions:

- `smap()`: The core function. Smartly create leaflet maps from R objects. Maps created with `smap()` contain two dialogues in the top right corner: one for switching the background map and one for measuring distances and areas.
- `smart_as_sf()`: Applies the same heuristics as `smap()`, but returns sf objects instead.
- `as_coord_matrix`: Applies the same heuristics as `smap()`, but returns a numeric matrix with longitude and latitude instead.

## Data frames

Viewing `data.frames` with longitude and latitude columns was the basic use case for which smartmap was created. The heuristics for automatically determining the geo-coordinate columns are simple, but should work for most use cases.

    print(cities)
    smap(cities)

    #>              city LaTituDE       lng       pop country         province
    #> 1         Bregenz 47.51670  9.766702   26928.0 Austria       Vorarlberg
    #> 2      Eisenstadt 47.83330 16.533297   13165.0 Austria       Burgenland
    #> 3 Wiener Neustadt 47.81598 16.249954   60621.5 Austria Niederösterreich
    #> 4            Graz 47.07776 15.410005  242780.0 Austria       Steiermark
    #> 5      Klagenfurt 46.62034 14.310020   88588.0 Austria          Kärnten
    #> 6            Linz 48.31923 14.288781  265161.5 Austria   Oberösterreich
    #> 7        Salzburg 47.81048 13.040020  178274.0 Austria         Salzburg
    #> 8       Innsbruck 47.28041 11.409991  133840.5 Austria            Tirol
    #> 9          Vienna 48.20002 16.366639 2065500.0 Austria             Wien

source: World Cities Database

## Shapefiles

`smap()` also works with file system paths or urls to shapefiles. It even looks inside zip files if necessary!

    # disabled url broken
    # smap("https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/cultural/ne_50m_admin_0_countries.zip")

## Point coordinates

Numeric vectors of length 2 are interpreted as longitude/latitude coordinate pairs. If you supply names, smap uses them to identify the correct order.

    smap(c(16.422524, 48.185686))
    smap(c(LAT = 48.185686, LoNgItuDE = 16.422524))

## Leaflet maps

You can call `smap()` on existing leaflet objects to add background tiles and a ruler for measuring distance

    library(leaflet)
    lf <- leaflet::leaflet(height=200) %>%
      leaflet::addCircleMarkers(lng = 16.422524, lat = 48.185686)

    lf
    smap(lf)

## Simple features (sf)

Everyone who has worked with simple features will know that it is sometimes a bit awkward to convert between the different datatypes provided by the package (`sf::sf`, `sf::sfc` and `sf::sfg`). Toe remedy this `smap()` works natively with all three.

    smap(sf::st_point(c(16.373311, 48.208482)))
    smap(sf::st_sfc(sf::st_point(c(16.373311, 48.208482))))
    smap(sf::st_sf(coords = sf::st_sfc(sf::st_point(c(16.373311, 48.208482))), crs = 4326))