Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.fasterxml.jackson.core.JsonFactory;
Expand Down Expand Up @@ -74,12 +75,24 @@ protected void doPostProcess(Location location) {
* @throws IOException exception in parsing
*/
public void parseJson(File inputJsonFile) throws IOException {
// conduct JSON parsing ////////////////////////////
parseJson(inputJsonFile,null,null);
}

/**
* parse JSON object from the indicated JSON File
* @param inputJsonFile JSON file of location history obtained from Google take out
* @param fromDate start time of time range. null allowable
* @param toDate end time of time range. null allowable
* @throws IOException exception in parsing
*/
public void parseJson(File inputJsonFile,Date fromDate,Date toDate) throws IOException {
// prepare JSON parser instance ////////////////////
JsonFactory jsonFactory = new JsonFactory();
JsonParser jsonParser = jsonFactory.createParser(inputJsonFile);

// conduct JSON parsing ////////////////////////////
parseLocations(jsonParser);
parseLocations(jsonParser,fromDate,toDate);
}

/**
Expand All @@ -88,20 +101,34 @@ public void parseJson(File inputJsonFile) throws IOException {
* @throws IOException exception in parsing
*/
public void parseJson(InputStream jsonStream) throws IOException {
// conduct JSON parsing ////////////////////////////
parseJson(jsonStream,null,null);
}

/**
* parse JSON object from the indicated JSON stream
* @param jsonStream JSON input stream of location history obtained from Google take out
* @param fromDate start time of time range. null allowable
* @param toDate end time of time range. null allowable
* @throws IOException exception in parsing
*/
public void parseJson(InputStream jsonStream,Date fromDate,Date toDate) throws IOException {
// prepare JSON parser instance ////////////////////
JsonFactory jsonFactory = new JsonFactory();
JsonParser jsonParser = jsonFactory.createParser(jsonStream);

// conduct JSON parsing ////////////////////////////
parseLocations(jsonParser);
parseLocations(jsonParser,fromDate,toDate);
}

/**
* parse JSON object of location log data
* @param jsonParser JSON parser instance starting at root.
* @param fromDate start time of time range. null allowable
* @param toDate end time of time range. null allowable
* @throws IOException exception in parsing
*/
private void parseLocations(JsonParser jsonParser) throws IOException {
private void parseLocations(JsonParser jsonParser,Date fromDate,Date toDate) throws IOException {
// root object
if( jsonParser.nextToken() == JsonToken.START_OBJECT ) {
// loop until the end of root object
Expand Down Expand Up @@ -144,8 +171,16 @@ else if( locationKey.equals(KEY_ACTIVITYS) || locationKey.equals(KEY_ACTIVITY) )
activitys = parseActivitys(jsonParser);
}
}
// check if object is within time range
Location location = new Location(timestampMs,longitudeE7,latitudeE7,accuracy,velocity,heading,altitude,activitys);
doPostProcess(location);
if( fromDate == null && toDate == null );
else if( fromDate != null && toDate == null && timestampMs < fromDate.getTime() ) { location = null; }
else if( fromDate == null && toDate != null && toDate.getTime() < timestampMs ) { location = null; }
else if( fromDate != null && toDate != null && (timestampMs < fromDate.getTime() || toDate.getTime() < timestampMs) ) { location = null; }

if( location != null ) {
doPostProcess(location);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package jp.utokyo.shibalab.googletakeoutparser.locationlog;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;

Expand All @@ -23,14 +24,14 @@ public class Test01 {
* @param args 0:input JSON file, 1:output csv file
*/
public static void main(String[] args) {
File inputFile = new File(args[0]);
File outputFile = new File(args[1]);
Path inputPath = Paths.get(args[0]);
Path outputPath = Paths.get(args[1]);

long t0 = System.currentTimeMillis();
try (BufferedWriter bw=new BufferedWriter(new FileWriter(outputFile))) {
try (BufferedWriter bw=Files.newBufferedWriter(outputPath)) {
// parse JSON data ////////////////////////////
ObjectMapper jsonMapper = new ObjectMapper();
Locations locs = jsonMapper.readValue(inputFile,Locations.class);
Locations locs = jsonMapper.readValue(inputPath.toFile(),Locations.class);
List<Location> list = locs.listLocations();
Collections.sort(list);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package jp.utokyo.shibalab.googletakeoutparser.locationlog;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
Expand All @@ -20,11 +21,11 @@ public class Test02 {
* @param args 0:input JSON file, 1:output csv file
*/
public static void main(String[] args) {
File inputFile = new File(args[0]);
File outputFile = new File(args[1]);

Path inputPath = Paths.get(args[0]);
Path outputPath = Paths.get(args[1]);
long t0 = System.currentTimeMillis();
try (final BufferedWriter bw=new BufferedWriter(new FileWriter(outputFile))) {
try (BufferedWriter bw=Files.newBufferedWriter(outputPath)) {
// export header ///////////////////////////////
bw.write("timestamp,longitudde,latitude,accuracy,velocity,heading,altitude,activities");
bw.newLine();
Expand All @@ -41,7 +42,7 @@ public void doPostProcess(Location location) {
}
}
};
parser.parseJson(inputFile);
parser.parseJson(inputPath.toFile());
}
catch(JsonMappingException|JsonParseException exp) {
exp.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package jp.utokyo.shibalab.googletakeoutparser.locationlog;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;


/**
* convert JSON data of Google Location Logs into CSV data(stream input)
*/
public class Test03 {
/* ==============================================================
* static methods
* ============================================================== */
/**
* entry point
* @param args 0:input JSON file, 1:output csv file, 2:from date, 3:to date
*/
public static void main(String[] args) {
Path inputPath = Paths.get(args[0]);
Path outputPath = Paths.get(args[1]);

Date fromDate = args.length >= 3 ? parseDate(args[2]) : null;
Date toDate = args.length >= 4 ? parseDate(args[3]) : null;

long t0 = System.currentTimeMillis();
try (BufferedWriter bw=Files.newBufferedWriter(outputPath)) {
// export header ///////////////////////////////
bw.write("timestamp,longitudde,latitude,accuracy,velocity,heading,altitude,activities");
bw.newLine();

// parse JSON data ////////////////////////////
LocationParser parser = new LocationParser() {
public void doPostProcess(Location location) {
try {
bw.write(location.toCsvString());
bw.newLine();
}
catch(IOException exp) {
exp.printStackTrace();
}
}
};
parser.parseJson(inputPath.toFile(),fromDate,toDate);
}
catch(JsonMappingException|JsonParseException exp) {
exp.printStackTrace();
}
catch(IOException exp) {
exp.printStackTrace();
}
long t1 = System.currentTimeMillis();
System.out.printf("time duration: %.03f (sec) ", (t1-t0)/1000d);
}

/**
* parse date string into date instance
* @param string date string
* @return date instance, if successfully parsed
*/
private static Date parseDate(String string) {
Date date = null;
try{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date = string != null ? sdf.parse(string) : null;
}
catch(ParseException exp) {
exp.printStackTrace();
date = null;
}

return date;
}
}