Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/wri/cities-cif into feature…
Browse files Browse the repository at this point in the history
…/era5
  • Loading branch information
weiqi-tori committed Aug 14, 2024
2 parents 5ce1b04 + 5378750 commit 87ab037
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 102 deletions.
7 changes: 0 additions & 7 deletions city_metrix/layers/open_street_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,3 @@ def get_data(self, bbox):
osm_feature = osm_feature.reset_index()[keep_col]

return osm_feature

def write(self, output_path):
self.data['bbox'] = str(self.data.total_bounds)
self.data['osm_class'] = str(self.osm_class.value)

# Write to a GeoJSON file
self.data.to_file(output_path, driver='GeoJSON')
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies:
- xarray-spatial=0.3.7
- xee=0.0.3
- utm=0.7.0
- osmnx=1.8.1
- osmnx=1.9.0
- dask[complete]=2023.11.0
- matplotlib=3.8.2
- jupyterlab=4.0.10
Expand Down
170 changes: 76 additions & 94 deletions tests/test_layers.py
Original file line number Diff line number Diff line change
@@ -1,132 +1,113 @@
import ee
import numpy as np
import pytest

from city_metrix.layers import (
Albedo,
AlosDSM,
AverageNetBuildingHeight,
Era5HottestDay,
EsaWorldCover,
EsaWorldCoverClass,
HighLandSurfaceTemperature,
LandsatCollection2,
LandSurfaceTemperature,
NasaDEM,
NaturalAreas,
OpenBuildings,
OpenStreetMap,
OpenStreetMapClass,
OvertureBuildings,
Sentinel2Level2,
SmartSurfaceLULC,
TreeCanopyHeight,
TreeCover,
UrbanLandUse,
Era5HottestDay,
WorldPop
)
from city_metrix.layers.layer import get_image_collection
from tests.fixtures.bbox_constants import BBOX_BRAZIL_LAURO_DE_FREITAS_1

from .conftest import (
LARGE_ZONES,
ZONES,
MockGroupByLayer,
MockLargeGroupByLayer,
MockLargeLayer,
MockLayer,
MockMaskLayer,
)
from .fixtures.bbox_constants import *


def test_count():
counts = MockLayer().groupby(ZONES).count()
assert counts.size == 100
assert all([count == 100 for count in counts])


def test_mean():
means = MockLayer().groupby(ZONES).mean()
assert means.size == 100
assert all([mean == i for i, mean in enumerate(means)])


def test_fishnetted_count():
counts = MockLargeLayer().groupby(LARGE_ZONES).count()
assert counts.size == 100
assert all([count == 100 for count in counts])
EE_IMAGE_DIMENSION_TOLERANCE = 1 # Tolerance compensates for variable results from GEE service

def test_albedo():
assert Albedo().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).mean()

def test_fishnetted_mean():
means = MockLargeLayer().groupby(LARGE_ZONES).mean()
assert means.size == 100
assert all([mean == i for i, mean in enumerate(means)])

def test_alos_dsm():
mean = AlosDSM().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).mean()
assert mean

def test_masks():
counts = MockLayer().mask(MockMaskLayer()).groupby(ZONES).count()
assert counts.size == 100
for i, count in enumerate(counts):
if i % 2 == 0:
assert np.isnan(count)
else:
assert count == 100

def test_average_net_building_height():
assert AverageNetBuildingHeight().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).mean()

def test_group_by_layer():
counts = MockLayer().groupby(ZONES, layer=MockGroupByLayer()).count()
assert all([count == {1: 50.0, 2: 50.0} for count in counts])

def test_era_5_hottest_day():
mean = Era5HottestDay().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).mean()
assert mean


def test_group_by_large_layer():
counts = (
MockLargeLayer().groupby(LARGE_ZONES, layer=MockLargeGroupByLayer()).count()
def test_esa_world_cover():
count = (
EsaWorldCover(land_cover_class=EsaWorldCoverClass.BUILT_UP)
.get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1)
.count()
)
assert all([count == {1: 50.0, 2: 50.0} for count in counts])
assert count


def test_read_image_collection():
ic = ee.ImageCollection("ESA/WorldCover/v100")
data = get_image_collection(ic, BBOX_BRAZIL_LAURO_DE_FREITAS_1, 10, "test")

expected_crs = 32724
expected_crs = 32724
expected_x_dimension = 187
expected_y_dimension = 199

assert data.rio.crs == expected_crs
assert data.dims == {"x": expected_x_dimension, "y": expected_y_dimension}
assert (
pytest.approx(expected_x_dimension, rel=EE_IMAGE_DIMENSION_TOLERANCE) == "x",
pytest.approx(expected_y_dimension, rel=EE_IMAGE_DIMENSION_TOLERANCE) == "y"
)


def test_read_image_collection_scale():
ic = ee.ImageCollection("ESA/WorldCover/v100")
data = get_image_collection(ic, BBOX_BRAZIL_LAURO_DE_FREITAS_1, 100, "test")
assert data.dims == {"x": 19, "y": 20}
expected_x_dimension = 19
expected_y_dimension = 20
assert data.dims == {"x": expected_x_dimension, "y": expected_y_dimension}


def test_tree_cover():
actual = TreeCover().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).mean()
expected = 54.0
tolerance = 0.1
assert (
pytest.approx(expected, rel=tolerance) == actual
)
def test_high_land_surface_temperature():
data = HighLandSurfaceTemperature().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1)
assert data.any()


def test_albedo():
assert Albedo().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).mean()
def test_land_surface_temperature():
mean_lst = LandSurfaceTemperature().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).mean()
assert mean_lst


def test_landsat_collection_2():
bands = ['green']
data = LandsatCollection2(bands).get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1)
assert data.any()


def test_lst():
mean = LandSurfaceTemperature().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).mean()
def test_nasa_dem():
mean = NasaDEM().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).mean()
assert mean


def test_esa():
count = (
EsaWorldCover(land_cover_class=EsaWorldCoverClass.BUILT_UP)
.get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1)
.count()
)
assert count
def test_natural_areas():
data = NaturalAreas().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1)
assert data.any()


def test_average_net_building_height():
assert AverageNetBuildingHeight().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).mean()
def test_openbuildings():
count = OpenBuildings().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).count().sum()
assert count


def test_open_street_map():
Expand All @@ -139,39 +120,40 @@ def test_open_street_map():
assert count


def test_urban_land_use():
assert UrbanLandUse().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).count()


def test_openbuildings():
count = OpenBuildings().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).count().sum()
def test_overture_buildings():
count = OvertureBuildings().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).count().sum()
assert count


def test_tree_canopy_hight():
count = TreeCanopyHeight().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).count()
assert count


def test_alos_dsm():
mean = AlosDSM().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).mean()
assert mean
def test_sentinel_2_level2():
sentinel_2_bands = ["green"]
data = Sentinel2Level2(sentinel_2_bands).get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1)
assert data.any()


def test_smart_surface_lulc():
count = SmartSurfaceLULC().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).count()
assert count


def test_overture_buildings():
count = OvertureBuildings().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).count().sum()
def test_tree_canopy_height():
count = TreeCanopyHeight().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).count()
assert count


def test_nasa_dem():
mean = NasaDEM().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).mean()
assert mean
def test_tree_cover():
actual = TreeCover().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).mean()
expected = 54.0
tolerance = 0.1
assert (
pytest.approx(expected, rel=tolerance) == actual
)

def test_era_5_hottest_day():
mean = Era5HottestDay().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).mean()
assert mean

def test_urban_land_use():
assert UrbanLandUse().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).count()


def test_world_pop():
data = WorldPop().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1)
assert data.any()
58 changes: 58 additions & 0 deletions tests/test_methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import numpy as np

from .conftest import (
LARGE_ZONES,
ZONES,
MockGroupByLayer,
MockLargeGroupByLayer,
MockLargeLayer,
MockLayer,
MockMaskLayer,
)
from .fixtures.bbox_constants import *


def test_count():
counts = MockLayer().groupby(ZONES).count()
assert counts.size == 100
assert all([count == 100 for count in counts])


def test_mean():
means = MockLayer().groupby(ZONES).mean()
assert means.size == 100
assert all([mean == i for i, mean in enumerate(means)])


def test_fishnetted_count():
counts = MockLargeLayer().groupby(LARGE_ZONES).count()
assert counts.size == 100
assert all([count == 100 for count in counts])


def test_fishnetted_mean():
means = MockLargeLayer().groupby(LARGE_ZONES).mean()
assert means.size == 100
assert all([mean == i for i, mean in enumerate(means)])


def test_masks():
counts = MockLayer().mask(MockMaskLayer()).groupby(ZONES).count()
assert counts.size == 100
for i, count in enumerate(counts):
if i % 2 == 0:
assert np.isnan(count)
else:
assert count == 100


def test_group_by_layer():
counts = MockLayer().groupby(ZONES, layer=MockGroupByLayer()).count()
assert all([count == {1: 50.0, 2: 50.0} for count in counts])


def test_group_by_large_layer():
counts = (
MockLargeLayer().groupby(LARGE_ZONES, layer=MockLargeGroupByLayer()).count()
)
assert all([count == {1: 50.0, 2: 50.0} for count in counts])

0 comments on commit 87ab037

Please sign in to comment.