0

I'm fairly new to handling spatial data and leaflet in general. I'm having difficulty creating a popup for my map. Basically what I want in the popup is the coordinates of my polygon and one of the properties (the type of class). Below is an test example of my GeoJSON file:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {"class": "blah"},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -81.7987060546875,
              32.74570253945518
            ],
            [
              -81.6229248046875,
              32.16631295696736
            ],
            [
              -80.958251953125,
              32.4263401615464
            ],
            [
              -81.2713623046875,
              32.791892438123696
            ],
            [
              -81.7437744140625,
              32.97180377635759
            ],
            [
              -81.7987060546875,
              32.74570253945518
            ]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {"class": "blah2"},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -82.056884765625,
              33.55512901742288
            ],
            [
              -81.4471435546875,
              33.247875947924385
            ],
            [
              -81.40869140625,
              33.80653802509606
            ],
            [
              -82.078857421875,
              33.88865750124075
            ],
            [
              -82.40295410156249,
              33.58716733904656
            ],
            [
              -82.056884765625,
              33.55512901742288
            ]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {"class": "blahh3"},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -83.485107421875,
              32.930318199070534
            ],
            [
              -83.07861328125,
              31.863562548378965
            ],
            [
              -82.21618652343749,
              32.11049589629439
            ],
            [
              -82.97973632812499,
              33.22030778968541
            ],
            [
              -83.726806640625,
              33.211116472416855
            ],
            [
              -83.485107421875,
              32.930318199070534
            ]
          ]
        ]
      }
    }
  ]
}

Here is the code I have so far to create my map, but I'm struggling/don't even know where to start on creating a popup that includes my coordinates and property:

blahTest <- geojson_read("/file/path/...", what = "sp")
fpal <- colorFactor("viridis", blahTest$class)
leaflet(blahTest) %>%
  addTiles() %>%
  addPolygons(stroke = FALSE, smoothFactor = 0.3, fillOpacity = 1,
              color = ~fpal(class), popup = ~class) %>%
  addLegend(colors = ~fpal, opacity = 1.0, labels = ~fpal, values = ~class) %>%
  • I haven't used geojson with leaflet (and rarely work with them) so I'm not 100% sure on referencing variables etc. but first you need to work out if you want labels (mouse over pop-up label) or pop-ups (permanent or click based labels). So maybe start by having a look at this to explain the difference and see some examples: rstudio.github.io/leaflet/popups.html – Will Hore-Lacy Mar 2 '18 at 0:21
  • Maybe start by using the 'htmlEscape' function on the popup you have for the polygon: popup = ~htmlEscape(class) After than you will need to find a way to combining your coordinates and property into a standard string an or an html string (and remove the htmlEscape part, either way, paste should work for that. – Will Hore-Lacy Mar 2 '18 at 0:25
1

Ok I hacked something together. The hardest part is getting the coordinates and adding them to the data part of the spatial data frame so they can be used in the leaflet call. Try this:

blahTest <- geojson_read("./file.geojson", what = "sp")

# Function to get coordinates
getCoords <- function(x) {
    y <- as.numeric(x@labpt)
    names(y) <- c("lon","lat")
    y
}

# Run Function and bind to data frame component of spatial data frame
blahTest@data <- cbind( blahTest@data, t( sapply(blahTest@polygons, getCoords) ) )

# Leaflet call using paste to join together coordinates
leaflet(blahTest) %>%
    addTiles() %>%
    addPolygons(stroke = FALSE, smoothFactor = 0.3, fillOpacity = 1,
             popup = ~paste( class, round(lon,3), round(lat,3)) )

Note 1: I removed colours and legend for simpler example.
Note 2: I grabbed the 'labpt' coordinates however you could extract all the coordinates for the vertices of the polygon blahTest@polygons[[1]]@Polygons[[1]]@coords for the first and blahTest@polygons[[2]]@Polygons[[1]]@coords for the second.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.