Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addPolylines does not support lines broken in several pieces #213

Closed
jiho opened this issue Dec 2, 2015 · 5 comments
Closed

addPolylines does not support lines broken in several pieces #213

jiho opened this issue Dec 2, 2015 · 5 comments
Assignees
Labels
type: bug Maintainers have validated that it is a real bug in the project code
Milestone

Comments

@jiho
Copy link

jiho commented Dec 2, 2015

This example has two lines identified separately in a SpatialLines object and it works

x1 <- SpatialLines(
  LinesList = list(
    Lines( slinelist=list( Line(cbind(c(5,6,7),c(43,44,45))) ), ID=1),
    Lines( slinelist=list( Line(cbind(c(8,9,9),c(43,44,45))) ), ID=2)
  )
)
leaflet() %>% addTiles() %>% addPolylines(data=x1)

Now with two pieces of a line with the same ID, addPolylines only shows the first piece.

x2 <- SpatialLines(
  LinesList = list(
    Lines( slinelist=list(
        Line(cbind(c(5,6,7),c(43,44,45))),
        Line(cbind(c(8,9,9),c(43,44,45)))
      ), ID=1
    )
  )
)
leaflet() %>% addTiles() %>% addPolylines(data=x2)

Getting pieces of lines is usual when computing them (using rasterToContour for example).

There is probably a way to convert from

leaflet:::polygonData.SpatialLinesDataFrame(x2)

to

leaflet:::polygonData.SpatialLinesDataFrame(x1)

But I am guessing there is probably something more clever to be done!

NB: given that the code is much of the same, I suspect this bug is also true for polygons which are composed of several sub-polygons.

@coolbutuseless
Copy link

+1 for this problem. I went to the trouble of creating a minimal example, and so will leave it here for posterity

library(dplyr)
library(leaflet)
library(sp)

packageVersion('sp')
packageVersion('leaflet')

#----------------------------------------------------------
# Vertical and horizontal line to make
# left side and top of triangle
# i.e.    _____
#         |
#         |
#
#----------------------------------------------------------
coords1a <- matrix(c(
  0, 0,
  0, 1,
  1, 1
), nrow = 3, byrow = TRUE)


#----------------------------------------------------------
# Diagonal line to complete triangle i.e.
#
#              /
#             /
#            /
#
#----------------------------------------------------------
coords1b <- matrix(c(
  0, 0,
  1, 1
), nrow = 2, byrow = TRUE)


#----------------------------------------------------------
# Create an sp::SpatialLines object
#----------------------------------------------------------
line1a   <- Line(coords1a)
line1b   <- Line(coords1b)
S1       <- Lines(list(line1a, line1b), ID="tri")
sp_lines <- sp::SpatialLines(list(S1))


#----------------------------------------------------------
# plot looks like a triangle
#----------------------------------------------------------
plot(sp_lines)


#----------------------------------------------------------
# Leaflet only plots the first set of coordinates!  *******
# i.e. it does not draw the diagonal!               *******
#----------------------------------------------------------
sp_lines %>% leaflet() %>% addPolylines()

@jcheng5 jcheng5 added this to the v1.1 milestone Sep 1, 2016
@jcheng5
Copy link
Member

jcheng5 commented Sep 1, 2016

@bhaskarv Not sure what to do about this, but worth thinking about for a minute to see if there's a natural solution.

@bhaskarvk
Copy link
Collaborator

@jiho / @coolbutuseless

If I do this to sp_lines

if(length(sp_lines@lines) ==1 && length(sp_lines@lines[[1]]@Lines) >1) {
  id <- 0
  sp_lines.fix <- sp::SpatialLines(
    purrr::map(sp_lines@lines[[1]]@Lines, function(line){
      id <<- id + 1
      Lines(line,id)
    }))
}

And plot the sp_lines.fix object it works.
Essentially it's just a hack to convert a SpatialLines(list(Lines(list(l1, l2), id='ID'))) to
SpatialLines(list(Lines(l1, id="1") , Lines(l2, id="2"))).

Does this work for you ? @jcheng5 The fix is fairly trivial so not sure if this needs to go in Leaflet, or just document it for end user to implement on their own data.

@bhaskarvk bhaskarvk self-assigned this Oct 6, 2016
@bhaskarvk bhaskarvk added the type: bug Maintainers have validated that it is a real bug in the project code label Oct 6, 2016
@jcheng5
Copy link
Member

jcheng5 commented Oct 6, 2016

I'm inclined to just document the workaround, or, use the multiPolyline feature in Leaflet. Maybe for vNext.

@bhaskarvk
Copy link
Collaborator

Defi vNext, I would much rather upgrade to Leaflet 1.0 after v1.1 and then go after this.
I'm closing this w/ my workaround in #213 (comment)

jcheng5 pushed a commit that referenced this issue Nov 23, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Maintainers have validated that it is a real bug in the project code
Projects
None yet
Development

No branches or pull requests

4 participants