11
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

RでGIS(shapefileなどの読み込み)

Last updated at Posted at 2024-06-26

データの読み込み

RでShapefileなどのGISデータを読み込むにはsfライブラリーを使用します。

library(sf)

# Shapefileの読み込み
shapefile_data <- st_read("path/to/data/hoge.shp")

読み込んだshapefileはSimple featureとして定義される。

> shapefile_data

Simple feature collection with 9560 features and 6 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 139.334 ymin: 41.35165 xmax: 148.8944 ymax: 45.55724
Geodetic CRS:  JGD2011
First 10 features:
   N03_001    N03_002 N03_003 N03_004 N03_005 N03_007
1   北海道 石狩振興局    <NA>  札幌市  中央区   01101
2   北海道 石狩振興局    <NA>  札幌市    北区   01102
3   北海道 石狩振興局    <NA>  札幌市    東区   01103
4   北海道 石狩振興局    <NA>  札幌市  白石区   01104

ここでは試しに国土数値情報の行政区域ポリゴンを読んでみる。
地物数は9560あり、属性のフィールドは6つあることがわかります。
Geometry typeはポリゴンであることのほかデータ領域の座標(Bounding Box)や座標系(Geodetic CRS)などの情報もわかります。

変数名$フィールド名とすると、属性テーブルの各カラムの値にアクセスすることができる。
image.png

GeoJSONも同様に読める

river_data <- st_read("path/to/data/hogehoge.geojson")

image.png
(ここでは同じく国土数値情報の河川ラインデータ

地図の描画

そのままplotすると属性の各カラムで適当に色分けした図を出してくれる

plot(shapefile_data)

image.png

ggplotで描画するには以下のようにする

ggplot(data = shapefile_data) +
  geom_sf()

image.png

特定のカラムで色分けした図を描画するには以下のようにする

ggplot(data = shapefile_data) +
  geom_sf(aes(fill = カラム名))

ここではN03_002のカラムで色分け
image.png

複数のデータを重ね合わせるには以下のようにする

ggplot() +
  geom_sf(data = shapefile_data, fill = "lightgreen", color = "black", alpha = 0.5) +
  geom_sf(data = river_data, color = "lightblue",size =1) 


image.png

画像として保存

map_plot <- ggplot() +
  geom_sf(data = shapefile_data, fill = "green", color = "black", alpha = 0.5) +
  geom_sf(data = river_data, color = "lightblue",size =0.01) 


ggsave("Hokkaido.png", plot = map_plot, width = 12, height = 8, units = "in", dpi = 600)


画像として書き出せた
image.png

image.png

pngの他にpdfやjpgにも出力可能。
dpiで解像度を指定できます。

11
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?