Skip to content

Instantly share code, notes, and snippets.

@jbergler
Last active May 14, 2021 23:46
Show Gist options
  • Save jbergler/e9a172bb021396389b5db31a694cc8ec to your computer and use it in GitHub Desktop.
Save jbergler/e9a172bb021396389b5db31a694cc8ec to your computer and use it in GitHub Desktop.
Mapping Google location history using InfluxDB & R
require(influxdb)
require(OpenStreetMap)
results <- influxdb_query(
'localhost', '8086', 'my-database', '', '',
'SELECT * FROM "data.google.location_history" WHERE time >= \'2013-09-01\' AND time < \'2017-01-01\''
)
map <- openmap(
c(51.40,-0.5),
c(51.64,0.10),
type='http://a.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png'
)
png(
filename='map.png',
width=4000,
height=3000
)
plot(map)
points(
projectMercator(results$data.google.location_history$lat, results$data.google.location_history$lng),
col=rgb(0, 0, 1, 0.5),
pch=16,
cex=0.65
)
dev.off() # close the file
#!/usr/bin/env ruby
require 'JSON'
require 'influxdb'
data = JSON.parse(File.open('./LocationHistory.json'))
influxdb = InfluxDB::Client.new(
'mydatabase',
async: false, # time_precision is broken in async mode :/
time_precision: 'ms',
)
# process data in batches of 10k
data["locations"].each_slice(10_000).with_index do |slice, index|
puts "processing offset #{index}"
influxdb.write_points(slice.map { |r| {
series: "data.google.location_history",
timestamp: r["timestampMs"].to_i,
values: {
# normalise co-ordinates from 1*10^7
lat: r["latitudeE7"].to_f / 10**7,
lng: r["longitudeE7"].to_f / 10**7,
accuracy: r["accuracy"].to_i,
}
}})
end
require(influxdb)
require(OpenStreetMap)
map <- openmap(
c(51.40,-0.5),
c(51.64,0.10),
type='http://a.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png'
)
renderFrame <- function(i, map, t0, min) {
png(
filename=paste('~/map/map', formatC(i, width = 3, format = "d", flag = "0"), 'png', sep="."),
width=900,
height=580
)
plot(map)
for (i in 1:7) {
x0 <- t0 - (i*7)
x1 <- t0 - ((i-1)*7)
if (i == 7) {
x0 <- min
}
results <- try(influxdb_query(
'localhost', '8086', 'mytsd', '', '',
paste("SELECT * FROM \"data.google.location_history\" WHERE time >= '", x0, "' AND time < '", x1, "'", sep="")
))
if (length(results) > 0) {
points(
projectMercator(results$data.google.location_history2$lat, results$data.google.location_history2$lng),
col=rgb(0, 0, 1, 0.5),
pch=16,
cex=(0.65 - (i * 0.05))
)
}
}
usr <- par("usr")
text(usr[2] - 500, usr[3] + 500, t0, adj = c( 1, 0 ), col = "blue" )
dev.off()
}
dates <- seq(as.Date("2013-09-01"), as.Date("2017-01-01"), "week")
for (i in 1:(NROW(dates)-1)) {
t0 <- dates[i]
print(paste(i, ": doing ", t0, sep=""))
renderFrame(i, map, t0, dates[1])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment