rspatialcentroid

Calculate longitude/latitude for geographic centroid


I want to do some spatial statistic analysis with the county-level crop yield data in Nebraska for the STAT class. For that I need the longitude and latitude of the geographic centroids of each county. Anybody know how to do it in R? I know it can be done in ArcGIS but I have no access to it now.


Solution

  • You didn't give any details where you got your shapefile from, but I got one from here and you can use gCentroid from rgeos thusly:

    library(rgdal)
    library(sp)
    library(rgeos)
    
    nebraska <- readOGR("CountyBoundsUTM/", "CountyUTM")
    
    gCentroid(nebraska, byid=TRUE)
    
    ## SpatialPoints:
    ##           x       y
    ## 0  721768.5 4636738
    ## 1  430938.8 4524651
    ## 2  698036.4 4566570
    ## 3  370970.6 4641340
    ## ...
    ## 89 623301.6 4603228
    ## 90 618883.0 4486931
    ## 91 439295.3 4582756
    ## 92 493680.8 4522680
    ## Coordinate Reference System (CRS) arguments: +proj=utm +zone=14 +datum=NAD83
    ## +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0