add gpkg samples
This commit is contained in:
@@ -3,12 +3,15 @@
|
||||
|
||||
import argparse
|
||||
import geopandas
|
||||
import psycopg2
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
INCH = 25.4 # mm
|
||||
INCH = 25.4 # mm
|
||||
|
||||
|
||||
def plt_size(string):
|
||||
if not string:
|
||||
# using default matplotlib dimensions
|
||||
return None
|
||||
try:
|
||||
w, h = string.split("x")
|
||||
@@ -16,25 +19,36 @@ def plt_size(string):
|
||||
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)')
|
||||
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)')
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def main():
|
||||
args = parse_args()
|
||||
f = geopandas.read_file(args.infile)
|
||||
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)
|
||||
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)
|
||||
if args.outfile:
|
||||
plt.savefig(args.outfile, bbox_inches=0, dpi=300)
|
||||
else:
|
||||
plt.show()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user