Converts data.frame objects to GeoJSON. Each row is considerd a POINT
Usage
df_geojson(
df,
lon,
lat,
z = NULL,
m = NULL,
atomise = FALSE,
simplify = TRUE,
digits = NULL,
factors_as_string = TRUE
)Arguments
- df
data.frame
- lon
column of
dfcontaining the longitude data- lat
column of
dfcontaining the latitude data- z
column of
dfcontaining the Z attribute of the GeoJSON- m
column of
dfcontaining the M attribute of the GeoJSON. If supplied, you must also supplyz- atomise
logical indicating if the data.frame should be converted into a vector of GeoJSON objects
- simplify
logical indicating if data.frame without property columns should simplify (
TRUE) into a vector of GeoJSON, or (FALSE). Ifatomiseis TRUE this argument is ignored.- digits
integer specifying the number of decimal places to round numerics. numeric values are coorced using
as.integer, which may round-down the value you supply. Default isNULL- no rounding- factors_as_string
logical indicating if factors should be treated as strings. Defaults to TRUE.
Examples
df <- data.frame(lon = c(1:5, NA), lat = c(1:5, NA), id = 1:6, val = letters[1:6])
df_geojson( df, lon = "lon", lat = "lat")
#> {"type":"FeatureCollection","features":[{"type":"Feature","properties":{"id":1,"val":"a"},"geometry":{"type":"Point","coordinates":[1.0,1.0]}},{"type":"Feature","properties":{"id":2,"val":"b"},"geometry":{"type":"Point","coordinates":[2.0,2.0]}},{"type":"Feature","properties":{"id":3,"val":"c"},"geometry":{"type":"Point","coordinates":[3.0,3.0]}},{"type":"Feature","properties":{"id":4,"val":"d"},"geometry":{"type":"Point","coordinates":[4.0,4.0]}},{"type":"Feature","properties":{"id":5,"val":"e"},"geometry":{"type":"Point","coordinates":[5.0,5.0]}},{"type":"Feature","properties":{"id":6,"val":"f"},"geometry":null}]}
df_geojson( df, lon = "lon", lat = "lat", atomise = TRUE)
#> {"type":"Feature","properties":{"id":1,"val":"a"},"geometry":{"type":"Point","coordinates":[1.0,1.0]}}
#> {"type":"Feature","properties":{"id":2,"val":"b"},"geometry":{"type":"Point","coordinates":[2.0,2.0]}}
#> {"type":"Feature","properties":{"id":3,"val":"c"},"geometry":{"type":"Point","coordinates":[3.0,3.0]}}
#> {"type":"Feature","properties":{"id":4,"val":"d"},"geometry":{"type":"Point","coordinates":[4.0,4.0]}}
#> {"type":"Feature","properties":{"id":5,"val":"e"},"geometry":{"type":"Point","coordinates":[5.0,5.0]}}
#> {"type":"Feature","properties":{"id":6,"val":"f"},"geometry":null}
df <- data.frame(lon = c(1:5, NA), lat = c(1:5, NA) )
df_geojson( df, lon = "lon", lat = "lat")
#> {"type":"Point","coordinates":[1.0,1.0]}
#> {"type":"Point","coordinates":[2.0,2.0]}
#> {"type":"Point","coordinates":[3.0,3.0]}
#> {"type":"Point","coordinates":[4.0,4.0]}
#> {"type":"Point","coordinates":[5.0,5.0]}
#> null
df_geojson( df, lon = "lon", lat = "lat", simplify = FALSE)
#> {"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[1.0,1.0]}},{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[2.0,2.0]}},{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[3.0,3.0]}},{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[4.0,4.0]}},{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[5.0,5.0]}},{"type":"Feature","properties":{},"geometry":null}]}
df <- data.frame(lon = c(1:5), lat = c(1:5), elevation = c(1:5) )
df_geojson( df, lon = "lon", lat = "lat", z = "elevation")
#> {"type":"Point","coordinates":[1.0,1.0,1.0]}
#> {"type":"Point","coordinates":[2.0,2.0,2.0]}
#> {"type":"Point","coordinates":[3.0,3.0,3.0]}
#> {"type":"Point","coordinates":[4.0,4.0,4.0]}
#> {"type":"Point","coordinates":[5.0,5.0,5.0]}
df_geojson( df, lon = "lon", lat = "lat", z = "elevation", simplify = FALSE)
#> {"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[1.0,1.0,1.0]}},{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[2.0,2.0,2.0]}},{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[3.0,3.0,3.0]}},{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[4.0,4.0,4.0]}},{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[5.0,5.0,5.0]}}]}
df <- data.frame(lon = c(1:5), lat = c(1:5), elevation = c(1:5), id = 1:5 )
df_geojson( df, lon = "lon", lat = "lat", z = "elevation")
#> {"type":"FeatureCollection","features":[{"type":"Feature","properties":{"id":1},"geometry":{"type":"Point","coordinates":[1.0,1.0,1.0]}},{"type":"Feature","properties":{"id":2},"geometry":{"type":"Point","coordinates":[2.0,2.0,2.0]}},{"type":"Feature","properties":{"id":3},"geometry":{"type":"Point","coordinates":[3.0,3.0,3.0]}},{"type":"Feature","properties":{"id":4},"geometry":{"type":"Point","coordinates":[4.0,4.0,4.0]}},{"type":"Feature","properties":{"id":5},"geometry":{"type":"Point","coordinates":[5.0,5.0,5.0]}}]}
df_geojson( df, lon = "lon", lat = "lat", z = "elevation", atomise = TRUE)
#> {"type":"Feature","properties":{"id":1},"geometry":{"type":"Point","coordinates":[1.0,1.0,1.0]}}
#> {"type":"Feature","properties":{"id":2},"geometry":{"type":"Point","coordinates":[2.0,2.0,2.0]}}
#> {"type":"Feature","properties":{"id":3},"geometry":{"type":"Point","coordinates":[3.0,3.0,3.0]}}
#> {"type":"Feature","properties":{"id":4},"geometry":{"type":"Point","coordinates":[4.0,4.0,4.0]}}
#> {"type":"Feature","properties":{"id":5},"geometry":{"type":"Point","coordinates":[5.0,5.0,5.0]}}
## to sf objects
geo <- df_geojson( df, lon = "lon", lat = "lat", z = "elevation")
sf <- geojson_sf( geo )