diff --git a/py_tools_ds/geo/vector/conversion.py b/py_tools_ds/geo/vector/conversion.py index cacf66ba53a95f5a49a1afd86947877e13275666..466f6638c899bfcb550433f0b56904a964e0f12c 100644 --- a/py_tools_ds/geo/vector/conversion.py +++ b/py_tools_ds/geo/vector/conversion.py @@ -26,6 +26,7 @@ import numpy as np # custom from shapely.geometry import shape, mapping, box from shapely.geometry import Polygon # noqa F401 # flake8 issue +from shapely import wkt try: from osgeo import ogr @@ -81,12 +82,15 @@ def get_boxImXY_from_shapelyPoly(shapelyPoly, im_gt): return boxImXY -def round_shapelyPoly_coords(shapelyPoly, precision=10, out_dtype=None): - geojson = mapping(shapelyPoly) - geojson['coordinates'] = np.round(np.array(geojson['coordinates']), precision) - if out_dtype: - geojson['coordinates'] = geojson['coordinates'].astype(out_dtype) - return shape(geojson) +def round_shapelyPoly_coords(shapelyPoly, precision=10): + # type: (Polygon, int) -> Polygon + """Round the coordinates of the given shapely polygon. + + :param shapelyPoly: the shapely polygone + :param precision: number of decimals + :return: + """ + return wkt.loads(wkt.dumps(shapelyPoly, rounding_precision=precision)) def points_to_raster(points, values, tgt_res, prj=None, fillVal=None): diff --git a/py_tools_ds/geo/vector/geometry.py b/py_tools_ds/geo/vector/geometry.py index 79053460907ddb7ef212891043bf59b2a9995680..eabd24dc7619573710b3fdf051d9820e4d0d2ef0 100644 --- a/py_tools_ds/geo/vector/geometry.py +++ b/py_tools_ds/geo/vector/geometry.py @@ -126,7 +126,7 @@ class boxObj(object): @property def boxImYX(self): - temp_imPoly = round_shapelyPoly_coords(self.imPoly, precision=0, out_dtype=int) + temp_imPoly = round_shapelyPoly_coords(self.imPoly, precision=0) floatImBoxYX = shapelyBox2BoxYX(temp_imPoly, coord_type='image') return [[int(i[0]), int(i[1])] for i in floatImBoxYX]