Skip to content

DB Extract

main

main()

This program queries a postgres database as maintained by Underpass.

Source code in osm_merge/dbextract.py
 44
 45
 46
 47
 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
 75
 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
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", default='localhost/underpass', help="Database URI")

    args = parser.parse_args()

    # 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,
        )
    try:
        uri = "dbname=underpass"
        pg = psycopg2.connect(uri)
        curs = pg.cursor()
    except Exception as e:
        log.error(f"Couldn't connect to database: {e}")


    # Make a temporary view to reduce the data size
    if args.boundary:
        # optionally clip by a boundary
        file = open(args.boundary, "r")
        data = geojson.load(file)
        aoi = shape(data["geometry"])
        file.close()
        sql = f"CREATE 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)
        curs.execute(sql)
    else:
        # By default, get all the highways
        sql = f"CREATE TEMP VIEW highway_view AS SELECT * FROM ways_line WHERE tags->>'highway' IS NOT NULL"
        # print(sql)
        curs.execute(sql)

    sql = f"SELECT osm_id,version,timestamp,refs,tags,ST_AsTEXT(geom) FROM highway_view WHERE tags->>'highway' IS NOT NULL;"
    # print(sql)
    curs.execute(sql)
    result = curs.fetchall()
    features = list()
    for row in result:
        osm_id = row[0]
        version = row[1]
        timestamp = row[2]
        refs = row[3]
        tags = row[4]
        geom = shapely.from_wkt(row[5])
        data = {"osm_id": osm_id, "version": version, "refs": refs, "geom": geom}
        data.update(tags)
        # print(data)
        features.append(Feature(geometry=geom, properties=data))

    path = Path(args.outfile)

    if path.suffix == '.geojson':
        file = open(args.outfile, "w")
        geojson.dump(FeatureCollection(features), file)
        file.close()
    elif path.suffix == '.osm':
        osm = OsmFile()
        osm.writeOSM(features, args.outfile)

    log.info(f"Wrote {args.outfile}")

options: show_source: false heading_level: 3