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

label for overlapping polygons worked for one case but another #473

Open
xhdong-umd opened this issue Oct 31, 2017 · 1 comment
Open

label for overlapping polygons worked for one case but another #473

xhdong-umd opened this issue Oct 31, 2017 · 1 comment

Comments

@xhdong-umd
Copy link

xhdong-umd commented Oct 31, 2017

I need to label several overlapping polygons, but only the label of the biggest one is shown. However when I tested with some simulated data the labels were shown correctly. I compared the data in two cases carefully but cannot find the difference caused the problem.

Here is a minimal example of simulated overlapping polygons:

library(leaflet)
library(sp)

poly_a <- data.frame(lng = c(0, 0.5, 2, 3),
                     lat = c(0, 4, 4, 0))
poly_b <- data.frame(lng = c(1, 1.5, 1.8),
                     lat = c(2, 3, 2))
pgons = list(
  Polygons(list(Polygon(poly_a)), ID="1"),
  Polygons(list(Polygon(poly_b)), ID="2")
)
poly_dat <- data.frame(name = as.factor(c("a", "b")))
rownames(poly_dat) <- c("1", "2")

spgons = SpatialPolygons(pgons)
spgonsdf = SpatialPolygonsDataFrame(spgons, poly_dat, TRUE)

leaflet() %>% addPolygons(data = spgonsdf, label = ~name
                          #           ,
                          #           highlightOptions = highlightOptions(
                          # color = "red", weight = 2,bringToFront = TRUE)
)

It's working properly:
screen shot 2017-10-31 at 9 44 02 am
screen shot 2017-10-31 at 9 44 08 am

However it didn't work with my data.
Gabs.zip

You can drag the zip into this site and use the i button to see it's correctly labeled
screen shot 2017-10-31 at 9 47 01 am
screen shot 2017-10-31 at 9 47 09 am

library(rgdal)

# download Gabs.zip and extract files to Gabs folder
hr_shape_gabs <- readOGR(dsn = 'Gabs', layer = 'Gabs - OU anisotropic')
hr_shape_gabs_pro <- spTransform(hr_shape_gabs, 
                                 CRS("+proj=longlat +datum=WGS84 +no_defs"))
leaflet(hr_shape_gabs_pro) %>%
  addTiles() %>% 
  addPolygons(weight = 1, label = ~name)

screen shot 2017-10-31 at 9 45 39 am

screen shot 2017-10-31 at 9 45 45 am

The data in both case are SpatialPolygonsDataFrame, the data slot have proper polygon names.

@xhdong-umd
Copy link
Author

Turned out that if I change the polygons order in polygons slot, put the bigger one at first will solve the problem. I also tried to change plotOrder in SpatialPolygonsDataFrame, but that doesn't work.

It seemed that leaflet just register area under polygons in order of polygons slot (and not observing plotOrder field in SpatialPolygonsDataFrame), so the bigger polygons will override smaller ones, thus we need to put bigger one at first.

The label is also just a vector by order, so there will be mismatch if data slot in SpatialPolygonsDataFrame are in different order of polygons, even they are matched by rowname and polygon ID.

With more complex user cases, polygons can have intersections, changing order will not solve the problem for the intersection. I'm not sure if there is a better solution here, since it should not be a requirement that polygons in specific order. I used to import same data into Carto.com and the label can be added properly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant