11""" Module to plot data on an Australia map """
22
3- import re , os , json , tarfile
3+ import re
4+ import json
45from pathlib import Path
56
67import numpy as np
78import pandas as pd
89
9- import matplotlib as mpl
10- mpl .use ("Agg" )
11-
1210import matplotlib .pyplot as plt
13- from matplotlib .patches import Polygon
14- from matplotlib .collections import PatchCollection
1511import matplotlib .patheffects as patheff
1612
13+ import matplotlib as mpl
14+ mpl .use ("Agg" )
15+
1716HAS_PYSHP = False
1817try :
1918 import shapefile
2019 HAS_PYSHP = True
21- except (ImportError , FileNotFoundError ) as err :
20+ except (ImportError , FileNotFoundError ):
2221 pass
2322
2423# Decompress australia shoreline shapefile
2524FHERE = Path (__file__ ).resolve ().parent
2625FDATA = FHERE / "data"
2726SHAPEFILES = {
28- "ozcoast10m" : FDATA / "ne_10m_admin_0_countries_australia.shp" , \
29- "ozcoast50m" : FDATA / "ne_50m_admin_0_countries_australia.shp" , \
30- "ozstates50m" : FDATA / "ne_50m_admin_1_states_australia.shp" , \
31- "ozdrainage" : FDATA / "drainage_divisions_lines_simplified.shp" , \
27+ "ozcoast10m" : FDATA / "ne_10m_admin_0_countries_australia.shp" ,
28+ "ozcoast50m" : FDATA / "ne_50m_admin_0_countries_australia.shp" ,
29+ "ozstates50m" : FDATA / "ne_50m_admin_1_states_australia.shp" ,
30+ "ozdrainage" : FDATA / "drainage_divisions_lines_simplified.shp" ,
3231 "ozbasins" : FDATA / "rbasin_lines_simplified.shp"
33- }
32+ }
3433
3534# Lat long coordinate boxes for regions in Australia
3635freg = FDATA / "regions.json"
3938
4039# Capital cities
4140CAPITAL_CITIES = {
42- "Brisbane" : [153.026 , - 27.471 ], \
43- "Melbourne" : [144.960 , - 37.821 ],\
44- "Sydney" : [151.206 , - 33.864 ], \
45- "Canberra" : [149.134 , - 35.299 ], \
46- "Hobart" : [147.3265 , - 42.8818 ], \
47- "Adelaide" : [138.60 , - 34.92833 ], \
41+ "Brisbane" : [153.026 , - 27.471 ],
42+ "Melbourne" : [144.960 , - 37.821 ],
43+ "Sydney" : [151.206 , - 33.864 ],
44+ "Canberra" : [149.134 , - 35.299 ],
45+ "Hobart" : [147.3265 , - 42.8818 ],
46+ "Adelaide" : [138.60 , - 34.92833 ],
4847 "Perth" : [115.86134 , - 31.95182 ]
49- }
48+ }
5049
5150
52- def ozlayer (ax , name , filter_field = None , filter_regex = None , proj = None , \
53- fixed_lim = True , * args , ** kwargs ):
51+ def ozlayer (ax , name , filter_field = None , filter_regex = None , proj = None ,
52+ fixed_lim = True , * args , ** kwargs ):
5453 """ plot Australian geographic layer in axes using data
5554 from Natural Earth.
5655 (see https://www.naturalearthdata.com/)
@@ -81,7 +80,7 @@ def ozlayer(ax, name, filter_field=None, filter_regex=None, proj=None, \
8180 raise ValueError ("pyshp package could not be imported" )
8281
8382 # Select shapefile to draw
84- if not name in SHAPEFILES :
83+ if name not in SHAPEFILES :
8584 names = "|" .join (list (SHAPEFILES .keys ()))
8685 raise ValueError (f"Expected name in { names } , got { name } ." )
8786
@@ -94,7 +93,7 @@ def ozlayer(ax, name, filter_field=None, filter_regex=None, proj=None, \
9493 # Plotting function
9594 def plotit (x , y , recs ):
9695 # Project
97- if not proj is None :
96+ if proj is not None :
9897 x , y = np .array ([proj (xx , yy ) for xx , yy in zip (x , y )]).T
9998
10099 # Plot
@@ -112,11 +111,13 @@ def plotit(x, y, recs):
112111 # Get shapefile fields
113112 fields = np .array ([f [0 ] for f in shp_object .fields ])[1 :]
114113
115- if not filter_field is None :
114+ if filter_field is not None :
116115 # Filter field
117- if not filter_field in fields :
118- raise ValueError ("Expected filter_field in " + \
119- "/" .join (list (fields )) + ", got " + filter_field )
116+ if filter_field not in fields :
117+ ftxt = "/" .join (list (fields ))
118+ errmess = f"Expected filter_field in { ftxt } , " \
119+ + f"got { filter_field } ."
120+ raise ValueError (errmess )
120121
121122 ifilter = np .where (fields == filter_field )[0 ][0 ]
122123 else :
@@ -126,7 +127,7 @@ def plotit(x, y, recs):
126127 lines = []
127128 for shp , rec in zip (shp_object .shapes (), shp_object .records ()):
128129 # Apply filter if needed
129- if not ifilter is None :
130+ if ifilter is not None :
130131 if not re .search (filter_regex , rec [ifilter ]):
131132 continue
132133
@@ -159,10 +160,10 @@ def plotit(x, y, recs):
159160 return lines
160161
161162
162- def ozcities (ax , cities = None , \
163- filter_regex = None , \
164- fixed_lim = True , plot_kwargs = {}, \
165- text_kwargs = {}, proj = None ):
163+ def ozcities (ax , cities = None ,
164+ filter_regex = None ,
165+ fixed_lim = True , plot_kwargs = {},
166+ text_kwargs = {}, proj = None ):
166167 """ plot Australian capital cities.
167168
168169 Parameters
@@ -213,12 +214,12 @@ def ozcities(ax, cities=None, \
213214 elements = {}
214215 for icity , (city , xy ) in enumerate (cities .items ()):
215216 # Skip if filtered
216- if not filter_regex is None :
217+ if filter_regex is not None :
217218 if not re .search (filter_regex , city ):
218219 continue
219220
220221 xyproj = xy
221- if not proj is None :
222+ if proj is not None :
222223 xyproj = proj (* xyproj )
223224
224225 lab = "Capital city" if icity == 0 else ""
0 commit comments