Skip to content

Instantly share code, notes, and snippets.

@hugoledoux
Created September 27, 2022 14:05
Show Gist options
  • Save hugoledoux/6953e1a8024563623426588ddea40967 to your computer and use it in GitHub Desktop.
Save hugoledoux/6953e1a8024563623426588ddea40967 to your computer and use it in GitHub Desktop.
cityjson & polyscope
import polyscope as ps
import json
import sys
import numpy as np
import math
def recusionvisit(a, vs):
for each in a:
if isinstance(each, list):
recusionvisit(each, vs)
else:
# print(each)
vs.append(each)
def main():
# Initialize polyscope, creating graphics contexts and constructing a window.
# Should be called exactly once.
ps.init()
ps.set_program_name("my cityjson")
ps.set_up_dir("z_up")
ps.set_ground_plane_mode("shadow_only")
ps.set_ground_plane_height_factor(0.01, is_relative=True)
ps.set_autocenter_structures(True)
ps.set_autoscale_structures(True)
j = json.loads(open('out.json').read())
verts = np.asarray(j["vertices"], dtype=float)
#-- semantic for surfaces
sem = [1, 2, 3, 4, 5]
fs = []
fsem = []
for co in j["CityObjects"]:
sb = len(fs)
if 'geometry' in j['CityObjects'][co]:
for g in j['CityObjects'][co]['geometry']:
recusionvisit(g["boundaries"], fs)
s = math.floor((len(fs) - sb) / 3)
for i in range(s):
if j['CityObjects'][co]['type'] == 'TINRelief':
fsem.append(1)
elif j['CityObjects'][co]['type'] == 'Building':
fsem.append(2)
else:
fsem.append(3)
fs2 = np.asarray(fs).reshape((-1, 3))
fsem2 = np.asarray(fsem)
# sys.exit()
### Register a mesh
# `verts` is a Nx3 numpy array of vertex positions
# `faces` is a Fx3 array of indices, or a nested list
ps_mesh = ps.register_surface_mesh("my mesh", verts, fs2)
ps_mesh.set_transparency(0.9)
ps.get_surface_mesh("my mesh").add_scalar_quantity("my_scalar",
fsem2, defined_on='faces', cmap='turbo', enabled=True)
# Pass control flow to polyscope, displaying the interactive window.
# Function will return when user closes the window.
ps.show()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment