将R中的Multipolygon SF对象从XY坐标转换为lat / lon

时间:2017-07-07 18:27:35

标签: r sf

我有一个shapefile,坐标是(我认为)NAD_1983_StatePlane_New_York_Long_Island_FIPS_3104_Feet(见Spatial Reference here )。

我想在使用sf包时读取shapefile,然后将MULTIPOLYGON几何转换为lat / lon坐标。

具体来说,我想转换为:

纽约长岛国家平面投影 见:http://spatialreference.org/ref/esri/102718/

示例:

library(tidyverse)
library(sf)
    x<-
structure(list(Borough = "MN", Block = 1L, Lot = 201L, ZipCode = 10004L, 
Address = "1 ELLIS ISLAND", UnitsRes = 0L, YearBuilt = 1900L, 
XCoord = 972790L, YCoord = 193648L, geometry = structure(list(
structure(list(list(structure(c(973154.711799994, 973733.157600001, 
973685.917400002, 973648.663599998, 973648.052399993, 
973525.341800004, 973489.243000001, 973434.629199997, 
973338.158999994, 973282.610799998, 973265.806400001, 
973246.2016, 973221.928200006, 973190.808799997, 973150.042600006, 
973064.464000002, 973003.781800002, 973005.960199997, 
972911.979800001, 972812.709000006, 972728.686399996, 
972671.738399997, 972693.988600001, 972697.255999997, 
973180.850199997, 973178.983400002, 973156.266399994, 
973078.467999995, 972940.609200001, 972792.169799998, 
972726.197400004, 972696.944999993, 972695.077199996, 
972499.959600002, 972248.203799993, 972136.797000006, 
972147.688600004, 972141.775800005, 972405.979200006, 
972333.160400003, 972499.959600002, 972676.084199995, 
972860.2456, 972904.511000007, 972936.564400002, 972963.794, 
973154.711799994, 194614.331599995, 194006.278799996, 
193958.042199999, 193712.475600004, 193711.894199997, 
193713.444399998, 193713.755799994, 193713.755799994, 
193619.152199998, 193672.832800001, 193687.926599994, 
193707.220599994, 193730.870800003, 193761.989800006, 
193801.512400001, 193885.534199998, 193945.282800004, 
193949.0176, 194040.508599997, 194138.845400006, 194053.889799997, 
193996.007799998, 193973.445800006, 193970.179000005, 
193471.335999995, 193469.468199998, 193449.708000004, 
193380.777600005, 193251.3222, 193107.862000003, 193043.755999997, 
193014.815200001, 193016.3706, 193216.468199998, 193474.136399999, 
193588.033199996, 193598.302200004, 193603.904200003, 
193869.974000007, 193941.548800007, 194116.750200003, 
194301.772200003, 194476.966399997, 194434.790399998, 
194403.671399996, 194430.277799994, 194614.331599995), .Dim = c(47L, 
2L)))), class = c("XY", "MULTIPOLYGON", "sfg"))), n_empty = 0L, crs = structure(list(
epsg = 4326L, proj4string = "+proj=longlat +datum=WGS84 +no_defs"), .Names = c("epsg", 
"proj4string"), class = "crs"), class = c("sfc_MULTIPOLYGON", 
"sfc"), precision = 0, bbox = structure(c(972136.797000006, 
193014.815200001, 973733.157600001, 194614.331599995), .Names = c("xmin", 
"ymin", "xmax", "ymax"), class = "bbox", crs = structure(list(
epsg = 4326L, proj4string = "+proj=longlat +datum=WGS84 +no_defs"), .Names = c("epsg", 
"proj4string"), class = "crs")))), .Names = c("Borough", 
"Block", "Lot", "ZipCode", "Address", "UnitsRes", "YearBuilt", 
"XCoord", "YCoord", "geometry"), sf_column = "geometry", agr = structure(c(NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_), .Names = c("Borough", 
"Block", "Lot", "ZipCode", "Address", "UnitsRes", "YearBuilt", 
"XCoord", "YCoord"), .Label = c("constant", "aggregate", "identity"
), class = "factor"), row.names = 1L, class = c("sf", "data.frame"
))


class(x)
[1] "sf"         "data.frame"

不起作用:

st_transform(select(x,geometry),4326)

Simple feature collection with 1 feature and 0 fields
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: 972136.8 ymin: 193014.8 xmax: 973733.2 ymax: 194614.3
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs
                        geometry
1 MULTIPOLYGON(((973154.71179...

1 个答案:

答案 0 :(得分:2)

在我看来:

  1. 输入数据的参考系统未正确设置为epsg:4326

      

    X
      简单的功能集合,1个功能和9个字段
          几何类型:MULTIPOLYGON
          尺寸:XY
          bbox:xmin:972136.8 ymin:193014.8 xmax:973733.2 ymax:194614.3
          epsg(SRID):4326
          proj4string:+ proj = longlat + datum = WGS84 + no_defs

  2. 你不想重新投影到“纽约长岛国家平面投射”#34;如你的问题中所述(顺便说一句,这似乎是你输入的投影),但是对于纬度/经度4326.

  3. 要解决您的问题,您可以使用:

    # Set the correct projection to the input ([http://www.spatialreference.org/ref/esri/102718/][1])
    
    sf::st_crs(x) <- "+proj=lcc +lat_1=40.66666666666666 +lat_2=41.03333333333333 +lat_0=40.16666666666666 +lon_0=-74 +x_0=300000 +y_0=0 +ellps=GRS80 +datum=NAD83 +to_meter=0.3048006096012192 +no_defs"
    
    # now that you have the correct epsg set, reproject to 4326
    
    x_repr = st_transform(select(x,geometry), 4326)
    
    > x_repr
    Simple feature collection with 1 feature and 0 fields
    geometry type:  MULTIPOLYGON
    dimension:      XY
    bbox:           xmin: -74.04368 ymin: 40.69645 xmax: -74.03793 ymax: 40.70084
    epsg (SRID):    4326
    proj4string:    +proj=longlat +datum=WGS84 +no_defs
                            geometry
    1 MULTIPOLYGON(((-74.04001513...
    

    (我通过使用mapview绘图来检查,它对应于埃利斯岛)

    HTH