Skip to content

Commit

Permalink
extend decorator added (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
davemlz committed May 21, 2021
1 parent 51cc409 commit 707f680
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 157 deletions.
29 changes: 27 additions & 2 deletions eemont/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,24 @@ def _get_kernel_parameters(img, lookup, kernel, sigma):
return kernelParameters


def _index(self, index, G, C1, C2, L, cexp, nexp, alpha, slope, intercept, kernel, sigma, p, c, online):
def _index(
self,
index,
G,
C1,
C2,
L,
cexp,
nexp,
alpha,
slope,
intercept,
kernel,
sigma,
p,
c,
online,
):
"""Computes one or more spectral indices (indices are added as bands) for an image oir image collection.
Parameters
Expand Down Expand Up @@ -399,7 +416,15 @@ def _index(self, index, G, C1, C2, L, cexp, nexp, alpha, slope, intercept, kerne
if not isinstance(index, list):
if index == "all":
index = list(spectralIndices.keys())
elif index in ["vegetation", "burn", "water", "snow", "drought", "urban","kernel"]:
elif index in [
"vegetation",
"burn",
"water",
"snow",
"drought",
"urban",
"kernel",
]:
temporalListOfIndices = []
for idx in indicesNames:
if spectralIndices[idx]["type"] == index:
Expand Down
14 changes: 5 additions & 9 deletions eemont/eeList.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import ee
import warnings
from .extending import extend


def _extend_eeList():
"""Decorator. Extends the ee.List class."""
return lambda f: (setattr(ee.ee_list.List, f.__name__, f) or f)


@_extend_eeList()
@extend(ee.ee_list.List)
def __add__(self, other):
"""Concatenates the contents of other onto list.
Expand All @@ -26,7 +22,7 @@ def __add__(self, other):
return self.cat(other)


@_extend_eeList()
@extend(ee.ee_list.List)
def __radd__(self, other):
"""Concatenates the contents of other onto list.
Expand All @@ -45,7 +41,7 @@ def __radd__(self, other):
return ee.List(other).cat(self)


@_extend_eeList()
@extend(ee.ee_list.List)
def __mul__(self, other):
"""Returns a new list containing a list repeated n (other) times.
Expand All @@ -64,7 +60,7 @@ def __mul__(self, other):
return ee.List.repeat(self, other).flatten()


@_extend_eeList()
@extend(ee.ee_list.List)
def __rmul__(self, other):
"""Returns a new list containing a list repeated n (other) times.
Expand Down
19 changes: 19 additions & 0 deletions eemont/extending.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def extend(cls, static=False):
"""Extends the cls class.
Parameters
----------
cls : class
Class to extend.
static : boolean, default = False
Wheter extend as a static method.
Returns
-------
decorator
Decorator for extending classes.
"""
if static:
return lambda f: (setattr(cls, f.__name__, staticmethod(f)) or f)
else:
return lambda f: (setattr(cls, f.__name__, f) or f)
15 changes: 3 additions & 12 deletions eemont/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,10 @@
import geopy
from geopy.geocoders import get_geocoder_for_service
from .geometry import *
from .extending import extend


def _extend_staticmethod_eeFeature():
"""Decorator. Extends the ee.Feature class with a static method."""
return lambda f: (setattr(ee.feature.Feature, f.__name__, staticmethod(f)) or f)


def _extend_eeFeature():
"""Decorator. Extends the ee.Feature class."""
return lambda f: (setattr(ee.feature.Feature, f.__name__, f) or f)


@_extend_staticmethod_eeFeature()
@extend(ee.feature.Feature, static=True)
def PointFromQuery(query, geocoder="nominatim", **kwargs):
"""Constructs an ee.Feature describing a point from a query submitted to a geodocer using the geopy package. This returns exactly one pair of coordinates.
The properties of the feature correspond to the raw properties retrieved by the location of the query.
Expand Down Expand Up @@ -75,7 +66,7 @@ def PointFromQuery(query, geocoder="nominatim", **kwargs):
return ee.Feature(geometry, location.raw)


@_extend_staticmethod_eeFeature()
@extend(ee.feature.Feature, static=True)
def BBoxFromQuery(query, geocoder="nominatim", **kwargs):
"""Constructs an ee.Feature describing a bounding box from a query submitted to a geodocer using the geopy package.
The properties of the feature correspond to the raw properties retrieved by the location of the query.
Expand Down
18 changes: 2 additions & 16 deletions eemont/featurecollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,10 @@
import geopy
from geopy.geocoders import get_geocoder_for_service
from .geometry import *
from .extending import extend


def _extend_staticmethod_eeFeatureCollection():
"""Decorator. Extends the ee.FeatureCollection class with a static method."""
return lambda f: (
setattr(ee.featurecollection.FeatureCollection, f.__name__, staticmethod(f))
or f
)


def _extend_eeFeatureCollection():
"""Decorator. Extends the ee.FeatureCollection class."""
return lambda f: (
setattr(ee.featurecollection.FeatureCollection, f.__name__, f) or f
)


@_extend_staticmethod_eeFeatureCollection()
@extend(ee.featurecollection.FeatureCollection, static=True)
def MultiPointFromQuery(query, geocoder="nominatim", **kwargs):
"""Constructs an ee.Feature describing a point from a query submitted to a geodocer using the geopy package. This returns all pairs of coordinates retrieved by the query.
The properties of the feature collection correspond to the raw properties retrieved by the locations of the query.
Expand Down
17 changes: 4 additions & 13 deletions eemont/geometry.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
import ee
import geopy
from geopy.geocoders import get_geocoder_for_service
from .extending import extend


def _extend_staticmethod_eeGeometry():
"""Decorator. Extends the ee.Geometry class with a static method."""
return lambda f: (setattr(ee.geometry.Geometry, f.__name__, staticmethod(f)) or f)


def _extend_eeGeometry():
"""Decorator. Extends the ee.Geometry class."""
return lambda f: (setattr(ee.geometry.Geometry, f.__name__, f) or f)


@_extend_staticmethod_eeGeometry()
@extend(ee.geometry.Geometry, static=True)
def BBoxFromQuery(query, geocoder="nominatim", **kwargs):
"""Constructs an ee.Geometry describing a bounding box from a query submitted to a geodocer using the geopy package.
Expand Down Expand Up @@ -103,7 +94,7 @@ def BBoxFromQuery(query, geocoder="nominatim", **kwargs):
raise Exception('Invalid geocoder! Use one of "nominatim" or "arcgis".')


@_extend_staticmethod_eeGeometry()
@extend(ee.geometry.Geometry, static=True)
def PointFromQuery(query, geocoder="nominatim", **kwargs):
"""Constructs an ee.Geometry describing a point from a query submitted to a geodocer using the geopy package. This returns exactly one pair of coordinates.
Expand Down Expand Up @@ -160,7 +151,7 @@ def PointFromQuery(query, geocoder="nominatim", **kwargs):
return ee.Geometry.Point([location.longitude, location.latitude])


@_extend_staticmethod_eeGeometry()
@extend(ee.geometry.Geometry, static=True)
def MultiPointFromQuery(query, geocoder="nominatim", **kwargs):
"""Constructs an ee.Geometry describing a multi-point from a query submitted to a geodocer using the geopy package. This returns all pairs of coordinates retrieved by the query.
Expand Down
Loading

0 comments on commit 707f680

Please sign in to comment.