successfully export to png

This commit is contained in:
Motiejus Jakštys 2020-05-22 10:03:07 +03:00
parent 2fbbc78233
commit b515e40435
2 changed files with 40 additions and 16 deletions

40
II/Referatas/layer2img.py Executable file
View File

@ -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='<file>', 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()

View File

@ -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()