Пространственные данные. Построение графических объектов.

1. Загрузка библиотек для работы с пространственной графикой.

#пакеты для обработки данных и прочего
library("terra")
## terra 1.7.65
#library("rgdal") Пакет «rgdal» был удален из репозитория CRAN.
library("stringr")
library("RColorBrewer")

#форматы sp 
#пакеты для создания карт:
library("ggplot2")
#library("maptools") Пакет «maptools» был удален из репозитория CRAN.
library("tmap")
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
library("mapproj")
## Загрузка требуемого пакета: maps
#форматы sf
library(sf)
## Linking to GEOS 3.11.2, GDAL 3.7.2, PROJ 9.3.0; sf_use_s2() is TRUE
library(maps)
library(stars)
## Загрузка требуемого пакета: abind
library(mapview)
library(dplyr)
## 
## Присоединяю пакет: 'dplyr'
## Следующие объекты скрыты от 'package:terra':
## 
##     intersect, union
## Следующие объекты скрыты от 'package:stats':
## 
##     filter, lag
## Следующие объекты скрыты от 'package:base':
## 
##     intersect, setdiff, setequal, union
library(readr)

2. Задать точки пространственной графики.

#Задание 2. Фиксированные значения.

# Точка в пространстве XY
(x <- st_point(c(1,2)))
## POINT (1 2)
str(x)
##  'XY' num [1:2] 1 2
# Точка в пространстве XYZ
(x <- st_point(c(1,2,3)))
## POINT Z (1 2 3)
str(x)
##  'XYZ' num [1:3] 1 2 3
# Точка в пространстве XYM
(x <- st_point(c(1,2,3), "XYM"))
## POINT M (1 2 3)
str(x)
##  'XYM' num [1:3] 1 2 3
# Точка в пространстве XYZM
(x <- st_point(c(1,2,3,4)))
## POINT ZM (1 2 3 4)
str(x)
##  'XYZM' num [1:4] 1 2 3 4
#Задание 2. Сгенерированные значения.

# Точка в пространстве XY
(x <- st_point(runif(2,1,5)))
## POINT (4.922729 2.05897)
str(x)
##  'XY' num [1:2] 4.92 2.06
# Точка в пространстве XYZ
(x <- st_point(runif(3,1,5)))
## POINT Z (3.384689 2.414927 2.520382)
str(x)
##  'XYZ' num [1:3] 3.38 2.41 2.52
# Точка в пространстве XYM
(x <- st_point(runif(3,1,5), "XYM"))
## POINT M (3.377844 3.232271 2.715769)
str(x)
##  'XYM' num [1:3] 3.38 3.23 2.72
# Точка в пространстве XYZM
(x <- st_point(runif(4,1,5)))
## POINT ZM (1.708378 3.206553 4.605636 4.563448)
str(x)
##  'XYZM' num [1:4] 1.71 3.21 4.61 4.56

3. Задать мультиточки.

#Задание 3. Мультиточки.

p <- rbind(runif(2,1,5), runif(2,1,5), runif(2,1,5), runif(2,1,5), runif(2,1,5), runif(2,1,5))
(mp <- st_multipoint(p))
## MULTIPOINT ((2.935687 2.32336), (2.658171 4.56971), (3.103163 2.056467), (3.460343 1.378067), (4.392457 1.94664), (1.957137 3.205421))
plot(p)
plot(mp, col = 'blue', pch = 1, add = TRUE)

4. Задать линии и мультилинии.

# Линия LINESTRING.

ps <- rbind(runif(2,1,5), runif(2,1,5), runif(2,1,5), runif(2,1,5))
(ls <- st_linestring(ps))
## LINESTRING (4.524787 4.802644, 2.476723 2.762961, 2.576314 1.302781, 2.81254 4.811999)
plot(ps)
plot(ls, col = 'blue', pch = 2, add = TRUE, lwd = 5)

# Мультиточка MULTIPOINT и линия LINESTRING.

p6 <- matrix(c(
  0, 2,
  1, 3,
  3, 1,
  5, 0
), ncol = 2, byrow = TRUE)

mp <- st_multipoint(p6) # XY MULTIPOINT
print(mp)
## MULTIPOINT ((0 2), (1 3), (3 1), (5 0))
ls <- st_linestring(p6) # XY LINESTRING
print(ls)
## LINESTRING (0 2, 1 3, 3 1, 5 0)
plot(p6)
plot(ls, col = 'red', pch = 3, add = TRUE)

# Мультиточка MULTIPOINT, линия LINESTRING и мультилиния MULTILINESTRING.

ps <- rbind(runif(2,1,5), runif(2,1,5), runif(2,1,5), runif(2,1,5), ncol = 2, byrow = TRUE)

mp <- st_multipoint(ps) # XY MULTIPOINT
print(mp)
## MULTIPOINT ((2.706527 4.43987), (4.891081 2.590263), (3.13849 3.692451), (2.880785 3.542314), (2 2), (1 1))
ls <- st_linestring(ps) # XY LINESTRING
print(ls)
## LINESTRING (2.706527 4.43987, 4.891081 2.590263, 3.13849 3.692451, 2.880785 3.542314, 2 2, 1 1)
mls <- st_multilinestring(list(ps, p6)) # XY MULTILINESTRING
print(mls)
## MULTILINESTRING ((2.706527 4.43987, 4.891081 2.590263, 3.13849 3.692451, 2.880785 3.542314, 2 2, 1 1), (0 2, 1 3, 3 1, 5 0))
plot(ps)
plot(mls, col = 'blue', pch = 3, add = TRUE, lwd = 5)

# Задание 4.

# Три матрицы по четыре точки в каждой
dots1 <- matrix(c(
  0, 2,
  1, 3,
  3, 1,
  5, 0
), ncol = 2, byrow = TRUE)

dots2 <- matrix(c(
  0, 0,
  1, 2,
  2, 3,
  4, 1
), ncol = 2, byrow = TRUE)

dots3 <- matrix(c(
  1, 0,
  1.5, 1,
  2, 1,
  3, 5
), ncol = 2, byrow = TRUE)

# XY MULTIPOINT
mp <- st_multipoint(rbind(dots1, dots2, dots3))
print(mp)
## MULTIPOINT ((0 2), (1 3), (3 1), (5 0), (0 0), (1 2), (2 3), (4 1), (1 0), (1.5 1), (2 1), (3 5))
# XY MULTILINESTRING
mls <- st_multilinestring(list(dots1, dots2, dots3))
print(mls)
## MULTILINESTRING ((0 2, 1 3, 3 1, 5 0), (0 0, 1 2, 2 3, 4 1), (1 0, 1.5 1, 2 1, 3 5))
# визуализировать мультиточки
plot(mp, axes = TRUE)
# визуализировать мультилинию
plot(mls, col = 'blue', pch = 3, add = TRUE, lwd = 5)

5. Задать полигон.

# Задание 5.    Задать полигоны с числом точек равным семи.

# Координаты полигона
pnts <- matrix(c( # Координаты полигона
  1, 0,
  -0.5, 1,
  0, 2,
  2, 3,
  3.5, 2.5,
  3.8, 0.8,
  3, 0,
  1, 0
), ncol = 2, byrow = TRUE)

# Построение и отрисовка полигона
pol <- st_polygon(list(pnts)) # Простой полигон
print(pol) # Вывести координаты полигона
## POLYGON ((1 0, -0.5 1, 0 2, 2 3, 3.5 2.5, 3.8 0.8, 3 0, 1 0))
plot(pol, col = 'blue')

# Задание 5.

# Координаты полигона
points <- matrix(c(1, 0), ncol = 2, byrow = TRUE)
points <- rbind(points, matrix(runif(10, 1, 4), ncol = 2, byrow = TRUE), c(1, 0))
points <- matrix(c(
  1.000000, 0.000000,
2.115481, 1.384400,
1.479969, 1.877102,
1.579013, 2.607980,
2.446371, 2.579943,
3.469619, 1.578839,
1.000000, 0.000000), ncol = 2, byrow = TRUE)

# Построение и отрисовка полигона
pol <- st_polygon(list(points)) # Простой полигон
print(pol) # Вывести координаты полигона
## POLYGON ((1 0, 2.115481 1.3844, 1.479969 1.877102, 1.579013 2.60798, 2.446371 2.579943, 3.469619 1.578839, 1 0))
plot(pol, col = 'blue')

6. Задать полигоны с «дырами».

# Задание 6.

hole <- matrix(c( # Координаты дыры
  2, 1,
  3, 1.5,
  3, 2,
  2, 2,
  1.5, 1.5,
  2, 1
), ncol = 2, byrow = TRUE)

pol2 <- st_polygon(list(pnts, hole)) # Полигон с 7 гранями с дырой
print(pol2)
## POLYGON ((1 0, -0.5 1, 0 2, 2 3, 3.5 2.5, 3.8 0.8, 3 0, 1 0), (2 1, 3 1.5, 3 2, 2 2, 1.5 1.5, 2 1))
plot(pol2, col = 'blue')

#Задание 6. 

hole <- matrix(c( # Координаты дыры
  2, 1.7,
  2.3, 2.3,
  2.5, 2.3,
  3, 1.8,
  3, 1.7,
  3, 1.5,
  2.4, 1.3,
  2, 1.7
), ncol = 2, byrow = TRUE)

points
##          [,1]     [,2]
## [1,] 1.000000 0.000000
## [2,] 2.115481 1.384400
## [3,] 1.479969 1.877102
## [4,] 1.579013 2.607980
## [5,] 2.446371 2.579943
## [6,] 3.469619 1.578839
## [7,] 1.000000 0.000000
pol2 <- st_polygon(list(points, hole)) # Полигон с дырой
print(pol2)
## POLYGON ((1 0, 2.115481 1.3844, 1.479969 1.877102, 1.579013 2.60798, 2.446371 2.579943, 3.469619 1.578839, 1 0), (2 1.7, 2.3 2.3, 2.5 2.3, 3 1.8, 3 1.7, 3 1.5, 2.4 1.3, 2 1.7))
plot(pol2, col = 'blue')

7. Задать мультиполигоны.

# Задание 7.

# координаты для мультиполигона 1
first_point <- runif(2, 1, 4)
points1 <- matrix(first_point, ncol = 2, byrow = TRUE)
points1 <- rbind(first_point, matrix(runif(8, 1, 4), ncol = 2, byrow = TRUE), first_point)

first_point <- runif(2, 1, 4)
points2 <- matrix(first_point, ncol = 2, byrow = TRUE)
points2 <- rbind(first_point, matrix(runif(8, 1, 4), ncol = 2, byrow = TRUE), first_point)

pnts1 <- matrix(c(
  0.5, 0,
  0, 1,
  1, 1.5,
  2, 1,
  1.5, 0.25,
  0.5, 0
), ncol = 2, byrow = TRUE)

pnts2 <- matrix(c(
  5, 1,
  4.5, 2,
  4.5, 2.5,
  4, 2,
  4, 1.25,
  5, 1
), ncol = 2, byrow = TRUE)

# координаты для мультиполигона 2
first_point <- runif(2, 0, 4)
points3 <- matrix(first_point, ncol = 2, byrow = TRUE)
points3 <- rbind(first_point, matrix(runif(8, 1, 4), ncol = 2, byrow = TRUE), first_point)

points4 <- matrix(c(
  2, 1,
  3, 1.5,
  3, 2,
  2, 2,
  1.5, 1.5,
  2, 1
), ncol = 2, byrow = TRUE)

multipol1 <-st_multipolygon(list(list(points1),list(points2), list(pnts1), list(pnts2)))
multipol2<-st_multipolygon(list(list(points3),list(points4)))

plot(pol, col = 'navajowhite2') # Полигон (персиковый)
plot(multipol1, col = 'pink', add = TRUE) # Мультиполигон (розовый)
plot(multipol2, col = 'maroon', add = TRUE) # Мультиполигон (фуксия)

8. Построить озеро и реку.

# Задание 8. Озеро и река.

#Координаты земли
terra <- matrix(c(
  -3, -3,
  -3, 6,
  6, 6,
  6, -3,
  -3, -3
), ncol = 2, byrow = TRUE)

#Координаты озера
lake <- matrix(c(
  1, 0,
  -0.5, 1,
  0, 2,
  2, 3,
  3.5, 2.5,
  4, 1.8,
  3.8, 0.8,
  3, 0,
  2, 0,
  1, 0
), ncol = 2, byrow = TRUE)

#Координаты дыры (остров)
isle <- matrix(c(
  2, 1,
  3, 1.5,
  3, 2,
  2, 2,
  1.5, 1.5,
  2, 1
), ncol = 2, byrow = TRUE)

#Координаты первой линии (река)
line1 <- matrix(c(
  -3, 0,
  -1, 1.5,
  -0.7, 2,
  0, 1.95
), ncol = 2, byrow = TRUE)

#Координаты второй линии (река)
line2 <- matrix(c(
  3.9, 1.7,
  5, 2,
  6, 2
), ncol = 2, byrow = TRUE)

water <- st_polygon(list(lake, isle)) #Полигон (вода)
land <- st_multipolygon(list(list(terra, lake), list(isle))) #Мультиполигон - земля + остров (суша)
river = st_multilinestring(list(line1, line2)) #Мультилиния (река)

plot(water, col = 'lightskyblue2') #Нарисовать воду
plot(land, col = 'olivedrab4', add = TRUE) #Нарисовать сушу
plot(river, lwd = 15, col = 'lightskyblue2', add = TRUE) #Нарисовать реку

st_layers('d:/GEO/packages/natural_earth_vector.gpkg')
## Driver: GPKG 
## Available layers:
##                                                   layer_name geometry_type
## 1                 ne_10m_admin_0_antarctic_claim_limit_lines   Line String
## 2                            ne_10m_admin_0_antarctic_claims       Polygon
## 3               ne_10m_admin_0_boundary_lines_disputed_areas   Line String
## 4                         ne_10m_admin_0_boundary_lines_land   Line String
## 5                    ne_10m_admin_0_boundary_lines_map_units   Line String
## 6           ne_10m_admin_0_boundary_lines_maritime_indicator   Line String
## 7       ne_10m_admin_0_boundary_lines_maritime_indicator_chn   Line String
## 8                                   ne_10m_admin_0_countries       Polygon
## 9                               ne_10m_admin_0_countries_arg       Polygon
## 10                              ne_10m_admin_0_countries_bdg       Polygon
## 11                              ne_10m_admin_0_countries_bra       Polygon
## 12                              ne_10m_admin_0_countries_chn       Polygon
## 13                              ne_10m_admin_0_countries_deu       Polygon
## 14                              ne_10m_admin_0_countries_egy       Polygon
## 15                              ne_10m_admin_0_countries_esp       Polygon
## 16                              ne_10m_admin_0_countries_fra       Polygon
## 17                              ne_10m_admin_0_countries_gbr       Polygon
## 18                              ne_10m_admin_0_countries_grc       Polygon
## 19                              ne_10m_admin_0_countries_idn       Polygon
## 20                              ne_10m_admin_0_countries_ind       Polygon
## 21                              ne_10m_admin_0_countries_iso       Polygon
## 22                              ne_10m_admin_0_countries_isr       Polygon
## 23                              ne_10m_admin_0_countries_ita       Polygon
## 24                              ne_10m_admin_0_countries_jpn       Polygon
## 25                              ne_10m_admin_0_countries_kor       Polygon
## 26                            ne_10m_admin_0_countries_lakes       Polygon
## 27                              ne_10m_admin_0_countries_mar       Polygon
## 28                              ne_10m_admin_0_countries_nep       Polygon
## 29                              ne_10m_admin_0_countries_nld       Polygon
## 30                              ne_10m_admin_0_countries_pak       Polygon
## 31                              ne_10m_admin_0_countries_pol       Polygon
## 32                              ne_10m_admin_0_countries_prt       Polygon
## 33                              ne_10m_admin_0_countries_pse       Polygon
## 34                              ne_10m_admin_0_countries_rus       Polygon
## 35                              ne_10m_admin_0_countries_sau       Polygon
## 36                              ne_10m_admin_0_countries_swe       Polygon
## 37                              ne_10m_admin_0_countries_tlc       Polygon
## 38                              ne_10m_admin_0_countries_tur       Polygon
## 39                              ne_10m_admin_0_countries_twn       Polygon
## 40                              ne_10m_admin_0_countries_ukr       Polygon
## 41                              ne_10m_admin_0_countries_usa       Polygon
## 42                              ne_10m_admin_0_countries_vnm       Polygon
## 43                             ne_10m_admin_0_disputed_areas       Polygon
## 44    ne_10m_admin_0_disputed_areas_scale_rank_minor_islands       Polygon
## 45                               ne_10m_admin_0_label_points         Point
## 46                               ne_10m_admin_0_map_subunits       Polygon
## 47                                  ne_10m_admin_0_map_units       Polygon
## 48                                      ne_10m_admin_0_names         Point
## 49                          ne_10m_admin_0_pacific_groupings   Line String
## 50                                 ne_10m_admin_0_scale_rank       Polygon
## 51                   ne_10m_admin_0_scale_rank_minor_islands       Polygon
## 52                                      ne_10m_admin_0_seams   Line String
## 53                                ne_10m_admin_0_sovereignty       Polygon
## 54                               ne_10m_admin_1_label_points         Point
## 55                       ne_10m_admin_1_label_points_details         Point
## 56                                      ne_10m_admin_1_seams   Line String
## 57                           ne_10m_admin_1_states_provinces       Polygon
## 58                     ne_10m_admin_1_states_provinces_lakes       Polygon
## 59                     ne_10m_admin_1_states_provinces_lines   Line String
## 60                ne_10m_admin_1_states_provinces_scale_rank       Polygon
## 61  ne_10m_admin_1_states_provinces_scale_rank_minor_islands       Polygon
## 62                                   ne_10m_admin_2_counties       Polygon
## 63                             ne_10m_admin_2_counties_lakes       Polygon
## 64                             ne_10m_admin_2_counties_lines   Line String
## 65                        ne_10m_admin_2_counties_scale_rank       Polygon
## 66          ne_10m_admin_2_counties_scale_rank_minor_islands       Polygon
## 67                          ne_10m_admin_2_counties_to_match       Polygon
## 68                               ne_10m_admin_2_label_points         Point
## 69                       ne_10m_admin_2_label_points_details         Point
## 70                                           ne_10m_airports         Point
## 71                     ne_10m_parks_and_protected_lands_area       Polygon
## 72                     ne_10m_parks_and_protected_lands_line   Line String
## 73                    ne_10m_parks_and_protected_lands_point         Point
## 74               ne_10m_parks_and_protected_lands_scale_rank       Polygon
## 75                                   ne_10m_populated_places         Point
## 76                            ne_10m_populated_places_simple         Point
## 77                                              ne_10m_ports         Point
## 78                                          ne_10m_railroads   Line String
## 79                            ne_10m_railroads_north_america   Line String
## 80                                              ne_10m_roads   Line String
## 81                                ne_10m_roads_north_america   Line String
## 82                                         ne_10m_time_zones       Polygon
## 83                                        ne_10m_urban_areas       Polygon
## 84                               ne_10m_urban_areas_landscan       Polygon
## 85                        ne_10m_antarctic_ice_shelves_lines   Line String
## 86                        ne_10m_antarctic_ice_shelves_polys       Polygon
## 87                                          ne_10m_coastline   Line String
## 88                                   ne_10m_geographic_lines   Line String
## 89                             ne_10m_geography_marine_polys       Polygon
## 90                 ne_10m_geography_regions_elevation_points         Point
## 91                           ne_10m_geography_regions_points         Point
## 92                            ne_10m_geography_regions_polys       Polygon
## 93                                    ne_10m_glaciated_areas       Polygon
## 94                                              ne_10m_lakes       Polygon
## 95                                    ne_10m_lakes_australia       Polygon
## 96                                       ne_10m_lakes_europe       Polygon
## 97                                     ne_10m_lakes_historic       Polygon
## 98                                ne_10m_lakes_north_america       Polygon
## 99                                      ne_10m_lakes_pluvial       Polygon
## 100                                              ne_10m_land       Polygon
## 101                           ne_10m_land_ocean_label_points         Point
## 102                                  ne_10m_land_ocean_seams   Line String
## 103                                   ne_10m_land_scale_rank       Polygon
## 104                                     ne_10m_minor_islands       Polygon
## 105                           ne_10m_minor_islands_coastline   Line String
## 106                        ne_10m_minor_islands_label_points         Point
## 107                                             ne_10m_ocean       Polygon
## 108                                  ne_10m_ocean_scale_rank       Polygon
## 109                                            ne_10m_playas       Polygon
## 110                                             ne_10m_reefs   Line String
## 111                                  ne_10m_rivers_australia   Line String
## 112                                     ne_10m_rivers_europe   Line String
## 113                           ne_10m_rivers_lake_centerlines   Line String
## 114                ne_10m_rivers_lake_centerlines_scale_rank   Line String
## 115                              ne_10m_rivers_north_america   Line String
## 116                                       ne_10m_admin_1_sel   Line String
## 117             ne_50m_admin_0_boundary_lines_disputed_areas   Line String
## 118                       ne_50m_admin_0_boundary_lines_land   Line String
## 119         ne_50m_admin_0_boundary_lines_maritime_indicator   Line String
## 120     ne_50m_admin_0_boundary_lines_maritime_indicator_chn   Line String
## 121                        ne_50m_admin_0_boundary_map_units   Line String
## 122                  ne_50m_admin_0_breakaway_disputed_areas       Polygon
## 123       ne_50m_admin_0_breakaway_disputed_areas_scale_rank       Polygon
## 124                                 ne_50m_admin_0_countries       Polygon
## 125                           ne_50m_admin_0_countries_lakes       Polygon
## 126                              ne_50m_admin_0_map_subunits       Polygon
## 127                                 ne_50m_admin_0_map_units       Polygon
## 128                         ne_50m_admin_0_pacific_groupings   Line String
## 129                                ne_50m_admin_0_scale_rank       Polygon
## 130                               ne_50m_admin_0_sovereignty       Polygon
## 131                            ne_50m_admin_0_tiny_countries         Point
## 132                 ne_50m_admin_0_tiny_countries_scale_rank         Point
## 133                                     ne_50m_admin_1_seams   Line String
## 134                          ne_50m_admin_1_states_provinces       Polygon
## 135                    ne_50m_admin_1_states_provinces_lakes       Polygon
## 136                    ne_50m_admin_1_states_provinces_lines   Line String
## 137               ne_50m_admin_1_states_provinces_scale_rank       Polygon
## 138                                          ne_50m_airports         Point
## 139                                  ne_50m_populated_places         Point
## 140                           ne_50m_populated_places_simple         Point
## 141                                             ne_50m_ports         Point
## 142                                       ne_50m_urban_areas       Polygon
## 143                       ne_50m_antarctic_ice_shelves_lines   Line String
## 144                       ne_50m_antarctic_ice_shelves_polys       Polygon
## 145                                         ne_50m_coastline   Line String
## 146                                  ne_50m_geographic_lines   Line String
## 147                            ne_50m_geography_marine_polys       Polygon
## 148                ne_50m_geography_regions_elevation_points         Point
## 149                          ne_50m_geography_regions_points         Point
## 150                           ne_50m_geography_regions_polys       Polygon
## 151                                   ne_50m_glaciated_areas       Polygon
## 152                                             ne_50m_lakes       Polygon
## 153                                    ne_50m_lakes_historic       Polygon
## 154                                              ne_50m_land       Polygon
## 155                                             ne_50m_ocean       Polygon
## 156                                            ne_50m_playas       Polygon
## 157                           ne_50m_rivers_lake_centerlines   Line String
## 158                ne_50m_rivers_lake_centerlines_scale_rank   Line String
## 159                      ne_110m_admin_0_boundary_lines_land   Line String
## 160                                ne_110m_admin_0_countries       Polygon
## 161                          ne_110m_admin_0_countries_lakes       Polygon
## 162                                ne_110m_admin_0_map_units       Polygon
## 163                        ne_110m_admin_0_pacific_groupings   Line String
## 164                               ne_110m_admin_0_scale_rank       Polygon
## 165                              ne_110m_admin_0_sovereignty       Polygon
## 166                           ne_110m_admin_0_tiny_countries         Point
## 167                         ne_110m_admin_1_states_provinces       Polygon
## 168                   ne_110m_admin_1_states_provinces_lakes       Polygon
## 169                   ne_110m_admin_1_states_provinces_lines   Line String
## 170              ne_110m_admin_1_states_provinces_scale_rank       Polygon
## 171                                 ne_110m_populated_places         Point
## 172                          ne_110m_populated_places_simple         Point
## 173                                        ne_110m_coastline   Line String
## 174                                 ne_110m_geographic_lines   Line String
## 175                           ne_110m_geography_marine_polys       Polygon
## 176               ne_110m_geography_regions_elevation_points         Point
## 177                         ne_110m_geography_regions_points         Point
## 178                          ne_110m_geography_regions_polys       Polygon
## 179                                  ne_110m_glaciated_areas       Polygon
## 180                                            ne_110m_lakes       Polygon
## 181                                             ne_110m_land       Polygon
## 182                                            ne_110m_ocean       Polygon
## 183                          ne_110m_rivers_lake_centerlines   Line String
##     features fields                 crs_name
## 1         23      5                   WGS 84
## 2         10      8                   WGS 84
## 3         75     55                   WGS 84
## 4        515     54                   WGS 84
## 5         15     44                   WGS 84
## 6        221     42                   WGS 84
## 7          9     42                   WGS 84
## 8        258    168                   WGS 84
## 9        246    168                   WGS 84
## 10       248    168                   WGS 84
## 11       249    168                   WGS 84
## 12       246    168                   WGS 84
## 13       250    168                   WGS 84
## 14       249    168                   WGS 84
## 15       249    168                   WGS 84
## 16       249    168                   WGS 84
## 17       251    168                   WGS 84
## 18       249    168                   WGS 84
## 19       246    168                   WGS 84
## 20       248    168                   WGS 84
## 21       249    168                   WGS 84
## 22       250    168                   WGS 84
## 23       250    168                   WGS 84
## 24       250    168                   WGS 84
## 25       250    168                   WGS 84
## 26       258    168                   WGS 84
## 27       247    168                   WGS 84
## 28       248    168                   WGS 84
## 29       249    168                   WGS 84
## 30       248    168                   WGS 84
## 31       249    168                   WGS 84
## 32       250    168                   WGS 84
## 33       257    168                   WGS 84
## 34       257    168                   WGS 84
## 35       249    168                   WGS 84
## 36       250    168                   WGS 84
## 37       333    168                   WGS 84
## 38       250    168                   WGS 84
## 39       249    168                   WGS 84
## 40       249    168                   WGS 84
## 41       252    168                   WGS 84
## 42       248    168                   WGS 84
## 43        99    168                   WGS 84
## 44       298      9                   WGS 84
## 45      7124      9                   WGS 84
## 46       360    168                   WGS 84
## 47       298    168                   WGS 84
## 48       505    159 Undefined geographic SRS
## 49        63      4                   WGS 84
## 50      4352      9                   WGS 84
## 51      7146      9                   WGS 84
## 52        26      2                   WGS 84
## 53       209    168                   WGS 84
## 54     11291     13                   WGS 84
## 55      4650    121                   WGS 84
## 56        12      2                   WGS 84
## 57      4596    121                   WGS 84
## 58      4596    121                   WGS 84
## 59     10179     57                   WGS 84
## 60      8544     13                   WGS 84
## 61     11291     13                   WGS 84
## 62      3224     61                   WGS 84
## 63      3224     61                   WGS 84
## 64      8155     49                   WGS 84
## 65      3610     10                   WGS 84
## 66      3610     10                   WGS 84
## 67      3225     33                   WGS 84
## 68      3612     10                   WGS 84
## 69      3224     61                   WGS 84
## 70       893     40                   WGS 84
## 71        61      8                   WGS 84
## 72        29      8                   WGS 84
## 73       315      7                   WGS 84
## 74       110      8                   WGS 84
## 75      7342    137                   WGS 84
## 76      7342     31                   WGS 84
## 77      1081      6                   WGS 84
## 78     25413     12                   WGS 84
## 79      1127      7                   WGS 84
## 80     56600     31                   WGS 84
## 81     49183     13                   WGS 84
## 82       120     15                   WGS 84
## 83     11878      4                   WGS 84
## 84      6018     25                   WGS 84
## 85       160      3                   WGS 84
## 86       159      5                   WGS 84
## 87      4133      3                   WGS 84
## 88         6     36                   WGS 84
## 89       306     37                   WGS 84
## 90       711     43                   WGS 84
## 91       235     39                   WGS 84
## 92      1047     37                   WGS 84
## 93      1886      5                   WGS 84
## 94      1355     41                   WGS 84
## 95        84     40                   WGS 84
## 96       767     42                   WGS 84
## 97         4     36                   WGS 84
## 98      1162     36                   WGS 84
## 99       139      6                   WGS 84
## 100       11      3                   WGS 84
## 101     4110      3                   WGS 84
## 102       80      3                   WGS 84
## 103     6861      3                   WGS 84
## 104     2795      3                   WGS 84
## 105     2741      3                   WGS 84
## 106     2738      3                   WGS 84
## 107        1      3                   WGS 84
## 108       22      3                   WGS 84
## 109       73     36                   WGS 84
## 110     1043      3                   WGS 84
## 111      339     38                   WGS 84
## 112     1325     41                   WGS 84
## 113     1473     38                   WGS 84
## 114     4224     39                   WGS 84
## 115     4897     40                   WGS 84
## 116      724     55                   WGS 84
## 117       34     41                   WGS 84
## 118      390     39                   WGS 84
## 119      205     40                   WGS 84
## 120        9     41                   WGS 84
## 121        9     37                   WGS 84
## 122       28    168                   WGS 84
## 123       28      5                   WGS 84
## 124      242    168                   WGS 84
## 125      242    168                   WGS 84
## 126      308    168                   WGS 84
## 127      265    168                   WGS 84
## 128       63      4                   WGS 84
## 129     1636      8                   WGS 84
## 130      201    168                   WGS 84
## 131       76    170                   WGS 84
## 132       76      9                   WGS 84
## 133        4      2                   WGS 84
## 134      294    121                   WGS 84
## 135      294    121                   WGS 84
## 136      581     43                   WGS 84
## 137      531      3                   WGS 84
## 138      284     40                   WGS 84
## 139     1251    137                   WGS 84
## 140     1251     31                   WGS 84
## 141      143      6                   WGS 84
## 142     2143      4                   WGS 84
## 143       99      3                   WGS 84
## 144       64      3                   WGS 84
## 145     1428      3                   WGS 84
## 146        6     36                   WGS 84
## 147      118     37                   WGS 84
## 148       86     43                   WGS 84
## 149      162     39                   WGS 84
## 150      522     37                   WGS 84
## 151      377      3                   WGS 84
## 152      412     39                   WGS 84
## 153        2     35                   WGS 84
## 154     1420      3                   WGS 84
## 155        1      3                   WGS 84
## 156       27     34                   WGS 84
## 157      478     36                   WGS 84
## 158     1633     38                   WGS 84
## 159      331     40                   WGS 84
## 160      177    168                   WGS 84
## 161      177    168                   WGS 84
## 162      183    168                   WGS 84
## 163       62      2                   WGS 84
## 164      290      7                   WGS 84
## 165      171    168                   WGS 84
## 166       37    170                   WGS 84
## 167       51    121                   WGS 84
## 168       51    121                   WGS 84
## 169      109     43                   WGS 84
## 170       51      3                   WGS 84
## 171      243    137                   WGS 84
## 172      243     31                   WGS 84
## 173      134      3                   WGS 84
## 174        6     36                   WGS 84
## 175       29     37                   WGS 84
## 176       19     43                   WGS 84
## 177        3     39                   WGS 84
## 178       60     37                   WGS 84
## 179       11      3                   WGS 84
## 180       24     37                   WGS 84
## 181      127      3                   WGS 84
## 182        2      3                   WGS 84
## 183       13     35                   WGS 84
countries <- st_read('d:/GEO/packages/natural_earth_vector.gpkg')
## Multiple layers are present in data source D:\GEO\packages\natural_earth_vector.gpkg, reading layer `ne_10m_admin_0_antarctic_claim_limit_lines'.
## Use `st_layers' to list all layer names and their type in a data source.
## Set the `layer' argument in `st_read' to read a particular layer.
## Warning in CPL_read_ogr(dsn, layer, query, as.character(options), quiet, :
## automatically selected the first layer in a data source containing more than
## one.
## Reading layer `ne_10m_admin_0_antarctic_claim_limit_lines' from data source 
##   `D:\GEO\packages\natural_earth_vector.gpkg' using driver `GPKG'
## Simple feature collection with 23 features and 5 fields
## Geometry type: LINESTRING
## Dimension:     XY
## Bounding box:  xmin: -150 ymin: -90 xmax: 160.1 ymax: -60
## Geodetic CRS:  WGS 84

Пространственные данные. Базовая графика.

Построить карту мира с использованием базовой графической системой.

# Загрузка shp-файлов.

countries <- st_read('d:/GEO/countries.gpkg')
## Reading layer `admin_0_map_units' from data source `D:\GEO\countries.gpkg' using driver `GPKG'
## Simple feature collection with 183 features and 72 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -180 ymin: -90 xmax: 180 ymax: 83.64513
## Geodetic CRS:  WGS 84
oceans <- st_read('d:/GEO/oceans.gpkg')
## Reading layer `ocean' from data source `D:\GEO\oceans.gpkg' using driver `GPKG'
## Simple feature collection with 2 features and 4 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -180 ymin: -85.60904 xmax: 180 ymax: 90
## Geodetic CRS:  WGS 84
rivers <- st_read('d:/GEO/rivers.gpkg')
## Reading layer `rivers_lake_centerlines' from data source `D:\GEO\rivers.gpkg' using driver `GPKG'
## Simple feature collection with 13 features and 8 fields
## Geometry type: LINESTRING
## Dimension:     XY
## Bounding box:  xmin: -135.3134 ymin: -33.99358 xmax: 129.956 ymax: 72.90651
## Geodetic CRS:  WGS 84
lakes <- st_read('d:/GEO/lakes.gpkg')
## Reading layer `lakes' from data source `D:\GEO\lakes.gpkg' using driver `GPKG'
## Simple feature collection with 25 features and 8 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -124.9536 ymin: -16.53641 xmax: 109.9298 ymax: 66.9693
## Geodetic CRS:  WGS 84
cities <- st_read('d:/GEO/cities.gpkg')
## Reading layer `populated_places' from data source `D:\GEO\cities.gpkg' using driver `GPKG'
## Simple feature collection with 243 features and 103 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -175.2206 ymin: -41.29999 xmax: 179.2166 ymax: 64.15002
## Geodetic CRS:  WGS 84
#M-110M
gpkgWorldMap <- lst(oceans, countries, rivers, lakes, cities)
# Задание 1.
outlines = st_geometry(countries)
plot(countries)
## Warning: plotting the first 9 out of 72 attributes; use max.plot = 72 to plot
## all

# монотонная заливка
plot(outlines, col = 'mistyrose3')

# стилизация
plot(countries['sovereignt'], key.pos = NULL)

# с координатной сеткой и подписями координат
plot(countries['gdp_md_est'], graticule = TRUE, axes = TRUE)

2-3. Построить интерактивную карту.

#Добавить города Paris, Rome
#Удалить города Lagos, Bogota
cities_large <- cities %>%  
  filter(scalerank == 0, 
         ! name %in% c('Washington, D.C.', 'Riyadh', 'São Paulo', 'Kolkata', 'Lagos', 'Bogota'))

#Включить только города
plot(st_geometry(countries), lwd = 0.5, border = 'gray')
plot(oceans, col = 'steelblue1', border = 'steelblue', add = TRUE)
## Warning in plot.sf(oceans, col = "steelblue1", border = "steelblue", add =
## TRUE): ignoring all but the first attribute
plot(lakes, col = 'steelblue1', border = 'steelblue', add = TRUE)
## Warning in plot.sf(lakes, col = "steelblue1", border = "steelblue", add =
## TRUE): ignoring all but the first attribute
plot(rivers, col = 'steelblue', add = TRUE)
## Warning in plot.sf(rivers, col = "steelblue", add = TRUE): ignoring all but the
## first attribute
plot(cities_large, col = 'black', pch = 19, cex = 0.25, add = TRUE)
## Warning in plot.sf(cities_large, col = "black", pch = 19, cex = 0.25, add =
## TRUE): ignoring all but the first attribute
text(cities_large$longitude, cities_large$latitude, 
     label = cities_large$name, cex = 0.5, pos = 2, offset = 0.25)

# Интерактиваня карта мира.
mapview(countries)
# Определить количество континентов
nconts <- length(unique(countries$continent))

# Интерактивная карта континетов
mapview(countries, zcol = 'continent', 
        col.regions = RColorBrewer::brewer.pal(nconts, 'Set2'))
# Палитра подобрана с помощью ColorBrewer
# Экономическая карта
nconts <- length(unique(countries$economy))
mapview(countries, zcol = 'economy', 
        col.regions = RColorBrewer::brewer.pal(nconts, 'Set2'))
# Карта государств
nconts <- length(unique(countries$admin))
mapview(countries, zcol = 'admin', 
        col.regions = RColorBrewer::brewer.pal(nconts, 'Set2'))
## Warning in RColorBrewer::brewer.pal(nconts, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
## Warning: Found less unique colors (8) than unique zcol values (177)! 
## Recycling color vector.
# Карта континетов с крупнейшими городами
{ mapview(countries, zcol = 'continent', 
        col.regions = RColorBrewer::brewer.pal(nconts, 'Set1')) + 
  mapview(cities_large, col.regions = 'black', label = 'name', cex = 3) } %>% 
  leafem::addStaticLabels(cities_large, label = cities_large$name,
                          offset = c(0.1, 0),
                          style = list("color" = "black", "font-weight" = "bold"))
## Warning in RColorBrewer::brewer.pal(nconts, "Set1"): n too large, allowed maximum for palette Set1 is 9
## Returning the palette you asked for with that many colors
# Выбрать Россию из датафрейма стран
russia <- countries %>% 
  filter(sovereignt == 'Russia')
russia.borders = st_cast(russia, 'MULTILINESTRING')
russia.points = st_cast(russia.borders, 'POINT')
## Warning in st_cast.sf(russia.borders, "POINT"): repeating attributes for all
## sub-geometries for which they may not be constant
# отобразить Россию на отдельной карте
plot(st_geometry(russia), lwd = 0.5,col='green')
plot(russia.points, pch = 20, add = TRUE)
## Warning in plot.sf(russia.points, pch = 20, add = TRUE): ignoring all but the
## first attribute

plot(st_geometry(russia))
plot(st_geometry(russia), col = 'blue', add = TRUE)

4. Использование библиотеки ggplot2.

# Задание 4.
ggplot() +
  geom_sf(data = countries) +
  theme_void()

ggplot() +
  geom_sf(data = gpkgWorldMap$countries, fill = 'palegreen3', color = 'linen') +
  geom_sf(data = gpkgWorldMap$oceans, linewidth = 0.4, 
          fill = 'skyblue1', color = 'steelblue') +
  theme_void()