在rnaturalearth中添加一个常数离岸线。

huangapple go评论78阅读模式
英文:

Adding a constant offshore line in rnaturalearth

问题

以下是您要翻译的内容:

library("rnaturalearth")
library("rnaturalearthdata")
library("sf")

world <- ne_countries(scale = "medium", returnclass = "sf")
offshore_line <- st_buffer(simplified_world, dist = 0.06)

ggplot(data = world) +
  geom_sf(fill = "antiquewhite") +
  coord_sf(xlim = c(-60, -45), ylim = c(59, 72), expand = FALSE) +
  geom_sf(data = offshore_line, colour = "blue", linetype = "solid", size = 1)

我认为这应该能够完成任务,但它返回以下错误:

Error in wk_handle.wk_wkb(wkb, s2_geography_writer(oriented = oriented,  : 
  Loop 0 is not valid: Edge 14 crosses edge 25

祝好。

英文:

I am producing a map of Greenland using the package rnaturalearth and would like to add a line which indicates an offshore zone (60m from the coast) to the plot.

library(&quot;rnaturalearth&quot;)
library(&quot;rnaturalearthdata&quot;)
library(&quot;sf&quot;)


world &lt;- ne_countries(scale = &quot;medium&quot;, returnclass = &quot;sf&quot;)
offshore_line &lt;- st_buffer(simplified_world, dist = 0.06)

ggplot(data = world) +
  geom_sf(fill = &quot;antiquewhite&quot;) +
  coord_sf(xlim = c(-60, -45), ylim = c(59, 72), expand = FALSE) +
  geom_sf(data = offshore_line, colour = &quot;blue&quot;, linetype = &quot;solid&quot;, size = 1)
  

I thought this would do the job but it returns the error:

Error in wk_handle.wk_wkb(wkb, s2_geography_writer(oriented = oriented,  : 
  Loop 0 is not valid: Edge 14 crosses edge 25

Cheers.

答案1

得分: 2

不要有别的内容。以下是要翻译的内容:

  • Rather than trying to apply an st_buffer to the whole world, you could just get the data for Greenland and apply st_buffer to that. Due to the way that st_buffer works, you would probably best to transform to a planar projection for the buffering part, otherwise you will get a 'jagged' result. You can always transform it back again afterwards.
  • When plotting you need to ensure that your buffer zone explicitly has its fill set to fill = NA, otherwise the filled polygon will be drawn over your map.
  • Note that adding another geom_sf layer after setting coord_sf removes your coord_sf and resets it to the second layer's co-ordinate system, so set the coord_sf after all the geom layers.
  • It's going to be difficult to see a 60m buffer if you are zoomed out enough to see hundreds of miles of coastline: it will be so close to the coast itself that it will look as though you have just drawn the coastline blue. Ensure that you are drawing 60 metres by specifying the units as well as the values in st_buffer.

以下是示例代码:

这里是在60米处绘制的海岸线周围的蓝线:

library("rnaturalearth")
library("rnaturalearthdata")
library("sf")

greenland <- ne_countries(scale = 'large', country = 'Greenland', 
                          returnclass = 'sf')


offshore_line <- greenland %>%
                 st_transform(crs = '+proj=utm +zone=22 +datum=WGS84') %>%
                 st_buffer(dist = units::set_units(60, 'metre')) %>%
                 st_transform(st_crs(greenland))

ggplot(data = greenland) +
  geom_sf(fill = "antiquewhite") +
  geom_sf(data = offshore_line, colour = "blue", 
          linetype = "solid", size = 1, fill = NA) +
  coord_sf(xlim = c(-60, -45), ylim = c(59, 72), expand = FALSE) 

要显示这是正确的线,我们可以将缓冲距离设置为20,000米,并显示离岸20公里的线。

英文:

Rather than trying to apply an st_buffer to the whole world, you could just get the data for Greenland and apply st_buffer to that. Due to the way that st_buffer works, you would probably best to transform to a planar projection for the buffering part, otherwise you will get a 'jagged' result. You can always transform it back again afterwards.

When plotting you need to ensure that your buffer zone explicitly has its fill set to fill = NA, otherwise the filled polygon will be drawn over your map.

Note that adding another geom_sf layer after setting coord_sf removes your coord_sf and resets it to the second layer's co-ordinate system, so set the coord_sf after all the geom layers.

It's going to be difficult to see a 60m buffer if you are zoomed out enough to see hundreds of miles of coastline: it will be so close to the coast itself that it will look as though you have just drawn the coastline blue. Ensure that you are drawing 60 metres by specifying the units as well as the values in st_buffer

Here is a blue line drawn around the coast at 60m:

library(&quot;rnaturalearth&quot;)
library(&quot;rnaturalearthdata&quot;)
library(&quot;sf&quot;)

greenland &lt;- ne_countries(scale = &#39;large&#39;, country = &#39;Greenland&#39;, 
                          returnclass = &#39;sf&#39;)


offshore_line &lt;- greenland |&gt;
                 st_transform(crs = &#39;+proj=utm +zone=22 +datum=WGS84&#39;) |&gt;
                 st_buffer(dist = units::set_units(60, &#39;metre&#39;)) |&gt;
                 st_transform(st_crs(greenland))

ggplot(data = greenland) +
  geom_sf(fill = &quot;antiquewhite&quot;) +
  geom_sf(data = offshore_line, colour = &quot;blue&quot;, 
          linetype = &quot;solid&quot;, size = 1, fill = NA) +
  coord_sf(xlim = c(-60, -45), ylim = c(59, 72), expand = FALSE) 

在rnaturalearth中添加一个常数离岸线。

To show that this is the correct line, we can set the buffer distance to 20,000m and show a line 20km from the shore:

在rnaturalearth中添加一个常数离岸线。

This is probably close to the type of picture you expected, but the top image is the one that is 60m.

huangapple
  • 本文由 发表于 2023年6月5日 18:22:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405483.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定