Skip to content

Commit

Permalink
rebase on main and return multiple outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
weiqi-tori committed Dec 18, 2024
1 parent 8d1f3ea commit 5f8a297
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
4 changes: 2 additions & 2 deletions city_metrix/layers/albedo.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def _mcc(im):

# s2_with_clouds=s2_with_clouds.limit(image_limit,'nb_cloudy_pixels')
s2_with_clouds = (ee.ImageCollection(s2_with_clouds)
.map(mask_clouds_and_rescale)
.map(self.mask_clouds_and_rescale)
) # .limit(image_limit,'CLOUDY_PIXEL_PERCENTAGE')

s2_with_clouds_ic = ee.ImageCollection(s2_with_clouds)
Expand All @@ -102,7 +102,7 @@ def calc_s2_albedo(image):
'SWIR2': image.select('B12')
}

albedo = image.expression(S2_ALBEDO_EQN, config).double().rename('albedo')
albedo = image.expression(self.S2_ALBEDO_EQN, config).double().rename('albedo')

return albedo

Expand Down
13 changes: 10 additions & 3 deletions city_metrix/layers/vegetation_water_map.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import xarray as xr
import ee

from .layer import Layer, get_image_collection
Expand Down Expand Up @@ -46,13 +45,16 @@ def AnnualImgGreen(ic, year):
bluestThres = bluest.updateMask(bluest.select('NDWI').gte(NDWIthreshold))
greenestnowater = greenest.updateMask(bluestThres.select('NDWI').unmask().Not())
return greenestnowater

def AnnualImgWater(ic, year):
bluest = ic.select('NDWI').reduce(ee.Reducer.median()).rename('NDWI').addBands(ee.Image(year).rename('time_start')).float()
return bluest

def AnnualImgWatermask(ic, year):
bluest = ic.qualityMosaic('NDWI').select('NDWI').addBands(ee.Image(year).rename('time_start')).float()
bluestThres = bluest.updateMask(bluest.select('NDWI').gte(NDWIthreshold))
return bluestThres

def AnnualImgGreenmask(ic, year):
greenest = ic.qualityMosaic('NDVI').select('NDVI').addBands(ee.Image(year).rename('time_start')).float()
greenestThres = greenest.updateMask(greenest.select('NDVI').gte(NDVIthreshold))
Expand Down Expand Up @@ -222,6 +224,11 @@ def get_map_vegwaterchange(IC, greenwater_layer):
s2cloudmasked = Albedo().get_masked_s2_collection(ee.Geometry.BBox(*bbox), self.start_date, self.end_date)
vegwatermap = get_map_vegwaterchange(s2cloudmasked, self.greenwater_layer)

data = get_image_collection(ee.ImageCollection(vegwatermap), bbox, self.spatial_resolution, "vegetation water map")
data = get_image_collection(
ee.ImageCollection(vegwatermap),
bbox,
self.spatial_resolution,
"vegetation water map"
).greenwater_layer

return data.greenwater_layer
return data
19 changes: 16 additions & 3 deletions city_metrix/metrics/vegetation_water_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,25 @@

from city_metrix.layers import VegetationWaterMap

# TODO: layer generation and zonal stats use different spatial resolutions

def vegetation_water_change(zones: GeoDataFrame) -> GeoSeries:
def vegetation_water_change_gain_area(zones: GeoDataFrame, spatial_resolution=10) -> GeoSeries:
gain_counts = VegetationWaterMap(greenwater_layer='gaingreenwaterSlope').groupby(zones).count()
gain_area = gain_counts * spatial_resolution ** 2

return gain_area


def vegetation_water_change_loss_area(zones: GeoDataFrame, spatial_resolution=10) -> GeoSeries:
loss_counts = VegetationWaterMap(greenwater_layer='lossgreenwaterSlope').groupby(zones).count()
loss_area = loss_counts * spatial_resolution ** 2

return loss_area


def vegetation_water_change_gain_loss_ratio(zones: GeoDataFrame) -> GeoSeries:
start_counts = VegetationWaterMap(greenwater_layer='startgreenwaterIndex').groupby(zones).count()
loss_counts = VegetationWaterMap(greenwater_layer='lossgreenwaterSlope').groupby(zones).count()
gain_counts = VegetationWaterMap(greenwater_layer='gaingreenwaterSlope').groupby(zones).count()

# TODO: layer generation and zonal stats use different spatial resolutions

return (gain_counts - loss_counts) / start_counts
18 changes: 16 additions & 2 deletions tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,22 @@ def test_urban_open_space():
assert expected_zone_size == actual_indicator_size


def test_vegetation_water_change():
indicator = vegetation_water_change(ZONES)
def test_vegetation_water_change_gain_area():
indicator = vegetation_water_change_gain_area(ZONES)
expected_zone_size = ZONES.geometry.size
actual_indicator_size = indicator.size
assert expected_zone_size == actual_indicator_size


def test_vegetation_water_change_loss_area():
indicator = vegetation_water_change_loss_area(ZONES)
expected_zone_size = ZONES.geometry.size
actual_indicator_size = indicator.size
assert expected_zone_size == actual_indicator_size


def test_vegetation_water_change_gain_loss_ratio():
indicator = vegetation_water_change_gain_loss_ratio(ZONES)
expected_zone_size = ZONES.geometry.size
actual_indicator_size = indicator.size
assert expected_zone_size == actual_indicator_size

0 comments on commit 5f8a297

Please sign in to comment.