stud/II/Referatas/layer2img.py

86 lines
2.4 KiB
Python
Raw Permalink Normal View History

2020-05-22 10:03:07 +03:00
#!/usr/bin/python3
import argparse
import geopandas
2020-05-22 10:19:17 +03:00
import psycopg2
2020-05-22 10:03:07 +03:00
import matplotlib.pyplot as plt
2020-05-27 10:26:59 +03:00
from matplotlib import rc, patches
2020-05-22 10:03:07 +03:00
2020-05-22 10:19:17 +03:00
INCH = 25.4 # mm
2020-05-27 10:20:03 +03:00
BOUNDS = ('xmin', 'ymin', 'xmax', 'ymax')
2020-05-27 11:11:35 +03:00
GREEN, ORANGE, PURPLE = '#1b9e77', '#d95f02', '#7570b3'
2020-05-22 10:19:17 +03:00
2020-05-22 10:03:07 +03:00
def plt_size(string):
if not string:
return None
try:
w, h = string.split("x")
return float(w) / INCH, float(h) / INCH
except Exception as e:
raise argparse.ArgumentTypeError from e
2020-05-22 10:19:17 +03:00
2020-05-22 10:03:07 +03:00
def parse_args():
2020-05-22 10:19:17 +03:00
parser = argparse.ArgumentParser(
description='Convert geopackage to an image')
2020-05-27 12:23:09 +03:00
group1 = parser.add_mutually_exclusive_group()
group1.add_argument('--group1-infile')
group1.add_argument('--group1-table')
2020-05-27 10:53:54 +03:00
parser.add_argument('-o', '--outfile', metavar='<file>')
2020-05-22 10:19:17 +03:00
parser.add_argument(
'--size', type=plt_size, help='Figure size in mm (WWxHH)')
2020-05-27 10:20:03 +03:00
parser.add_argument( '--clip', type=float, nargs=4, metavar=BOUNDS)
2020-05-27 10:53:54 +03:00
2020-05-27 12:23:09 +03:00
group2 = parser.add_mutually_exclusive_group()
group2.add_argument('--group2-infile', type=str)
group2.add_argument('--group2-table', type=str)
2020-06-18 10:03:55 +03:00
group3 = parser.add_mutually_exclusive_group()
group3.add_argument('--group3-infile', type=str)
group3.add_argument('--group3-table', type=str)
2020-05-22 10:03:07 +03:00
return parser.parse_args()
2020-05-22 10:19:17 +03:00
2020-05-27 10:53:54 +03:00
def read_layer(maybe_table, maybe_file):
if maybe_table:
conn = psycopg2.connect("host=127.0.0.1 dbname=osm user=osm")
sql = "SELECT geom FROM %s" % maybe_table
return geopandas.read_postgis(sql, con=conn, geom_col='geom')
elif maybe_file:
return geopandas.read_file(maybe_file)
2020-05-22 10:03:07 +03:00
def main():
args = parse_args()
2020-05-27 12:23:09 +03:00
group1 = read_layer(args.group1_table, args.group1_infile)
group2 = read_layer(args.group2_table, args.group2_infile)
2020-06-18 10:03:55 +03:00
group3 = read_layer(args.group3_table, args.group3_infile)
2020-05-27 10:26:59 +03:00
rc('text', usetex=True)
2020-05-26 17:08:35 +03:00
fig, ax = plt.subplots()
2020-05-26 18:33:03 +03:00
if args.size:
fig.set_size_inches(args.size)
2020-05-26 17:18:12 +03:00
if c := args.clip:
ax.set_xlim(left=c[0], right=c[2])
ax.set_ylim(bottom=c[1], top=c[3])
2020-05-27 10:53:54 +03:00
2020-05-27 12:23:09 +03:00
if group1 is not None:
group1.plot(ax=ax, color=PURPLE)
if group2 is not None:
group2.plot(ax=ax, color=ORANGE)
2020-06-18 10:03:55 +03:00
if group3 is not None:
group3.plot(ax=ax, color=GREEN)
2020-05-26 17:18:12 +03:00
2020-05-26 15:54:16 +03:00
ax.axis('off')
ax.margins(0, 0)
2020-05-26 17:08:35 +03:00
fig.tight_layout(0)
2020-05-22 10:19:17 +03:00
if args.outfile:
2020-05-26 17:08:35 +03:00
fig.savefig(args.outfile, bbox_inches=0, dpi=600)
2020-05-22 10:03:07 +03:00
else:
plt.show()
2020-05-22 10:19:17 +03:00
2020-05-22 10:03:07 +03:00
if __name__ == '__main__':
main()