I have downloaded a shape file of Canadian federal districts. When I load the file into the namespace, it is a SpatialPolygonsDataFrame. Examining an individual polygon, I can see the following:
I can see that these are in the UTM system. I would like to be able to convert these into the normal longitude/latitude system using decimal units. The SPDF object has attributes: data, polygons, plotorder, bounding box and proj4string.
I have tried, for example:
sf::st_as_sf(MAP_HERE, coords = c('lon', 'lat'), crs = 4326)
In the resulting sf dataframe, I cannot find a way to access the coordinates of the new object in long/lat pairs. When I run st_coordinates() on the resulting dataframe, I can see that these have not been changed.
Ideally, I would like to access the coordinates of the bounding boxes in the original SPDF, and use those to calculate centroids for individual polygons.
Is there a way to do this?
You should read the data into an sf
object from the shapefile and skip the SpatialPolygonsDataFrame
step:
library(sf)
map = st_read("./path/to/mymapdata.shp")
then you use st_transform
to convert to lat-long, which is epsg code 4326:
map_trans = st_transform(map, 4326)
then if you want to see the coordinates in full:
st_coordinates(map_trans)
Gets you something like this:
X Y L1 L2 L3
[1,] -81.47276 36.23436 1 1 1
[2,] -81.54084 36.27251 1 1 1
[3,] -81.56198 36.27359 1 1 1
[4,] -81.63306 36.34069 1 1 1
[5,] -81.74107 36.39178 1 1 1
[6,] -81.69828 36.47178 1 1 1
where L1
groups each feature (row) in the data, and L2
and L3
encode for multiple polygons and holes in these features.