Skip to content

Commit

Permalink
refactor: use custom types
Browse files Browse the repository at this point in the history
  • Loading branch information
12rambau committed Jan 22, 2024
1 parent ff54bf9 commit 3c80097
Show file tree
Hide file tree
Showing 17 changed files with 183 additions and 252 deletions.
15 changes: 7 additions & 8 deletions geetools/Array/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
"""Extra methods for the ``ee.Array`` class."""
from __future__ import annotations

from typing import Union

import ee

from geetools.accessors import geetools_accessor
from geetools.types import ee_int, ee_number

# hack to have the generated Array class available
# it might create issues in the future with libs that have exotic init methods
Expand All @@ -23,9 +22,9 @@ def __init__(self, obj: ee.Array):
# -- alternative constructor -----------------------------------------------
def full(
self,
width: Union[int, float, ee.Number],
height: Union[int, float, ee.Number],
value: Union[int, ee.Number, float],
width: ee_number,
height: ee_number,
value: ee_number,
) -> ee.Array:
"""Create an array with the given dimensions, initialized to the given value.
Expand Down Expand Up @@ -53,9 +52,9 @@ def full(
# -- data maniputlation ----------------------------------------------------
def set(
self,
x: Union[int, ee.number],
y: Union[int, ee.number],
value: Union[int, float, ee.Number],
x: ee_int,
y: ee_int,
value: ee_number,
) -> ee.Array:
"""Set the value of a cell in an array.
Expand Down
9 changes: 5 additions & 4 deletions geetools/ComputedObject/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

import json
from pathlib import Path
from typing import Type, Union
from typing import Type

import ee

from geetools.accessors import geetools_extend
from geetools.types import pathlike


# -- types management ----------------------------------------------------------
Expand Down Expand Up @@ -36,7 +37,7 @@ def isInstance(self, klass: Type) -> ee.Number:

# -- .gee files ----------------------------------------------------------------
@geetools_extend(ee.ComputedObject)
def save(self, path: Union[str, Path]) -> Path:
def save(self, path: pathlike) -> Path:
"""Save a ``ComputedObject`` to a .gee file.
The file contains the JSON representation of the object. it still need to be computed via ``getInfo()`` to be used.
Expand Down Expand Up @@ -67,9 +68,9 @@ def save(self, path: Union[str, Path]) -> Path:
return path


@geetools_extend(ee.ComputedObject)
@geetools_extend(ee.ComputedObject) # type: ignore
@classmethod
def open(cls, path: Union[str, Path]) -> ee.ComputedObject:
def open(cls, path: pathlike) -> ee.ComputedObject:
"""Open a .gee file as a ComputedObject.
Parameters:
Expand Down
4 changes: 2 additions & 2 deletions geetools/Date/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def fromDOY(cls, doy: int, year: int) -> ee.Date:
d = ee.Date.geetools.fromDOY(1, 2020)
d.getInfo()
"""
doy, year = ee.Number(doy).toInt(), ee.Number(year).toInt()
return ee.Date.fromYMD(year, 1, 1).advance(doy.subtract(1), "day")
d, y = ee.Number(doy).toInt(), ee.Number(year).toInt()
return ee.Date.fromYMD(y, 1, 1).advance(d.subtract(1), "day")

# -- export date -----------------------------------------------------------
def to_datetime(self) -> datetime:
Expand Down
5 changes: 2 additions & 3 deletions geetools/DateRange/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
"""Extra tools for the ``ee.DateRange`` class."""
from __future__ import annotations

from typing import Union

import ee

from geetools.accessors import geetools_accessor
from geetools.types import ee_int


@geetools_accessor(ee.DateRange)
Expand All @@ -17,7 +16,7 @@ def __init__(self, obj: ee.DateRange):
self._obj = obj

# -- date range operations -------------------------------------------------
def split(self, interval: Union[int, ee.Number], unit: str = "day") -> ee.List:
def split(self, interval: ee_int, unit: str = "day") -> ee.List:
"""Convert a ``ee.DateRange`` to a list of ``ee.DateRange``.
The DateRange will be split in multiple DateRanges of the specified interval and Unit.
Expand Down
7 changes: 3 additions & 4 deletions geetools/Dictionary/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
"""Extra methods for the ``ee.Dictionary`` class."""
from __future__ import annotations

from typing import Union

import ee

from geetools.accessors import geetools_accessor
from geetools.types import ee_list


@geetools_accessor(ee.Dictionary)
Expand All @@ -17,7 +16,7 @@ def __init__(self, obj: ee.Dictionary):
self._obj = obj

# -- alternative constructor -----------------------------------------------
def fromPairs(self, list: Union[list, ee.List]) -> ee.Dictionary:
def fromPairs(self, list: ee_list) -> ee.Dictionary:
"""Create a dictionary from a list of [[key, value], ...]] pairs.
Parameters:
Expand Down Expand Up @@ -62,7 +61,7 @@ def sort(self) -> ee.Dictionary:
values = orderededKeys.map(lambda key: self._obj.get(key))
return ee.Dictionary.fromLists(orderededKeys, values)

def getMany(self, list: Union[ee.List, list]) -> ee.List:
def getMany(self, list: ee_list) -> ee.List:
"""Extract values from a list of keys.
Parameters:
Expand Down
9 changes: 4 additions & 5 deletions geetools/FeatureCollection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import ee

from geetools.accessors import geetools_accessor
from geetools.types import ee_int, ee_str


@geetools_accessor(ee.FeatureCollection)
Expand All @@ -18,8 +19,8 @@ def __init__(self, obj: ee.FeatureCollection):

def toImage(
self,
color: Union[ee.String, str, ee.Number, int] = 0,
width: Union[ee.String, str, ee.Number, int] = "",
color: Union[ee_str, ee_int] = 0,
width: Union[ee_str, ee_int] = "",
) -> ee.Image:
"""Paint the current FeatureCollection to an Image.
Expand All @@ -33,9 +34,7 @@ def toImage(
width == "" or params.update(width=width)
return ee.Image().paint(self._obj, **params)

def addId(
self, name: Union[str, ee.String] = "id", start: Union[int, ee.Number] = 1
) -> ee.FeatureCollection:
def addId(self, name: ee_str = "id", start: ee_int = 1) -> ee.FeatureCollection:
"""Add a unique numeric identifier, starting from parameter ``start``.
Returns:
Expand Down
Loading

0 comments on commit 3c80097

Please sign in to comment.