Sample data
library(raster)
dat <- getData('GADM', country='FRA', level=1)
plot(dat)
text(dat, labels=as.character(dat$ID_1), col="darkred", font=2, offset=0.5, adj=c(0,2))
To save the IDs of provinces, I can do this
province.id <- dat$ID_1
However, I want to arrange these IDs according to some direction (i.e. south to north)
For example, my province.id
id should start from 10 (since it is the southern most province) all the way till 17 since it is the northern most province
One way I thought was I can generate centorid of each province and based on the centroid,
I can determine which are the most south to most north location.
library(rgeos)
trueCentroids = gCentroid(dat,byid=TRUE)
plot(dat)
points(coordinates(dat),pch=1)
But I still cannot export the output or arrange the centroids in the south-to-north direction to save as a vector
An easy approach would be to take the minimum latitude of each polygon and sort your IDs based on that:
# data
library(raster)
dat <- getData('GADM', country='FRA', level=1)
# create south to north index
sn_index <- unlist(lapply(dat@polygons, function(x) min(x@Polygons[[1]]@coords[,2])))
#sort IDs
dat$ID_1[order(sn_index)]
# [1] 10 13 21 16 22 3 2 14 20 6 18 8 11 7 1 9 15 4 5 19 12 17