Search code examples
rgisr-spcentroid

Get centroid from map with changing boundaries


I'm working with a package called histmaps (found here https://github.com/junkka/histmaps ) where information about historical boundaries of administrative zones in Sweden is stored. I'm interested in finding the centroid of each parish that existed between the years 1900 and 1920. Since the boundaries of the parishes were changing over time, some adjustments need to made.

The creator of the package has solved this by introducing hist_boundaries, like:

period_map <- hist_boundaries(c(1900, 1920))

Now, I'm completely clueless on how to find the centroids of each parish from the period map.

I've tried this code:

centroids <- 
  gCentroid(
    spgeom = methods::as( object = period_map, Class = 'Spatial' )
    , byid = TRUE
  )

but that only returns:

Error in methods::as(object = period_map, Class = "Spatial") : 
  no method or default for coercing “list” to “Spatial”

Any suggestions?


Solution

  • I suggest that you download the parish boundaries in {sf} package format (there seems to be format parameter just for this purpose); you will be then able to apply the function sf::st_centroid().

    library(histmaps)
    library(dplyr)
    library(sf)
    
    polygons <- hist_boundaries(1900, format = "sf")
    
    centroids <- polygons %>% 
      st_centroid()
    
    plot(st_geometry(polygons))
    plot(centroids, pch = 4, col = "red", add = T)
    

    enter image description here