2021-04-01 17:27:49 +03:00
|
|
|
#!/usr/bin/python3
|
|
|
|
import argparse
|
|
|
|
import geopandas
|
|
|
|
import psycopg2
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
from matplotlib import rc
|
|
|
|
|
2021-04-17 21:52:08 +03:00
|
|
|
# CMAP = 'Set3' # is nice too
|
2021-04-12 10:26:20 +03:00
|
|
|
CMAP = 'tab20c'
|
2021-04-12 14:14:35 +03:00
|
|
|
|
2021-04-01 17:27:49 +03:00
|
|
|
BOUNDS = ('xmin', 'ymin', 'xmax', 'ymax')
|
2021-04-18 18:11:21 +03:00
|
|
|
INCH_MM = 25.4
|
2021-04-11 09:34:44 +03:00
|
|
|
BLACK, GREEN, ORANGE, PURPLE = '#000000', '#1b9e77', '#d95f02', '#7570b3'
|
2021-04-10 19:41:44 +03:00
|
|
|
PSQL_CREDS = "host=127.0.0.1 dbname=osm user=osm password=osm"
|
2021-04-01 17:27:49 +03:00
|
|
|
|
2021-04-18 16:38:03 +03:00
|
|
|
# see `NOTICE` in the LaTeX document; this is the width of the main text block.
|
|
|
|
TEXTWIDTH_CM = 12.12364
|
|
|
|
TEXTWIDTH_INCH = TEXTWIDTH_CM * 10 / INCH_MM
|
2021-04-01 17:27:49 +03:00
|
|
|
|
2021-04-18 20:58:42 +03:00
|
|
|
SCALES = {
|
|
|
|
"GDR10": 10000,
|
|
|
|
"GDR50": 50000,
|
|
|
|
"GDR250": 250000,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def wm_clip(string):
|
2021-04-17 21:52:08 +03:00
|
|
|
if not string:
|
|
|
|
return None
|
2021-04-18 20:58:42 +03:00
|
|
|
gdr, name = string.split(":")
|
|
|
|
if scale := SCALES.get(gdr):
|
|
|
|
return gdr, scale
|
|
|
|
scales = ",".join(SCALES.keys())
|
|
|
|
raise argparse.ArgumentTypeError("invalid scale. Expected %s" % scales)
|
2021-04-17 21:52:08 +03:00
|
|
|
|
|
|
|
|
2021-04-01 17:27:49 +03:00
|
|
|
def parse_args():
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description='Convert geopackage to an image')
|
2021-04-13 10:52:52 +03:00
|
|
|
parser.add_argument('--group1-select', required=True)
|
2021-04-10 19:40:04 +03:00
|
|
|
parser.add_argument('--group1-cmap', type=bool)
|
2021-04-13 10:36:47 +03:00
|
|
|
parser.add_argument('--group1-linestyle')
|
2021-04-01 17:27:49 +03:00
|
|
|
|
2021-04-13 10:52:52 +03:00
|
|
|
parser.add_argument('--group2-select')
|
2021-04-10 19:40:04 +03:00
|
|
|
parser.add_argument('--group2-cmap', type=bool)
|
2021-04-13 10:36:47 +03:00
|
|
|
parser.add_argument('--group2-linestyle')
|
2021-04-01 17:27:49 +03:00
|
|
|
|
2021-04-13 10:52:52 +03:00
|
|
|
parser.add_argument('--group3-select')
|
2021-04-10 19:40:04 +03:00
|
|
|
parser.add_argument('--group3-cmap', type=bool)
|
2021-04-13 10:36:47 +03:00
|
|
|
parser.add_argument('--group3-linestyle')
|
2021-04-01 20:54:20 +03:00
|
|
|
|
2021-04-18 20:58:42 +03:00
|
|
|
parser.add_argument('--wmclip',
|
|
|
|
type=wm_clip, help="Clip. E.g. GDR10:nemunas-merkys")
|
2021-04-12 14:16:27 +03:00
|
|
|
parser.add_argument('--widthdiv',
|
2021-04-13 10:36:47 +03:00
|
|
|
default=1, type=float, help='Width divisor')
|
2021-04-12 14:14:35 +03:00
|
|
|
|
2021-04-01 20:54:20 +03:00
|
|
|
parser.add_argument('-o', '--outfile', metavar='<file>')
|
2021-04-01 17:27:49 +03:00
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
2021-04-18 20:58:42 +03:00
|
|
|
def read_layer(select, width, maybe_wmclip):
|
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"
|
|
|
|
if maybe_wmclip:
|
|
|
|
name, scale = maybe_wmclip
|
|
|
|
way = "wm_clip(way, {name}, {scale}, {width})".format(
|
|
|
|
name=name,
|
|
|
|
scale=scale,
|
|
|
|
width=width,
|
|
|
|
)
|
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)
|
|
|
|
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-04-13 10:36:47 +03:00
|
|
|
def plot_args(color, maybe_cmap, maybe_linestyle):
|
|
|
|
if maybe_cmap:
|
|
|
|
r = {'cmap': CMAP}
|
|
|
|
else:
|
|
|
|
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
|
|
|
|
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 20:58:42 +03:00
|
|
|
width = TEXTWIDTH_INCH / args.widthdiv
|
|
|
|
group1 = read_layer(args.group1_select, width, args.wmclip)
|
|
|
|
group2 = read_layer(args.group2_select, width, args.wmclip)
|
|
|
|
group3 = read_layer(args.group3_select, width, args.wmclip)
|
2021-04-13 10:36:47 +03:00
|
|
|
c1 = plot_args(BLACK, args.group1_cmap, args.group1_linestyle)
|
|
|
|
c2 = plot_args(ORANGE, args.group2_cmap, args.group2_linestyle)
|
|
|
|
c3 = plot_args(GREEN, args.group3_cmap, args.group3_linestyle)
|
2021-04-01 17:27:49 +03:00
|
|
|
|
|
|
|
rc('text', usetex=True)
|
|
|
|
fig, ax = plt.subplots()
|
2021-04-18 20:58:42 +03:00
|
|
|
fig.set_figwidth(width)
|
2021-04-01 17:27:49 +03:00
|
|
|
|
2021-04-10 19:41:44 +03:00
|
|
|
group1 is not None and group1.plot(ax=ax, **c1)
|
|
|
|
group2 is not None and group2.plot(ax=ax, **c2)
|
|
|
|
group3 is not None and group3.plot(ax=ax, **c3)
|
2021-04-01 17:27:49 +03:00
|
|
|
|
|
|
|
ax.axis('off')
|
|
|
|
ax.margins(0, 0)
|
|
|
|
if args.outfile:
|
2021-04-11 20:39:38 +03:00
|
|
|
fig.savefig(args.outfile, bbox_inches='tight', dpi=600)
|
2021-04-01 17:27:49 +03:00
|
|
|
else:
|
|
|
|
plt.show()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|