Skip to content

Commit

Permalink
disable redundant shapely.geos logging
Browse files Browse the repository at this point in the history
  • Loading branch information
aperrin66 committed Mar 5, 2024
1 parent 0c56fd5 commit c62a02c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
18 changes: 13 additions & 5 deletions geospaas_harvesting/providers/earthdata_cmr.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Code for searching EarthData CMR (https://www.earthdata.nasa.gov/)"""
import json
import logging

import shapely.errors
from shapely.geometry import LineString, Point, Polygon
Expand Down Expand Up @@ -66,16 +67,23 @@ class EarthDataSpatialArgument(WKTArgument):
"""
def parse(self, value):
valid_prefixes = ('polygon', 'bounding_box', 'point', 'line', 'circle')
geos_logger = logging.getLogger('shapely.geos')
result = None
try:
return super().parse(value)
geos_logger.disabled = True
result = super().parse(value)
geos_logger.disabled = False
except (shapely.errors.ShapelyError, ValueError) as error:
if isinstance(value, str):
for prefix in valid_prefixes:
if value.startswith(f"{prefix}=") or value.startswith(f"{prefix}[]="):
return value
raise ValueError(
"location should be a geometry or a valid CMR spatial parameter"
) from error
result = value
break
if result is None:
raise ValueError(
"location should be a geometry or a valid CMR spatial parameter"
) from error
return result


class EarthDataCMRCrawler(HTTPPaginatedAPICrawler):
Expand Down
23 changes: 11 additions & 12 deletions tests/providers/test_earthdata_cmr.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,17 @@ def test_parse_wkt(self):

def test_parse_raw_spatial_extent(self):
"""Test parsing raw CMR spatial extent parameters"""
with self.assertLogs('shapely.geos'):
self.assertEqual(self.argument.parse('bounding_box=-180,60,180,90'),
'bounding_box=-180,60,180,90')
self.assertEqual(self.argument.parse('polygon=1.0,2.0,2.0,3.0,3.0,4.0,1.0,2.0'),
'polygon=1.0,2.0,2.0,3.0,3.0,4.0,1.0,2.0')
self.assertEqual(self.argument.parse('line=1.0,2.0,3.0,4.0'), 'line=1.0,2.0,3.0,4.0')
self.assertEqual(self.argument.parse('point=1.0,2.0'), 'point=1.0,2.0')

with self.assertRaises(ValueError):
self.argument.parse('foo=1.0,2.0')
with self.assertRaises(ValueError):
self.argument.parse('point:1.0,2.0')
self.assertEqual(self.argument.parse('bounding_box=-180,60,180,90'),
'bounding_box=-180,60,180,90')
self.assertEqual(self.argument.parse('polygon=1.0,2.0,2.0,3.0,3.0,4.0,1.0,2.0'),
'polygon=1.0,2.0,2.0,3.0,3.0,4.0,1.0,2.0')
self.assertEqual(self.argument.parse('line=1.0,2.0,3.0,4.0'), 'line=1.0,2.0,3.0,4.0')
self.assertEqual(self.argument.parse('point=1.0,2.0'), 'point=1.0,2.0')

with self.assertRaises(ValueError):
self.argument.parse('foo=1.0,2.0')
with self.assertRaises(ValueError):
self.argument.parse('point:1.0,2.0')


class EarthdataCMRCrawlerTestCase(unittest.TestCase):
Expand Down

0 comments on commit c62a02c

Please sign in to comment.