0
2

More than 5 years have passed since last update.

GPSデータをUTM変換してポイント間の距離を計算する

Posted at

gDistance

以下のようなデータを使う。

image

UTM 座標に変換した後、rgeos パッケージの gDistance 関数で計算する。

gDistance
library(rgeos)
library(sp)

# ファイルの読み込み
d<-read.csv(適切な csv ファイル)
# GPSデータ(緯度経度形式)をまず sp 形式にする
d_longlat<-SpatialPoints(d[,c("longitude","latitude")],proj4string = CRS("+proj=longlat +ellps=WGS84"))
# UTM に変換
d_utm<-spTransform(d_longlat,CRS("+proj=utm +north +zone=52 +ellps=WGS84"))

# id 列を GPS データにひもづけ
idd_sp<-data.frame(d$id)
coordinates(idd_sp)<-d_utm@coords

# ポイント間の距離を計算する
distance<-gDistance(idd_sp,byid=TRUE)

# データフレームに変換・行と列に名前をつける・csv ファイルに書き出し
distance<-data.frame(distance)
colnames(distance)<-d$id
rownames(distance)<-d$id
write.csv(distance,"distance.csv")

計算結果は以下のような matrix で出力される。
image

gWithinDistance

値に興味がない場合は、2 つのポイントがある距離より離れているかどうかということのみ判定することもできる。
gDistance のかわりに gWithinDistance を使う。

gWithinDistance
# ポイント間の距離が dist (m) 以内かどうかを判定する
# 下の場合は 200 m
wdistance<-gWithinDistance(idd_sp,dist=200,byid=TRUE)

計算結果は以下のような matrix で出力される。
image

欲しい情報をここからどのように抽出していくかはあとで考える。

参考にしたページ

rgeos
https://cran.r-project.org/web/packages/rgeos/rgeos.pdf

0
2
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
0
2