I would like to expand a library which converts a DEM (Digital elevation model) to values stored in a dictionary. This dictionary can be used to create a 3D terrain in Three.js. I made a Django template to illustrate the use of this library which I will share the link of at the end if interested.
I would like to expand the library to convert vector data, using geopandas, also to dictionaries to be able to draw them in Three.js.
In an attempt to make it easier for the user I thought to create an import method to detect which spatial conversions are needed. So far I only added the point class but want to extend it to lines and polygons as well.
The idea is that the user will call import_spatial_layer function and it will return an object either of class raster, point, line or polygon and each class will have different methods to convert the spatial object to a dictionary which the user can pass to three.js.
Other than checking whether the value passed to the import layer function is of type raster, point, line or polygon, users will also be able to pass in a dem/heightmap which will add a z/height value to a 2d vector layer as well as a bounding box for the heightmap.
Is there a more elegant way of doing all these checks?
import class:
def import_spatial_layer(geo_dataframe, layer_name, **kwargs):
height_map = []
if kwargs.get("height_map"):
height_map = kwargs.get("height_map")
try:
geo_dataframe.geometry.all()
except:
try:
imghdr.what(geo_dataframe)
except:
print("could not find a valid geopandas or tif/tiff file")
return
else:
if imghdr.what(geo_dataframe) == 'tiff' or imghdr.what(geo_dataframe) == 'tif':
if 'tiff_bbox' in kwargs:
return RasterToThreeD(geo_dataframe, layer_name, tiff_bbox=kwargs.get('tiff_bbox'))
else:
return RasterToThreeD(geo_dataframe, layer_name)
else: print("raster file not in tiff/tif format")
else:
if geo_dataframe.geometry.all().geom_type == 'Point' or geo_dataframe.geometry.all().geom_type == 'MultiPoint':
if 'tiff_bbox' in kwargs:
return PointToThreeD(geo_dataframe, layer_name, height_map=height_map, tiff_bbox=kwargs.get('tiff_bbox'))
else:
return PointToThreeD(geo_dataframe, layer_name, height_map=height_map)
else: print('could not find a valid geopandas file')
calling the method
raster:
import_spatial_layer(tiff_raster, raster_layer_name).object_dict()
point:
import_spatial_layer(gpd_point_layer, point_layer_name,
height_map=raster_layer, tiff_bbox=raster_bbox).object_dict()