Skip to content

Commit

Permalink
add test for QgsTinInterpolator
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Jan 27, 2025
1 parent 524ea46 commit ef90e90
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
45 changes: 45 additions & 0 deletions tests/src/python/test_analysis_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from qgis.analysis import (
QgsInterpolator,
QgsIDWInterpolator,
QgsTinInterpolator,
QgsGridFileWriter,
)

Expand Down Expand Up @@ -93,6 +94,50 @@ def test_idw_interpolator(self):

self.assertTrue(ok)

def test_tin_interpolator(self):
layer = QgsVectorLayer(
os.path.join(TEST_DATA_DIR, "points.shp"), "points", "ogr"
)
self.assertTrue(layer.isValid())

pixel_size = 5
extent = layer.extent()
context = QgsCoordinateTransformContext()

cols = max(math.ceil(extent.width() / pixel_size), 1)
rows = max(math.ceil(extent.height() / pixel_size), 1)

data = QgsInterpolator.LayerData()
data.source = layer
data.sourceType = QgsInterpolator.SourceType.SourcePoints
data.transformContext = context
data.valueSource = QgsInterpolator.ValueSource.ValueAttribute
data.interpolationAttribute = 3

interpolator = QgsTinInterpolator(
[data], QgsTinInterpolator.TinInterpolation.Linear
)

output_file = os.path.join(tempfile.gettempdir(), "tin_interpolation.asc")

writer = QgsGridFileWriter(interpolator, output_file, extent, cols, rows)
writer.writeFile()

checker = QgsRasterChecker()
ok = checker.runTest(
"gdal",
output_file,
"gdal",
os.path.join(TEST_DATA_DIR, "analysis", "tin_interpolation.asc"),
)
self.report += checker.report()

report_file = os.path.join(tempfile.gettempdir(), "tin_interpolation_test.html")
with open(report_file, "w", encoding="utf-8") as f:
f.write(self.report)

self.assertTrue(ok)


if __name__ == "__main__":
unittest.main()
12 changes: 12 additions & 0 deletions tests/testdata/analysis/tin_interpolation.asc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
NCOLS 8
NROWS 5
XLLCORNER -118.88889
YLLCORNER 22.800207
DX 4.4444444
DY 4.8143547
NODATA_VALUE -9999
-9999 -9999 -9999 2.0980652 2.3606286 2.2927486 -9999 -9999
-9999 2.9180237 2.725426 2.5698732 1.8980129 2.5750412 2.8987709 -9999
-9999 1 1.0520833 1.60625 1.1508401 1.122037 1.2837485 2.2915296
-9999 -9999 1.8246582 1.4968504 1.3897458 1.3489822 -9999 -9999
-9999 -9999 -9999 1.8401575 -9999 -9999 -9999 -9999
1 change: 1 addition & 0 deletions tests/testdata/analysis/tin_interpolation.prj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]

0 comments on commit ef90e90

Please sign in to comment.