stud/II/Referatas/layer2img.py

73 lines
2.1 KiB
Python
Raw Normal View History

2020-05-22 10:03:07 +03:00
#!/usr/bin/python3
# https://gis.stackexchange.com/questions/131716/plot-shapefile-with-matplotlib
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-22 10:19:17 +03:00
INCH = 25.4 # mm
2020-05-22 10:03:07 +03:00
def plt_size(string):
if not string:
2020-05-22 10:19:17 +03:00
# using default matplotlib dimensions
2020-05-22 10:03:07 +03:00
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')
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='<file>', type=str)
parser.add_argument(
'--size', type=plt_size, help='Figure size in mm (WWxHH)')
2020-05-26 15:54:16 +03:00
parser.add_argument(
'--clip', type=float, nargs=4,
metavar=('xmin', 'ymin', 'xmax', 'ymax'))
2020-05-22 10:03:07 +03:00
return parser.parse_args()
2020-05-22 10:19:17 +03:00
2020-05-26 15:54:16 +03:00
#def makebox(t):
# xmin, ymin, xmax, ymax = t
# if xmin > xmax:
# raise Exception("xmin should be > xmax")
# if ymin > ymax:
# raise Exception("ymin should be > ymax")
# left, bottom = xmin, ymin
# width, height = xmax - xmin, ymax - ymin
# return {'left': left, 'bottom': bottom, 'width': width, 'height': height}
2020-05-22 10:03:07 +03:00
def main():
args = parse_args()
2020-05-22 10:19:17 +03:00
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)
2020-05-22 10:03:07 +03:00
f.plot(figsize=args.size)
2020-05-26 15:54:16 +03:00
ax = plt.gca()
if args.clip:
ax.set_ylim(bottom=args.clip[1], top=args.clip[3])
ax.set_xlim(left=args.clip[0], right=args.clip[2])
ax.axis('off')
ax.margins(0, 0)
2020-05-22 10:03:07 +03:00
plt.tight_layout(0)
2020-05-22 10:19:17 +03:00
if args.outfile:
plt.savefig(args.outfile, bbox_inches=0, dpi=300)
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()