如何在 R 中读取我的 Google 位置历史记录
How do I read my Google Location History in R
我在
找到了我的 Google 位置记录
https://maps.google.co.uk/locationhistory/b/0/?hl=en-GB
然后我下载了 KML 文件
我安装 rgdal
正确但无法读取文件
我把文件名和图层名从https://gis.stackexchange.com/questions/58131/how-to-efficiently-read-a-kml-file-into-r
hist = readOGR(dsn="/home/ajay/Desktop/history-05-04-2015",layer="Location history from 05/05/2015 to 06/04/2015")
这是文件的样子
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>Location history from 05/05/2015 to 06/04/2015</name>
<open>1</open>
<description/>
<StyleMap id="multiTrack">
这是错误
>Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, :
Cannot open file
附加信息
> ogrDrivers()
name write
1 AVCBin FALSE
2 AVCE00 FALSE
3 BNA TRUE
4 CSV TRUE
5 DGN TRUE
6 DODS FALSE
7 DXF TRUE
8 ESRI Shapefile TRUE
9 Geoconcept TRUE
10 GeoJSON TRUE
11 GeoRSS TRUE
12 GML TRUE
13 GMT TRUE
14 GPSTrackMaker TRUE
15 GPX TRUE
16 Interlis 1 TRUE
17 Interlis 2 TRUE
18 KML TRUE
19 MapInfo File TRUE
20 Memory TRUE
21 MySQL TRUE
22 ODBC TRUE
23 OGDI FALSE
24 PCIDSK FALSE
25 PGeo FALSE
26 PostgreSQL TRUE
27 REC FALSE
28 S57 TRUE
29 SDTS FALSE
30 SQLite TRUE
31 TIGER TRUE
32 UK .NTF FALSE
33 VFK FALSE
34 VRT FALSE
35 XPlane FALSE
> sessionInfo()
R version 3.2.0 (2015-04-16)
Platform: i686-pc-linux-gnu (32-bit)
Running under: Ubuntu precise (12.04.5 LTS)
locale:
[1] LC_CTYPE=en_IN.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_IN.UTF-8 LC_COLLATE=en_IN.UTF-8
[5] LC_MONETARY=en_IN.UTF-8 LC_MESSAGES=en_IN.UTF-8
[7] LC_PAPER=en_IN.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_IN.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base
other attached packages:
[1] rgdal_0.9-3 sp_1.1-0
loaded via a namespace (and not attached):
[1] tools_3.2.0 grid_3.2.0 lattice_0.20-31
我将文件读入 trajectories:Track
对象:
filename = "history-06-14-2015.kml"
library(XML)
kml <- xmlToList(filename)
tr = kml$Document$Placemark$Track
cc = which(names(tr) == "coord")
coord = t(sapply(kml$Document$Placemark$Track[cc], function(x) scan(text = x, quiet = TRUE)))[,1:2]
when = which(names(tr) == "when")
# convert the "-07:00" into " -0700" with sub:
time = strptime(sub("([+\-])(\d\d):(\d\d)$", " \1\2\3",
unlist(kml$Document$Placemark$Track[when])), "%Y-%m-%dT%H:%M:%OS %z")
library(sp)
library(spacetime)
library(trajectories)
track = Track(STI(SpatialPoints(coord, CRS("+proj=longlat +ellps=WGS84")),
time))
summary(track)
plot(track, axes = TRUE)
sub
调用处理不同的时区; R 的 strptime
无法读取,例如-07:00,但理解 -0700。
您可以通过以下方式将其转换为 data.frame
:
as(track, "data.frame")
最近(自 2015 年 7 月起),google 允许您 download a copy of all your data。您可以导入通过
获得的json文件
library(jsonlite)
system.time(x <- fromJSON("Location History/LocationHistory.json"))
loc = x$locations
loc$time = as.POSIXct(as.numeric(x$locations$timestampMs)/1000,
origin = "1970-01-01")
loc$lat = loc$latitudeE7 / 1e7
loc$lon = loc$longitudeE7 / 1e7
library(sp)
loc.sp = loc
coordinates(loc.sp) = ~lon+lat
proj4string(loc.sp) = CRS("+proj=longlat +datum=WGS84")
library(spacetime)
library(trajectories)
tr = Track(STIDF(geometry(loc.sp), loc.sp$time, loc.sp@data))
plot(tr)
这个数据集还有精度信息,activity模式分类。
通常它应该是这样工作的:
library(rgdal)
kml_fname <- "path/to/history-05-04-2015.kml"
tracks <- readOGR(kml_fname, ogrListLayers(kml_fname)[1])
但是我看到了:
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, :
object 'keepGeoms' not found
In addition: Warning message:
In ogrFIDs(dsn = dsn, layer = layer) : no features found
KML
驱动程序可能存在问题。稍微好一点的驱动程序是 LIBKML
,但我的 rgdal
版本没有安装它(而且很难安装)。
您可以尝试使用 GDAL 和 LIBKML 将 KML 文件转换为 GML。例如,在命令提示符下,首先 运行 ogrinfo
查看是否可以使用 LIBKML 读取文件,然后尝试 运行ning ogr2ogr 转换它:
ogrinfo history-05-04-2015.kml
INFO: Open of `history-05-04-2015.kml'
using driver `LIBKML' successful.
1: history-05-04-2015
ogr2ogr -f GML history-05-04-2015.gml history-05-04-2015.kml
(注:我也看到了"Warning 1: Layer name 'history-05-04-2015' adjusted to 'history_05_04_2015' for XML validity.")
然后在 R 中:
gml_fname <- "path/to/history-05-04-2015.gml"
tracks <- readOGR(gml_fname, ogrListLayers(gml_fname)[1])
显示
OGR data source with driver: GML
Source: "C:\Users\mtoews\Downloads\history-05-04-2015.gml", layer: "history_05_04_2015"
with 1 features
It has 12 fields
Warning message:
In readOGR(gml_fname, ogrListLayers(gml_fname)[1]) : Z-dimension discarded
但结果在其他情况下效果很好,例如plot(tracks)
library(jsonlite)
a=fromJSON("/home/rstudio/R/Takeout/Location History/LocationHistory.json")
b=as.data.frame(a)
mygoog=NULL
mygoog$latitude=b$locations.latitudeE7/10000000
mygoog$longitude=b$locations.longitudeE7/10000000
mygoog$time=as.POSIXct(as.numeric(b$locations.timestampMs)/1000 , origin="1970-01-01")
mygoog=as.data.frame(mygoog)
library(ggmap)
Map <- get_googlemap(center = c(lon = median(mygoog$longitude), lat = median(mygoog$latitude)),
zoom = 12,
size = c(640, 640),
scale = 2, maptype = c("terrain"),
color = "color")
plot1 <- ggmap(Map) +
geom_path(data = mygoog, aes(x = longitude, y = latitude
),
alpha = I(0.5),
size = 0.8)
suppressWarnings(print(plot1))
mygoog2=mygoog[time>"2015-09-21 12:09:31",,]
plot1 <- ggmap(Map) +
geom_path(data = mygoog2, aes(x = longitude, y = latitude
),
alpha = I(0.5),
size = 0.8)
suppressWarnings(print(plot1))
我在
找到了我的 Google 位置记录https://maps.google.co.uk/locationhistory/b/0/?hl=en-GB
然后我下载了 KML 文件
我安装 rgdal
正确但无法读取文件
我把文件名和图层名从https://gis.stackexchange.com/questions/58131/how-to-efficiently-read-a-kml-file-into-r
hist = readOGR(dsn="/home/ajay/Desktop/history-05-04-2015",layer="Location history from 05/05/2015 to 06/04/2015")
这是文件的样子
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>Location history from 05/05/2015 to 06/04/2015</name>
<open>1</open>
<description/>
<StyleMap id="multiTrack">
这是错误
>Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, :
Cannot open file
附加信息
> ogrDrivers()
name write
1 AVCBin FALSE
2 AVCE00 FALSE
3 BNA TRUE
4 CSV TRUE
5 DGN TRUE
6 DODS FALSE
7 DXF TRUE
8 ESRI Shapefile TRUE
9 Geoconcept TRUE
10 GeoJSON TRUE
11 GeoRSS TRUE
12 GML TRUE
13 GMT TRUE
14 GPSTrackMaker TRUE
15 GPX TRUE
16 Interlis 1 TRUE
17 Interlis 2 TRUE
18 KML TRUE
19 MapInfo File TRUE
20 Memory TRUE
21 MySQL TRUE
22 ODBC TRUE
23 OGDI FALSE
24 PCIDSK FALSE
25 PGeo FALSE
26 PostgreSQL TRUE
27 REC FALSE
28 S57 TRUE
29 SDTS FALSE
30 SQLite TRUE
31 TIGER TRUE
32 UK .NTF FALSE
33 VFK FALSE
34 VRT FALSE
35 XPlane FALSE
> sessionInfo()
R version 3.2.0 (2015-04-16)
Platform: i686-pc-linux-gnu (32-bit)
Running under: Ubuntu precise (12.04.5 LTS)
locale:
[1] LC_CTYPE=en_IN.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_IN.UTF-8 LC_COLLATE=en_IN.UTF-8
[5] LC_MONETARY=en_IN.UTF-8 LC_MESSAGES=en_IN.UTF-8
[7] LC_PAPER=en_IN.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_IN.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base
other attached packages:
[1] rgdal_0.9-3 sp_1.1-0
loaded via a namespace (and not attached):
[1] tools_3.2.0 grid_3.2.0 lattice_0.20-31
我将文件读入 trajectories:Track
对象:
filename = "history-06-14-2015.kml"
library(XML)
kml <- xmlToList(filename)
tr = kml$Document$Placemark$Track
cc = which(names(tr) == "coord")
coord = t(sapply(kml$Document$Placemark$Track[cc], function(x) scan(text = x, quiet = TRUE)))[,1:2]
when = which(names(tr) == "when")
# convert the "-07:00" into " -0700" with sub:
time = strptime(sub("([+\-])(\d\d):(\d\d)$", " \1\2\3",
unlist(kml$Document$Placemark$Track[when])), "%Y-%m-%dT%H:%M:%OS %z")
library(sp)
library(spacetime)
library(trajectories)
track = Track(STI(SpatialPoints(coord, CRS("+proj=longlat +ellps=WGS84")),
time))
summary(track)
plot(track, axes = TRUE)
sub
调用处理不同的时区; R 的 strptime
无法读取,例如-07:00,但理解 -0700。
您可以通过以下方式将其转换为 data.frame
:
as(track, "data.frame")
最近(自 2015 年 7 月起),google 允许您 download a copy of all your data。您可以导入通过
获得的json文件library(jsonlite)
system.time(x <- fromJSON("Location History/LocationHistory.json"))
loc = x$locations
loc$time = as.POSIXct(as.numeric(x$locations$timestampMs)/1000,
origin = "1970-01-01")
loc$lat = loc$latitudeE7 / 1e7
loc$lon = loc$longitudeE7 / 1e7
library(sp)
loc.sp = loc
coordinates(loc.sp) = ~lon+lat
proj4string(loc.sp) = CRS("+proj=longlat +datum=WGS84")
library(spacetime)
library(trajectories)
tr = Track(STIDF(geometry(loc.sp), loc.sp$time, loc.sp@data))
plot(tr)
这个数据集还有精度信息,activity模式分类。
通常它应该是这样工作的:
library(rgdal)
kml_fname <- "path/to/history-05-04-2015.kml"
tracks <- readOGR(kml_fname, ogrListLayers(kml_fname)[1])
但是我看到了:
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, :
object 'keepGeoms' not found
In addition: Warning message:
In ogrFIDs(dsn = dsn, layer = layer) : no features found
KML
驱动程序可能存在问题。稍微好一点的驱动程序是 LIBKML
,但我的 rgdal
版本没有安装它(而且很难安装)。
您可以尝试使用 GDAL 和 LIBKML 将 KML 文件转换为 GML。例如,在命令提示符下,首先 运行 ogrinfo
查看是否可以使用 LIBKML 读取文件,然后尝试 运行ning ogr2ogr 转换它:
ogrinfo history-05-04-2015.kml
INFO: Open of `history-05-04-2015.kml'
using driver `LIBKML' successful.
1: history-05-04-2015
ogr2ogr -f GML history-05-04-2015.gml history-05-04-2015.kml
(注:我也看到了"Warning 1: Layer name 'history-05-04-2015' adjusted to 'history_05_04_2015' for XML validity.")
然后在 R 中:
gml_fname <- "path/to/history-05-04-2015.gml"
tracks <- readOGR(gml_fname, ogrListLayers(gml_fname)[1])
显示
OGR data source with driver: GML
Source: "C:\Users\mtoews\Downloads\history-05-04-2015.gml", layer: "history_05_04_2015"
with 1 features
It has 12 fields
Warning message:
In readOGR(gml_fname, ogrListLayers(gml_fname)[1]) : Z-dimension discarded
但结果在其他情况下效果很好,例如plot(tracks)
library(jsonlite)
a=fromJSON("/home/rstudio/R/Takeout/Location History/LocationHistory.json")
b=as.data.frame(a)
mygoog=NULL
mygoog$latitude=b$locations.latitudeE7/10000000
mygoog$longitude=b$locations.longitudeE7/10000000
mygoog$time=as.POSIXct(as.numeric(b$locations.timestampMs)/1000 , origin="1970-01-01")
mygoog=as.data.frame(mygoog)
library(ggmap)
Map <- get_googlemap(center = c(lon = median(mygoog$longitude), lat = median(mygoog$latitude)),
zoom = 12,
size = c(640, 640),
scale = 2, maptype = c("terrain"),
color = "color")
plot1 <- ggmap(Map) +
geom_path(data = mygoog, aes(x = longitude, y = latitude
),
alpha = I(0.5),
size = 0.8)
suppressWarnings(print(plot1))
mygoog2=mygoog[time>"2015-09-21 12:09:31",,]
plot1 <- ggmap(Map) +
geom_path(data = mygoog2, aes(x = longitude, y = latitude
),
alpha = I(0.5),
size = 0.8)
suppressWarnings(print(plot1))