sta*_*lax 7 gis r latitude-longitude rgdal
我有一个纬度和经度的数据集,我想转换为伊利诺斯东部的州平面坐标,使用EPSG 2790(http://spatialreference.org/ref/epsg/2790/)或者ESRI 102672(http://spatialreference.org/ref/esri/102672/).
这肯定在之前被问过; 我的代码基于这里的答案(rgdal R包中的spTransform中的"非有限变换检测"和http://r-sig-geo.2731867.n2.nabble.com/Converting-State-Plane-Coordinates-td5457204. HTML).
但由于某种原因,我不能让它工作:
library(rgdal)
library(sp)
data = data.frame(long=c(41.20,40.05), lat=c(-86.14,-88.15))
coordinates(data) <- ~ long + lat
proj4string(data) <- CRS("+init=epsg:4326") # latitude/longitude
data.proj <- spTransform(data, CRS("+init=epsg:2790")) # illinois east
Run Code Online (Sandbox Code Playgroud)
得到:
non finite transformation detected:
long lat
41.20 -86.14 Inf Inf
Error in spTransform(data, CRS("+init=epsg:2790")) : failure in points 1
In addition: Warning message:
In spTransform(data, CRS("+init=epsg:2790")) :
2 projected point(s) not finite
Run Code Online (Sandbox Code Playgroud)
下面是一些更多的工作代码,可以阐明正在发生的事情:
# 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
Run Code Online (Sandbox Code Playgroud)
给出:
long lat
1 -77 37.66667
Run Code Online (Sandbox Code Playgroud)
和:
# 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
Run Code Online (Sandbox Code Playgroud)
给出:
> xy
y x
1 4e+05 -2.690839e-08
Run Code Online (Sandbox Code Playgroud)
这是一个函数:
# 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")])
}
Run Code Online (Sandbox Code Playgroud)
当您为数据设置 时coordinates,必须在经度之前设置纬度。
换句话说,改变:
coordinates(data) <- ~ long + lat
到
coordinates(data) <- ~ lat+long
它应该有效。
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
Run Code Online (Sandbox Code Playgroud)
给了我这个输出:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7828 次 |
| 最近记录: |