|
| 1 | +#Solution 1. |
| 2 | +import os |
| 3 | +import glob |
| 4 | +import json |
| 5 | +def merge_json(path, ip_fname, op_fname, max_size): |
| 6 | + |
| 7 | + os.chdir(path) |
| 8 | + |
| 9 | + i = 1 |
| 10 | + size = 0 |
| 11 | + result = [] |
| 12 | + result_dict = {} |
| 13 | + |
| 14 | + for json_file in glob.glob(ip_fname + '*.json'): |
| 15 | + |
| 16 | + json_opened = open(json_file, encoding='utf-8') |
| 17 | + logs = json.load(json_opened) |
| 18 | + |
| 19 | + json_opened.seek(0,2) |
| 20 | + fsize = json_opened.tell() |
| 21 | + |
| 22 | + key = list(logs) |
| 23 | + if fsize <= max_size: |
| 24 | + if size + fsize <= max_size: |
| 25 | + result.extend(logs[key[0]]) |
| 26 | + else: |
| 27 | + result_dict[key[0]] = result[:] |
| 28 | + result.extend(logs[key[0]]) |
| 29 | + outfile = open(op_fname + str(i) +'.json', 'w') |
| 30 | + json.dump(result_dict, outfile, ensure_ascii=False) |
| 31 | + i += 1 |
| 32 | + size = 0 |
| 33 | + result_dict = {} |
| 34 | + result = [] |
| 35 | + result.extend(logs[key[0]]) |
| 36 | + |
| 37 | + size += fsize |
| 38 | + |
| 39 | + else: |
| 40 | + print("file "+ json_file + " is too large to write in a new file, try increasing the max size") |
| 41 | + |
| 42 | + if size <= max_size: |
| 43 | + result_dict[key[0]] = result[:] |
| 44 | + outfile = open(op_fname + str(i) +'.json', 'w') |
| 45 | + json.dump(result_dict, outfile, ensure_ascii=False) |
| 46 | + |
| 47 | + |
| 48 | +try: |
| 49 | + path = str(input("Enter path: ")) |
| 50 | + input_base_name = str(input("Enter input json file name suffix: ")) |
| 51 | + output_base_name = str(input("Enter the output json file name suffix: ")) |
| 52 | + max_size = int(input("Enter the maximum file size(in bytes): ")) |
| 53 | + merge_json(path, input_base_name, output_base_name, max_size) |
| 54 | + |
| 55 | +except: |
| 56 | + print("Error occured, try entering valid parameters...") |
| 57 | + |
| 58 | +#Solution 2. |
| 59 | +from json import load, JSONEncoder |
| 60 | +from optparse import OptionParser |
| 61 | +from re import compile |
| 62 | + |
| 63 | +float_pat = compile(r'^-?\d+\.\d+(e-?\d+)?$') |
| 64 | +charfloat_pat = compile(r'^[\[,\,]-?\d+\.\d+(e-?\d+)?$') |
| 65 | + |
| 66 | +parser = OptionParser(usage="""%prog [options] |
| 67 | +Group multiple GeoJSON files into one output file. |
| 68 | +Example: |
| 69 | + python %prog -p 2 input-1.json input-2.json output.json""") |
| 70 | + |
| 71 | +defaults = dict(precision=6) |
| 72 | + |
| 73 | +parser.set_defaults(**defaults) |
| 74 | + |
| 75 | +parser.add_option('-p', '--precision', dest='precision', |
| 76 | + type='int', help='Digits of precision, default %(precision)d.' % defaults) |
| 77 | + |
| 78 | +if __name__ == '__main__': |
| 79 | + |
| 80 | + options, args = parser.parse_args() |
| 81 | + infiles, outfile = args[:-1], args[-1] |
| 82 | + |
| 83 | + outjson = dict(type='FeatureCollection', features=[]) |
| 84 | + |
| 85 | + for infile in infiles: |
| 86 | + injson = load(open(infile)) |
| 87 | + |
| 88 | + if injson.get('type', None) != 'FeatureCollection': |
| 89 | + raise Exception('Sorry, "%s" does not look like GeoJSON' % infile) |
| 90 | + |
| 91 | + if type(injson.get('features', None)) != list: |
| 92 | + raise Exception('Sorry, "%s" does not look like GeoJSON' % infile) |
| 93 | + |
| 94 | + outjson['features'] += injson['features'] |
| 95 | + |
| 96 | + encoder = JSONEncoder(separators=(',', ':')) |
| 97 | + encoded = encoder.iterencode(outjson) |
| 98 | + |
| 99 | + format = '%.' + str(options.precision) + 'f' |
| 100 | + output = open(outfile, 'w') |
| 101 | + |
| 102 | + for token in encoded: |
| 103 | + if charfloat_pat.match(token): |
| 104 | + output.write(token[0] + format % float(token[1:])) |
| 105 | + |
| 106 | + elif float_pat.match(token): |
| 107 | + output.write(format % float(token)) |
| 108 | + |
| 109 | + else: |
| 110 | + output.write(token) |
0 commit comments