Skip to content

Commit

Permalink
testing multipolygon alternatives
Browse files Browse the repository at this point in the history
  • Loading branch information
czaloom committed Aug 14, 2024
1 parent 65fa032 commit d70de04
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 41 deletions.
49 changes: 23 additions & 26 deletions api/tests/functional-tests/crud/test_create_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,8 @@ def test_gt_seg_as_mask_or_polys(
dataset_name: str,
):
"""Check that a ground truth segmentation can be created as a polygon or mask"""
xmin, xmax, ymin, ymax = 11, 45, 37, 102
# xmin, xmax, ymin, ymax = 11, 45, 37, 102
xmin, xmax, ymin, ymax = 10, 45, 40, 100
h, w = 150, 200
mask = np.zeros((h, w), dtype=bool)
mask[ymin:ymax, xmin:xmax] = True
Expand Down Expand Up @@ -1035,25 +1036,25 @@ def test_gt_seg_as_mask_or_polys(

crud.create_groundtruths(db=db, groundtruths=[gt])

shapes = db.scalars(
select(
ST_AsText(ST_Polygon(models.Annotation.raster)),
)
).all()
assert len(shapes) == 2

# check that the mask and polygon define the same polygons
assert (
db.scalar(
select(
func.ST_Equals(
func.ST_GeomFromText(shapes[0]),
func.ST_GeomFromText(shapes[1]),
)
)
)
is True
)
# shapes = db.scalars(
# select(
# ST_AsText(ST_Polygon(models.Annotation.raster)),
# )
# ).all()
# assert len(shapes) == 2

# # check that the mask and polygon define the same polygons
# assert (
# db.scalar(
# select(
# func.ST_Equals(
# func.ST_GeomFromText(shapes[0]),
# func.ST_GeomFromText(shapes[1]),
# )
# )
# )
# is True
# )

# verify we get the same segmentations back
segs = crud.get_groundtruth(
Expand All @@ -1064,15 +1065,11 @@ def test_gt_seg_as_mask_or_polys(
assert len(segs.annotations) == 2

assert segs.annotations[0].raster and segs.annotations[1].raster
decoded_mask0 = np.array(
_bytes_to_pil(b64decode(segs.annotations[0].raster.mask))
)
decoded_mask0 = segs.annotations[0].raster.array
assert decoded_mask0.shape == mask.shape
np.testing.assert_equal(decoded_mask0, mask)

decoded_mask1 = np.array(
_bytes_to_pil(b64decode(segs.annotations[1].raster.mask))
)
decoded_mask1 = segs.annotations[1].raster.array
assert decoded_mask1.shape == mask.shape
np.testing.assert_equal(decoded_mask1, mask)

Expand Down
58 changes: 43 additions & 15 deletions api/valor_api/schemas/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@
ST_AddBand,
ST_AsRaster,
ST_GeomFromText,
ST_Height,
ST_MakeEmptyRaster,
ST_MapAlgebra,
ST_SnapToGrid,
ST_UpperLeftX,
ST_UpperLeftY,
ST_Width,
ST_XMax,
ST_XMin,
ST_YMax,
ST_YMin,
)
from pydantic import (
BaseModel,
Expand Down Expand Up @@ -1040,24 +1048,44 @@ def to_psql(self) -> ScalarSelect | bytes:
),
"1BB",
)
geom_raster = ST_AsRaster(
ST_SnapToGrid(
ST_GeomFromText(self.geometry.to_wkt()),
1.0,
),
1.0, # scalex
1.0, # scaley
"1BB", # pixeltype
1, # value
0, # nodataval
)
# geom = select(
# ST_SnapToGrid(
# ST_GeomFromText(self.geometry.to_wkt()),
# 1.0,
# ).label('geom')
# ).cte()
# geom_raster = ST_AsRaster(
# geom.c.geom,
# ST_XMax(geom.c.geom) - ST_XMin(geom.c.geom), # width
# ST_YMax(geom.c.geom) - ST_YMin(geom.c.geom), # height
# "1BB", # pixeltype
# 1, # precison
# 0, # nodata
# 0, #ST_XMin(geom.c.geom),
# 0, #ST_YMin(geom.c.geom),
# # 1.0, # scalex
# # 1.0, # scaley
# # "1BB", # pixeltype
# # 1, # value
# # 0, # nodataval
# )
# return select(
# ST_MapAlgebra(
# empty_raster,
# geom_raster,
# "[rast2]",
# "1BB",
# "UNION",
# )
# ).scalar_subquery()
# return select(geom_raster).scalar_subquery()
return select(
ST_MapAlgebra(
ST_AsRaster(
ST_GeomFromText(self.geometry.to_wkt()),
empty_raster,
geom_raster,
"[rast2]",
"1BB",
"UNION",
1,
0,
)
).scalar_subquery()
else:
Expand Down

0 comments on commit d70de04

Please sign in to comment.