Learn R Programming

leafletR (version 0.1-2)

toGeoJSON: Create GeoJSON file from data frame or external spatial data file

Description

Creates a GeoJSON file from a data frame or a spatial data file, using the Ogre web API.

Usage

toGeoJSON(data, name, dest, lat.lon, overwrite=TRUE)
tg(data, name, dest, lat.lon, overwrite=TRUE)

Arguments

data
data.frame with at least two columns, representing the point coordinates. Or: Path to external spatial data file (see details below), as string.
name
Name of the resulting GeoJSON file, as string. Optional -- if missing, the name of the data frame or data file is used.
dest
Directory the file shall be saved to, as string. Optional -- if missing, the current working directory is used.
lat.lon
For data frame conversion only. Names or indices of the columns in data containing the coordinates, as vector of two: c(latitude, longitude). Optional -- if missing, the first two columns are used.
overwrite
TRUE (which is the default) overwrites existing files with the same name.

Value

  • GeoJSON file path, as string.

encoding

UTF-8

source

The code for the conversion of external data files is taken from the togeojson function of the rgbif package (http://cran.r-project.org/web/packages/rgbif). Package import would have unreasonably increased the dependencies of leafletR.

Details

Conversion of external spatial data files

toGeoJSON uses the Ogre web API (http://ogre.adc4gis.com). Ogre accepts the following file types directly: .bna, .csv, .dgn, .dxf, .gxt, .txt, .rss, .georss, .xml, .gmt, .kml and .kmz. Please note that for Shapefiles, GML, MapInfo, and VRT, Ogre only accepts a zip file. The Ogre API does not support large files (>15 MB). Have a look at the rgdal package and its writeOGR function, to convert files on your local machine.

See Also

leaflet

Examples

Run this code
# convert data frame
data(quakes)
toGeoJSON(data=quakes, name="quakes", dest=tempdir(), lat.lon=c(1,2))

# convert data frame - minimal call
# storing output file path in variable
data(quakes)
path <- toGeoJSON(data=quakes)

# preserve existing files from overwriting
toGeoJSON(data=quakes, overwrite=FALSE)

# convert a shapefile (in zipped archive)
toGeoJSON(data=system.file(package="leafletR", "files", "park_sk.zip"), 
  name="NatParks_SK", dest=tempdir())

# convert a DXF file
# storing output file path in variable
path <- toGeoJSON(data=system.file(package="leafletR", "files", 
  "lynx.dxf"), name="lynx_telemetry", dest=tempdir())

# convert a KML file
# using name of data file and saving to working directory
toGeoJSON(system.file(package="leafletR", "files", "peak_sk.kml"))

Run the code above in your browser using DataLab