Mohammad
Mohammad

Reputation: 67

How to create spatial line dataframe from encoded polylines?

I'm working with transportation network dataset and I want to import traffic speed data provided as JSON format.

I could read the data in R using read.scorata(). However, I couldn't covert it to a spatial dataframe to be used for further analysis. There is one field in the current dataframe which is encoded polyline. How can I convert using this field?

library(RSocrata)

#Loading only two rows for easy reproduction
Test_TSD <- read.socrata("https://data.cityofnewyork.us/resource/i4gi-tjb9.json?borough=Manhattan&id=225")

Upvotes: 3

Views: 442

Answers (1)

SymbolixAU
SymbolixAU

Reputation: 26248

library(googlePolylines) can decode those polylines into coordinates.

To convert to an sf object currently requires a couple of steps. (There are plans to make this part of the googlePolylines library)

coords <- googlePolylines::decode( Test_TSD$encoded_poly_line )

sfg <- lapply( coords, function(x) sf::st_linestring( x = as.matrix(x) ) )

sfc <- sf::st_sfc( sfg )

sf <- sf::st_as_sf( cbind( Test_TSD, sfc ) )

You now have a simple feature (sf) object. Given sf is the successor to sp you can stop here. But, if you still need the SpatialDataFrame you can do

sp <- as( sf, "Spatial" )

Upvotes: 3

Related Questions