Commit MetaInfo

Revisión0cca9132ca1c65a73545fb2ec0097b6e66458cef (tree)
Tiempo2022-06-30 11:10:27
Autorrdnielsen
Commiterrdnielsen

Log Message

Modified options and operation of mapdata_2.py

Cambiar Resumen

Diferencia incremental

diff -r 9e5a0b6dea22 -r 0cca9132ca1c mapdata/mapdata_2.py
--- a/mapdata/mapdata_2.py Sun Jan 16 11:13:48 2022 -0800
+++ b/mapdata/mapdata_2.py Wed Jun 29 19:10:27 2022 -0700
@@ -1,8 +1,9 @@
11 #!/usr/bin/python
2+#
23 # mapdata.py
34 #
45 # PURPOSE
5-# Create a simple static map of data points that can be saved to a static
6+# Create a simple map of data points that can be saved to a static
67 # image file.
78 #
89 # COPYRIGHT AND LICENSE
@@ -27,10 +28,15 @@
2728 # Date Remarks
2829 # ---------- -----------------------------------------------------
2930 # 2022-01-15 Created. RDN.
31+# 2022-06-28 Modified background selection and image creation,
32+# using code from map_cormix.py. RDN.
33+# 2022-06-29 Fixes and tweaks. Added 'annot_zoom', 'width', and
34+# 'height' as parameters to make_map() and as
35+# command-line options. RDN.
3036 # ==================================================================
3137
32-__version__ = "0.2.0"
33-__vdate = "2022-01-16"
38+__version__ = "0.3.0"
39+__vdate = "2022-06-28"
3440
3541
3642 import argparse
@@ -40,24 +46,79 @@
4046 import cartopy.io.img_tiles as cimgt
4147
4248
49+def geo_bounds(df, lat, lon):
50+ # Return min, max X (lon) and Y (lat)
51+ x = list(df[lon])
52+ y = list(df[lat])
53+ return min(x), max(x), min(y), max(y)
54+
55+def zoomedbounds(bounds, zoom=2):
56+ # bounds is a tuple of xmin, xmax, ymin, ymax.
57+ xrange = bounds[1] - bounds[0]
58+ yrange = bounds[3] - bounds[2]
59+ xmid = (bounds[0] + bounds[1]) / 2
60+ ymid = (bounds[2] + bounds[3]) / 2
61+ if yrange > xrange:
62+ ofs = zoom * (ymid - bounds[2])
63+ else:
64+ ofs = zoom * (xmid - bounds[0])
65+ return xmid-ofs, xmid+ofs, ymid-ofs, ymid+ofs
66+
4367
4468 def make_map(points_dataframe, lat="y_coord", lon="x_coord", name="location_id",
45- basemap_layer="carto-positron", outfile_name="location_map.png",
46- outwidth= 1600, outheight=1600, show=False):
47- plt.figure(figsize=(8,6))
48- stamen_terrain = cimgt.Stamen('terrain_background')
49- mapaxes = plt.axes(projection=stamen_terrain.crs)
50- plt.scatter(
51- x = points[lon],
52- y = points[lat],
69+ background="OSM", outfile_name="location_map.png", width=8.0, height=8.0,
70+ data_zoom=2, annot_zoom=7, show=False):
71+ if background == "OSM":
72+ bkgd = cimgt.OSM()
73+ elif background == "GoogleStreet":
74+ bkgd = cimgt.GoogleTiles(style="street")
75+ elif background == "GoogleSatellite":
76+ bkgd = cimgt.GoogleTiles(style="satellite")
77+ elif background == "GoogleTerrain":
78+ bkgd = cimgt.GoogleTiles(style="terrain")
79+ else:
80+ bkgd = cimgt.Stamen(background)
81+ fig = plt.figure(figsize=(width, height))
82+ ax = fig.add_subplot(1, 1, 1, projection=bkgd.crs)
83+ fig.subplots_adjust(left=0, right=1, bottom=0, top=1)
84+ bounds = zoomedbounds(geo_bounds(points_dataframe, lat, lon), data_zoom)
85+ ax.set_extent([*bounds], crs=ccrs.PlateCarree())
86+ sc = ax.scatter(
87+ x = points_dataframe[lon],
88+ y = points_dataframe[lat],
5389 color = "red",
5490 s = 4,
5591 alpha = 0.6,
56- transform = stamen_terrain.crs
92+ transform = ccrs.PlateCarree()
5793 )
94+ ax.add_image(bkgd, annot_zoom)
5895 if outfile_name is not None:
5996 plt.savefig(outfile_name)
6097 if show:
98+ # Hover code from https://stackoverflow.com/questions/7908636/how-to-add-hovering-annotations-in-matplotlib
99+ annot = ax.annotate("", xy=(0,0), xytext=(20,20), textcoords="offset points",
100+ bbox=dict(boxstyle="round", fc="w"),
101+ arrowprops=dict(arrowstyle="->"))
102+ annot.set_visible(False)
103+ def update_annot(ind):
104+ pos = sc.get_offsets()[ind["ind"][0]]
105+ annot.xy = pos
106+ text = " ".join(points_dataframe[name][n] for n in ind["ind"])
107+ annot.set_text(text)
108+ annot.get_bbox_patch().set_alpha(0.4)
109+ def hover(event):
110+ vis = annot.get_visible()
111+ if event.inaxes == ax:
112+ cont, ind = sc.contains(event)
113+ if cont:
114+ update_annot(ind)
115+ annot.set_visible(True)
116+ fig.canvas.draw_idle()
117+ else:
118+ if vis:
119+ annot.set_visible(False)
120+ fig.canvas.draw_idle()
121+ fig.canvas.mpl_connect("motion_notify_event", hover)
61122 plt.show()
62123
63124
@@ -71,22 +132,26 @@
71132 help="The name of a data file containing latitude and longitude coordinates, and an identifier")
72133 parser.add_argument('output_image_file',
73134 help="The name of the image file to create")
74- parser.add_argument('-y', '--lat', default='y_coord', dest='lat',
75- help="The name of the column in the data file containg latitude values")
76- parser.add_argument('-x', '--lon', default='x_coord', dest='lon',
77- help="The name of the column in the data file containing longitude values")
135+ parser.add_argument('-a', '--annot_zoom', type=int, default=7,
136+ help="Map annotation zoom level; good values are 6-8.")
137+ parser.add_argument('-b', '--background', choices=['OSM', 'terrain', 'toner', 'watercolor',
138+ 'GoogleStreet', 'GoogleSatellite', 'GoogleTerrain'],
139+ default='OSM',
140+ help='The name of the background layer to use.')
78141 parser.add_argument('-i', '--identifier', default='location_id', dest='id',
79142 help="The name of the column in the data file containing location identifiers")
80- parser.add_argument('-b', '--baselayer', choices=['open-street-map', 'carto-positron',
81- 'carto-darkmatter', 'stamen-terrain', 'stamen-toner', 'stamen-watercolor'],
82- default='carto-positron', dest='baselayer',
83- help="The name of the basemap layer to use")
84- parser.add_argument('-w', '--width', type=int, default=1600, dest='width',
85- help="The width of the output image, in pixels")
86- parser.add_argument('-t', '--height', type=int, default=1600, dest='height',
87- help="The height of the output image, in pixels")
88- parser.add_argument('-s', '--show', type=bool, default=True, dest='show',
89- help="Whether or not to show the map in the browser as well as saving it to a file")
143+ parser.add_argument('-s', '--show', default=False, action='store_true', dest='show',
144+ help="Whether or not to display the map as well as saving it to a file")
145+ parser.add_argument('-t', '--height', type=float, default=8.0,
146+ help="The height of the figure, in inches")
147+ parser.add_argument('-w', '--width', type=float, default=8.0,
148+ help="The width of the figure, in inches")
149+ parser.add_argument('-x', '--lon', default='x_coord', dest='lon',
150+ help="The name of the column in the data file containing longitude values")
151+ parser.add_argument('-y', '--lat', default='y_coord', dest='lat',
152+ help="The name of the column in the data file containg latitude values")
153+ parser.add_argument('-z', '--data_zoom', type=float, default=2.0,
154+ help="With a data zoom factor of 1.0, the points will span the image; larger numbers zoom out.")
90155
91156 return parser
92157
@@ -95,30 +160,11 @@
95160 def main():
96161 p= clparser().parse_args()
97162 pts = pd.read_csv(p.point_data_file)
98- make_map(pts, lat=p.lat, lon=p.lon, name=p.id, basemap_layer=p.baselayer,
99- outfile_name=p.output_image_file, outwidth=p.width, outheight=p.height, show=p.show)
163+ make_map(pts, lat=p.lat, lon=p.lon, name=p.id, background=p.background,
164+ outfile_name=p.output_image_file, width=p.width, height=p.height,
165+ data_zoom=p.data_zoom, annot_zoom=p.annot_zoom, show=p.show)
100166
101167
102168 main()
103169
104170
105-#try:
106-# main()
107-#except SystemExit as x:
108-# raise
109-#except ErrInfo as e:
110-# exit_now(1, e)
111-#except ConfigError as e:
112-# strace = traceback.extract_tb(sys.exc_info()[2])[-1:]
113-# lno = strace[0][1]
114-# sys.exit(u"Configuration error on line %d of execsql.py: %s" % (lno, e.value))
115-#except Exception:
116-# strace = traceback.extract_tb(sys.exc_info()[2])[-1:]
117-# lno = strace[0][1]
118-# msg1 = u"%s: Uncaught exception %s (%s) on line %s" % (os.path.basename(sys.argv[0]), sys.exc_info()[0], sys.exc_info()[1], lno)
119-# script, lno = current_script_line()
120-# if script is not None:
121-# msg1 = msg1 + " in script %s, line %d" % (script, lno)
122-# exit_now(1, ErrInfo("exception", exception_msg=msg1))
123-
124-
Show on old repository browser