Skip to content

Commit

Permalink
all plotting features of the featureCollections (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
12rambau authored Mar 13, 2024
2 parents e1ddfd8 + 32dfcec commit e310240
Show file tree
Hide file tree
Showing 23 changed files with 2,357 additions and 9 deletions.
6 changes: 0 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ repos:
- id: commitizen
stages: [commit-msg]

# - repo: "https://github.com/kynan/nbstripout"
# rev: "0.5.0"
# hooks:
# - id: nbstripout
# stages: [commit]

- repo: "https://github.com/pre-commit/mirrors-prettier"
rev: "v2.7.1"
hooks:
Expand Down
3 changes: 2 additions & 1 deletion docs/example/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ Then you can open a PR with the new file and it will be reviewed and merged.
:hidden:

template
asset
asset
plot_featureCollection
527 changes: 527 additions & 0 deletions docs/example/plot_featureCollection.ipynb

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions docs/usage/layout.rst
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ Converter
#########

- :py:meth:`ee.FeatureCollection.geetools.toImage <geetools.FeatureCollection.FeatureCollectionAccessor.toImage>`: :docstring:`geetools.FeatureCollectionAccessor.toImage`
- :py:meth:`ee.FeatureCollection.geetools.byFeatures <geetools.FeatureCollection.FeatureCollectionAccessor.byFeatures>`: :docstring:`geetools.FeatureCollectionAccessor.byFeatures`
- :py:meth:`ee.FeatureCollection.geetools.byProperties <geetools.FeatureCollection.FeatureCollectionAccessor.byProperties>`: :docstring:`geetools.FeatureCollectionAccessor.byProperties`

Plotting
########

- :py:meth:`ee.FeatureCollection.geetools.plot_by_features <geetools.FeatureCollection.FeatureCollectionAccessor.plot_by_features>`: :docstring:`geetools.FeatureCollectionAccessor.plot_by_features`
- :py:meth:`ee.FeatureCollection.geetools.plot_by_properties <geetools.FeatureCollection.FeatureCollectionAccessor.plot_by_properties>`: :docstring:`geetools.FeatureCollectionAccessor.plot_by_properties`
- :py:meth:`ee.FeatureCollection.geetools.plot_hist <geetools.FeatureCollection.FeatureCollectionAccessor.plot_hist>`: :docstring:`geetools.FeatureCollectionAccessor.plot_hist`

ee.Filter
^^^^^^^^^
Expand Down
390 changes: 388 additions & 2 deletions geetools/FeatureCollection/__init__.py

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies = [
"ee-extra",
"xee",
"yamlable",
"matplotlib",
]

[[project.authors]]
Expand Down
14 changes: 14 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,17 @@ def multipoint_feature():
"""Return a ``Feature`` instance."""
geoms = ee.Geometry.MultiPoint([[0, 0], [0, 1]])
return ee.Feature(geoms).set({"foo": "bar", "bar": "foo"})


@pytest.fixture
def ecoregions():
"""Return the ecoregion collection."""
return ee.FeatureCollection("projects/google/charts_feature_example")


@pytest.fixture
def climSamp():
"""Return the climate sample collection."""
normClim = ee.ImageCollection("OREGONSTATE/PRISM/Norm81m").toBands()
region = ee.Geometry.Rectangle(-123.41, 40.43, -116.38, 45.14)
return normClim.sample(region, 5000)
175 changes: 175 additions & 0 deletions tests/test_FeatureCollection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""Test the ``FeatureCollection`` class."""
import io

import ee
import pytest
from matplotlib import pyplot as plt

import geetools

Expand Down Expand Up @@ -77,3 +80,175 @@ def test_deprecated_clean(self, fc_instance, data_regression):
with pytest.deprecated_call():
fc = geetools.tools.featurecollection.clean(fc_instance)
data_regression.check(fc.getInfo())


class TestByProperties:
"""Test the ``byProperties`` method."""

def test_by_properties(self, ecoregions, data_regression):
fc = ecoregions.geetools.byProperties()
data_regression.check(fc.getInfo())

def test_by_properties_with_id(self, ecoregions, data_regression):
fc = ecoregions.geetools.byProperties("label")
data_regression.check(fc.getInfo())

def test_by_properties_with_properties(self, ecoregions, data_regression):
fc = ecoregions.geetools.byProperties("label", properties=["01_tmean", "02_tmean"])
data_regression.check(fc.getInfo())


class TestByFeatures:
"""Test the ``byFeatures`` method."""

def test_by_features(self, ecoregions, data_regression):
fc = ecoregions.geetools.byFeatures()
data_regression.check(fc.getInfo())

def test_by_features_with_id(self, ecoregions, data_regression):
fc = ecoregions.geetools.byFeatures("label")
data_regression.check(fc.getInfo())

def test_by_features_with_properties(self, ecoregions, data_regression):
fc = ecoregions.geetools.byFeatures("label", properties=["01_tmean", "02_tmean"])
data_regression.check(fc.getInfo())


class TestPlotByFeatures:
"""Test the ``plot_by_features`` method."""

def test_plot_by_features_bar(self, ecoregions, image_regression):
fig, ax = plt.subplots()
# fmt: off
ecoregions.geetools.plot_by_features(
type="bar",
featureId="label",
properties=["01_tmean", "02_tmean", "03_tmean", "04_tmean", "05_tmean", "06_tmean", "07_tmean", "08_tmean", "09_tmean", "10_tmean","11_tmean", "12_tmean"],
labels=["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"],
colors=["#604791", "#1d6b99", "#39a8a7", "#0f8755", "#76b349", "#f0af07", "#e37d05", "#cf513e", "#96356f", "#724173", "#9c4f97", "#696969"],
ax=ax,
)
# fmt: on
with io.BytesIO() as buffer:
fig.savefig(buffer)
image_regression.check(buffer.getvalue())

def test_plot_by_features_stacked(self, ecoregions, image_regression):
fig, ax = plt.subplots()
# fmt: off
ecoregions.geetools.plot_by_features(
type="stacked",
featureId="label",
properties=["01_tmean", "02_tmean", "03_tmean", "04_tmean", "05_tmean", "06_tmean", "07_tmean", "08_tmean", "09_tmean", "10_tmean","11_tmean", "12_tmean"],
labels=["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"],
colors=["#604791", "#1d6b99", "#39a8a7", "#0f8755", "#76b349", "#f0af07", "#e37d05", "#cf513e", "#96356f", "#724173", "#9c4f97", "#696969"],
ax=ax,
)
# fmt: on
with io.BytesIO() as buffer:
fig.savefig(buffer)
image_regression.check(buffer.getvalue())

def test_plot_by_features_scatter(self, ecoregions, image_regression):
fig, ax = plt.subplots()
ecoregions.geetools.plot_by_features(
type="scatter",
featureId="label",
properties=["01_ppt", "06_ppt", "09_ppt"],
labels=["jan", "jun", "sep"],
ax=ax,
)
with io.BytesIO() as buffer:
fig.savefig(buffer)
image_regression.check(buffer.getvalue())

def test_plot_by_features_pie(self, ecoregions, image_regression):
fig, ax = plt.subplots()
ecoregions.geetools.plot_by_features(
type="pie",
featureId="label",
properties=["06_ppt"],
colors=["#f0af07", "#0f8755", "#76b349"],
ax=ax,
)
with io.BytesIO() as buffer:
fig.savefig(buffer)
image_regression.check(buffer.getvalue())

def test_plot_by_features_donut(self, ecoregions, image_regression):
fig, ax = plt.subplots()
ecoregions.geetools.plot_by_features(
type="donut",
featureId="label",
properties=["06_ppt"],
colors=["#f0af07", "#0f8755", "#76b349"],
ax=ax,
)
with io.BytesIO() as buffer:
fig.savefig(buffer)
image_regression.check(buffer.getvalue())


class TestPlotByPropperties:
"""Test the ``plot_by_properties`` method."""

def test_plot_by_properties_bar(self, ecoregions, image_regression):
fig, ax = plt.subplots()
# fmt: off
ecoregions.geetools.plot_by_properties(
type="bar",
properties=["01_ppt", "02_ppt", "03_ppt", "04_ppt", "05_ppt", "06_ppt", "07_ppt", "08_ppt", "09_ppt", "10_ppt", "11_ppt", "12_ppt"],
labels=["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"],
featureId="label",
colors=["#f0af07", "#0f8755", "#76b349"],
ax=ax,
)
# fmt: on
with io.BytesIO() as buffer:
fig.savefig(buffer)
image_regression.check(buffer.getvalue())

def test_plot_by_properties_plot(self, ecoregions, image_regression):
fig, ax = plt.subplots()
# fmt: off
ecoregions.geetools.plot_by_properties(
type="plot",
properties=["01_ppt", "02_ppt", "03_ppt", "04_ppt", "05_ppt", "06_ppt", "07_ppt", "08_ppt", "09_ppt", "10_ppt", "11_ppt", "12_ppt"],
labels=["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"],
featureId="label",
colors=["#f0af07", "#0f8755", "#76b349"],
ax=ax,
)
# fmt: on
with io.BytesIO() as buffer:
fig.savefig(buffer)
image_regression.check(buffer.getvalue())

def test_plot_by_properties_area(self, ecoregions, image_regression):
fig, ax = plt.subplots()
# fmt: off
ecoregions.geetools.plot_by_properties(
type="fill_between",
properties=["01_ppt", "02_ppt", "03_ppt", "04_ppt", "05_ppt", "06_ppt", "07_ppt", "08_ppt", "09_ppt", "10_ppt", "11_ppt", "12_ppt"],
labels=["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"],
featureId="label",
colors=["#f0af07", "#0f8755", "#76b349"],
ax=ax,
)
# fmt: on
with io.BytesIO() as buffer:
fig.savefig(buffer)
image_regression.check(buffer.getvalue())


class TestPlotHist:
"""Test the ``plot_hist`` method."""

def test_plot_hist(self, climSamp, image_regression):
fig, ax = plt.subplots()
climSamp.geetools.plot_hist(
property="07_ppt", label="July Precipitation (mm)", color="#1d6b99", ax=ax, bins=30
)
with io.BytesIO() as buffer:
fig.savefig(buffer)
image_regression.check(buffer.getvalue())
Loading

0 comments on commit e310240

Please sign in to comment.