1
1
""" Module to plot data on an Australia map """
2
2
3
- import re , os , json , tarfile
3
+ import re
4
+ import json
4
5
from pathlib import Path
5
6
6
7
import numpy as np
7
8
import pandas as pd
8
9
9
- import matplotlib as mpl
10
- mpl .use ("Agg" )
11
-
12
10
import matplotlib .pyplot as plt
13
- from matplotlib .patches import Polygon
14
- from matplotlib .collections import PatchCollection
15
11
import matplotlib .patheffects as patheff
16
12
13
+ import matplotlib as mpl
14
+ mpl .use ("Agg" )
15
+
17
16
HAS_PYSHP = False
18
17
try :
19
18
import shapefile
20
19
HAS_PYSHP = True
21
- except (ImportError , FileNotFoundError ) as err :
20
+ except (ImportError , FileNotFoundError ):
22
21
pass
23
22
24
23
# Decompress australia shoreline shapefile
25
24
FHERE = Path (__file__ ).resolve ().parent
26
25
FDATA = FHERE / "data"
27
26
SHAPEFILES = {
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" ,
32
31
"ozbasins" : FDATA / "rbasin_lines_simplified.shp"
33
- }
32
+ }
34
33
35
34
# Lat long coordinate boxes for regions in Australia
36
35
freg = FDATA / "regions.json"
39
38
40
39
# Capital cities
41
40
CAPITAL_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 ],
48
47
"Perth" : [115.86134 , - 31.95182 ]
49
- }
48
+ }
50
49
51
50
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 ):
54
53
""" plot Australian geographic layer in axes using data
55
54
from Natural Earth.
56
55
(see https://www.naturalearthdata.com/)
@@ -81,7 +80,7 @@ def ozlayer(ax, name, filter_field=None, filter_regex=None, proj=None, \
81
80
raise ValueError ("pyshp package could not be imported" )
82
81
83
82
# Select shapefile to draw
84
- if not name in SHAPEFILES :
83
+ if name not in SHAPEFILES :
85
84
names = "|" .join (list (SHAPEFILES .keys ()))
86
85
raise ValueError (f"Expected name in { names } , got { name } ." )
87
86
@@ -94,7 +93,7 @@ def ozlayer(ax, name, filter_field=None, filter_regex=None, proj=None, \
94
93
# Plotting function
95
94
def plotit (x , y , recs ):
96
95
# Project
97
- if not proj is None :
96
+ if proj is not None :
98
97
x , y = np .array ([proj (xx , yy ) for xx , yy in zip (x , y )]).T
99
98
100
99
# Plot
@@ -112,11 +111,13 @@ def plotit(x, y, recs):
112
111
# Get shapefile fields
113
112
fields = np .array ([f [0 ] for f in shp_object .fields ])[1 :]
114
113
115
- if not filter_field is None :
114
+ if filter_field is not None :
116
115
# 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 )
120
121
121
122
ifilter = np .where (fields == filter_field )[0 ][0 ]
122
123
else :
@@ -126,7 +127,7 @@ def plotit(x, y, recs):
126
127
lines = []
127
128
for shp , rec in zip (shp_object .shapes (), shp_object .records ()):
128
129
# Apply filter if needed
129
- if not ifilter is None :
130
+ if ifilter is not None :
130
131
if not re .search (filter_regex , rec [ifilter ]):
131
132
continue
132
133
@@ -159,10 +160,10 @@ def plotit(x, y, recs):
159
160
return lines
160
161
161
162
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 ):
166
167
""" plot Australian capital cities.
167
168
168
169
Parameters
@@ -213,12 +214,12 @@ def ozcities(ax, cities=None, \
213
214
elements = {}
214
215
for icity , (city , xy ) in enumerate (cities .items ()):
215
216
# Skip if filtered
216
- if not filter_regex is None :
217
+ if filter_regex is not None :
217
218
if not re .search (filter_regex , city ):
218
219
continue
219
220
220
221
xyproj = xy
221
- if not proj is None :
222
+ if proj is not None :
222
223
xyproj = proj (* xyproj )
223
224
224
225
lab = "Capital city" if icity == 0 else ""
0 commit comments