forked from Unidata/MetPy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Unidata#927 from jrleeman/county_features
Add US County features
- Loading branch information
Showing
15 changed files
with
131 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Copyright (c) 2018 MetPy Developers. | ||
# Distributed under the terms of the BSD 3-Clause License. | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
""" | ||
US Counties | ||
=========== | ||
Demonstrate how to plot US counties at all three available resolutions. | ||
""" | ||
import cartopy.crs as ccrs | ||
import matplotlib.pyplot as plt | ||
|
||
from metpy.plots import USCOUNTIES | ||
|
||
########################################### | ||
|
||
proj = ccrs.LambertConformal(central_longitude=-85.0, central_latitude=45.0) | ||
|
||
fig = plt.figure(figsize=(12, 9)) | ||
ax1 = fig.add_subplot(1, 3, 1, projection=proj) | ||
ax2 = fig.add_subplot(1, 3, 2, projection=proj) | ||
ax3 = fig.add_subplot(1, 3, 3, projection=proj) | ||
|
||
for scale, axis in zip(['20m', '5m', '500k'], [ax1, ax2, ax3]): | ||
axis.set_extent([270.25, 270.9, 38.15, 38.75], ccrs.Geodetic()) | ||
axis.add_feature(USCOUNTIES.with_scale(scale), edgecolor='black') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Copyright (c) 2018 MetPy Developers. | ||
# Distributed under the terms of the BSD 3-Clause License. | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
"""Cartopy specific mapping utilities.""" | ||
|
||
import cartopy.feature as cfeat | ||
import cartopy.io.shapereader as shpreader | ||
|
||
from ..cbook import get_test_data | ||
|
||
|
||
class USCountiesFeature(cfeat.NaturalEarthFeature): | ||
"""A simple interface to US County shapefiles.""" | ||
|
||
def __init__(self, scale, **kwargs): | ||
"""Create USCountiesFeature instance.""" | ||
super(USCountiesFeature, self).__init__('', 'us_counties', scale, **kwargs) | ||
|
||
def geometries(self): | ||
"""Return an iterator of (shapely) geometries for this feature.""" | ||
# Ensure that the associated files are in the cache | ||
fname = 'us_counties_{}'.format(self.scale) | ||
for extension in ['.dbf', '.shx']: | ||
get_test_data(fname + extension) | ||
path = get_test_data(fname + '.shp', as_file_obj=False) | ||
return iter(tuple(shpreader.Reader(path).geometries())) | ||
|
||
def with_scale(self, new_scale): | ||
""" | ||
Return a copy of the feature with a new scale. | ||
Parameters | ||
---------- | ||
new_scale | ||
The new dataset scale, i.e. one of '500k', '5m', or '20m'. | ||
Corresponding to 1:500,000, 1:5,000,000, and 1:20,000,000 | ||
respectively. | ||
""" | ||
return USCountiesFeature(new_scale, **self.kwargs) | ||
|
||
|
||
USCOUNTIES = USCountiesFeature('20m', facecolor='None') |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Copyright (c) 2018 MetPy Developers. | ||
# Distributed under the terms of the BSD 3-Clause License. | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
"""Test the cartopy utilities.""" | ||
|
||
import cartopy.crs as ccrs | ||
import matplotlib | ||
import matplotlib.pyplot as plt | ||
import pytest | ||
|
||
from metpy.plots import USCOUNTIES | ||
# Fixtures to make sure we have the right backend and consistent round | ||
from metpy.testing import patch_round, set_agg_backend # noqa: F401, I202 | ||
|
||
MPL_VERSION = matplotlib.__version__[:3] | ||
|
||
|
||
@pytest.mark.mpl_image_compare(tolerance=0.053, remove_text=True) | ||
def test_us_county_defaults(): | ||
"""Test the default US county plotting.""" | ||
proj = ccrs.LambertConformal(central_longitude=-85.0, central_latitude=45.0) | ||
|
||
fig = plt.figure(figsize=(12, 9)) | ||
ax = fig.add_subplot(1, 1, 1, projection=proj) | ||
ax.set_extent([270.25, 270.9, 38.15, 38.75], ccrs.Geodetic()) | ||
ax.add_feature(USCOUNTIES, edgecolor='black') | ||
return fig | ||
|
||
|
||
@pytest.mark.mpl_image_compare(tolerance=0.092, remove_text=True) | ||
def test_us_county_scales(): | ||
"""Test US county plotting with all scales.""" | ||
proj = ccrs.LambertConformal(central_longitude=-85.0, central_latitude=45.0) | ||
|
||
fig = plt.figure(figsize=(12, 9)) | ||
ax1 = fig.add_subplot(1, 3, 1, projection=proj) | ||
ax2 = fig.add_subplot(1, 3, 2, projection=proj) | ||
ax3 = fig.add_subplot(1, 3, 3, projection=proj) | ||
|
||
for scale, axis in zip(['20m', '5m', '500k'], [ax1, ax2, ax3]): | ||
axis.set_extent([270.25, 270.9, 38.15, 38.75], ccrs.Geodetic()) | ||
axis.add_feature(USCOUNTIES.with_scale(scale), edgecolor='black') | ||
return fig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.