Convert latitude/longitude to state plane coordinates

怎甘沉沦 提交于 2019-12-01 04:51:53

When you set the coordinates for your data, you have to set the latitude before the longitude.

In other words, change:

coordinates(data) <- ~ long + lat

to

coordinates(data) <- ~ lat+long

And it should work.

library(rgdal)
library(sp)
data = data.frame(long=c(41.20,40.05), lat=c(-86.14,-88.15))
coordinates(data) <- ~ lat+long
proj4string(data) <- CRS("+init=epsg:4326")
data.proj <- spTransform(data, CRS("+init=epsg:2790"))
data.proj

Gave me this output:

SpatialPoints:
          lat     long
[1,] 483979.0 505572.6
[2,] 315643.7 375568.0
Coordinate Reference System (CRS) arguments: +init=epsg:2790 +proj=tmerc
+lat_0=36.66666666666666 +lon_0=-88.33333333333333 +k=0.9999749999999999 +x_0=300000
+y_0=0 +ellps=GRS80 +units=m +no_defs 

Here's some more working code that clarifies what's going on:

# convert a state-plane coordinate to lat/long
data = data.frame(x=400000,y=0)
coordinates(data) <- ~ x+y
proj4string(data) <- CRS("+init=epsg:2804")
latlong = data.frame(spTransform(data, CRS("+init=epsg:4326")))
setnames(latlong,c("long","lat"))
latlong

gives:

  long      lat
 1  -77 37.66667

and:

# convert a lat/long to state-plane
data = latlong
coordinates(data) <- ~ long+lat
proj4string(data) <- CRS("+init=epsg:4326")
xy = data.frame(spTransform(data, CRS("+init=epsg:2804")))
setnames(xy,c("y","x"))
xy

gives:

> xy
      y             x
1 4e+05 -2.690839e-08

And here's a function:

# this is for Maryland
lat_long_to_xy = function(lat,long) {
  library(rgdal)
  library(sp)
  data = data.frame(long=long, lat=lat)
  coordinates(data) <- ~ long+lat
  proj4string(data) <- CRS("+init=epsg:4326")
  xy = data.frame(spTransform(data, CRS("+init=epsg:2804")))
  setnames(xy,c("y","x"))
  return(xy[,c("x","y")])
}
Charlotte

I had an issue converting in the other direction and found this response on GIS Stack Exchange which may be helpful to future seekers. Depending on whether your coordinate system is NAD83 or NAD83 (HARN), the spatial reference epsg code will differ. If you use the wrong system, it may not be able to convert the point to values beyond the limits of any coordinate plane.

https://gis.stackexchange.com/questions/64654/choosing-the-correct-value-for-proj4string-for-shape-file-reading-in-r-maptools

Reading in lat+long versus long+lat does make a difference in the output- in my case (going from State Plane to WGS84) I had to write

coordinates(data) <- ~ long+lat

You can confirm this by plotting a known reference point to determine if it converted correctly.

The esri code (102649) in rgdal didn't work for me, I had to manually code it in from the proj4js page to go from state plane (0202 Arizona Central) to WGS84:

d<- data.frame(lon=XCord, lat=YCord)
coordinates(d) <- c("lon", "lat")
proj4string(d) <- CRS("+proj=tmerc +lat_0=31 +lon_0=-111.9166666666667 +k=0.9999 +x_0=213360 +y_0=0 +ellps=GRS80 +datum=NAD83 +to_meter=0.3048006096012192 +no_defs")
CRS.new <- CRS("+init=epsg:4326") # WGS 84
d.ch102649 <- spTransform(d, CRS.new)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!