Skip to content

Commit bf28d4a

Browse files
authored
feat: added option to define cities manually in ozcities (#21)
* feat: added option to define cities manually in ozcities * test: added manual city test
1 parent 192f843 commit bf28d4a

File tree

2 files changed

+63
-6
lines changed

2 files changed

+63
-6
lines changed

hydrodiy/gis/oz.py

+28-4
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ def plotit(x, y, recs):
159159
return lines
160160

161161

162-
def ozcities(ax, filter_regex=None, \
162+
def ozcities(ax, cities=None, \
163+
filter_regex=None, \
163164
fixed_lim=True, plot_kwargs={}, \
164165
text_kwargs={}, proj=None):
165166
""" plot Australian capital cities.
@@ -168,10 +169,33 @@ def ozcities(ax, filter_regex=None, \
168169
-----------
169170
ax : matplotlib.axes
170171
Axe to draw data on
171-
Shapefile field to filter on.
172+
Shapefile field to filter on.
173+
cities : dict
174+
Dictionnary containing cities and their coordinates:
175+
{ city_name: [x, y] }
172176
fiter_regex : str
173-
Regular expression to filter cities.
177+
Regular expression to filter city name.
178+
plot_kwargs : dict
179+
Argument passed to plot the point representing a city.
180+
text_kwargs : dict
181+
Argument passed to write the city name.
182+
This can be used to adjust label placement. For example,
183+
the following can be used to add shadow (path effect) and
184+
offset the labels:
185+
186+
text_kwargs = dict(
187+
path_effects=[pe.withStroke(linewidth=3, foreground="w")], \
188+
textcoords="offset pixels",\
189+
fontsize=12, \
190+
xytext=(20, 8)
191+
)
192+
proj : pyproj.CRS
193+
Projec coordinates.
194+
174195
"""
196+
if cities is None:
197+
cities = CAPITAL_CITIES
198+
175199
# Plot options
176200
plot_kwargs["marker"] = plot_kwargs.get("marker", "s")
177201
plot_kwargs["mfc"] = plot_kwargs.get("mfc", "tab:orange")
@@ -187,7 +211,7 @@ def ozcities(ax, filter_regex=None, \
187211
xlim, ylim = ax.get_xlim(), ax.get_ylim()
188212

189213
elements = {}
190-
for icity, (city, xy) in enumerate(CAPITAL_CITIES.items()):
214+
for icity, (city, xy) in enumerate(cities.items()):
191215
# Skip if filtered
192216
if not filter_regex is None:
193217
if not re.search(filter_regex, city):

hydrodiy/gis/tests/test_hygis_oz.py

+35-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from pathlib import Path
33
import pytest
44

5-
from hydrodiy.gis.oz import REGIONS, HAS_PYSHP, ozlayer, ozcities
5+
from hydrodiy.gis.oz import REGIONS, HAS_PYSHP, ozlayer, ozcities, CAPITAL_CITIES
66
from hydrodiy.plot import putils
77

88
import numpy as np
@@ -80,7 +80,7 @@ def test_ozlayer_proj():
8080
plt.savefig(fp)
8181

8282

83-
def test_ozcities():
83+
def test_ozcities_kw():
8484
""" Test plotting oz cities """
8585
plt.close("all")
8686
fig, ax = plt.subplots()
@@ -141,6 +141,39 @@ def test_ozcities_proj():
141141
plt.savefig(fp)
142142

143143

144+
def test_ozcities_manual():
145+
""" Test plotting oz cities using filter """
146+
plt.close("all")
147+
fig, ax = plt.subplots()
148+
lines = ozlayer(ax, "ozcoast50m", \
149+
color="k", lw=0.5, fixed_lim=False)
150+
151+
cities = dict(
152+
Sydney=[-33.86785, 151.20732], \
153+
Melbourne=[ -37.814, 144.96332], \
154+
Brisbane=[-27.46794, 153.02809], \
155+
Perth=[-31.95224, 115.8614], \
156+
Adelaide=[-34.92866, 138.59863], \
157+
Canberra=[-35.28346, 149.12807], \
158+
Newcastle=[-32.92953, 151.7801], \
159+
Wollongong=[-34.424, 150.89345], \
160+
Geelong=[-38.14711, 144.36069], \
161+
Hobart=[-42.87936, 147.32941], \
162+
Townsville=[-19.26639, 146.80569], \
163+
Cairns=[-16.92366, 145.76613], \
164+
Ballarat=[-37.56622, 143.84957], \
165+
Toowoomba=[-27.56056, 151.95386], \
166+
Darwin=[-12.46113, 130.84185], \
167+
Mandurah=[-32.5269, 115.7217], \
168+
Mackay=[-21.15345, 149.16554], \
169+
Bundaberg=[-24.86621, 152.3479]
170+
)
171+
c = {n: (cc[1], cc[0]) for n, cc in cities.items()}
172+
elems = ozcities(ax, cities=c)
173+
174+
fp = FIMG / "ozcities_manual.png"
175+
plt.savefig(fp)
176+
144177

145178

146179

0 commit comments

Comments
 (0)