Finding Postcodes using arcgisgeocode

Fun R

Using the package arcgisgeocoder, along with a shiny app and leaflet to smooth ways of finding postal codes.

Eugene https://www.fizzics.ie
2024-06-25

I’m old fashioned in a lot of ways, one of which is that I like to send postcards when on holidays. This means making sure they make their way to the intended recipients. And while I can usually remember an address, coming up with the corresponding postcode is at best hit-and-miss. There’s no telling how many postcards down the years are still out there drifting through mail-sorting limbo.

Then I listened to a presentation by Josiah Parry and felt I had a solution to my problem. He introduced a package called arcgisgeocoder he’s been working on, part of the arcgis system. One of its capabilities is to take a geographic location and feed back details about the address, including the postal code. Kind of what I need. Josiah went on to incorporate this into a shiny app using leaflet, something I’m going to copy here. Then, when you scroll around the map and click on the location, it gives you the address and postal code.

Let’s see how this works. First, we’ll need some packages:

Then we create our shiny app itself. Note the function reverse_geocode from arcgisgeocode. This does all the heavy lifting. The rest is fairly standard map interaction; displaying a map and capturing the latitude and longitude of a point clicked.

ui <- page_fillable(
  card(
    card_title(textOutput("rev_result")),
    leafletOutput("map", width = "100%", height = "100%"),
  )
)

server <- function(input, output, session){
  observeEvent(input$map_click, {
    click <- input$map_click
    x <- click$lng
    y <- click$lat
    loc <- c(x, y)
    dput(loc)
    
    geocoded <- reverse_geocode(loc)
    
    output$rev_result <- renderText(
      glue::glue("{geocoded$long_label}\n{geocoded$postal}")
      )
    
    leafletProxy("map", data = st_geometry(geocoded)) |> 
      clearMarkers() |> 
      addMarkers()
  })
  
  output$map <- renderLeaflet({
    leaflet() |> 
      addProviderTiles(providers$Esri.WorldGrayCanvas) |> 
      setView(lat = 53.32041, lng = -6.23956, zoom = 14)
  })
}


shinyApp(ui, server)

At the moment, shinyapps.io is having trouble working with arcgisgeocoder, otherwise I’d link to a web version of this app. I’ll work on this. For the moment, here is a version based on tidygeocoder that doesn’t quite work as well.

Corrections

If you see mistakes or want to suggest changes, please create an issue on the source repository.

Reuse

Text and figures are licensed under Creative Commons Attribution CC BY 4.0. Source code is available at https://github.com/eugene100hickey/fizzics, unless otherwise noted. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".

Citation

For attribution, please cite this work as

Eugene (2024, June 25). Euge: Finding Postcodes using `arcgisgeocode`. Retrieved from https://www.fizzics.ie/posts/2024-06-25-finding-postcodes-using-arcgisgeocode/

BibTeX citation

@misc{eugene2024finding,
  author = {Eugene, },
  title = {Euge: Finding Postcodes using `arcgisgeocode`},
  url = {https://www.fizzics.ie/posts/2024-06-25-finding-postcodes-using-arcgisgeocode/},
  year = {2024}
}