添加一个浮雕(GEOtiff,.tif)到瑞士国家边界的ggplot((polygon)shapefile,.shp)

时间:2018-03-29 13:32:08

标签: r ggplot2 raster shapefile geotiff

我绘制了瑞士的面积(polygon shapefile)并通过坐标添加了点(瑞士气象站)。

# Boundaries with data-points plotted

library(rgdal)
library(readxl)
library(sp)

library(ggplot2)
library(maptools)
library(plyr)
library(raster)

# import swiss country frontiers (.shp file)

gb <- readOGR("swissBOUNDARIES3D_1_3_TLM_KANTONSGEBIET.shp")

# import coordinates of weather stations from excel file (.xlsx)

coord <- read_excel("SMN-Stationen_20151222.xlsx")
head(coord)
# A tibble: 6 x 10
    SINCE_DT NAT_IND_TX NAT_ABBR_TX           NAME_TX X_KM_COORD_NU     Y_KM_COORD_NU HEIGHT_ASL_NU
      <dttm>      <chr>       <chr>             <chr>         <dbl>             <dbl>         <dbl>
1 2015-12-15       0600         ARO             Arosa        771030            184826          1878
2 2015-10-27       3420         LAC Lachen / Galgenen        707637            226334           468
3 2015-09-08       8040         VEV  Vevey / Corseaux        552106            146847           405
4 2015-08-25       7770         MAR    Les Marécottes        567375            107577           990
5 2015-08-18       5290         FRU          Frutigen        616926            160532           753
6 2015-04-28       0380         BIV             Bivio        771282            148120          1856
# ... with 3 more variables: LONGITUDE_NU <dbl>, LATITUDE_NU <dbl>,     NAME_G_TX <chr>

# plot with ggplot2

gbb <- fortify(gb)

ggplot() +
  geom_polygon(data = gbb, aes(long, lat, group = group, fill =         c("grey40")), color = "white") +
  geom_point(data = coord, aes(x = X_KM_COORD_NU, y = Y_KM_COORD_NU), colour     = "black", size = 3) +
  geom_point(data = coord, aes(x = X_KM_COORD_NU, y = Y_KM_COORD_NU), colour     = "skyblue1", size = 1.5) +  
  scale_fill_identity()

plot image

现在我想用地形浮雕来支撑图形,我以GEOtiff文件的形式存在。

当我尝试绘制栅格时,错误:&#34;无法分配大小为196.7 Mb的矢量&#34;出现了。

另一个问题是,这两个文件的范围不同。所以我无法在一定程度上裁剪它们。是否有可能通过他们的CRS进行叠加?

relief <- raster("25_HYPSO.tif")
> extent(relief)
class       : Extent 
xmin        : 2317050 
xmax        : 3057050 
ymin        : 912950.2 
ymax        : 1412916 
> projection(relief)
[1] "+proj=somerc +lat_0=46.95240555555556 +lon_0=7.439583333333333 +k_0=1     +x_0=2600000 +y_0=1200000 +ellps=bessel +units=m +no_defs"
> 
> extent(gb)
class       : Extent 
xmin        : 485411 
xmax        : 833840.7 
ymin        : 75269.68 
ymax        : 295934 
> projection(gb)
[1] "+proj=somerc +lat_0=46.95240555555556 +lon_0=7.439583333333333 +k_0=1     +x_0=600000 +y_0=200000 +ellps=bessel +units=m +no_defs"  

您可以从联邦地形办公室下载数据&#34; swisstopo&#34;。

可以在此地图中找到Countryborders(swissBOUNDARIES3D_1_3_TLM_KANTONSGEBIET.shp):

https://shop.swisstopo.admin.ch/de/products/landscape/boundaries3D

地形图(25_HYPSO.tif)可在此地图中找到:

https://shop.swisstopo.admin.ch/de/products/maps/national/vector/smv1000

最终产品看起来应该像Timo Grossenbacher在他的博文(https://timogrossenbacher.ch/2016/12/beautiful-thematic-maps-with-ggplot2-only/)中所做的那样。我尝试了他的方法,但没有找到成功,因为他事先编辑了他的救济档案,我不知道如何。

example image, Timo Grossenbacher

2 个答案:

答案 0 :(得分:3)

我设法现在就做了。 Timo Grossenbacher告诉我,他改变了CRS(从CH1903 + / LV95到CH1903 / LV03)并使用开源软件QGIS修剪(输出瑞士国家边界的范围)。

这就是我使用相同的.tif文件所做的,但导出的另一个.shp文件只包含国家边界(/swissBOUNDARIES3D_1_3_TLM_LANDESGEBIET.shp,可以在同一个下载中找到)。

我调用了生成的文件:“landesgebiet_edit.tif”

这是我的代码:

## Plotting data points by coordinates (CH1903/LV03) on relief of switzerland

library(readxl)
library(raster)
library(ggplot2)

# import coordinates of weather stationes

coord <- read_excel("Q:/Projekte/Tarik/Coordinate_Plot/coordinate_plot/SMN-Stationen_20151222.xlsx")

# import raster (relief) file and modify it to be used in ggplot

rel <- raster("C:/Users/qualpra/Desktop/tif/landesgebiet_edit.tif")
rel_spdf <- as(rel, "SpatialPixelsDataFrame")

rel <- as.data.frame(rel_spdf)

head(rel)
landesgebiet_edit        x        y
               137 684552.0 295862.1
               122 684552.0 295777.5
               108 684636.7 295777.5
               116 684467.4 295692.8
               105 684552.0 295692.8
                93 684636.7 295692.8

ggplot() +
  geom_raster(data = rel, aes_string(x = "x", y = "y", alpha = "landesgebiet_edit")) +
  scale_alpha(name = "", range = c(0.6, 0), guide = F) +
  geom_point(data = coord, aes(x = X_KM_COORD_NU, y = Y_KM_COORD_NU), colour = "black", size = 3) +
  geom_point(data = coord, aes(x = X_KM_COORD_NU, y = Y_KM_COORD_NU), colour = "skyblue1", size = 1.5) +  
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank()) +
  theme(axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank()) +
  scale_fill_identity()

输出:

Output plot

非常感谢Timo Grossenbacher!

答案 1 :(得分:0)

我也遇到了同样的问题。

这是我使用 rgdal 直接在 R 中编辑浮雕的解决方案:

library(ggplot2)
library(raster)
library(rgdal)

#Downloads from swisstopo:
r <-raster("..../20161001_SMV1000_SHAPE_CHLV95/Shapefiles/25_RELI.tif")

shapeCH <- readOGR("..../BOUNDARIES_2020_10/DATEN/swissBOUNDARIES3D/SHAPEFILE_LV03_LN02/swissBOUNDARIES3D_1_3_TLM_LANDESGEBIET.shp")
shapeCH <- spTransform(shapeCH, CRS(proj4string(r))) #Transform shape to raster CRS

#crop and mask
r2 <- crop(r, extent(shapeCH)) 
r3 <- rasterize(shapeCH, r2, mask=TRUE)

#ggplot relief
rel_spdf <- as(r3, "SpatialPixelsDataFrame")
rel <- as.data.frame(rel_spdf)

ggplot() +
  geom_raster(data = rel, aes_string(x = "x", y = "y", alpha = "X25_RELI"))