从R中的空间对象获取经度和纬度

问题描述 投票:1回答:3

我想从shapefile获取纬度和经度。到目前为止,我只知道如何读取shapefile。

library(rgdal)
centroids.mp <- readOGR(".","35DSE250GC_SIR")

但是如何从centroids.mp中提取纬度和经度?

r gdal rgdal
3个回答
9
投票

这个问题有几个层次。

您要求的经度和纬度,但这可能不是该对象使用的坐标系。您可以像这样获得坐标

   coordinates(centroids.mp)

请注意,如果这是SpatialPointsDataFrame,则“质心”将是所有坐标;如果这是SpatialLinesDataFrame,则将是所有线坐标的列表;如果这是SpatialPolygonsDataFrame,则将是质心。

坐标可能是经度和纬度,但是对象可能不知道。使用

   proj4string(centroids.mp) 

如果是“ NA”,则对象不知道(A)。如果它包含“ + proj = longlat”,则对象确实知道并且它们是经度/纬度(B)。如果它包含“ + proj =“和其他名称(不是“ longlat”),则该对象确实知道并且不是经度/纬度(C)。

如果是(A),您必须找出答案,否则从值中可能会很明显。

如果(B),您已经完成(尽管您应该先检查假设,但这些元数据可能不正确)。

如果是(C),则可以(虽然应该首先检查假设,但确实可靠)像这样转换为经度(在WGS84上):

 coordinates(spTransform(centroids.mp, CRS("+proj=longlat +datum=WGS84")))

4
投票

使用coordinates(),例如:

library(maptools)
xx <- readShapePoints(system.file("shapes/baltim.shp", package="maptools")[1])
coordinates(xx)
#     coords.x1 coords.x2
# 0       907.0     534.0
# 1       922.0     574.0
# 2       920.0     581.0
# 3       923.0     578.0
# 4       918.0     574.0
#       [.......]

0
投票

st_coordinates解决了问题,但是它删除了删除链接到sf对象坐标的协变量。如果您需要它们,我在这里分享了一个替代方法:

# useful enough
sites_sf %>%
  st_coordinates()
#>           X        Y
#> 1 -80.14401 26.47901
#> 2 -80.10900 26.83000

# alternative to keep covariates within a tibble/sf
sites_sf %>%
  st_coordinates_tidy()
#> Joining, by = "rowname"
#> Simple feature collection with 2 features and 3 fields
#> geometry type:  POINT
#> dimension:      XY
#> bbox:           xmin: -80.14401 ymin: 26.479 xmax: -80.109 ymax: 26.83
#> epsg (SRID):    4326
#> proj4string:    +proj=longlat +datum=WGS84 +no_defs
#> # A tibble: 2 x 4
#>   gpx_point     X     Y             geometry
#>   <chr>     <dbl> <dbl>          <POINT [°]>
#> 1 a         -80.1  26.5 (-80.14401 26.47901)
#> 2 b         -80.1  26.8      (-80.109 26.83)
© www.soinside.com 2019 - 2024. All rights reserved.