Converting Google Location History into GeoJSON for Compass

After a lot of procrastinating, I finally decided to import all of my Google Location History into Compass. This meant I had to convert Google’s export format into Compass’s GeoJSON.

A few notes

  • timeStampMs is the timestamp in milliseconds.  Most functions use the timestamp as seconds, so this needed to be converted into an ISO8601 date format.
  • latitudeE7 and longitudeE7 are the coordinates, and must be divided by 1e7 in order to get the more conventional format. Also, there was an issue where some of the numbers were incorrectly exported, so there is a sanity check on that below
  • accuracy is, as suggested, the accuracy of the location
  • Finally, there is activity. I considered doing more with this, but I’m just storing it. Will explain why below the example of my simple PHP code to export.
json = json_decode( $file, true );
$json = $json['locations'];
foreach( $json as $item ) {
	$date = date_create_from_format( 'U', round( $item['timestampMs'] / 1000 ) );
	if ( $item['latitudeE7'] > 900000000 ) {
		$item['latitudeE7'] = $item['latitudeE7'] - 4294967296;
	}
	if ( $item['longitudeE7'] > 900000000 ) {
		$item['longitudeE7'] = $item['longitudeE7'] - 4294967296;
	}
	$output = array(
		'locations' => array(
			array(
				'type' => 'Feature',
				'geometry' => array(
					'type' => 'Point',
					'coordinates' => array(
						( $item['longitudeE7'] / 1e7 ),
						( $item['latitudeE7'] / 1e7 )
					)
				),
				'properties' => array(
					'timestamp' => $date->format( DATE_W3C ),
					'horizontal_accuracy' => $item['accuracy']
				)
			)
		)
	);
				
	if ( array_key_exists( 'activity', $item ) ) {
		$output['locations'][0]['properties']['activity'] = $item['activity'];
	}

For activity, it only appears on some entries, and is for the most part, accurate enough..

  "activity" : [ {
        "type" : "IN_VEHICLE",
        "confidence" : 100
      }

It notes the type of activity, and the confidence level.   However, in this example…

  "activity":[ 
    {"type":"UNKNOWN","confidence":50},          
    {"type":"ON_FOOT","confidence":36}, 
    {"type":"ON_BICYCLE","confidence":11},  
    {"type":"IN_VEHICLE","confidence":2}
  ]

In this example, Google thinks I am either doing something unknown, walking, riding, or on a bicycle. I promise, never a bicycle. So, for now, I consider the information to be less than useful, but am storing it for future use only.

Looking forward to using the data in interesting ways.

David Shanske

My day job is in training for an airline. I also develop Indieweb WordPress plugins so that others can take control of their online identity.

Leave a Reply

Your email address will not be published. Required fields are marked *