Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating SpatialLinesDataFrame from SpatialLines object and basic df

Tags:

r

r-sp

Using leaflet, I'm trying to plot some lines and set their color based on a 'speed' variable. My data start at an encoded polyline level (i.e. a series of lat/long points, encoded as an alphanumeric string) with a single speed value for each EPL.

I'm able to decode the polylines to get lat/long series of (thanks to Max, here) and I'm able to create segments from those series of points and format them as a SpatialLines object (thanks to Kyle Walker, here).

My problem: I can plot the lines properly using leaflet, but I can't join the SpatialLines object to the base data to create a SpatialLinesDataFrame, and so I can't code the line color based on the speed var. I suspect the issue is that the IDs I'm assigning SL segments aren't matching to those present in the base df.

The objects I've tried to join, with SpatialLinesDataFrame():

  1. "sl_object", a SpatialLines object with ~140 observations, one for each segment; I'm using Kyle's code, linked above, with one key change - instead of creating an arbitrary iterative ID value for each segment, I'm pulling the associated ID from my base data. (Or at least I'm trying to.) So, I've replaced:

    id <- paste0("line", as.character(p))

    with

    lguy <- data.frame(paths[[p]][1]) id <- unique(lguy[,1])

  2. "speed_object", a df with ~140 observations of a single speed var and row.names set to the same id var that I thought I created in the SL object above. (The number of observations will never exceed but may be smaller than the number of segments in the SL object.)

My joining code:

splndf <- SpatialLinesDataFrame(sl = sl_object, data = speed_object)

And the result:

row.names of data and Lines IDs do not match

Thanks, all. I'm posting this in part because I've seen some similar questions - including some referring specifically to changing the ID output of Kyle's great tool - and haven't been able to find a good answer.

EDIT: Including data samples.

From sl_obj, a single segment:

print(sl_obj)

Slot "ID":
[1] "4763655"

[[151]]
An object of class "Lines"
Slot "Lines":
[[1]]
An object of class "Line"
Slot "coords":
           lon      lat
1955 -74.05228 40.60397
1956 -74.05021 40.60465
1957 -74.04182 40.60737
1958 -74.03997 40.60795
1959 -74.03919 40.60821

And the corresponding record from speed_obj:

row.names speed
...     ...
4763657 44.74
4763655 34.8 # this one matches the ID above
4616250 57.79
...     ...
like image 641
Andrew Cheesman Avatar asked Dec 07 '25 01:12

Andrew Cheesman


1 Answers

To get rid of this error message, either make the row.names of data and Lines IDs match by preparing sl_object and/or speed_object, or, in case you are certain that they should be matched in the order they appear, use

splndf <- SpatialLinesDataFrame(sl = sl_object, data = speed_object, match.ID = FALSE)

This is documented in ?SpatialLinesDataFrame.

like image 150
Edzer Pebesma Avatar answered Dec 08 '25 14:12

Edzer Pebesma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!