add layer2img.py

This commit is contained in:
Motiejus Jakštys 2020-12-11 11:10:12 +02:00
parent 6648926a83
commit d99842b426
2 changed files with 73 additions and 0 deletions

View File

@ -9,6 +9,7 @@ Usage:
3. Run `make -j$(nproc) smooth_<X>.gpkg`. This will output a geo-package with 3. Run `make -j$(nproc) smooth_<X>.gpkg`. This will output a geo-package with
contour lines every `X` meters. X can be fractional (e.g. contour lines every `X` meters. X can be fractional (e.g.
`smooth_2.5.gpkg`). `smooth_2.5.gpkg`).
4. Optional: you may generate a raster image with `layer2img.py`.
Dependencies: Dependencies:

72
contours/layer2img.py Executable file
View File

@ -0,0 +1,72 @@
#!/usr/bin/python3
import argparse
import geopandas
import psycopg2
import matplotlib.pyplot as plt
INCH = 25.4 # mm
BROWN = '#d15c00' # CMYK: 0_56_100_18
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 geopackage to an image')
group1 = parser.add_mutually_exclusive_group()
group1.add_argument('--infile')
group1.add_argument('--table')
parser.add_argument('-o', '--outfile', metavar='<file>')
parser.add_argument(
'--dpi', type=int, help='Dots per inch', default=300)
parser.add_argument(
'--size', type=plt_size, help='Figure size in mm (WWxHH)')
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 plot_layer(layer, ax, color):
layer.plot(ax=ax, color=BROWN, linewidth=.5)
def main():
args = parse_args()
layer = read_layer(args.table, args.infile)
fig, ax = plt.subplots()
if args.size:
fig.set_size_inches(args.size)
plot_layer(layer, ax=ax, color=BROWN)
ax.axis('off')
ax.margins(0, 0)
fig.tight_layout(0)
if args.outfile:
fig.savefig(
args.outfile, bbox_inches=0, dpi=args.dpi,
pil_kwargs={'compression': 'tiff_deflate'},
)
else:
plt.show()
if __name__ == '__main__':
main()