Skip to content

Commit

Permalink
remove unused methods in geo-referencing
Browse files Browse the repository at this point in the history
  • Loading branch information
Mary-h86 committed Aug 6, 2024
1 parent ac246a4 commit fb65aa7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 98 deletions.
97 changes: 44 additions & 53 deletions src/tile2net/raster/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ def base_height(self):
def base_width(self):
return abs(self.xtilem - self.xtile) + 1 # horizontal tiles (#columns)


def pos2id(self, col_idx: int, row_idx: int):
"""
calculate the tile id based on the position in np.array
Expand Down Expand Up @@ -411,7 +410,7 @@ def height_pixel(self):
def width_pixel(self):
return self.width * self.tile_size

def tilexy2pos(self, xtile, ytile):
def tilexy2pos(self, xtile: int, ytile: int):
"""
converts xy coordinates to tile position
Expand All @@ -432,7 +431,7 @@ def tilexy2pos(self, xtile, ytile):
j = ytile - self.ytile
return i, j

def tilexy2id(self, xtile, ytile):
def tilexy2id(self, xtile: int, ytile: int):
"""
converts xy coordinates to tile idd
Expand All @@ -452,7 +451,7 @@ def tilexy2id(self, xtile, ytile):
x, y = self.tilexy2pos(xtile, ytile)
return self.pos2id(x, y)

def _create_info_dict(self, df=False) -> dict | pd.DataFrame:
def _create_info_dict(self, df: bool = False) -> dict | pd.DataFrame:
"""
Parameters
Expand Down Expand Up @@ -533,7 +532,7 @@ def save_csv(self, dst_path: str = None):
tileinfo_df = self._create_info_dict(df=True)
tileinfo_df.to_csv(os.path.join(dst_path, f'{self.name}_{self.tile_size}_info.csv'))

def get_boundary(self, city=None, address=None, path=None):
def get_boundary(self, city: str = None, address: str = None, path: str = None):
if city:
# define the place query
query = {'city': city}
Expand All @@ -552,7 +551,7 @@ def get_boundary(self, city=None, address=None, path=None):
else:
raise ValueError("You must pass a textual address or name of the region")

def get_in_boundary(self, clipped=None, city=None, address=None, path=None):
def get_in_boundary(self, clipped: bool = None, city: str = None, address: str = None, path: str = None):
"""
Makes the tiles outside the boundary defined by the city or address inactive.
This is used to speed up analysis, especially when a region has a shape that results
Expand Down Expand Up @@ -591,7 +590,7 @@ def get_in_boundary(self, clipped=None, city=None, address=None, path=None):
if clipped:
return new

def make_inactive(self, lst):
def make_inactive(self, lst: list[int]):
"""
make the listed tiles inactive.
Used for setting the boundaries around a region or excluding certain regions.
Expand Down Expand Up @@ -652,7 +651,7 @@ def save_ntw_polygon(self, crs_metric: int = 3857):
path = os.path.join(poly_fold, f'{self.name}-Polygons-{datetime.datetime.now().strftime("%d-%m-%Y_%H_%M")}')
if os.path.exists(path):
shutil.rmtree(path)
simplified.to_file(path )
simplified.to_file(path)
logging.info('Polygons are generated and saved!')

def save_ntw_polygons(
Expand Down Expand Up @@ -719,34 +718,34 @@ class specific :class:`GeoDataFrame` in metric projection
return nt

# adopted from solaris library to overcome dependency issues
def get_geo_transform(self, raster_src):
"""
*Adopted from the Solaris library to overcome dependency issues*
Get the geotransform for a raster image source.
Parameters
----------
raster_src : str, :class:`rasterio.DatasetReader`, or `osgeo.gdal.Dataset`
Path to a raster image with georeferencing data to apply to `geom`.
Alternatively, an opened :class:`rasterio.Band` object or
:class:`osgeo.gdal.Dataset` object can be provided. Required if not
using `affine_obj`.
Returns
-------
transform : :class:`affine.Affine`
An affine transformation object to the image's location in its CRS.
"""

if isinstance(raster_src, str):
affine_obj = rasterio.open(raster_src).transform
elif isinstance(raster_src, rasterio.DatasetReader):
affine_obj = raster_src.transform

return affine_obj

def convert_poly_coords(self, geom, raster_src=None, affine_obj=None, inverse=False,
# def get_geo_transform(self, raster_src: str):
# """
# *Adopted from the Solaris library to overcome dependency issues*
#
# Get the geotransform for a raster image source.
#
# Parameters
# ----------
# raster_src : str, :class:`rasterio.DatasetReader`, or `osgeo.gdal.Dataset`
# Path to a raster image with georeferencing data to apply to `geom`.
# Alternatively, an opened :class:`rasterio.Band` object or
# :class:`osgeo.gdal.Dataset` object can be provided. Required if not
# using `affine_obj`.
#
# Returns
# -------
# transform : :class:`affine.Affine`
# An affine transformation object to the image's location in its CRS.
# """
#
# if isinstance(raster_src, str):
# affine_obj = rasterio.open(raster_src).transform
# elif isinstance(raster_src, rasterio.DatasetReader):
# affine_obj = raster_src.transform
#
# return affine_obj

def convert_poly_coords(self, geom, affine_obj=None, inverse=False,
precision=None):
"""
*Adopted from the Solaris library to overcome dependency issues*
Expand All @@ -755,11 +754,6 @@ def convert_poly_coords(self, geom, raster_src=None, affine_obj=None, inverse=Fa
Parameters
----------
raster_src : str, optional
Path to a raster image with georeferencing data to apply to `geom`.
Alternatively, an opened :class:`rasterio.Band` object or
:class:`osgeo.gdal.Dataset` object can be provided. Required if not
using `affine_obj`.
affine_obj : list or :class:`affine.Affine`
An affine transformation to apply to `geom` in the form of an
``[a, b, d, e, xoff, yoff]`` list or an :class:`affine.Affine` object.
Expand All @@ -778,20 +772,17 @@ def convert_poly_coords(self, geom, raster_src=None, affine_obj=None, inverse=Fa
transformed to match the destination object.
"""

if not raster_src and not affine_obj:
raise ValueError("Either raster_src or affine_obj must be provided.")
if not affine_obj:
raise ValueError("affine_obj must be provided.")

if raster_src is not None:
affine_xform = self.get_geo_transform(raster_src)
if isinstance(affine_obj, Affine):
affine_xform = affine_obj
else:
if isinstance(affine_obj, Affine):
affine_xform = affine_obj
else:
# assume it's a list in either gdal or "standard" order
# (list_to_affine checks which it is)
if len(affine_obj) == 9: # if it's straight from rasterio
affine_obj = affine_obj[0:6]
affine_xform = list_to_affine(affine_obj)
# assume it's a list in either gdal or "standard" order
# (list_to_affine checks which it is)
if len(affine_obj) == 9: # if it's straight from rasterio
affine_obj = affine_obj[0:6]
affine_xform = list_to_affine(affine_obj)

if inverse: # geo->px transform
affine_xform = ~affine_xform
Expand Down
54 changes: 9 additions & 45 deletions src/tile2net/raster/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,36 +564,8 @@ def mask_to_poly_geojson(self, pred_arr, channel_scaling=None, reference_im=None
)

return polygon_gdf
@staticmethod
def get_geo_transform(raster_src):
"""
*Adopted from the Solaris library to overcome dependency issues*
Get the geotransform for a raster image source.
Arguments
---------
raster_src : str, :class:`rasterio.DatasetReader`, or `osgeo.gdal.Dataset`
Path to a raster image with georeferencing data to apply to `geom`.
Alternatively, an opened :class:`rasterio.Band` object or
:class:`osgeo.gdal.Dataset` object can be provided. Required if not
using `affine_obj`.

Returns
-------
transform : :class:`affine.Affine`
An affine transformation object to the image's location in its CRS.
"""

if isinstance(raster_src, str):
affine_obj = rasterio.open(raster_src).transform
elif isinstance(raster_src, rasterio.DatasetReader):
affine_obj = raster_src.transform
# elif isinstance(raster_src, gdal.Dataset):
# affine_obj = Affine.from_gdal(*raster_src.GetGeoTransform())
return affine_obj

def convert_poly_coords(self, geom, raster_src=None, affine_obj=None, inverse=False,
def convert_poly_coords(self, geom, affine_obj=None, inverse=False,
precision=None):
"""
*Adopted from the Solaris library to overcome dependency issues*
Expand All @@ -605,11 +577,6 @@ def convert_poly_coords(self, geom, raster_src=None, affine_obj=None, inverse=Fa
geom : :class:`shapely.geometry.shape` or str
A :class:`shapely.geometry.shape`, or WKT string-formatted geometry
object currently in pixel coordinates.
raster_src : str, optional
Path to a raster image with georeferencing data to apply to `geom`.
Alternatively, an opened :class:`rasterio.Band` object or
:class:`osgeo.gdal.Dataset` object can be provided. Required if not
using `affine_obj`.
affine_obj: list or :class:`affine.Affine`
An affine transformation to apply to `geom` in the form of an
``[a, b, d, e, xoff, yoff]`` list or an :class:`affine.Affine` object.
Expand All @@ -628,19 +595,16 @@ def convert_poly_coords(self, geom, raster_src=None, affine_obj=None, inverse=Fa
transformed to match the destination object.
"""

if not raster_src and not affine_obj:
raise ValueError("Either raster_src or affine_obj must be provided.")
if not affine_obj:
raise ValueError("affine_obj must be provided.")

if raster_src is not None:
affine_xform = self.get_geo_transform(raster_src)
if isinstance(affine_obj, Affine):
affine_xform = affine_obj
else:
if isinstance(affine_obj, Affine):
affine_xform = affine_obj
else:
# assume it's a list in either gdal or "standard" order
if len(affine_obj) == 9: # if it's straight from rasterio
affine_obj = affine_obj[0:6]
affine_xform = list_to_affine(affine_obj)
# assume it's a list in either gdal or "standard" order
if len(affine_obj) == 9: # if it's straight from rasterio
affine_obj = affine_obj[0:6]
affine_xform = list_to_affine(affine_obj)

if inverse: # geo->px transform
affine_xform = ~affine_xform
Expand Down

0 comments on commit fb65aa7

Please sign in to comment.