从R中的空间对象获取经度和纬度
我想从shapefile获取纬度和经度.到目前为止,我只知道如何读取shapefile.
I want to obtain the latitude and longitude from a shapefile. Until now, I only know how to read the shapefile.
library(rgdal)
centroids.mp <- readOGR(".","35DSE250GC_SIR")
但是如何从centroids.mp中提取纬度和经度?
But how I can extract the latitude and longitude from centroids.mp?
此问题有几个层次.
您要求的经度和纬度,但这可能不是该对象使用的坐标系.你可以得到这样的坐标
You ask for longitude and latitude, but that may not be the coordinate system used by this object. You can get the coordinates like this
coordinates(centroids.mp)
请注意,如果这是SpatialPointsDataFrame,则质心"将是所有坐标;如果这是SpatialLinesDataFrame,则将是所有线坐标的列表;如果这是SpatialPolygonsDataFrame,则将是质心.
Note that the "centroids" will be all of the coordinates if this is a SpatialPointsDataFrame, a list of all the line coordinates if this is a SpatialLinesDataFrame, and just the centroids if this is a SpatialPolygonsDataFrame.
坐标可能是经度和纬度,但是对象可能不知道.使用
The coordinates may be longitude and latitude, but the object may not know that. Use
proj4string(centroids.mp)
如果这是"NA",则对象不知道(A).如果包含"+ proj = longlat",则对象确实知道并且它们是经度/纬度(B).如果它包含"+ proj =和其他名称(不是"longlat"),则该对象确实知道并且不是经度/纬度(C).
If that is "NA", then the object does not know (A). If it includes "+proj=longlat", the object does know and they are longitude/latitude (B). If it includes "+proj=" and some other name (not "longlat") then the object does know and it's not longitude/latitude (C).
如果必须(A),您必须找出答案,否则从值中可能会很明显.
If (A) you'll have to find out, or it might be obvious from the values.
如果(B)已完成(尽管您应该先检查假设,但这些元数据可能不正确).
If (B) you are done (though you should check assumptions first, these metadata can be incorrect).
如果(C),您可以(虽然应该先检查假设,但还是非常可靠的)转换为经度纬度(在WGS84基准面上),如下所示:
If (C) you can (pretty reliably though you should check assumptions first) transform to longitude latitude (on datum WGS84) like this:
coordinates(spTransform(centroids.mp, CRS("+proj=longlat +datum=WGS84")))