diff --git a/gsshapyorm/orm/wms_dataset.py b/gsshapyorm/orm/wms_dataset.py index 6921379..994d6e1 100644 --- a/gsshapyorm/orm/wms_dataset.py +++ b/gsshapyorm/orm/wms_dataset.py @@ -11,6 +11,7 @@ from datetime import datetime, timedelta import logging +import json import os from zipfile import ZipFile @@ -369,14 +370,17 @@ def _read(self, directory, filename, session, path, name, extension, spatial, sp # If spatial is enabled create PostGIS rasters if spatial: + # Validate array + if not self._cellArrayIsValid(filename, timeStep, rows, columns, timeStepRaster): + # Save raster text as fallback + wmsRasterDatasetFile.rasterText = timeStepRaster['rasterText'] + continue + # Process the values/cell array - wmsRasterDatasetFile.raster = RasterLoader.makeSingleBandWKBRaster(session, - columns, rows, - upperLeftX, upperLeftY, - cellSizeX, cellSizeY, - 0, 0, - spatialReferenceID, - timeStepRaster['cellArray']) + wmsRasterDatasetFile.raster = RasterLoader.makeSingleBandWKBRaster( + session, columns, rows, upperLeftX, upperLeftY, cellSizeX, cellSizeY, + 0, 0, spatialReferenceID, timeStepRaster['cellArray'] + ) # Otherwise, set the raster text properties else: @@ -488,6 +492,31 @@ def _assembleRasterParams(self, projectFile, rasters): return timeStampedRasters + def _cellArrayIsValid(self, filename, timeStep, rows, columns, timeStepRaster): + """Compare the number of rows and columns in the cell array to the expected number of rows and columns.""" + cellArray = json.loads(timeStepRaster['cellArray']) + irows = int(rows) + icolumns = int(columns) + + if len(cellArray) != irows: + print(f'WARNING: The raster for timestep {timeStep} from WMS Dataset "{filename}" is invalid. ' + f'The number of rows in the raster ({len(cellArray)}) ' + f'does not match number of rows expected ({irows}).') + return False + + elif any([len(row) != icolumns for row in cellArray]): + bad_count = None + for row in cellArray: + if len(row) != icolumns: + bad_count = len(row) + break + print(f'WARNING: The raster for timestep {timeStep} from WMS Dataset "{filename}" is invalid. ' + f'The number of columns in at least one of the rows in the raster ({bad_count}) ' + f'does not match number of columns expected ({icolumns}).') + return False + + return True + class WMSDatasetRaster(DeclarativeBase, RasterObjectBase): """ diff --git a/gsshapyorm/util/metadata.py b/gsshapyorm/util/metadata.py index f780a40..a4c02b4 100644 --- a/gsshapyorm/util/metadata.py +++ b/gsshapyorm/util/metadata.py @@ -9,4 +9,4 @@ def version(): - return '0.3.1' + return '0.3.2' diff --git a/setup.py b/setup.py index a61c8f9..c4fbf37 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ 'numpy', 'pandas', 'psycopg2', - 'sqlalchemy', + 'sqlalchemy<2', 'timezonefinder', 'pyyaml' ]