There and Back Again competition entry https://shiny.petras.space/commute/
rstats
rshiny
census
competition
leaflet
javascript
stats-nz

maps.R 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Map simplification
  2. # In future, might be better to do this online at https://mapshaper.org/
  3. # Save installing v8!
  4. library(rgdal)
  5. library(rmapshaper)
  6. # sf <- readOGR(dsn = "shapefiles/statsnzstatistical-area-2-2018-generalised-SHP/")
  7. sf <- readOGR(dsn = "shapefiles/SA2-2018-WGS/")
  8. nrow(sf@data)
  9. plot(sf, xlim=c(1480000, 1510000), ylim=c(5150000, 5180000))
  10. # insf <- ms_innerlines(sf)
  11. ssf1 <- ms_simplify(sf, keep_shapes=TRUE)
  12. plot(ssf1, xlim=c(1480000, 1510000), ylim=c(5150000, 5180000))
  13. nrow(ssf1@data)
  14. # Ended up going with this version
  15. ssf2.5 <- ms_simplify(sf, 0.025, keep_shapes=TRUE)
  16. plot(ssf2.5, xlim=c(1480000, 1510000), ylim=c(5150000, 5180000))
  17. nrow(ssf2.5@data)
  18. ssf2 <- ms_simplify(sf, 0.01, keep_shapes=TRUE)
  19. plot(ssf2, xlim=c(1480000, 1510000), ylim=c(5150000, 5180000))
  20. nrow(ssf2@data)
  21. ssf3 <- ms_simplify(sf, 0.002, keep_shapes=TRUE)
  22. plot(ssf3, xlim=c(1480000, 1510000), ylim=c(5150000, 5180000))
  23. nrow(ssf3@data)
  24. writeOGR(ssf2.5, dsn = "shapefiles/sa20025WGS/", layer = "SA2", driver="ESRI Shapefile")
  25. ssf3b <- ms_simplify(sf, 0.002, keep_shapes=FALSE)
  26. plot(ssf3b, xlim=c(1480000, 1510000), ylim=c(5150000, 5180000))
  27. nrow(ssf3b@data)
  28. ci <- sf@data$SA22018__1 != "Chatham Islands"
  29. head(ssf2.5@data)
  30. laper <- (sf@data$LAND_AREA_ / sf@data$AREA_SQ_KM)
  31. sf@data$SA22018__1[laper > 0.1 & laper < 0.4]
  32. # Filter out: chatham islands (due to meridian issues) + areas more than 10% water
  33. plot(ssf2.5[(laper > 0.1) & ci,])
  34. writeOGR(ssf2.5[(laper > 0.1) & ci,], dsn = "shapefiles/sa20025WGSfil/", layer = "SA2", driver="ESRI Shapefile")