【问题标题】:Convert latitude/longitude points to map with geom_sf使用 geom_sf 将纬度/经度点转换为地图
【发布时间】:2020-08-29 16:28:49
【问题描述】:

我有一个纬度/经度点数据集,旨在转换为 R 中的简单特征 (sf)。 我的目标是使用从 urbnmapr 库中检索到的多边形在美国地图上绘制这些位置。

使用我们的地理参考进行绘图,如代码所示,会显示所有点。

当使用 geom_sf() 绘制点时,它们最终位于南达科他州。尽管我认为正确使用了 st_as_sf() 函数,但似乎纬度/经度点没有被转换为正确的坐标参考系。

需要对此代码进行哪些更正才能在美国地图上正确显示风力涡轮机位置的分布?

# Map the locations of US Wind Turbines
library(urbnmapr)
library(ggplot2)
library(readr)
library(dplyr)
library(sf)

# This file is available from https://eerscmap.usgs.gov/uswtdb/assets/data/uswtdbCSV.zip
turbine <- read_csv("C:\\mydir\\uswtdb_v3_1_20200717.csv")

# Convert lat/long to a sf
turbine_sf <- turbine %>%
  st_as_sf(coords = c("xlong", "ylat"), crs=2163)

# obtain state geometries
states_sf <- get_urbn_map(map = "states", sf = TRUE)

# Remove AK, HI from state and PR and GU from turbines as well
states_sf <- states_sf[!(states_sf$state_abbv %in% c("HI","AK")),]
turbine   <- turbine  [!(turbine$t_state      %in% c('HI','AK','PR','GU')),]

# simple plot shows all locations
ggplot(turbine, aes(x=xlong, y=ylat)) + geom_point()

#plot locations over map
  ggplot() +
  geom_sf(data = turbine_sf) + 
  geom_sf(data = states_sf, fill = NA, color = "black", size = 0.15, alpha = 0) +
  coord_sf(datum = st_crs(2163)) +   
  labs(fill  = "", 
       title = "",
       caption='') + 
  theme_bw()

【问题讨论】:

    标签: r sf


    【解决方案1】:

    您的涡轮机数据集包含以度为单位的“xlong”和“ylat”,即具有 WGS84 基准的地理坐标系(EPSG 代码:4326)。因此,首先将其设为crs = 4326,然后使用st_transform(turbine_sf, crs=2163)states_sf 建立相同的坐标系。您可以使用以下代码

    # Map the locations of US Wind Turbines
    library(urbnmapr)
    library(ggplot2)
    library(readr)
    library(dplyr)
    library(sf)
    
    # This file is available from https://eerscmap.usgs.gov/uswtdb/assets/data/uswtdbCSV.zip
    turbine <- read_csv("uswtdb_v3_1_20200717.csv")
    
    # Convert lat/long to a sf
    turbine_sf <- turbine %>%
      st_as_sf(coords = c("xlong", "ylat"), crs=4326)
    
    turbine_sf_t <- st_transform(turbine_sf, crs=2163)
    # obtain state geometries
    states_sf <- get_urbn_map(map = "states", sf = TRUE)
    
    st_crs(states_sf)
    # Remove AK, HI from state and PR and GU from turbines as well
    states_sf <- states_sf[!(states_sf$state_abbv %in% c("HI","AK")),]
    turbine   <- turbine  [!(turbine$t_state      %in% c('HI','AK','PR','GU')),]
    
    # simple plot shows all locations
    ggplot(turbine, aes(x=xlong, y=ylat)) + geom_point()
    
    #plot locations over map
    ggplot() +
      geom_sf(data = turbine_sf_t) + 
      geom_sf(data = states_sf, fill = NA, color = "black", size = 0.15, alpha = 0) +
      coord_sf(datum = st_crs(2163)) +   
      labs(fill  = "", 
           title = "",
           caption='') + 
      theme_bw()
    

    【讨论】:

      【解决方案2】:

      通过st_as_sf(coords = c("xlong", "ylat"), crs=2163) 你是说你的turbine 表中的 original long, lat 基于 2163 的 CRS。我认为你想将它们设置为 4326,即WGS84 下的长纬度。

      设置初始 CRS 后,使用 st_transform() 将形状的 CRS 转换为新的 CRS,例如turbine_sf &lt;- st_transform(turbine_sf, crs=2163)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-09
        相关资源
        最近更新 更多