From b515e40435efda7cfddb8d68b1c4318c0b947bad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Fri, 22 May 2020 10:03:07 +0300 Subject: [PATCH] successfully export to png --- II/Referatas/layer2img.py | 40 +++++++++++++++++++++++++++++++++++++++ II/Referatas/layer2pdf.py | 16 ---------------- 2 files changed, 40 insertions(+), 16 deletions(-) create mode 100755 II/Referatas/layer2img.py delete mode 100755 II/Referatas/layer2pdf.py diff --git a/II/Referatas/layer2img.py b/II/Referatas/layer2img.py new file mode 100755 index 0000000..f50ffa5 --- /dev/null +++ b/II/Referatas/layer2img.py @@ -0,0 +1,40 @@ +#!/usr/bin/python3 +# https://gis.stackexchange.com/questions/131716/plot-shapefile-with-matplotlib + +import argparse +import geopandas +import matplotlib.pyplot as plt + +INCH = 25.4 # mm + +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 + +def parse_args(): + parser = argparse.ArgumentParser(description='Convert layer to an image') + parser.add_argument('infile', type=str) + parser.add_argument('layer', type=str) + parser.add_argument('-o', '--output', metavar='', type=str) + parser.add_argument('--size', type=plt_size, help='Figure size in mm (WWxHH)') + return parser.parse_args() + +def main(): + args = parse_args() + f = geopandas.read_file(args.infile) + f.plot(figsize=args.size) + plt.axis('off') + plt.margins(0, 0) + plt.tight_layout(0) + if args.output: + plt.savefig(args.output, bbox_inches=0, dpi=300) + else: + plt.show() + +if __name__ == '__main__': + main() diff --git a/II/Referatas/layer2pdf.py b/II/Referatas/layer2pdf.py deleted file mode 100755 index 0fb2626..0000000 --- a/II/Referatas/layer2pdf.py +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/python3 -# https://gis.stackexchange.com/questions/131716/plot-shapefile-with-matplotlib - -import argparse -import fiona -import matplotlib.pyplot as plt - -def main(): - parser = argparse.ArgumentParser(description='Convert layer to a nice PDF') - parser.add_argument('infile', type=argparse.FileType('r')) - parser.add_argument('layer', type=str) - parser.add_argument('-o', metavar='OUTFILE', type=argparse.FileType('w')) - args = parser.parse_args() - -if __name__ == '__main__': - main()