Skip to content

Commit

Permalink
Merge pull request #1506 from greglucas/validate_ne_scale
Browse files Browse the repository at this point in the history
Validating Natural Earth scales
  • Loading branch information
QuLogic authored Apr 6, 2020
2 parents 411b98c + c7736fd commit de1cd82
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
9 changes: 9 additions & 0 deletions lib/cartopy/feature/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,20 @@ def __init__(self, category, name, scale, **kwargs):
scale = Scaler(scale)

self.scaler = scale
# Make sure this is a valid resolution
self._validate_scale()

@property
def scale(self):
return self.scaler.scale

def _validate_scale(self):
if self.scale not in ('110m', '50m', '10m'):
raise ValueError(
'{} is not a valid Natural Earth scale. '.format(self.scale) +
'Valid scales are "110m", "50m", and "10m".'
)

def geometries(self):
"""
Returns an iterator of (shapely) geometries for this feature.
Expand Down
25 changes: 10 additions & 15 deletions lib/cartopy/tests/test_features.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
# (C) British Crown Copyright 2017 - 2018, Met Office
# Copyright Cartopy Contributors
#
# This file is part of cartopy.
#
# cartopy is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cartopy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with cartopy. If not, see <https://www.gnu.org/licenses/>.
# This file is part of Cartopy and is released under the LGPL license.
# See COPYING and COPYING.LESSER in the root of the repository for full
# licensing details.

from __future__ import (absolute_import, division, print_function)

import cartopy.feature as cfeature
import pytest

small_extent = (-6, -8, 56, 59)
medium_extent = (-20, 20, 20, 60)
Expand Down Expand Up @@ -71,3 +61,8 @@ def test_intersecting_geometries_large(self, monkeypatch):
# '110m' when the extent is large and autoscale is True.
auto_land.intersecting_geometries(large_extent)
assert auto_land.scale == '110m'


def test_bad_ne_scale():
with pytest.raises(ValueError, match='not a valid Natural Earth scale'):
cfeature.NaturalEarthFeature('physical', 'land', '30m')

0 comments on commit de1cd82

Please sign in to comment.