OpenStreetMap

Querying elements from osm and doing practical things with them is really a useful and fun way of code “hacking” for me. In this case, I wanted to show farmers markets with a reactive map like leaflet like I’ve done before in my journal entries. In this case, I wanted to make it easy to browse the times, seasons, and contact info for farmers markets in the Anchorage, Alaska area. I think with some refinement this map could be useful to sellers and buyers to determine which farmers markets they can fit into their schedules.


library(tidyverse)
library(osmdata)
library(leaflet)
library(htmlwidgets)


marketplace <- opq(c(xmin = -150.494625, ymin = 60.771028, xmax = -148.866047, ymax = 61.550720)) %>%
  add_osm_feature("amenity", "marketplace") %>%
  osmdata_sf()

marketplace_points <- marketplace$osm_points

greenLeafIcon <- makeIcon(
  iconUrl = "https://leafletjs.com/examples/custom-icons/leaf-green.png",
  iconWidth = 38, iconHeight = 95,
  iconAnchorX = 22, iconAnchorY = 94,
  shadowUrl = "https://leafletjs.com/examples/custom-icons/leaf-shadow.png",
  shadowWidth = 50, shadowHeight = 64,
  shadowAnchorX = 4, shadowAnchorY = 62
)

content <- paste(sep = "<br/>",
                 paste0("<b><a href='", marketplace_points$website,"'>", marketplace_points$name, "</a></b>"),
                 paste("<i>", marketplace_points$description, "</i>"),
                 #paste(marketplace_points$addr.housenumber, marketplace_points$addr.street),
                 #paste(marketplace_points$addr.city, marketplace_points$addr.state, marketplace_points$addr.postcode),
                 paste("Phone:", marketplace_points$phone),
                 paste("Email:", marketplace_points$email),
                 #paste("Hours:", marketplace_points$opening_hours),
                 paste( paste0("<b><a href='", marketplace_points$website,"'>", "Website", "</a></b>"))
)


(m <- leaflet(marketplace$osm_points) %>%
  addTiles() %>%
  addMarkers(icon = greenLeafIcon,
             popup = lapply(content, htmltools::HTML)))

saveWidget(m, "farmers_markets.html")
#write.csv(marketplace_points, file = "marketplace_points.csv")

Location: Campbell, Anchorage, Alaska, 99503, United States

Discussion

Comment from PierZen on 29 January 2023 at 17:09

Thanks Hans,

Is this R language that is used to interpret this source ?

Comment from Hans Thompson on 29 January 2023 at 23:46

Yes. I should have made that more clear.

Log in to leave a comment