我一直在按照这个例子计算英国的最小距离。因此,我使用EPSG:27700作为英国的坐标参考系统,它具有以下投影字符串:
"+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +datum=OSGB36 +units=m +no_defs"
然而,我不确定应该遵循哪个wgs.84代码。目前我正在使用:
"+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
我也尝试使用了datum=OSGB36和+ellps=airy。
完整的代码如下:
library(rgeos)
library(maptools)
library(rgdal)
epsg.27700 <- '+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +datum=OSGB36 +units=m +no_defs'
wgs.84 <- '+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0'
coast <- readShapeLines("ne_10m_coastline",CRS(wgs.84)) #have tried other shapefiles with the same issue
MAD <- readWKT("POINT(-0.1830372 51.1197467)",p4s=CRS(wgs.84)) #Crawley, West Sussex
gDistance(MAD,coast)
[1] 0.28958
Warning messages:
1: In RGEOSDistanceFunc(spgeom1, spgeom2, byid, "rgeos_distance") :
Spatial object 1 is not projected; GEOS expects planar coordinates
2: In RGEOSDistanceFunc(spgeom1, spgeom2, byid, "rgeos_distance") :
Spatial object 2 is not projected; GEOS expects planar coordinates
3: In RGEOSDistanceFunc(spgeom1, spgeom2, byid, "rgeos_distance") :
spgeom1 and spgeom2 have different proj4 strings
尝试完成投影线时出现错误提示。
coast.proj <- spTransform(coast,CRS(Epsg.27700))
non finite transformation detected:
[1] 111.01051 19.68378 Inf Inf
Error in .spTransform_Line(input[[i]], to_args = to_args, from_args = from_args, :
failure in Lines 22 Line 1 points 1
In addition: Warning message:
In .spTransform_Line(input[[i]], to_args = to_args, from_args = from_args, :
671 projected point(s) not finite
我在这里做错了什么,我有些困惑。
spatialPoints
对象中(为方便起见,我们再次称之为MAD
),然后将其转换为MAD.proj
,然后执行以下操作:sapply(1:length(MAD.proj),function(i)gDistance(MAD.proj[i],coast.proj))
。这将生成一个距离向量,表示每个点到最近海岸的距离。 - jlhowardMAD.proj <- spTransform(points,CRS(epsg.27700))
,显示无法从NA参考系统进行转换
。不确定我错过了什么? - MikeMspTransform(sp,...)
将投影从一个转换到另一个。它必须知道起始CRS,这意味着sp
必须有一个定义的CRS。如果您的点在名为df
的矩阵或数据框中,请尝试使用以下代码:MAD <- SpatialPoints(df,proj4string=CRS(wgs.84))
。然后spTransform(...)
应该可以工作了。 - jlhoward