-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconsolidate_buildings.py
46 lines (33 loc) · 1.64 KB
/
consolidate_buildings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import argparse
import logging
from logging import info
from pathlib import Path
import geopandas as gpd
import pandas as pd
from shapely.geometry import Polygon, Point
def to_polygon(geometry):
try:
return Polygon(geometry)
except ValueError:
return None
def main(polygons_path: Path, linestrings_path: Path, output: Path):
info("Unifying %s, %s", polygons_path.resolve(), linestrings_path.resolve())
polygons = gpd.read_file(str(polygons_path)).explode()
linestrings = gpd.read_file(str(linestrings_path))
linestrings["geometry"] = linestrings["geometry"].apply(to_polygon)
linestrings = linestrings[pd.notnull(linestrings["geometry"])].explode()
concat = pd.concat([polygons, linestrings], sort=True)
info("Saving valid geometries to %s", output.resolve())
concat[(~concat.is_empty) & (concat.geom_type == "Polygon")].to_file(str(output), driver='GeoJSON')
def setup(args=None):
# logging
logging.basicConfig(format="%(asctime)s/%(filename)s/%(funcName)s | %(levelname)s - %(message)s", datefmt='%Y-%m-%d %H:%M:%S')
logging.getLogger().setLevel("INFO")
# read arguments
parser = argparse.ArgumentParser(description='Consolidate building footprint geometries.')
parser.add_argument('--polygons', required=True, type=Path, help='path to polygons', dest="polygons_path")
parser.add_argument('--linestrings', required=True, type=Path, help='path to linestrings', dest="linestrings_path")
parser.add_argument('--output', required=True, type=Path, help='path to output')
return parser.parse_args(args)
if __name__ == "__main__":
main(**vars(setup()))