Skip to content

Commit

Permalink
Patch to handle invalid WMS datasets.
Browse files Browse the repository at this point in the history
  • Loading branch information
swainn committed Sep 21, 2023
1 parent 6759ddf commit fadac83
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
43 changes: 36 additions & 7 deletions gsshapyorm/orm/wms_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from datetime import datetime, timedelta
import logging
import json
import os
from zipfile import ZipFile

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
"""
Expand Down
2 changes: 1 addition & 1 deletion gsshapyorm/util/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@


def version():
return '0.3.1'
return '0.3.2'
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
'numpy',
'pandas',
'psycopg2',
'sqlalchemy',
'sqlalchemy<2',
'timezonefinder',
'pyyaml'
]
Expand Down

0 comments on commit fadac83

Please sign in to comment.