为什么我的点图与 shapefile 的方向不同?

Why are my point plots not in the same direction as my shapefile?

我正在使用 R,使用 rastersf 包。我正在从 .csv 文件加载一些数据,将其转换为 sf object,然后绘制数据以将其可视化。但是,当我绘制数据框时,这些点旋转了 90 度,这与原始 shapefile 的方向不同。我想稍后做一些插值,所以我需要解决这个问题。我怎样才能使他们的方向一致?

这是数据:

data <- data.frame(latitude = c(39.120549,
                                39.120399,
                                39.120339,
                                39.120463,
                                39.120478,
                                39.120368,
                                39.120405,
                                39.120279,
                                39.120257,
                                39.120178,
                                39.120168,
                                39.120137,
                                39.119995,
                                39.119912,
                                39.119806,
                                39.119848,
                                39.119858,
                                39.119941,
                                39.120056,
                                39.120082,
                                39.120252,
                                39.120278,
                                39.120335,
                                39.120323,
                                39.120273,
                                39.120306,
                                39.120224,
                                39.120152,
                                39.120096,
                                39.120144,
                                39.120091,
                                39.120073,
                                39.119971,
                                39.119939,
                                39.12002,
                                39.120252,
                                39.120182,
                                39.120295,
                                39.120308,
                                39.120215,
                                39.12026,
                                39.120221,
                                39.12026,
                                39.120185,
                                39.120122,
                                39.119996,
                                39.119947,
                                39.120035,
                                39.119964,
                                39.119989,
                                39.12,
                                39.120101,
                                39.12008,
                                39.120059,
                                39.119993,
                                39.11986,
                                39.119758,
                                39.119736,
                                39.119793,
                                39.119886,
                                39.1199,
                                39.119829,
                                39.119853,
                                39.119855,
                                39.119826,
                                39.119583,
                                39.119667,
                                39.119707,
                                39.119657,
                                39.119666,
                                39.119704,
                                39.11971,
                                39.119761,
                                39.119789,
                                39.119783,
                                39.119688,
                                39.119674,
                                39.11962,
                                39.119591,
                                39.119568,
                                39.119536,
                                39.119514,
                                39.119539,
                                39.119577,
                                39.119657,
                                39.119681,
                                39.119713,
                                39.119745,
                                39.119763,
                                39.119733,
                                39.11969,
                                39.119645,
                                39.119336,
                                39.11931,
                                39.119467,
                                39.119597,
                                39.11948,
                                39.11938,
                                39.119444,
                                39.119553,
                                39.119489,
                                39.119529,
                                39.119444,
                                39.119408,
                                39.119381,
                                39.119368,
                                39.119283,
                                39.119386,
                                39.119466,
                                39.119445,
                                39.119295,
                                39.119242,
                                39.119071,
                                39.119174,
                                39.119178,
                                39.119137,
                                39.119107,
                                39.119039,
                                39.118993,
                                39.119045,
                                39.11914,
                                39.119175,
                                39.119055,
                                39.119108,
                                39.119115,
                                39.119205,
                                39.119259,
                                39.119309,
                                39.119228,
                                39.119209,
                                39.119335,
                                39.11925,
                                39.119039,
                                39.119036,
                                39.119005,
                                39.119105,
                                39.119187,
                                39.119116,
                                39.119074,
                                39.119135,
                                39.119053,
                                39.119103,
                                39.119093,
                                39.119165,
                                39.119164,
                                39.119185,
                                39.119283,
                                39.119287,
                                39.119185,
                                39.119276),
                   longitude = c(-96.635806,
                                 -96.635783,
                                 -96.635664,
                                 -96.635696,
                                 -96.635785,
                                 -96.635589,
                                 -96.635697,
                                 -96.635819,
                                 -96.635649,
                                 -96.635772,
                                 -96.635606,
                                 -96.635669,
                                 -96.635644,
                                 -96.635631,
                                 -96.635662,
                                 -96.635791,
                                 -96.635706,
                                 -96.635769,
                                 -96.635803,
                                 -96.635684,
                                 -96.635718,
                                 -96.635574,
                                 -96.635544,
                                 -96.635418,
                                 -96.635403,
                                 -96.635486,
                                 -96.635474,
                                 -96.635347,
                                 -96.635395,
                                 -96.635486,
                                 -96.635558,
                                 -96.635453,
                                 -96.635453,
                                 -96.635544,
                                 -96.63554,
                                 -96.635381,
                                 -96.635392,
                                 -96.635346,
                                 -96.63522,
                                 -96.635108,
                                 -96.635244,
                                 -96.635294,
                                 -96.63534,
                                 -96.635182,
                                 -96.635273,
                                 -96.635179,
                                 -96.635325,
                                 -96.635289,
                                 -96.635225,
                                 -96.635308,
                                 -96.635214,
                                 -96.635233,
                                 -96.635307,
                                 -96.635364,
                                 -96.635386,
                                 -96.635386,
                                 -96.63541,
                                 -96.635503,
                                 -96.635585,
                                 -96.635567,
                                 -96.635473,
                                 -96.635438,
                                 -96.635535,
                                 -96.635487,
                                 -96.635488,
                                 -96.635425,
                                 -96.635502,
                                 -96.635428,
                                 -96.635408,
                                 -96.635475,
                                 -96.635532,
                                 -96.635587,
                                 -96.635634,
                                 -96.635733,
                                 -96.63578,
                                 -96.63577,
                                 -96.635706,
                                 -96.635695,
                                 -96.635631,
                                 -96.63556,
                                 -96.635537,
                                 -96.635491,
                                 -96.635446,
                                 -96.635499,
                                 -96.635597,
                                 -96.635661,
                                 -96.635692,
                                 -96.635776,
                                 -96.635708,
                                 -96.63565,
                                 -96.635597,
                                 -96.635543,
                                 -96.635396,
                                 -96.63564,
                                 -96.635763,
                                 -96.63579,
                                 -96.635626,
                                 -96.635705,
                                 -96.635719,
                                 -96.635709,
                                 -96.635696,
                                 -96.635618,
                                 -96.635531,
                                 -96.635635,
                                 -96.635514,
                                 -96.635363,
                                 -96.635515,
                                 -96.635447,
                                 -96.635468,
                                 -96.635332,
                                 -96.635301,
                                 -96.635439,
                                 -96.635335,
                                 -96.635285,
                                 -96.635407,
                                 -96.635327,
                                 -96.635456,
                                 -96.635513,
                                 -96.635735,
                                 -96.635771,
                                 -96.63578,
                                 -96.635602,
                                 -96.635627,
                                 -96.635526,
                                 -96.635655,
                                 -96.635749,
                                 -96.635634,
                                 -96.635744,
                                 -96.635716,
                                 -96.635539,
                                 -96.635562,
                                 -96.635358,
                                 -96.635277,
                                 -96.635063,
                                 -96.634839,
                                 -96.634792,
                                 -96.634912,
                                 -96.634947,
                                 -96.634863,
                                 -96.634873,
                                 -96.634981,
                                 -96.635073,
                                 -96.635195,
                                 -96.635249,
                                 -96.635134,
                                 -96.634946,
                                 -96.635006,
                                 -96.635084,
                                 -96.634986,
                                 -96.635169),
                   data = c(2.74,
                            2.31,
                            2.79,
                            2.48,
                            2.62,
                            2.42,
                            2.62,
                            2.52,
                            2.32,
                            2.56,
                            2.57,
                            2.45,
                            2.62,
                            2.49,
                            2.44,
                            2.56,
                            2.77,
                            2.4,
                            2.42,
                            2.59,
                            2.36,
                            2.6,
                            2.38,
                            2.58,
                            2.52,
                            2.55,
                            2.5,
                            2.47,
                            2.8,
                            2.77,
                            2.66,
                            2.57,
                            2.64,
                            2.57,
                            2.51,
                            2.76,
                            2.39,
                            2.71,
                            2.76,
                            2.74,
                            2.44,
                            2.35,
                            2.54,
                            2.47,
                            2.65,
                            2.38,
                            2.54,
                            2.79,
                            2.52,
                            2.61,
                            2.72,
                            2.8,
                            2.78,
                            2.31,
                            2.37,
                            2.42,
                            2.68,
                            2.61,
                            2.46,
                            2.51,
                            2.79,
                            2.35,
                            2.38,
                            2.67,
                            2.48,
                            2.36,
                            2.77,
                            2.68,
                            2.38,
                            2.45,
                            2.69,
                            2.59,
                            2.68,
                            2.64,
                            2.59,
                            2.72,
                            2.37,
                            2.41,
                            2.46,
                            2.8,
                            2.65,
                            2.46,
                            2.46,
                            2.71,
                            2.63,
                            2.32,
                            2.35,
                            2.32,
                            2.42,
                            2.49,
                            2.69,
                            2.72,
                            2.49,
                            2.56,
                            2.5,
                            2.49,
                            2.64,
                            2.66,
                            2.54,
                            2.48,
                            2.68,
                            2.6,
                            2.35,
                            2.68,
                            2.74,
                            2.6,
                            2.74,
                            2.53,
                            2.78,
                            2.79,
                            2.33,
                            2.73,
                            2.58,
                            2.44,
                            2.61,
                            2.38,
                            2.5,
                            2.57,
                            2.32,
                            2.48,
                            2.52,
                            2.63,
                            2.76,
                            2.33,
                            2.37,
                            2.38,
                            2.4,
                            2.7,
                            2.56,
                            2.69,
                            2.63,
                            2.34,
                            2.74,
                            2.33,
                            2.33,
                            2.73,
                            2.4,
                            2.6,
                            2.48,
                            2.36,
                            2.58,
                            2.48,
                            2.59,
                            2.39,
                            2.76,
                            2.61,
                            2.69,
                            2.39,
                            2.48,
                            2.56))

重现错误的代码(此 post 中不包含 Shapefile,但输出的图片是):

library(pacman)
p_load(raster, sf, dplyr, ggplot2, scales, magrittr)

#Read shapefile
shp <- st_read("field_shapefile.shp")
crs(shp)

shp 的参考系统:

Coordinate Reference System:
Deprecated Proj.4 representation: +proj=longlat +datum=WGS84 +no_defs 
WKT2 2019 representation:
GEOGCRS["WGS 84",
    DATUM["World Geodetic System 1984",
        ELLIPSOID["WGS 84",6378137,298.257223563,
            LENGTHUNIT["metre",1]]],
    PRIMEM["Greenwich",0,
        ANGLEUNIT["degree",0.0174532925199433]],
    CS[ellipsoidal,2],
        AXIS["latitude",north,
            ORDER[1],
            ANGLEUNIT["degree",0.0174532925199433]],
        AXIS["longitude",east,
            ORDER[2],
            ANGLEUNIT["degree",0.0174532925199433]],
    ID["EPSG",4326]] 
#Read in data, which is provided above
data <- read.csv('data.csv')
plot(shp)

上面命令绘制的图像:

#Convert csv file to shapefile with the same projection as the input shapefile
data <- st_as_sf(data, coords = c('latitude', 'longitude'), crs = crs(shp))
crs(data)

crs(data) 命令表示两个数据集都投影在同一参考系统中:

Coordinate Reference System:
Deprecated Proj.4 representation: +proj=longlat +datum=WGS84 +no_defs 
WKT2 2019 representation:
GEOGCRS["WGS 84",
    DATUM["World Geodetic System 1984",
        ELLIPSOID["WGS 84",6378137,298.257223563,
            LENGTHUNIT["metre",1]]],
    PRIMEM["Greenwich",0,
        ANGLEUNIT["degree",0.0174532925199433]],
    CS[ellipsoidal,2],
        AXIS["geodetic latitude (Lat)",north,
            ORDER[1],
            ANGLEUNIT["degree",0.0174532925199433]],
        AXIS["geodetic longitude (Lon)",east,
            ORDER[2],
            ANGLEUNIT["degree",0.0174532925199433]],
    ID["EPSG",4326]] 

这是数据点图:

plot(data$geometry)

由于大小的原因,没有给出所有的点,但是你应该能看到形状与shapefile相似,只是旋转不同。我把它画进去是为了更清楚地说明这一点:

如何旋转这些点,使它们与我的 shapefile 方向相同?

在将 .csv 转换为 sf 时,您只需反转 latitudelongitude

所以,请在下面找到一个代表。

Reprex

  • 代码
library(sf)

data <- st_as_sf(data, coords = c('longitude', 'latitude'), crs = 4326)
  • 输出
plot(st_geometry(data))

reprex package (v2.0.1)

于 2022-03-24 创建