brad7192
brad7192

Reputation: 41

Creating GEOJSON files in R using GEOJSON_WRITE

I am new to geospatial data and I am trying to convert a csv to GEOJSON using geojsonio and sf packages. I want my output to be in this format

{
                "type": "FeatureCollection",
                "features": [
                  {
                    "type": "Feature",
                    "properties": { "vendor":  "A",
                    "vol":20},
                    "geometry": {
                      "type": "LineString",
                      "coordinates": [
                        [-74.20986, 40.81773, 0, 1564184363],
                        [-74.20987, 40.81765, 0, 1564184396],
                        [-74.20998, 40.81746, 0, 1564184409]
                      ]
                    }
                  }
                ]
              }

As you note, here the co-ordinates have 4 dimensions : lat,long, elevation / altitude , Timestamp

My code is as follows

library(geojsonio)
library(sf)
library(tidyverse)

sf_data <- st_as_sf(Master_GPX, coords = c("lon", "lat","ele","timestamp"),dim="XYZM")

sf_data  %>%
  group_by(Msisdn) %>%
  summarise(geometry = st_combine(geometry)) %>%
  st_cast("LINESTRING") -> res_sfdata


geojson_write(res_sfdata,geometry = "LINESTRING", file = "C:/Project/checks/res_sfdata.geojson")

Master_GPX file as shown below

enter image description here

However,While writing the geojson file using geojson_write, I get a warning suggesting that it drops dimension M (which is timestamp) from the geojson file

> geojson_write(res_sfdata,geometry = "LINESTRING", file = "C:/Project/checks/res_sfdata.geojson",)
removing M dimension as not supported in GeoJSON format
Success! File is at C:/Project/checks/res_sfdata.geojson

I would need the dimension "M" which is timestamp in the geojson file for the visualization that I am building. How can I have all four values : latitude, longitude, elevation and timestamp written back to the geojson file?. Are there any alternative packages/functions which can achieve the same result? As am new to this space, any help would be much appreciated.

Upvotes: 4

Views: 4155

Answers (1)

SymbolixAU
SymbolixAU

Reputation: 26258

The {sf} library can read and write directly to geojson. Here's a simple working example

library(sf)

nc <- sf::st_read( system.file("./shape/nc.shp", package = "sf"))

sf::st_write(nc, dsn = "~/Desktop/nc.geojson", layer = "nc.geojson")

And a slightly more detailed example to show it handles the Z & M dimensions as well.

## - convert to data.frame
df <- sfheaders::sf_to_df(nc, fill = TRUE)

## - add Z & M columns
df$z <- rnorm(nrow(df))
df$m <- rnorm(nrow(df))

## - convert back to 'sf' object
sf <- sfheaders::sf_multipolygon(
  obj = df
  , x = "x"
  , y = "y"
  , z = "z"
  , m = "m"
  , keep = T
  , multipolygon_id = "multipolygon_id"
  , polygon_id = "polygon_id"
  , linestring_id = "linestring_id"
)

sf

sf::st_write(sf, dsn = "~/Desktop/nc_zm.geojson", layer = "nc_zm.geojson")

Upvotes: 5

Related Questions