Skip to content

Instantly share code, notes, and snippets.

@Scarygami
Created November 10, 2014 11:18
Show Gist options
  • Save Scarygami/746494e6615218331afe to your computer and use it in GitHub Desktop.
Save Scarygami/746494e6615218331afe to your computer and use it in GitHub Desktop.
location_history_minify
#!/usr/bin/env python
# Copyright 2014 Gerwin Sturm, FoldedSoft e.U. / www.foldedsoft.at
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import json
import math
from argparse import ArgumentParser
def main(argv):
arg_parser = ArgumentParser()
arg_parser.add_argument("input", help="Input File (JSON)")
arg_parser.add_argument("-o", "--output", help="Output File")
args = arg_parser.parse_args()
if not args.output:
args.output = '.'.join(args.input.split('.')[:-1]) + '_small.json'
if args.input == args.output:
arg_parser.error("Input and output have to be different files")
return
try:
json_data = open(args.input).read()
except:
print("Error opening input file")
return
try:
data = json.loads(json_data)
except:
print("Error decoding json")
return
if "locations" in data and len(data["locations"]) > 0:
try:
f_out = open(args.output, "w")
except:
print("Error creating output file for writing")
return
locations = [{
"timestampMs": item["timestampMs"],
"latitudeE7": item["latitudeE7"],
"longitudeE7": item["longitudeE7"],
} for item in data["locations"]]
json.dump({"locations": locations}, f_out)
f_out.close()
else:
print("No data found in json")
return
if __name__ == "__main__":
sys.exit(main(sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment