Skip to content

Instantly share code, notes, and snippets.

@balazsdukai
Created June 10, 2021 08:38
Show Gist options
  • Save balazsdukai/e4b8d01d4ddb728cf4a109d4eb3174fa to your computer and use it in GitHub Desktop.
Save balazsdukai/e4b8d01d4ddb728cf4a109d4eb3174fa to your computer and use it in GitHub Desktop.
import requests
import pathlib
import gzip
import json
from cjio import cityjson
datadir = pathlib.Path(__file__).parent.absolute()
tile = "5908"
url = f"https://data.3dbag.nl/cityjson/v21031_7425c21b/3dbag_v21031_7425c21b_{tile}.json.gz"
filepath = datadir / pathlib.Path(url.split("/")[-1]).stem
if not filepath.exists():
print(f"Downloading tile {tile} in CityJSON...")
r = requests.get(url)
with open(filepath, "wb") as output_file:
output_file.write(gzip.decompress(r.content))
print(f"Downloaded tile {tile} to {filepath}")
print("Creating subsets for testing")
cm = cityjson.reader(filepath.open(mode="r"))
cm.extract_lod(2)
outp = datadir / "lod2.json"
with outp.open(mode="w") as fo:
json_str = json.dumps(cm.j, separators=(',', ':'))
fo.write(json_str)
print(f"Written LoD2 subset to {outp}")
# two buildings with different heights and a shared wall, at the corner of Spieringstraat-Zuiderstraat, Delft
two = cm.get_subset_ids(["4827033", "1692118"])
outp = datadir / "two.json"
with outp.open(mode="w") as fo:
json_str = json.dumps(two.j, separators=(',', ':'))
fo.write(json_str)
print(f"Written subset to {outp}")
ids = ["3510225",
"46780",
"9788221",
"5745340",
"8284079",
"5376687",
"6614843",
"7520945",
"6165741",
"9285843",
"2569272",
"106540",
"1692118",
"5790776",
"7331837",
"4340473",
"4827033",
"3655432",
"4362324",
"6483601",
"2580567",
"1500262",
"176794",
"4359161",
"7494710",
"3813989",
"5326225",
"199341",
"9245459"]
# building block at the corner of Oosteinde-Zuiderstraat, Delft
block = cm.get_subset_ids(ids)
outp = datadir / "block.json"
with outp.open(mode="w") as fo:
json_str = json.dumps(block.j, separators=(',', ':'))
fo.write(json_str)
print(f"Written subset to {outp}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment