toGeoJSON: Create GeoJSON file from spatial data

View source: R/toGeoJSON.R

toGeoJSONR Documentation

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 (or dplyr:tbl_df) 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, toGeoJSON tries to detect them by name or takes the first two columns.

overwrite

TRUE (which is the default) overwrites existing files with the same name.

Details

toGeoJSON can handle three types of spatial data: a simple data.frame, Spatial objects and external spatial data files.

data.frame objects

Point data might be available as simple data.frame or tbl_df (see dplyr package) objects. The data.frame must contain latitudes and longitudes in two separate columns and optionally may contain data columns.

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). See the Ogre website for a list of supported formats. Please note that for Shapefiles, 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.

Value

GeoJSON file path, as string.

Author(s)

Christian Graul

Source

The code for the conversion of external data files is taken from the togeojson function of the rgbif package. Package import would have unreasonably increased the dependencies of leafletR.

See Also

leaflet

Examples

## Not run: 
# 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)
# (httr package required)
toGeoJSON(data=system.file(package="leafletR", "files", "lynx.zip"), 
  name="lynx_telemetry", dest=tempdir())

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

## End(Not run)

chgrl/leafletR documentation built on March 24, 2022, 7:53 a.m.