stud/IV/layer2img.py

132 lines
3.9 KiB
Python
Raw Normal View History

2021-04-01 17:27:49 +03:00
#!/usr/bin/python3
2021-05-16 11:58:23 +03:00
"""
Convert PostGIS geometries to an image. To scale.
Accepts a few geometry fine-tuning parameters.
"""
2021-04-01 17:27:49 +03:00
import argparse
import geopandas
import psycopg2
from matplotlib import rc
2021-05-17 23:41:02 +03:00
import matplotlib.pyplot as plt
2021-04-01 17:27:49 +03:00
2021-05-15 23:38:21 +03:00
CMAP = 'tab20c' # 'Set3' # is nice too
2021-04-10 19:41:44 +03:00
PSQL_CREDS = "host=127.0.0.1 dbname=osm user=osm password=osm"
2021-05-09 14:33:53 +03:00
COLORS = {
'black': '#000000',
'green': '#1b9e77',
'orange': '#d95f02',
'purple': '#7570b3',
}
2021-05-15 23:38:21 +03:00
# see `NOTICE` in the LaTeX document; this is the width of the main text block.
TEXTWIDTH_CM = 12.12364
2021-05-09 14:33:53 +03:00
def color(string):
2021-05-15 23:42:27 +03:00
return COLORS[string if string else 'black']
2021-05-09 14:33:53 +03:00
2021-04-18 23:33:25 +03:00
def inch(cm):
return cm / 2.54
2021-04-18 20:58:42 +03:00
2021-04-17 21:52:08 +03:00
2021-04-01 17:27:49 +03:00
def parse_args():
2021-05-09 14:33:53 +03:00
kwcolor = {'type': color, 'default': 'black'}
2021-05-16 11:58:23 +03:00
parser = argparse.ArgumentParser(description=__doc__)
2021-05-17 23:41:02 +03:00
parser.add_argument('--g1-select')
parser.add_argument('--g1-linestyle')
parser.add_argument('--g1-label')
parser.add_argument('--g1-color', **kwcolor)
parser.add_argument('--g2-select')
parser.add_argument('--g2-linestyle')
parser.add_argument('--g2-label')
parser.add_argument('--g2-color', **kwcolor)
parser.add_argument('--g3-select')
parser.add_argument('--g3-linestyle')
parser.add_argument('--g3-label')
parser.add_argument('--g3-color', **kwcolor)
parser.add_argument('--legend',
help="Legend location, following matplotlib rules", default='best')
2021-05-16 11:58:23 +03:00
parser.add_argument('--widthdiv', default=1, type=float,
help="Divide the width by this number "
"(useful when two images are laid horizontally "
"in the resulting file")
parser.add_argument('--quadrant', type=int, choices=(1, 2, 3, 4),
help="Image is comprised of 4 quadrants. This variable, "
"when non-empty, will clip and return the requested quadrant")
parser.add_argument('--outfile', metavar='<file>',
help="If unset, displayed on the screen")
2021-04-01 17:27:49 +03:00
return parser.parse_args()
2021-04-28 17:23:26 +03:00
def read_layer(select, width, maybe_quadrant):
2021-04-13 10:52:52 +03:00
if not select:
2021-04-10 19:40:04 +03:00
return
2021-04-18 20:58:42 +03:00
way = "way"
2021-04-28 17:23:26 +03:00
if maybe_quadrant:
way = "wm_quadrant(way, {})".format(maybe_quadrant)
2021-04-10 19:40:04 +03:00
conn = psycopg2.connect(PSQL_CREDS)
2021-04-18 20:58:42 +03:00
sql = "SELECT {way} as way1 FROM {select}".format(way=way, select=select)
2021-05-16 23:35:38 +03:00
2021-04-18 20:58:42 +03:00
return geopandas.read_postgis(sql, con=conn, geom_col='way1')
2021-04-01 17:27:49 +03:00
2021-04-17 21:52:08 +03:00
2021-05-17 23:41:02 +03:00
def plot_args(geom, color, maybe_linestyle, maybe_label):
if geom is None:
return
2021-05-09 11:29:34 +03:00
# polygons either have fillings or lines
if geom.geom_type[0] == 'Polygon':
2021-05-09 11:29:34 +03:00
if maybe_linestyle:
return {
'edgecolor': 'black',
'linestyle': maybe_linestyle,
'color': (0, 0, 0, 0),
}
else:
2021-05-09 11:39:34 +03:00
return {'cmap': CMAP, 'alpha': .25}
r = {'color': color}
2021-04-17 20:25:42 +03:00
if maybe_linestyle == 'invisible':
r['color'] = (0, 0, 0, 0)
elif maybe_linestyle:
2021-04-13 10:36:47 +03:00
r['linestyle'] = maybe_linestyle
2021-05-17 23:41:02 +03:00
if maybe_label:
r['label'] = '\\normalfont %s' % maybe_label
2021-04-13 10:36:47 +03:00
return r
2021-04-01 17:27:49 +03:00
2021-04-17 21:52:08 +03:00
2021-04-01 17:27:49 +03:00
def main():
args = parse_args()
2021-04-18 23:33:25 +03:00
width = TEXTWIDTH_CM / args.widthdiv
2021-05-17 23:41:02 +03:00
g1 = read_layer(args.g1_select, width, args.quadrant)
g2 = read_layer(args.g2_select, width, args.quadrant)
g3 = read_layer(args.g3_select, width, args.quadrant)
c1 = plot_args(g1, args.g1_color, args.g1_linestyle, args.g1_label)
c2 = plot_args(g2, args.g2_color, args.g2_linestyle, args.g2_label)
c3 = plot_args(g3, args.g3_color, args.g3_linestyle, args.g3_label)
2021-04-01 17:27:49 +03:00
rc('text', usetex=True)
2021-05-17 23:41:02 +03:00
fig, ax = plt.subplots(constrained_layout=True)
2021-04-18 23:33:25 +03:00
fig.set_figwidth(inch(width))
2021-04-01 17:27:49 +03:00
2021-05-17 23:41:02 +03:00
g1 is not None and g1.plot(ax=ax, linewidth=.75, **c1)
g2 is not None and g2.plot(ax=ax, linewidth=.75, **c2)
g3 is not None and g3.plot(ax=ax, linewidth=.75, **c3)
2021-04-01 17:27:49 +03:00
2021-05-17 23:41:02 +03:00
ax.legend(loc=args.legend, frameon=False)
2021-04-01 17:27:49 +03:00
ax.axis('off')
ax.margins(0, 0)
if args.outfile:
fig.savefig(args.outfile, bbox_inches='tight', dpi=600)
2021-04-01 17:27:49 +03:00
else:
plt.show()
if __name__ == '__main__':
main()