使用 sf 和替代坐标参考系统
Working with sf and alternative co-ordinate reference systems
我正在尝试使用不同的 CRS 绘制具有大圆 (GC) 路径的地图。当使用 WGS 84 (EPGS 4326) 时,下面的代码可以很好地绘制香港和芝加哥机场之间的 GC。但是,当我尝试使用不同的 CRS(例如 EPGS 3035)时,该线不会绘制。关于我在这里缺少什么的任何想法?查看绘制的地图,看起来 GC 线和点没有正确转换?请注意,此 post.
底部的参考图像图
# Load packages
library(sf)
library(tidyverse)
library(rnaturalearth)
# Define Co-ordinate Reference System
crs.inuse <- 4326 # WGS 84
crs.inuse <- 3035 # ETRS89-extended / LAEA Europe
# Define points of interest as Chicago O'Hare Airport (ORD)
# and Hong Kong Airport (HKG)
poi.df <- data.frame('id'=c(1, 1),
'IATA'=c('ORD', 'HKG'),
'lat'=c(41.974522, 22.308889),
'lon'=c(-87.906596, 113.914722),
stringsAsFactors=FALSE)
# Convert coordinate data to sf object
poi.sf <- st_as_sf(poi.df, coords=c('lon', 'lat'), crs=crs.inuse)
# Convert POINT geometry to LINESTRING
poi.sf.line <- poi.sf %>%
group_by(id) %>%
summarise(do_union=FALSE) %>%
st_cast('LINESTRING')
# Convert rhumb lines to great circles
poi.sf.gcline <- poi.sf.line %>%
st_segmentize(units::set_units(200, km))
# Tidy up great circles so that plotted lines wrap cleanly
# across the dateline
poi.sf.gcline <- st_wrap_dateline(poi.sf.gcline, options=c('WRAPDATELINE=YES', 'DATELINEOFFSET=180'))
# Load the background map
world <- ne_countries(scale='medium', returnclass='sf')
# Plot the map
ggplot() +
geom_sf(data=world) +
geom_sf(data=poi.sf, aes(geometry=geometry), size=2.5, colour='red') +
geom_sf(data=poi.sf.gcline, aes(geometry=geometry), size=0.5, colour='red') +
coord_sf(crs=st_crs(crs.inuse))
使用 EPGS 4326 绘制
使用 EPGS 3035 绘制
简单的修复,确保你的 crs.inuse 是你所有代码的:
crs.inuse <- 4326
当您使用 ggplot 绘图时,请在 coord_sf 参数中手动提供 crs:
ggplot() +
geom_sf(data=world) +
geom_sf(data=poi.sf, aes(geometry=geometry), size=2.5, colour='red') +
geom_sf(data=poi.sf.gcline, aes(geometry=geometry), size=0.5, colour='red') +
coord_sf(crs=st_crs(3035))
这是你得到的:
我正在尝试使用不同的 CRS 绘制具有大圆 (GC) 路径的地图。当使用 WGS 84 (EPGS 4326) 时,下面的代码可以很好地绘制香港和芝加哥机场之间的 GC。但是,当我尝试使用不同的 CRS(例如 EPGS 3035)时,该线不会绘制。关于我在这里缺少什么的任何想法?查看绘制的地图,看起来 GC 线和点没有正确转换?请注意,此 post.
底部的参考图像图# Load packages
library(sf)
library(tidyverse)
library(rnaturalearth)
# Define Co-ordinate Reference System
crs.inuse <- 4326 # WGS 84
crs.inuse <- 3035 # ETRS89-extended / LAEA Europe
# Define points of interest as Chicago O'Hare Airport (ORD)
# and Hong Kong Airport (HKG)
poi.df <- data.frame('id'=c(1, 1),
'IATA'=c('ORD', 'HKG'),
'lat'=c(41.974522, 22.308889),
'lon'=c(-87.906596, 113.914722),
stringsAsFactors=FALSE)
# Convert coordinate data to sf object
poi.sf <- st_as_sf(poi.df, coords=c('lon', 'lat'), crs=crs.inuse)
# Convert POINT geometry to LINESTRING
poi.sf.line <- poi.sf %>%
group_by(id) %>%
summarise(do_union=FALSE) %>%
st_cast('LINESTRING')
# Convert rhumb lines to great circles
poi.sf.gcline <- poi.sf.line %>%
st_segmentize(units::set_units(200, km))
# Tidy up great circles so that plotted lines wrap cleanly
# across the dateline
poi.sf.gcline <- st_wrap_dateline(poi.sf.gcline, options=c('WRAPDATELINE=YES', 'DATELINEOFFSET=180'))
# Load the background map
world <- ne_countries(scale='medium', returnclass='sf')
# Plot the map
ggplot() +
geom_sf(data=world) +
geom_sf(data=poi.sf, aes(geometry=geometry), size=2.5, colour='red') +
geom_sf(data=poi.sf.gcline, aes(geometry=geometry), size=0.5, colour='red') +
coord_sf(crs=st_crs(crs.inuse))
使用 EPGS 4326 绘制
简单的修复,确保你的 crs.inuse 是你所有代码的:
crs.inuse <- 4326
当您使用 ggplot 绘图时,请在 coord_sf 参数中手动提供 crs:
ggplot() +
geom_sf(data=world) +
geom_sf(data=poi.sf, aes(geometry=geometry), size=2.5, colour='red') +
geom_sf(data=poi.sf.gcline, aes(geometry=geometry), size=0.5, colour='red') +
coord_sf(crs=st_crs(3035))
这是你得到的: