Calculate the longitude / latitude of the geographical center of gravity

I want to do some spatial statistical analysis with county level yield data in Nebraska for the STAT class. To do this, I need the longitude and latitude of the geographic centroids of each district. Does anyone know how to do this in R? I know this can be done in ArcGIS, but now I do not have access to it.

0
source share
3 answers

You did not specify any details in which you received your shapefile, but I got one from here , and you can use gCentroid from rgeos this way:

 library(rgdal) library(sp) library(rgeos) nebraska <- readOGR("CountyBoundsUTM/", "CountyUTM") gCentroid(nebraska, byid=TRUE) ## SpatialPoints: ## xy ## 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 
+3
source

You can also extract the centroids of SpatialPolygons* objects using coordinates , although centroids will not return as SpatialPoints , as in rgeos::gCentroid .

For instance:

 library(rgdal) download.file('http://dds.cr.usgs.gov/pub/data/nationalatlas/countyp020_nt00009.tar.gz', f <- tempfile()) # ~ 4.5 Mb untar(f, exdir=tempdir()) counties <- readOGR(tempdir(), 'countyp020') xy <- coordinates(counties) head(xy) # [,1] [,2] # 0 -153.3905 69.30193 # 1 -156.0582 71.33094 # 2 -155.6695 71.24763 # 3 -155.5164 71.23148 # 4 -155.1846 71.18189 # 5 -155.6126 71.00725 

Note that, as @Spacedman points out in the comments, polygons must first be projected onto a flat coordinate system.

0
source

You can use the get_map() function from the ggplot2 package to extract US county map data from the maps package into a data framework. You can then calculate the midpoints of the lat / lon column ranges by county (or any other method that you want to use to determine the geographical center).

-2
source

All Articles