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}")