Learn R Programming

leafletR (version 0.2-1)

toGeoJSON: Create GeoJSON file from spatial data

Description

Creates a GeoJSON file from data frame, Spatial object or an external spatial data file.

Usage

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

Arguments

data
Spatial data: data.frame with at least two columns, representing the point coordinates, Spatial object (sp package) or path to external spatial data file as string. See below for details.
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

toGeoJSON can handle three types of spatial data: a simple data.frame containing point coordinates and optional data columns, Spatial objects and external spatial data files.

Spatial objects

Spatial objects (sp package) should have geographical coordinates (longlat, WGS84). If other projections are used, toGeoJSON can transform the coordinates on the fly, using the rgdal package.

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 Spatial objects
library(sp)
data(meuse)
coordinates(meuse) <- ~x+y
proj4string(meuse) <- CRS("+init=epsg:28992")
toGeoJSON(data=meuse, dest=tempdir()) # rgdal package required

crd <- coordinates(meuse)
msl <- SpatialLines(list(Lines(list(Line(crd)), "line1")), 
  proj4string=CRS("+init=epsg:28992"))
toGeoJSON(data=msl, dest=tempdir()) # rgdal package required

data(meuse.riv)
msp <- SpatialPolygons(list(Polygons(list(Polygon(meuse.riv)), 
  "meuse.riv")), proj4string=CRS("+init=epsg:28992"))
toGeoJSON(data=msp, dest=tempdir()) # rgdal package required

# 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