Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import datetime
- import json
- def read_json(file):
- with open(file) as fd:
- return json.load(fd)["locations"]
- def convert_timestamps(locations):
- for loc in locations:
- convert_timestamp(loc)
- def convert_timestamp(location):
- from_ts = datetime.datetime.fromtimestamp
- location["timestamp"] = from_ts(int(location["timestampMs"]) / 1000).astimezone(datetime.timezone.utc)
- del location["timestampMs"]
- def convert_locations(locations):
- for loc in locations:
- convert_location(loc)
- def convert_location(location):
- lat, long = int(location['latitudeE7']), int(location['longitudeE7'])
- # 32 bit overflow bugfix
- if lat > 900000000:
- lat -= 4294967296
- if long > 1800000000:
- long -= 4294967296
- lat /= 1e7
- long /= 1e7
- del location['latitudeE7'], location['longitudeE7']
- location['lat'] = lat
- location['long'] = long
- def by_datetime(locations, start=None, end=None):
- if start:
- start = start.astimezone(datetime.timezone.utc)
- if end:
- end = end.astimezone(datetime.timezone.utc)
- if start and end:
- condition = lambda ts: start <= ts <= end
- elif start and end is None:
- condition = lambda ts: start <= ts
- elif start is None and end:
- condition = lambda tx: ts <= end
- else:
- raise TypeError("start and/or end must be given")
- for loc in locations:
- if condition(loc["timestamp"]):
- yield loc
- def area(locations):
- iterator = iter(locations)
- first = next(iterator)
- lat, long = first["lat"], first["long"]
- min_lat = lat
- max_lat = lat
- min_long = long
- max_long = long
- for loc in iterator:
- lat, long = loc["lat"], loc["long"]
- min_lat = min(min_lat, lat)
- max_lat = max(max_lat, lat)
- min_long = min(min_long, long)
- max_long = max(max_long, long)
- return {
- "min_lat": min_lat,
- "max_lat": max_lat,
- "min_long": min_long,
- "max_long": max_long,
- }
- locations = read_json("Standortverlauf.json")
- convert_timestamps(locations)
- convert_locations(locations)
- week = datetime.timedelta(days=7)
- today = datetime.datetime.now()
- last_7_days = list(by_datetime(locations, today - week))
- print(f"Last 7 days: {len(last_7_days)} datapoints")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement