R Map CSV& SHP

时间:2017-11-16 15:26:07

标签: r csv gis shapefile

我试图绘制纬度& shapefile顶部的经度坐标。我被告知他们有相同的坐标系(NAD27)。在研究之后我尝试了很多方法,但没有任何方法可以完美运行。它们要么映射一个,要么映射两个,但不是两个。以下是阅读它们的示例,但我甚至不知道如何开始绘制,因为我的csv来自sp包,而shp来自maptools包。

pts <- read.csv("locations.csv")
pts <- pts[,4:5]
names(pts) <- c("Latitude", "Longitude")
pts <- pts[complete.cases(pts),]
pts <- SpatialPoints(pts)
border <- maptools::readShapePoly("landman_one shape.shp")

非常感谢任何帮助。

以下是不同的结构。

> str(pts)
Formal class 'SpatialPoints' [package "sp"] with 3 slots
  ..@ coords     : num [1:372, 1:2] 30.9 30.9 31 31 31 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:372] "1" "2" "3" "4" ...
  .. .. ..$ : chr [1:2] "Latitude" "Longitude"
  ..@ bbox       : num [1:2, 1:2] 29.5 -105.2 36 -76.1
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:2] "Latitude" "Longitude"
  .. .. ..$ : chr [1:2] "min" "max"
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
  .. .. ..@ projargs: chr NA

> str(border)
Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
  ..@ data       :'data.frame': 1 obs. of  2 variables:
  .. ..$ Id       : int 0
  .. ..$ PERIM_GEO: num 998432
  .. ..- attr(*, "data_types")= chr [1:2] "N" "F"
  ..@ polygons   :List of 1
  .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
  .. .. .. ..@ Polygons :List of 1
  .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
  .. .. .. .. .. .. ..@ labpt  : num [1:2] 922511 545445
  .. .. .. .. .. .. ..@ area   : num 3.45e+10
  .. .. .. .. .. .. ..@ hole   : logi FALSE
  .. .. .. .. .. .. ..@ ringDir: int 1
  .. .. .. .. .. .. ..@ coords : num [1:252, 1:2] 954182 954073 954006 953914 953828 ...
  .. .. .. ..@ plotOrder: int 1
  .. .. .. ..@ labpt    : num [1:2] 922511 545445
  .. .. .. ..@ ID       : chr "0"
  .. .. .. ..@ area     : num 3.45e+10
  ..@ plotOrder  : int 1
  ..@ bbox       : num [1:2, 1:2] 819851 386743 1042230 669865
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:2] "x" "y"
  .. .. ..$ : chr [1:2] "min" "max"
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
  .. .. ..@ projargs: chr NA

另外,我对预测并不太熟悉,因此任何有关预测的指导都会受到赞赏。此外,如果有一种方法可以使用相同的包装,我会对此持开放态度。

1 个答案:

答案 0 :(得分:2)

问题在于,您的空间数据集都没有投影集并且使用不同的坐标系。您可以在@proj4string行中看到这一点,这两行当前都是NA。确定坐标系不同的快速方法是@bbox插槽(&#39;边界框&#39;)。在pts中,这些是经度/纬度,因为它们分别在-180 / + 180和-90 / + 90之内。另一方面,border的值具有完全不同的单位(819851 386743 1042230 669865)。这些都看起来都不在NAD27坐标系中。

要解决此问题,首先我会使用rgdal::readOGR()sf::read_sf()来读取您的border形状文件。如果存在这些函数,这些函数将加载到投影中,因此这应该告诉您此数据的投影内容。

其次,要设置pts的预测,您可以使用proj4string()

proj4string(pts) = CRS("+init=epsg:4326")

ptsborder都有投影集后,您可以使用spTransform()将其转换为另一个坐标系。由于我不知道border的CRS /投影是什么,我将其转换为WGS84(pts的投影),但只是交换它们以转换pts一旦你知道它的CRS就会与border相同的投影。

border = spTransform(border, CRS("+init=epsg:4326"))

一旦进入相同的投影,他们应该一起绘制。您可以确认他们的预测符合:

proj4string(border) == proj4string(pts)
相关问题