简体   繁体   English

使用 geom_sf 将纬度/经度点转换为地图

[英]Convert latitude/longitude points to map with geom_sf

I have a data set of latitude/longitude points that seek to convert to a simple feature (sf) in R. My goal is to plot these locations on a US map with polygons retrieved from the urbnmapr library.我有一个纬度/经度点的数据集,试图在 R 中转换为一个简单的特征 (sf)。我的目标是在美国地图上绘制这些位置,并使用从 urbnmapr 库中检索到的多边形。

Plotting with our a geographic reference, as shown in the code, results in all points being displayed.使用我们的地理参考绘图,如代码所示,会显示所有点。

When the points are plotted using geom_sf() they end up in South Dakota.当使用 geom_sf() 绘制点时,它们最终位于南达科他州。 It seems the latitude/longitude points are not being converted into the correct coordinate reference system, despite what I think is the correct use of the st_as_sf() function.尽管我认为正确使用了 st_as_sf() 函数,但似乎纬度/经度点并未转换为正确的坐标参考系。

What correction needs to be made to this code to show the distribution of wind turbine locations properly on the US map?需要对此代码进行哪些更正才能在美国地图上正确显示风力涡轮机位置的分布? 当前地图结果

# 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()

Your turbine dataset contains "xlong" and "ylat" in degrees ie geographic coordinate system with WGS84 datum (EPSG code: 4326).您的涡轮机数据集包含以度为单位的“xlong”和“ylat”,即具有 WGS84 基准(EPSG 代码:4326)的地理坐标系。 So, first, make it as crs = 4326 and then use st_transform(turbine_sf, crs=2163) to make same coordinate system with states_sf .因此,首先,将其设为crs = 4326 ,然后使用st_transform(turbine_sf, crs=2163)states_sf相同的坐标系。 You can use the following code您可以使用以下代码

# 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()

在此处输入图片说明

By doing st_as_sf(coords = c("xlong", "ylat"), crs=2163) you're saying that the original long, lat from your turbine table are based on CRS of 2163. I think you want to set them as 4326 which is the long lat under WGS84.通过执行st_as_sf(coords = c("xlong", "ylat"), crs=2163)您是说turbine表中的原始长经纬度基于 2163 的 CRS。我认为您想将它们设置为4326 这是 WGS84 下的长纬度。

After setting the initial CRS, use st_transform() to transform the CRS of your shape to new CRS, eg turbine_sf <- st_transform(turbine_sf, crs=2163)设置初始 CRS 后,使用st_transform()将您形状的 CRS 转换为新的 CRS,例如turbine_sf <- st_transform(turbine_sf, crs=2163)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM