Skip to content

Instantly share code, notes, and snippets.

@andybak
Created April 10, 2020 11:26
Show Gist options
  • Save andybak/350b55fdacbabd690b789c7958fb7995 to your computer and use it in GitHub Desktop.
Save andybak/350b55fdacbabd690b789c7958fb7995 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__distance_lte=(pnt, D(km=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:
scene.add_geometry(creation.extrude_polygon(polygon, height))
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