Skip to content

Commit

Permalink
Added bilinear functionality for Albedo, DSM, and DEM
Browse files Browse the repository at this point in the history
  • Loading branch information
kcartier-wri committed Dec 18, 2024
1 parent fbc5a58 commit 7a4012d
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 7 deletions.
7 changes: 5 additions & 2 deletions city_metrix/layers/albedo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ee

from .layer import Layer, get_image_collection
from .layer import Layer, get_image_collection, set_bilinear_resampling

class Albedo(Layer):
"""
Expand Down Expand Up @@ -115,7 +115,10 @@ def calc_s2_albedo(image):
## S2 MOSAIC AND ALBEDO
dataset = get_masked_s2_collection(ee.Geometry.BBox(*bbox), self.start_date, self.end_date)
s2_albedo = dataset.map(calc_s2_albedo)
albedo_mean = s2_albedo.reduce(ee.Reducer.mean())
albedo_mean = (s2_albedo
.map(set_bilinear_resampling)
.reduce(ee.Reducer.mean())
)

albedo_mean_ic = ee.ImageCollection(albedo_mean)
data = get_image_collection(
Expand Down
3 changes: 2 additions & 1 deletion city_metrix/layers/alos_dsm.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ee

from .layer import Layer, get_image_collection
from .layer import Layer, get_image_collection, set_bilinear_resampling


class AlosDSM(Layer):
Expand All @@ -18,6 +18,7 @@ def get_data(self, bbox):

alos_dsm_ic = ee.ImageCollection(alos_dsm
.filterBounds(ee.Geometry.BBox(*bbox))
.map(set_bilinear_resampling)
.select('DSM')
.mean()
)
Expand Down
14 changes: 14 additions & 0 deletions city_metrix/layers/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,20 @@ def get_stats_funcs(stats_func):
return [stats_func]


def set_bilinear_resampling(data):
if isinstance(data, ee.ImageCollection):
resampled = data.map(_assign_bilinear_interpolation)
else:
resampled = data.resample('bilinear')

return resampled


def _assign_bilinear_interpolation(dataset):
data = dataset.resample('bilinear')
return data


def get_image_collection(
image_collection: ImageCollection,
bbox: Tuple[float],
Expand Down
3 changes: 2 additions & 1 deletion city_metrix/layers/nasa_dem.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ee

from .layer import Layer, get_image_collection
from .layer import Layer, get_image_collection, set_bilinear_resampling


class NasaDEM(Layer):
Expand All @@ -18,6 +18,7 @@ def get_data(self, bbox):

nasa_dem_elev = (ee.ImageCollection(nasa_dem)
.filterBounds(ee.Geometry.BBox(*bbox))
.map(set_bilinear_resampling)
.select('elevation')
.mean()
)
Expand Down
4 changes: 4 additions & 0 deletions tests/resources/bbox_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@
-38.39993,-12.93239
)

BBOX_NLD_AMSTERDAM_TEST = (
4.9012,52.3720,
4.9083,52.3752
)
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import shutil
from collections import namedtuple

from tests.resources.bbox_constants import BBOX_BRA_LAURO_DE_FREITAS_1
from tests.resources.bbox_constants import BBOX_BRA_LAURO_DE_FREITAS_1, BBOX_NLD_AMSTERDAM_TEST
from tests.tools.general_tools import create_target_folder, is_valid_path

# RUN_DUMPS is the master control for whether the writes and tests are executed
Expand All @@ -18,7 +18,8 @@

# Both the tests and QGIS file are implemented for the same bounding box in Brazil.
COUNTRY_CODE_FOR_BBOX = 'BRA'
BBOX = BBOX_BRA_LAURO_DE_FREITAS_1
# BBOX = BBOX_BRA_LAURO_DE_FREITAS_1
BBOX = BBOX_NLD_AMSTERDAM_TEST

# Specify None to write to a temporary default folder otherwise specify a valid custom target path.
CUSTOM_DUMP_DIRECTORY = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

from city_metrix.layers import *
from .conftest import RUN_DUMPS, prep_output_path, verify_file_is_populated, get_file_count_in_folder
from .conftest import RUN_DUMPS, prep_output_path, verify_file_is_populated

TARGET_RESOLUTION = 5

Expand Down

0 comments on commit 7a4012d

Please sign in to comment.