diff --git a/II/Referatas/Makefile b/II/Referatas/Makefile index 74dba76..7672367 100644 --- a/II/Referatas/Makefile +++ b/II/Referatas/Makefile @@ -38,7 +38,7 @@ sinewave.gpkg: fig2layer.py ./fig2layer.py -o $@ sine rectangle.gpkg: fig2layer.py - ./fig2layer.py -o $@ rectangle --bounds $(CROSSING) + ./fig2layer.py -o $@ rect --bounds $(CROSSING) db/.faux_ready: zeimena.gpkg sinewave.gpkg managedb -./managedb stop; rm -fr db diff --git a/II/Referatas/layer2img.py b/II/Referatas/layer2img.py index 0a0408c..a2fe677 100755 --- a/II/Referatas/layer2img.py +++ b/II/Referatas/layer2img.py @@ -25,41 +25,45 @@ def plt_size(string): def parse_args(): parser = argparse.ArgumentParser( description='Convert geopackage to an image') - group = parser.add_mutually_exclusive_group(required=True) - group.add_argument('--infile', type=str) - group.add_argument('--table', type=str) - parser.add_argument('-o', '--outfile', metavar='', type=str) + ingroup = parser.add_mutually_exclusive_group(required=True) + ingroup.add_argument('--infile') + ingroup.add_argument('--table') + parser.add_argument('-o', '--outfile', metavar='') parser.add_argument( '--size', type=plt_size, help='Figure size in mm (WWxHH)') parser.add_argument( '--clip', type=float, nargs=4, metavar=BOUNDS) - parser.add_argument( - '--rect', type=float, nargs=4, help="Overlay a rectangle", - metavar=BOUNDS) + + overlay = parser.add_mutually_exclusive_group() + overlay.add_argument('--overlay-infile', type=str) + overlay.add_argument('--overlay-table', type=str) return parser.parse_args() +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) + + def main(): args = parse_args() - if args.table: - conn = psycopg2.connect("host=127.0.0.1 dbname=osm user=osm") - sql = "SELECT geom FROM %s" % args.table - f = geopandas.read_postgis(sql, con=conn, geom_col='geom') - else: - f = geopandas.read_file(args.infile) + primary = read_layer(args.table, args.infile) + overlay = read_layer(args.overlay_table, args.overlay_infile) rc('text', usetex=True) fig, ax = plt.subplots() if args.size: fig.set_size_inches(args.size) - f.plot(ax=ax) + primary.plot(ax=ax) if c := args.clip: ax.set_xlim(left=c[0], right=c[2]) ax.set_ylim(bottom=c[1], top=c[3]) - if r := args.rect: - w, h = r[2] - r[0], r[3] - r[1] - rect = patches.Rectangle( - (r[0], r[1]), w, h, linewidth=1,edgecolor='r',facecolor='none') - ax.add_patch(rect) + + if overlay is not None: + overlay.plot(ax=ax) ax.axis('off') ax.margins(0, 0)