Skip to content

Instantly share code, notes, and snippets.

@andybak
Created October 16, 2020 15:59
Show Gist options
  • Save andybak/63540e65c30a453a7e2e9b0345aa42ce to your computer and use it in GitHub Desktop.
Save andybak/63540e65c30a453a7e2e9b0345aa42ce to your computer and use it in GitHub Desktop.
def gltf(request, lat, lng, distance):
scale = 100000
pnt = GEOSGeometry(f"POINT({lng} {lat})", srid=4326)
buildings = Building.objects.filter(outline__dwithin=(pnt, km2deg(distance)))
scene = Scene()
centroid_total = (0, 0)
count = 0.0
for building in buildings:
height = (building.height / scale / 10) + 0.0001
c = building.outline.centroid
centroid_total = (centroid_total[0] + c[0], centroid_total[1] + c[1])
count += 1
for o0 in building.outline:
for o1 in o0:
polygon = shapely.geometry.Polygon(o1)
try:
mesh = creation.extrude_polygon(polygon, height)
scene.add_geometry(mesh)
colors = trimesh.visual.color.ColorVisuals(mesh)
col = colors.vertex_colors
ones = np.ones_like(col)
zeros = np.zeros_like(col)
rgbs = np.concatenate((ones[:, :3], zeros[:, :1]), 1)
alphas = np.concatenate((zeros[:, :3], ones[:, :1]), 1)
new_color = 102 * rgbs + 150 * alphas
mesh.visual.vertex_colors = new_color
except IndexError:
pass
centroid = (-centroid_total[0] / count, -centroid_total[1] / count, 0)
t = kwargs_to_matrix(matrix=identity_matrix(), translation=centroid)
scene.apply_transform(t)
scene = scene.scaled(scale)
gltf_data = exchange.gltf.export_glb(scene)
file_name = f"{lat}-{lng}-{distance}.glb"
response = HttpResponse(
content=gltf_data, content_type="application/force-download"
)
response["Content-Disposition"] = f"attachment; filename={smart_str(file_name)}"
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment