tsukubar / r-spatial-guide

Guide to Spatial Data Analysis and Mapping Using R
https://tsukubar.github.io/r-spatial-guide/
9 stars 2 forks source link

サンプルコード中のgeom_rot2関数が見つからない #7

Open niszet opened 3 years ago

niszet commented 3 years ago

コードをコピペしながらsfパッケージの勉強をしていたところ、下記のgeom_rot2という関数が見つからなくてプロットが出来ませんでした。

https://github.com/tsukubar/r-spatial-guide/blob/master/simple-feature-for-r.Rmd#L295

検索しても情報見つからず、何か別途関数定義が必要でしょうか?

uribo commented 3 years ago

申し訳ありません。

確かに関数の呼び出し部分が不足しておりました。 取り急ぎ、 https://github.com/uribo/foss4g-2018-tokyo/blob/d1f5d49d09164d7183a9eea6a1747b7ac25fbd28/R/readme-banner.R#L233-L248の部分

tran <- function(geometry, rotang, center) {
  rot = function(a) matrix(c(cos(a), sin(a), -sin(a), cos(a)), 2, 2)
  (geometry - center) * rot(rotang * pi / 180) + center
}

geom_rot2 <- function(geometry, rotang) {
  center <- 
    suppressWarnings(st_centroid(st_combine(geometry)))

  inpoly_rot <- 
    tran(st_combine(geometry), 
         rotang, 
         center)

  inpoly_rot
}

を読み込んでもらえば 指摘いただいた箇所のプロットが実行されます。

niszet commented 3 years ago

ご対応ありがとうございました。無事に動いてプロットできました。

実行していて気付いたのですが、いくつかwarningが出たりしますので、この件でのサイト修正のときにでもあわせて更新していただければ…。

sf_poi <-
  tibble::data_frame(
  id = seq(1, 2),
  lng = c(140.112, 140.12),
  lat = c(36.083, 36.05)) %>%
  st_as_sf(coords = c("lng", "lat"),
  crs = 4326)

#> Warning: `data_frame()` is deprecated as of tibble 1.1.0.
#> Please use `tibble()` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_warnings()` to see where this warning was generated.
ne_jpn <-
  ne_states(country = "Japan", returnclass = "sf") %>%
  tibble::new_tibble(subclass = "sf")

#> Warning: The `subclass` argument of `new_tibble()` is deprecated as of tibble 2.0.0.
#> Please use the `class` argument instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_warnings()` to see where this warning was generated.
#> Warning: The `nrow` argument of `new_tibble()` can't be missing as of tibble 2.0.0.
#> `x` must be a scalar integer.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_warnings()` to see where this warning was generated.
st_read(dsn, options = c("ENCODING=CP932"))

まだコードコピペして実行しているだけですが、大変勉強になってます。ありがとうございます🍣🍣🍣