R Shapefile – Transforming Longitude Latitude Coordinates

coordinateslatitude longitudershapefile

I want to find out the coordinates (latitude and longitude) of Texas precincts. I download the shapefile from here.

I am using R to read the file and extract the coordinates:

dsn.tx <- "~/.../Texas.shp"        
shp <- readOGR(dsn=dsn.tx, stringsAsFactor = F)
coords <- coordinates(shp)

But the resulting coordinates are in an unusual format:

    X         Y 

 1411246  864457.9
 1266383 1231047.3
 1213326  905663.0
 1307353 1213580.7
 1168558  814300.4

I am not sure why this happens. In other (not sure if related) posts some suggested it may be related to the proj4.

How do I transform the resultant coordiantes into "standard" longitude latitude coordinates?

Best Answer

With sf (more modern R spatial toolkit):

library(sf)

tx <- st_read("~/Downloads/Texas_Shapefile/Texas_VTD.shp", stringsAsFactors=FALSE)
## Reading layer `Texas_VTD' from data source `/Users/bob/Downloads/Texas_Shapefile/Texas_VTD.shp' using driver `ESRI Shapefile'
## Simple feature collection with 8400 features and 21 fields
## geometry type:  POLYGON
## dimension:      XY
## bbox:           xmin: 372991.6 ymin: 412835.3 xmax: 1618133 ymax: 1594958
## epsg (SRID):    NA
## proj4string:    +proj=lcc +lat_1=34.91666666666666 +lat_2=27.41666666666667 +lat_0=31.16666666666667 +lon_0=-100 +x_0=1000000 +y_0=1000000 +datum=NAD83 +units=m +no_defs

tx_ll <- st_transform(tx, "+proj=longlat +ellps=WGS84 +datum=WGS84")

head(st_coordinates(tx_ll))
##              X        Y L1 L2
## [1,] -97.72317 30.40926  1  1
## [2,] -97.72264 30.40920  1  1
## [3,] -97.72255 30.40920  1  1
## [4,] -97.72202 30.40927  1  1
## [5,] -97.72187 30.40931  1  1
## [6,] -97.72156 30.40941  1  1

With legacy tooling:

library(sp)
library(rgdal)

tx <- readOGR("~/Downloads/Texas_Shapefile/Texas_VTD.shp", stringsAsFactors=FALSE)
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/bob/Downloads/Texas_Shapefile/Texas_VTD.shp", layer: "Texas_VTD"
## with 8400 features
## It has 21 fields
## Integer64 fields read as strings:  CNTY COLOR VTDKEY CNTYKEY Gov_D_02 Gov_R_02 Pres_D_04 Pres_R_04 Gov_D_06 Gov_R_06 Pres_D_08 Pres_R_08 Gov_D_10 Gov_R_10 vap 

proj4string(tx)
## [1] "+proj=lcc +lat_1=34.91666666666666 +lat_2=27.41666666666667 +lat_0=31.16666666666667 +lon_0=-100 +x_0=1000000 +y_0=1000000 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0"

tx_ll <- spTransform(tx, CRS("+proj=longlat +ellps=WGS84 +datum=WGS84"))

head(coordinates(tx_ll))
##        [,1]     [,2]
## 0 -97.71355 30.40027
## 1 -95.73430 29.86986
## 2 -97.13773 33.22311
## 3 -97.77786 30.29460
## 4 -96.70334 33.05514
## 5 -98.25908 29.47624