Skip to content

DB Extract

DBExtract

DBExtract(uri)

Bases: object

Parameters:

Name Type Description Default
database str

The database to use

required

Returns:

Name Type Description
DBExtract DBExtract

An instance of this class

Source code in osm_merge/dbextract.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
def __init__(self,
            uri: str,
            ) -> DBExtract:
    """
    Initialize the database connection

    Args:
        database (str): The database to use

    Returns:
        DBExtract: An instance of this class
    """
    self.curs = None
    self.db = None
    if uri:
        host = uri.split('/')[0]
        db = uri.split('/')[1]
        try:
            pargs = f"dbname={db}"
            if host != "localhost":
                pargs+= f" host={host}"
            self.db = psycopg2.connect(pargs)
            self.curs = self.db.cursor()
            self.db.autocommit = True
            log.info(f"Connected to database {db} on {host}")
        except Exception as e:
            log.error(f"Couldn't connect to database: {e}")

create_highway_view

create_highway_view(boundary=None)

Filter the data by a multi-polygon to reduce the size.

Parameters:

Name Type Description Default
boundary str

The boundary data file

None

Returns:

Type Description
bool

Whether the view was created or not

Source code in osm_merge/dbextract.py
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
def create_highway_view(self,
                boundary: str = None,
                ) -> bool:
    """
    Filter the data by a multi-polygon to reduce the size.

    Args:
        boundary (str): The boundary data file

    Returns:
        (bool): Whether the view was created or not
    """
    log.info(f"Creating temporary views of highways, this make take awhile...")
    views = self.list_views("highway")
    if len(views) > 0:
        log.debug(f"highway_view already exists")
        return True

    if boundary:
        file = open(boundary, "r")
        data = geojson.load(file)
        file.close()
        index = 0
        if "geometry" not in data:  # It's a FeatureCollection or MultiPolygon
            polys = list()
            for feature  in data["features"]:
                feat = feature["geometry"]
                foo = wkt.dumps(feat)
                sql = f"CREATE OR REPLACE TEMP VIEW highway_view{index} AS SELECT * FROM ways_line WHERE tags->>'highway' IS NOT NULL AND ST_CONTAINS(ST_GeomFromEWKT('SRID=4326;{foo}'), geom)"
                self.curs.execute(sql)
                log.debug(f"Created temporary view highway_view{index}")
                index += 1
        else:
            aoi = shape(data["geometry"])
            sql = f"CREATE OR REPLACE TEMP VIEW highway_view AS SELECT * FROM ways_line WHERE tags->>'highway' IS NOT NULL AND ST_CONTAINS(ST_GeomFromEWKT('SRID=4326;{aoi.wkt}'), geom)"
            # print(sql)
            self.curs.execute(sql)
    else:
        # By default, get all the highways
        sql = f"CREATE OR REPLACE TEMP VIEW highway_view AS SELECT * FROM ways_line WHERE tags->>'highway' IS NOT NULL"
        # print(sql)
        self.curs.execute(sql)

create_address_view

create_address_view(boundary=None)

Filter the data by a multi-polygon to reduce the size.

Parameters:

Name Type Description Default
boundary str

The boundary data file

None

Returns:

Type Description
bool

Whether the view was created or not

Source code in osm_merge/dbextract.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
def create_address_view(self,
                boundary: str = None,
                ) -> bool:
    """
    Filter the data by a multi-polygon to reduce the size.

    Args:
        boundary (str): The boundary data file

    Returns:
        (bool): Whether the view was created or not
    """
    log.info(f"Creating temporary views of addresses, this make take awhile...")
    views = self.list_views("address")
    if len(views) > 0:
        log.debug(f"address_view already exists")
        return True

    if boundary:
        file = open(boundary, "r")
        data = geojson.load(file)
        file.close()
        index = 0
        if "geometry" not in data:  # It's a FeatureCollection or MultiPolygon
            polys = list()
            for feature  in data["features"]:
                feat = feature["geometry"]
                foo = wkt.dumps(feat)
                sql = f"CREATE OR REPLACE TEMP VIEW tmp_view{index} AS SELECT * FROM nodes WHERE tags->>'addr:housenumber' IS NOT NULL AND ST_CONTAINS(ST_GeomFromEWKT('SRID=4326;{foo}'), geom)"
                self.curs.execute(sql)
                log.debug(f"Created temporary address_view{index}")
                index += 1
        else:
            aoi = shape(data["geometry"])
            sql = f"CREATE OR REPLACE TEMP VIEW tmp_view AS SELECT * FROM nodes WHERE tags->>'addr:housenumber' IS NOT NULL AND ST_CONTAINS(ST_GeomFromEWKT('SRID=4326;{aoi.wkt}'), geom)"
            # print(sql)
            # Addresses may be on either a node or the building polygon
            self.curs.execute(sql)
            sql = f"CREATE OR REPLACE TEMP VIEW tmp_view2 AS SELECT * FROM ways_poly WHERE tags->>'addr:housenumber' IS NOT NULL AND ST_CONTAINS(ST_GeomFromEWKT('SRID=4326;{aoi.wkt}'), geom)"
            # print(sql)
            self.curs.execute(sql)
            sql = "CREATE OR REPLACE TEMP VIEW address_view AS SELECT tags,geom FROM tmp_view UNION ALL SELECT tags,ST_CENTROID(geom) FROM tmp_view2;"
            # print(sql)
            self.curs.execute(sql)
    else:
        sql = f"CREATE OR REPLACE TEMP VIEW tmp_view AS SELECT * FROM nodes WHERE tags->>'addr:housenumber' IS NOT NULL;"
        print(sql)
        # Addresses may be on either a node or the building polygon
        self.curs.execute(sql, ("auto-committed",))
        sql = f"CREATE OR REPLACE TEMP VIEW tmp_view2 AS SELECT * FROM ways_poly WHERE tags->>'addr:housenumber' IS NOT NULL;"
        print(sql)
        self.curs.execute(sql, ("auto-committed",))
        sql = "CREATE OR REPLACE TEMP VIEW address_view AS SELECT tags,geom FROM tmp_view UNION ALL SELECT tags,ST_CENTROID(geom) FROM tmp_view2;"
        print(sql)
        # print(sql)
        self.curs.execute(sql, ("auto-committed",))

filter_rows

filter_rows(rows, strip_refs=False)

Filter the rows to create a real geometry.

Parameters:

Name Type Description Default
rows list

The rows to process

required

Returns:

Type Description
list

The filtered data

Source code in osm_merge/dbextract.py
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
def filter_rows(self,
                rows: list,
                strip_refs: bool = False,
                ) -> list:
    """
    Filter the rows to create a real geometry.

    Args:
        rows (list): The rows to process

    Returns:
        (list): The filtered data
    """
    log.info(f"Filtering the query output to create real geometries")
    features = list()
    pbar = tqdm.tqdm(rows)

    for row in pbar:
        osm_id = row[0]
        version = row[1]
        timestamp = row[2]
        if strip_refs:
            tags = row[3]
            geom = shapely.from_wkt(row[4])
            data = {"osm_id": osm_id, "version": version}
        else:
            refs = row[3]
            tags = row[4]
            geom = shapely.from_wkt(row[5])
            data = {"osm_id": osm_id, "version": version, "refs": refs}
        data.update(tags)
        # print(data)
        features.append(Feature(geometry=geom, properties=data))

    return features

execute_query

execute_query(sql=None, view='highway_view')

Execute an SQL query.

Parameters:

Name Type Description Default
sql str

The SQL to execute

None
view str

Optional database view

'highway_view'

Returns:

Type Description
list

The result of the SQL query

Source code in osm_merge/dbextract.py
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
def execute_query(self,
                sql: str = None,
                view: str = "highway_view",
                ) -> list:
    """
    Execute an SQL query.

    Args:
        sql (str): The SQL to execute
        view (str): Optional database view

    Returns:
        (list): The result of the SQL query
    """
    if not sql:
        log.error(f"Need to specify an SQL query!")

    # data = list()
    # temp_views = self.list_views(view)
    # if len(temp_views) == 0:
    #     log.error(f"There are no views!")
    #     return data

    # for dbview in temp_views:
    if sql.find("_view") > 0:
        newsql = sql
    else:
        newsql = sql + view + ';'
    # log.debug(newsql)
    self.curs.execute(newsql)
    result = self.curs.fetchall()

    return result

get_vertices

get_vertices()

Query the database to get only rhe major highways we want to route to..

Source code in osm_merge/dbextract.py
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
def get_vertices(self,
                ) -> dict:
    """
    Query the database to get only rhe major highways we want to route to..

    """
    primaries = list
    sql = f"SELECT row_to_json(highway_view) FROM highway_view WHERE tags->>'highway'='primary'"
    rows = self.execute_query(sql)
    if len(rows) == 0:
        log.error(f"No data returned from the query!")

    # for name in rows["name"]:
    #     sql = f"SELECT source, target, source_osm, target_osm FROM ways WHERE name LIKE '{name}%'"
    #     self.pgrcurs.execute(sql)
    #     result = self.pgrcurs.fetchall()
    #     nodes = list()
    #     for entry in result[0]:
    #         vertex = {"source": entry[0],
    #                 "target": entry[1],
    #                 "source_osm": entry[2],
    #                 "target_osm": entry[3],
    #                 }
    #         nodes.append(vertex)

    return nodes

list_views

list_views(view)

List all the temp views that match a pattern.

Parameters:

Name Type Description Default
view str

The views to look for

required
Source code in osm_merge/dbextract.py
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
def list_views(self,
               view: str,
               ) -> list:
    """
    List all the temp views that match a pattern.

    Args:
        view (str): The views to look for
    """
    temp_views = list()
    sql = "SELECT table_schema AS schema, table_name AS view FROM information_schema.views WHERE table_schema NOT IN ('information_schema', 'pg_catalog') ORDER BY schema ASC, view ASC;"
    self.curs.execute(sql)
    views =  self.curs.fetchall()
    pat = re.compile(f"{view}.*")
    temp_views = list()
    for entry in views:
        match = re.search(pat, entry[1])
        if match:
            temp_views.append(match.string)

    # log.debug(f"There are {len(temp_views)} temporary views in the database")
    return temp_views

main

main()

This program queries a postgres database as maintained by Underpass.

Source code in osm_merge/dbextract.py
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
def main():
    """
    This program queries a postgres database as maintained by Underpass.
    """
    parser = argparse.ArgumentParser(description="Query a DB and output to OSM XML format")
    parser.add_argument("-v", "--verbose", nargs="?", const="0", help="verbose output")
    parser.add_argument("-b","--boundary", help='Optional boundary to clip the data')
    parser.add_argument("-o","--outfile", default='out.geojson', help='The output file')
    parser.add_argument("-u", "--uri", help="Database URI")
    parser.add_argument("-s", "--sql", help="Custom SQL Query")

    args = parser.parse_args()

    # Need at least one operation
    if len(argv) == 1:
        parser.print_help()
        quit()


    # if verbose, dump to the terminal
    if args.verbose is not None:
        logging.basicConfig(
            level=logging.DEBUG,
            format=("%(threadName)10s - %(name)s - %(levelname)s - %(message)s"),
            datefmt="%y-%m-%d %H:%M:%S",
            stream=sys.stdout,
        )


    db = DBExtract(args.uri)
    # Make a temporary view to reduce the data size
    if args.boundary:
        db.create_view(args.boundary)
    else:
        db.create_view()

    # Query the database for what we want
    if not args.sql:
        sql = f"SELECT osm_id,version,timestamp,refs,tags,ST_AsTEXT(geom) FROM "
    else:
        sql = args.sql
    rows = db.execute_query(sql)

    if len(rows) == 0:
        log.error(f"No data returned from the query!")
        quit()
    features = db.filter_rows(rows)

    path = Path(args.outfile)
    if path.suffix == ".geojson":
        log.debug(f"Writing data to GeoJson file, this make take awhile...")
        file = open(args.outfile, "w")
        geojson.dump(FeatureCollection(features), file, indent=2, default=str)
        file.close()
        log.info(f"Wrote {args.outfile}")
    elif path.suffix == ".osm":
        osm = OsmFile()
        osm.writeOSM(features, args.outfile)
        log.info(f"Wrote {args.outfile}")

options: show_source: false heading_level: 3