Skip to content

Instantly share code, notes, and snippets.

@hhkaos
Last active April 1, 2024 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hhkaos/b37297a2f213bddc017ef28a3c34fc09 to your computer and use it in GitHub Desktop.
Save hhkaos/b37297a2f213bddc017ef28a3c34fc09 to your computer and use it in GitHub Desktop.
Export your Google Location History to CSV

Install: google_takeout_parser

Run: google_takeout_parser parse -f Location Takeout

Type this to create a CSV file:

def write_to_csv(file_path, data_instances):
    import csv
    with open(file_path, mode='w', newline='') as file:
        writer = csv.writer(file)

        # Write header
        writer.writerow(data_instances[0].__annotations__.keys())

        # Write data
        for instance in data_instances:
            writer.writerow(instance.__dict__.values())


write_to_csv('location_history.csv', res)

Finally load that data in with any JS SDK, like Esri's ArcGIS Maps SDK for JavaScript:

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="initial-scale=1,maximum-scale=1,user-scalable=no"
    />
    <title>
      Load location history data
    </title>

    <style>
      html,
      body,
      #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
      }
    </style>

    <link
      rel="stylesheet"
      href="https://js.arcgis.com/4.27/esri/themes/light/main.css"
    />
    <script src="https://js.arcgis.com/4.27/"></script>

    <script>
      require([
        "esri/Map",
        "esri/layers/CSVLayer",
        "esri/views/SceneView"
      ], (Map, CSVLayer, SceneView) => {

        const csvLayer = new CSVLayer({
          url: url
        });


        const map = new Map({
          basemap: "satellite",
          ground: "world-elevation"
        });

        map.add(csvLayer);

        const view = new SceneView({
          container: "viewDiv",
          map: map
        });
      });
    </script>
  </head>

  <body>
    <div id="viewDiv"></div>
  </body>
</html>

Count records using jq:

jq '.locations | map(select ( .timestamp | contains("2024-02")) ) | length' Records.json
@hhkaos
Copy link
Author

hhkaos commented Apr 1, 2024

As a reference, Counting records (my location) using DuckDB:
Screenshot 2024-04-01 at 17 35 20

# maximum_object_size = 2147483648 = 2GB
CREATE TABLE locations AS SELECT unnest(locations, recursive:= true) from read_json_auto('Records.json', maximum_object_size = 2147483648);
ALTER TABLE locations ALTER timestamp TYPE TIMESTAMPTZ;
CREATE TABLE stats AS
	SELECT YEAR(timestamp) AS y,
           MONTH(timestamp) AS m,
           COUNT(*) AS records
    FROM   locations
    GROUP BY
           YEAR(timestamp),
           MONTH(timestamp)
           ORDER BY y, m ASC;
COPY stats TO  'stats.csv';
y m records
2011 3 1
2011 5 5722
2011 6 1402
2011 7 2565
2011 8 629
2011 9 18
2011 10 288
2011 11 284
2011 12 60
2012 1 10
2012 3 462
2012 4 225
2012 5 2701
2012 6 493
2012 12 5355
2013 1 25501
2013 2 22985
2013 3 21957
2013 4 23620
2013 5 25031
2013 6 17820
2013 7 275
2013 11 1445
2014 2 389
2014 3 3983
2014 5 10464
2014 6 31135
2014 7 22704
2014 8 16898
2014 9 15570
2014 10 20842
2014 11 16521
2014 12 16079
2015 1 8056
2015 2 5287
2015 3 4042
2015 4 1962
2015 5 15197
2015 6 20262
2015 7 33242
2015 8 6113
2015 9 13242
2015 10 12664
2015 11 21446
2015 12 33711
2016 1 31776
2016 2 44333
2016 3 39387
2016 4 58776
2016 5 86022
2016 6 70616
2016 7 27133
2016 8 24383
2016 9 1669
2016 10 1541
2016 12 178
2017 1 2665
2017 2 7825
2017 3 2517
2017 4 36749
2017 5 37541
2017 6 19008
2017 7 30676
2017 8 52915
2017 9 43244
2017 10 73855
2017 11 38248
2017 12 20482
2018 1 18910
2018 2 12346
2018 3 22906
2018 4 32217
2018 5 15052
2018 6 28839
2018 7 20731
2018 8 17824
2018 9 30485
2018 10 20498
2018 11 31862
2018 12 17509
2019 1 19085
2019 2 14666
2019 3 15729
2019 4 18531
2019 5 17589
2019 6 21178
2019 7 22452
2019 8 24970
2019 9 22832
2019 10 18281
2019 11 19920
2019 12 26725
2020 1 39820
2020 2 24533
2020 3 31466
2020 4 15419
2020 5 13207
2020 6 20096
2020 7 25267
2020 8 19738
2020 9 11351
2020 10 20787
2020 11 13954
2020 12 13271
2021 1 12610
2021 2 17284
2021 3 26782
2021 4 22498
2021 5 30102
2021 6 28319
2021 7 25068
2021 8 36067
2021 9 24654
2021 10 22459
2021 11 24448
2021 12 35768
2022 1 15458
2022 2 15564
2022 3 15783
2022 4 18745
2022 5 12617
2022 6 22160
2022 7 37395
2022 8 17598
2022 9 14563
2022 10 23596
2022 11 13638
2022 12 17364
2023 1 17100
2023 2 18715
2023 3 21737
2023 4 22825
2023 5 21189
2023 6 15199
2023 7 17404
2023 8 15825
2023 9 15271
2023 10 14830
2023 11 12047
2023 12 12982
2024 1 10988
2024 2 13539
2024 3 12274

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment