|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +
|
| 5 | + * Generate a geohash of the (lat, lon) |
| 6 | + * Add the geohash and some shortened versions to the JSON (See https://github.com/vinsci/geohash/) |
| 7 | + - pip3 install Geohash |
| 8 | + - geohash = Geohash.encode(lat, lon) |
| 9 | + - geohash precision: https://gis.stackexchange.com/questions/115280/what-is-the-precision-of-a-geohash |
| 10 | + * Store entire record as JSONB |
| 11 | + * Index the JSONB |
| 12 | + * Pull out id, timestamp, the 4-char geohash, and a GEOGRAPHY as separate columns |
| 13 | + * PK => (geohash4, id) |
| 14 | + * Pub review sources: |
| 15 | + - https://whatpub.com/ |
| 16 | +
|
| 17 | + * ./osm_xml_to_json.py extracted.osm.bz2 5000000 5567.39s user 28.17s system 6% cpu 25:15:00.26 total |
| 18 | + * cockroach dump defaultdb osm_json --insecure 279.00s user 61.36s system 179% cpu 3:10.11 total |
| 19 | + * gzip - > backup.sql.gz 57.24s user 1.92s system 31% cpu 3:10.11 total |
| 20 | +
|
| 21 | + * DDL: |
| 22 | +
|
| 23 | + DROP TABLE IF EXISTS osm_json; |
| 24 | + CREATE TABLE osm_json |
| 25 | + ( |
| 26 | + id STRING |
| 27 | + , geo_20km CHAR(4) |
| 28 | + , ts TIMESTAMP |
| 29 | + , x GEOGRAPHY |
| 30 | + , obj JSONB |
| 31 | + , PRIMARY KEY (geo_20km ASC, id ASC) |
| 32 | + ); |
| 33 | +
|
| 34 | +""" |
| 35 | + |
| 36 | +import sys |
| 37 | +import os |
| 38 | +import json |
| 39 | +import bz2 |
| 40 | +import re |
| 41 | +import Geohash |
| 42 | +import psycopg2 |
| 43 | +import psycopg2.errorcodes |
| 44 | +import psycopg2.extras |
| 45 | +import html |
| 46 | +import time |
| 47 | + |
| 48 | +# Example input data |
| 49 | +""" |
| 50 | + <node id="114" version="4" timestamp="2018-07-21T22:01:43Z" uid="207581" user="Hjart" changeset="60940511" lat="59.9506757" lon="10.784339"/> |
| 51 | + <node id="115" version="3" timestamp="2018-07-21T22:01:43Z" uid="207581" user="Hjart" changeset="60940511" lat="59.9510531" lon="10.7796921"/> |
| 52 | + ... |
| 53 | + <node id="108042" version="22" timestamp="2019-11-07T19:01:18Z" uid="2773866" user="kreuzschnabel" changeset="76773501" lat="51.5235613" lon="-0.1355134"> |
| 54 | + <tag k="name" v="Simmons"/> |
| 55 | + <tag k="amenity" v="pub"/> |
| 56 | + <tag k="toilets" v="yes"/> |
| 57 | + <tag k="old_name" v="The Jeremy Bentham"/> |
| 58 | + <tag k="addr:street" v="University Street"/> |
| 59 | + <tag k="addr:postcode" v="WC1E 6JL"/> |
| 60 | + <tag k="contact:phone" v="+44 20 73771843"/> |
| 61 | + <tag k="opening_hours" v="Mo-We 16:00-23:30; Th-Fr 16:00-01:00; Sa 16:00-23:30"/> |
| 62 | + <tag k="contact:website" v="http://www.simmonsbar.co.uk/euston-square/4593769006"/> |
| 63 | + <tag k="addr:housenumber" v="31"/> |
| 64 | + </node> |
| 65 | +
|
| 66 | +""" |
| 67 | + |
| 68 | +if len(sys.argv) != 3: |
| 69 | + print("Usage: %s osm_xml.bz2 max_points" % sys.argv[0]) |
| 70 | + sys.exit(1) |
| 71 | + |
| 72 | +in_file = sys.argv[1] |
| 73 | +max_points = int(sys.argv[2]) |
| 74 | + |
| 75 | +conn = psycopg2.connect( |
| 76 | + database=os.getenv("PGDATABASE", "defaultdb") |
| 77 | + , user=os.getenv("PGUSER", "root") |
| 78 | + , port=int(os.getenv("PGPORT", "26257")) |
| 79 | + , host=os.getenv("PGHOST", "localhost") |
| 80 | + , application_name="OSM JSON" |
| 81 | +) |
| 82 | +conn.autocommit = True |
| 83 | + |
| 84 | +# rows is a list of lists |
| 85 | +def do_inserts (conn, rows): |
| 86 | + t0 = time.time() |
| 87 | + with conn.cursor() as cur: |
| 88 | + """ |
| 89 | + psycopg2.extras.execute_batch(cur, "INSERT INTO osm_json (id, geo_20km, ts, x, obj)" |
| 90 | + + "VALUES (%s, %s, %s, ST_MakePoint(%s, %s)::GEOGRAPHY, %s::JSONB)", |
| 91 | + rows, page_size=100) |
| 92 | + """ |
| 93 | + print("INSERTED %d rows in %.2f seconds" % (len(rows), time.time() - t0)) |
| 94 | + |
| 95 | +n_read = 0 |
| 96 | +kv = {} |
| 97 | +node = {} |
| 98 | +batch_size = 1000 |
| 99 | + |
| 100 | +# See above data examples for how this is derived |
| 101 | +node_pat = re.compile(r'<node id="([^"]+)" version="(\d+)" timestamp="([^"]+)" uid="(\d+)" user="([^"]+)" changeset="(\d+)" lat="(-?\d+\.\d+)" lon="(-?\d+\.\d+)">') |
| 102 | +tag_pat = re.compile(r'^<tag +k="([^"]+)" +v="([^"]+)" */>$') |
| 103 | + |
| 104 | +rows = [] |
| 105 | +with bz2.open(in_file, mode="rt", encoding="utf8", newline='\n') as f: |
| 106 | + while n_read < max_points: |
| 107 | + line = f.readline().strip() |
| 108 | + if line.startswith("</node>"): |
| 109 | + if not bool(node) or not bool(kv): # Is either empty? |
| 110 | + continue |
| 111 | + if "name" in kv: # I think it's interesting only is it has a name |
| 112 | + node["kv"] = kv |
| 113 | + rows.append([node["id"], node["geo_20km"], node["timestamp"], node["lon"], node["lat"], json.dumps(node)]) |
| 114 | + if len(rows) == batch_size: |
| 115 | + do_inserts(conn, rows) |
| 116 | + rows = [] |
| 117 | + #print(json.dumps(node)) |
| 118 | + n_read += 1 |
| 119 | + elif line.startswith("<node "): |
| 120 | + if line.endswith("/>"): |
| 121 | + continue |
| 122 | + node.clear() |
| 123 | + kv.clear() |
| 124 | + m = node_pat.match(line) |
| 125 | + if m is not None: |
| 126 | + node["id"] = m.group(1) |
| 127 | + node["version"] = m.group(2) |
| 128 | + node["timestamp"] = m.group(3) |
| 129 | + node["uid"] = m.group(4) |
| 130 | + node["user"] = html.unescape(m.group(5)) |
| 131 | + node["changeset"] = m.group(6) |
| 132 | + lat = float(m.group(7)) |
| 133 | + lon = float(m.group(8)) |
| 134 | + node["lat"] = lat |
| 135 | + node["lon"] = lon |
| 136 | + geohash = Geohash.encode(lat, lon) |
| 137 | + # Add some geohash values. The "20km" suffix means it's accurate to +/- 20 kilometers |
| 138 | + node["geo_20m"] = geohash[0:8] |
| 139 | + node["geo_2400m"] = geohash[0:5] |
| 140 | + node["geo_20km"] = geohash[0:4] |
| 141 | + node["geo_80km"] = geohash[0:3] |
| 142 | + else: |
| 143 | + pass |
| 144 | + elif line.startswith("<tag "): |
| 145 | + m = tag_pat.match(line) |
| 146 | + if m is not None: |
| 147 | + kv[m.group(1)] = html.unescape(str(m.group(2))) |
| 148 | + else: |
| 149 | + pass |
| 150 | +if len(rows) > 0: |
| 151 | + do_inserts(conn, rows) |
| 152 | +conn.close() |
| 153 | + |
0 commit comments