From a86cf02a8023f9d2e579c40bf44fe8f3ef5bac4c Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Wed, 29 Jan 2020 08:31:35 -0500 Subject: [PATCH] Handle volume measurements in legacy web mercator --- plugins/measure/api.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/plugins/measure/api.py b/plugins/measure/api.py index bd5b95e1..b45f5e4e 100644 --- a/plugins/measure/api.py +++ b/plugins/measure/api.py @@ -4,6 +4,7 @@ import math from rest_framework import serializers from rest_framework import status from rest_framework.response import Response +import rasterio from app.api.workers import GetTaskResult, TaskResultOutputError, CheckTask from app.models import Task @@ -61,10 +62,16 @@ class TaskVolumeResult(GetTaskResult): cols = output.split(':') if len(cols) == 7: + # Legacy: we had rasters in EPSG:3857 for a while + # This could be removed at some point in the future # Correct scale measurement for web mercator # https://gis.stackexchange.com/questions/93332/calculating-distance-scale-factor-by-latitude-for-mercator#93335 - latitude = task.dsm_extent.centroid[1] - scale_factor = math.cos(math.radians(latitude)) ** 2 + scale_factor = 1.0 + dsm = os.path.abspath(task.get_asset_download_path("dsm.tif")) + with rasterio.open(dsm) as dst: + if str(dst.crs) == 'EPSG:3857': + latitude = task.dsm_extent.centroid[1] + scale_factor = math.cos(math.radians(latitude)) ** 2 volume = abs(float(cols[6]) * scale_factor) return str(volume)