I am working with the R programming language.
I am trying to calculate the geographic centroids of different polygons within Canada.
I downloaded the following shapefile and tried to calculate and visualize the centroids of each polygon:
library(dplyr)
library(sf)
library(data.table)
library(rvest)
library(leaflet)
library(ggplot2)
library(urltools)
library(leaflet.extras)
library(stringr)
library(magrittr)
# Download zip files
url_1 <- "https://www12.statcan.gc.ca/census-recensement/alternative_alternatif.cfm?l=eng&dispext=zip&teng=lada000b21a_e.zip&k=%20%20%20151162&loc=//www12.statcan.gc.ca/census-recensement/2021/geo/sip-pis/boundary-limites/files-fichiers/lada000b21a_e.zip"
download.file(url_1, destfile = "lada000b21a_e.zip")
# Extract zip files
unzip("lada000b21a_e.zip")
# Read shapefiles
ada <- st_read("lada000b21a_e.shp")
shapefile_1 = ada %>% st_transform(32617)
#sf_cent <- st_centroid(shapefile_1)
sf_cent <- st_point_on_surface(shapefile_1)
# Transform the centroids to the WGS84 CRS
sf_cent_geo <- st_transform(sf_cent, crs = 4326)
# Extract the longitude and latitude coordinates of the centroids
lon <- st_coordinates(sf_cent_geo)[,1]
lat <- st_coordinates(sf_cent_geo)[,2]
ADAUID <- sf_cent_geo$ADAUID
lon <- st_coordinates(sf_cent_geo)[,1]
lat <- st_coordinates(sf_cent_geo)[,2]
shapefile_1 = ada %>% st_transform(32617)
sf_cent <- st_centroid(ada)
ggplot() +
geom_sf(data = shapefile_1, fill = 'white') +
geom_sf(data = sf_cent, color = 'red')
However, when I examine the results:
Problem: When I examine the results, I see that at times there are multiple centroids within each polygon.
I tried to do some research and consult other references on this topic (e.g. r sf package centroid within polygon), but so far I am unable to figure out how to resolve this problem.
For logical purposes, I am trying to only have one centroid in each polygon.
Can someone please show me how to fix this?