def __init__(self, **kwargs): super().__init__(**kwargs) layers = OrderedDict() for raster_layer in RasterAggregatedLayer.objects.order_by("id"): if raster_layer.legend is not None: # Only add layers with defined legends # --> raster tiles cannot be created without a color scheme, a legend is necessary for tile generation! DataModel = raster_layer.get_data_model() qs = DataModel.objects.filter(layer=raster_layer) layers[str(raster_layer.id)] = { "pixel_size": raster_layer.pixel_size_meters, # currently hard-coded in measurements.management.commands.load_safecast_csv "point_position": "upperleft", "model_queryset": qs, "model_point_fieldname": "location", "model_value_fieldname": raster_layer.value_fieldname, "round_pixels": False, "legend_instance": raster_layer.legend, # object with '.get_color_str()' method that returns an rgb() or hsl() color string. } else: logger.warn("RasterAggregatedLayer: {} has no legend defined!".format(str(raster_layer))) self.tilemgr = DjangoRasterTileLayerManager(layers)
class RasterLayersTileView(View): def __init__(self, **kwargs): super().__init__(**kwargs) layers = OrderedDict() for raster_layer in RasterAggregatedLayer.objects.order_by("id"): if raster_layer.legend is not None: # Only add layers with defined legends # --> raster tiles cannot be created without a color scheme, a legend is necessary for tile generation! DataModel = raster_layer.get_data_model() qs = DataModel.objects.filter(layer=raster_layer) layers[str(raster_layer.id)] = { "pixel_size": raster_layer.pixel_size_meters, # currently hard-coded in measurements.management.commands.load_safecast_csv "point_position": "upperleft", "model_queryset": qs, "model_point_fieldname": "location", "model_value_fieldname": raster_layer.value_fieldname, "round_pixels": False, "legend_instance": raster_layer.legend, # object with '.get_color_str()' method that returns an rgb() or hsl() color string. } else: logger.warn("RasterAggregatedLayer: {} has no legend defined!".format(str(raster_layer))) self.tilemgr = DjangoRasterTileLayerManager(layers) def get(self, request): layername, zoom, x, y, image_format = self.tilemgr.parse_url(request.path) logger.info("layername({}) zoom({}) x({}) y({}) image_format({})".format(layername, zoom, x, y, image_format)) try: mimetype, tile_pil_img_object = self.tilemgr.get_tile(layername, zoom, x, y) except LayerNotConfigured: return HttpResponseBadRequest("Requested RasterLayer({}) Does Not Exist!".format(layername)) image_encoding = image_format.replace(".", "") # pillow tile_pil_img_object.tobytes() doesn't seem to work, workaround to serve raw bytes via BytesIO() image_fileio = BytesIO() tile_pil_img_object.save(image_fileio, image_encoding) image_fileio.seek(0) return HttpResponse(image_fileio, content_type=mimetype)