stud

study spacejunk
Log | Files | Refs | LICENSE

layer2img.py (2496B) - Raw


      1 #!/usr/bin/python3
      2 import argparse
      3 import geopandas
      4 import psycopg2
      5 import matplotlib.pyplot as plt
      6 
      7 from matplotlib import rc, patches
      8 
      9 INCH = 25.4  # mm
     10 BOUNDS = ('xmin', 'ymin', 'xmax', 'ymax')
     11 GREEN, ORANGE, PURPLE = '#1b9e77', '#d95f02', '#7570b3'
     12 
     13 
     14 def plt_size(string):
     15     if not string:
     16         return None
     17     try:
     18         w, h = string.split("x")
     19         return float(w) / INCH, float(h) / INCH
     20     except Exception as e:
     21         raise argparse.ArgumentTypeError from e
     22 
     23 
     24 def parse_args():
     25     parser = argparse.ArgumentParser(
     26             description='Convert geopackage to an image')
     27     group1 = parser.add_mutually_exclusive_group()
     28     group1.add_argument('--group1-infile')
     29     group1.add_argument('--group1-table')
     30     parser.add_argument('-o', '--outfile', metavar='<file>')
     31     parser.add_argument(
     32             '--size', type=plt_size, help='Figure size in mm (WWxHH)')
     33     parser.add_argument( '--clip', type=float, nargs=4, metavar=BOUNDS)
     34 
     35     group2 = parser.add_mutually_exclusive_group()
     36     group2.add_argument('--group2-infile', type=str)
     37     group2.add_argument('--group2-table', type=str)
     38 
     39     group3 = parser.add_mutually_exclusive_group()
     40     group3.add_argument('--group3-infile', type=str)
     41     group3.add_argument('--group3-table', type=str)
     42     return parser.parse_args()
     43 
     44 
     45 def read_layer(maybe_table, maybe_file):
     46     if maybe_table:
     47         conn = psycopg2.connect("host=127.0.0.1 dbname=osm user=osm")
     48         sql = "SELECT geom FROM %s" % maybe_table
     49         return geopandas.read_postgis(sql, con=conn, geom_col='geom')
     50     elif maybe_file:
     51         return geopandas.read_file(maybe_file)
     52 
     53 
     54 def main():
     55     args = parse_args()
     56     group1 = read_layer(args.group1_table, args.group1_infile)
     57     group2 = read_layer(args.group2_table, args.group2_infile)
     58     group3 = read_layer(args.group3_table, args.group3_infile)
     59 
     60     rc('text', usetex=True)
     61     fig, ax = plt.subplots()
     62     if args.size:
     63         fig.set_size_inches(args.size)
     64     if c := args.clip:
     65         ax.set_xlim(left=c[0], right=c[2])
     66         ax.set_ylim(bottom=c[1], top=c[3])
     67 
     68     if group1 is not None:
     69         group1.plot(ax=ax, color=PURPLE)
     70     if group2 is not None:
     71         group2.plot(ax=ax, color=ORANGE)
     72     if group3 is not None:
     73         group3.plot(ax=ax, color=GREEN)
     74 
     75     ax.axis('off')
     76     ax.margins(0, 0)
     77     fig.tight_layout(0)
     78     if args.outfile:
     79         fig.savefig(args.outfile, bbox_inches=0, dpi=600)
     80     else:
     81         plt.show()
     82 
     83 
     84 if __name__ == '__main__':
     85     main()