In this blog post I will post a visualization of my trips between 2015 and 2018 mainly in germany. Further it contains the Java code that I used to convert the proprietary JSON format used by the google maps timeline for locations logs into a csv file.
If you collected your location data with Google like me you can download it here: https://www.google.de/maps/timeline
You can convert it into a csv file that I used for kepler.gl with the following Java snippet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
package de.incentergy.blog.trips; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import javax.json.Json; import javax.json.stream.JsonParser; public class ConvertGoogleJSONToCSV { public static void main(String[] args) throws IOException { StringBuilder jsonFeatureCollection = new StringBuilder("time,on_lon,on_lat,off_lon,off_lat\n"); jsonFeatureCollection.append(loadWaypoints().stream() .filter(map -> map.get("longitudeE7") != null && map.get("latitudeE7") != null && ("inVehicle".equals(map.get("activity")) || "exitingVehicle".equals(map.get("activity")))) .map(map -> "inVehicle".equals(map.get("activity")) ? (("\n" + Long.parseLong((String) map.get("timestampMs")) / 1000) + "," + (((long) map.get("longitudeE7")) / 10000000d) + "," + (((long) map.get("latitudeE7")) / 10000000d)) : ("," + (((long) map.get("longitudeE7")) / 10000000d) + "," + (((long) map.get("latitudeE7")) / 10000000d))) .collect(Collectors.joining())); Files.write(Paths.get("src/test/resources/CarOnOff.csv"), jsonFeatureCollection.toString().getBytes()); } private static List<Map<String, Object>> loadWaypoints() throws FileNotFoundException { List<Map<String, Object>> list = new ArrayList<>(); try (JsonParser parser = Json.createParser(new FileInputStream("src/test/resources/Standortverlauf.json"))) { // INFO: START_OBJECT JsonParser.Event event = parser.next(); // INFO: KEY_NAME event = parser.next(); // INFO: START_ARRAY event = parser.next(); Map<String, Object> waypoint = new HashMap<>(); while (parser.hasNext()) { event = parser.next(); if (event == JsonParser.Event.KEY_NAME && parser.getString().equals("timestampMs")) { event = parser.next(); String timestampMs = parser.getString(); waypoint.put("timestampMs", timestampMs); } else if (event == JsonParser.Event.KEY_NAME && parser.getString().equals("activities")) { while (parser.hasNext()) { event = parser.next(); if (event == JsonParser.Event.KEY_NAME && parser.getString().equals("type")) { event = parser.next(); String type = parser.getString(); waypoint.put("activity", type); list.add(waypoint); waypoint = new HashMap<>(); break; } } } else if (event == JsonParser.Event.KEY_NAME && parser.getString().equals("latitudeE7")) { event = parser.next(); long latitudeE7 = parser.getLong(); waypoint.put("latitudeE7", latitudeE7); } else if (event == JsonParser.Event.KEY_NAME && parser.getString().equals("longitudeE7")) { event = parser.next(); long longitudeE7 = parser.getLong(); waypoint.put("longitudeE7", longitudeE7); } } } return list; } } |
Output:
time,on_lon,on_lat,off_lon,off_lat
1475855065,11.2699902,48.2149136,11.3288093,48.2121081
1475854938,11.3468685,48.2070096
1475854877,11.3849883,48.1889583
1475854816,11.4114246,48.1743441
1475854754,11.4334412,48.1623226
1475854692,11.4521627,48.1526343,11.4614298,48.1498995
1475854174,11.4777896,48.1460463
1475854098,11.4981929,48.1449333
…
The maven pom.xml including the needed dependencies should look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>de.incentergy.blog</groupId> <artifactId>trip-visualization</artifactId> <version>0.0.1-SNAPSHOT</version> <name>Trip Visualization</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.glassfish</groupId> <artifactId>javax.json</artifactId> <version>1.0.4</version> </dependency> </dependencies> </project> |
Conclusion
In this blog post I have shown how you can easily visualize your trip data that was collected by google. If you need support visualizing your own data please contact us.
Leave a Reply