From 802bdc802279ee009eecd962525c345e2a7bbfbc Mon Sep 17 00:00:00 2001 From: PennyHow Date: Thu, 30 May 2024 12:24:36 -0100 Subject: [PATCH] clean-up --- setup.py | 7 ++++++- src/griml/convert/convert.py | 9 +++------ src/griml/filter/filter_vectors.py | 9 ++++----- src/griml/load/load.py | 8 +++----- src/griml/merge/merge_vectors.py | 6 +++--- src/griml/metadata/add_metadata.py | 14 ++++---------- src/griml/metadata/assign_names.py | 13 +------------ src/griml/metadata/assign_regions.py | 17 +---------------- src/griml/test/process_cli.py | 6 ++++-- src/griml/test/process_example.py | 3 ++- 10 files changed, 31 insertions(+), 61 deletions(-) diff --git a/setup.py b/setup.py index 7554b0b..f0791a7 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="griml", - version="0.1.0", + version="0.1.1", author="Penelope How", author_email="pho@geus.dk", description="A workflow for classifying lakes from satellite imagery and compiling lake inventories", @@ -34,4 +34,9 @@ package_data={"griml.test": ["*"]}, python_requires=">=3.8", install_requires=['geopandas', 'pandas', 'scipy', 'Shapely', 'rasterio'], + entry_points={ + 'console_scripts': [ + 'griml = griml.test.process_cli:griml' + ], +}, ) diff --git a/src/griml/convert/convert.py b/src/griml/convert/convert.py index 5b1ce61..e75cfe5 100644 --- a/src/griml/convert/convert.py +++ b/src/griml/convert/convert.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- from griml.convert import raster_to_vector -import glob +import glob, os from pathlib import Path def convert(indir, proj, band_info, startdate, enddate, outdir=None): @@ -41,9 +41,7 @@ def convert(indir, proj, band_info, startdate, enddate, outdir=None): if __name__ == "__main__": - # Define file attributes - indir = list(glob.glob('/home/pho/Desktop/python_workspace/GrIML/other/iml_2017/binary_images/*.tif')) - outdir = '/home/pho/Desktop/python_workspace/GrIML/other/iml_2017/vectors/' + infile = os.path.join(os.path.dirname(griml.__file__),'test/test_north_greenland.tif') proj = 'EPSG:3413' band_info = [{'b_number':1, 'method':'VIS', 'source':'S2'}, {'b_number':2, 'method':'SAR', 'source':'S1'}, @@ -51,5 +49,4 @@ def convert(indir, proj, band_info, startdate, enddate, outdir=None): start='20170701' end='20170831' - # Perform conversion - convert(indir, outdir, proj, band_info, start, end) + convert([infile], proj, band_info, start, end) diff --git a/src/griml/filter/filter_vectors.py b/src/griml/filter/filter_vectors.py index 2337c55..422bfb9 100644 --- a/src/griml/filter/filter_vectors.py +++ b/src/griml/filter/filter_vectors.py @@ -5,6 +5,7 @@ from griml.load import load import geopandas as gpd from pathlib import Path +import os def filter_vectors(inlist, margin_file, outdir=None, min_area=0.05): '''Filter vectors by area and margin proximity @@ -66,8 +67,6 @@ def filter_vectors(inlist, margin_file, outdir=None, min_area=0.05): if __name__ == "__main__": - infile1 = '../test/test_icemask.shp' - margin_buff = gpd.read_file(infile1) - - infile2 = ['../test/test_filter.shp'] - filter_vectors(infile2, margin_buff) + infile1 = os.path.join(os.path.dirname(griml.__file__),'test/test_filter.shp') + infile2 = os.path.join(os.path.dirname(griml.__file__),'test/test_icemask.shp') + filter_vectors([infile1], infile2) diff --git a/src/griml/load/load.py b/src/griml/load/load.py index 86dac35..b8ad32d 100644 --- a/src/griml/load/load.py +++ b/src/griml/load/load.py @@ -69,12 +69,10 @@ def is_geo_object(typ): else: return False -if __name__ == "__main__": - infile1 = '../test/test_icemask.shp' +if __name__ == "__main__": + + infile1 = os.path.join(os.path.dirname(griml.__file__),'test/test_icemask.shp') test1 = load(infile1) g = gpd.read_file(infile1) test2 = load(g) - - infile2 = '../test/test_icemsak.shp' - test2 = load(infile2) \ No newline at end of file diff --git a/src/griml/merge/merge_vectors.py b/src/griml/merge/merge_vectors.py index 89686b2..253945c 100644 --- a/src/griml/merge/merge_vectors.py +++ b/src/griml/merge/merge_vectors.py @@ -108,6 +108,6 @@ def _dissolve_vectors(gdf): return gdf2 if __name__ == "__main__": - infile1 = '../test/test_merge_1.shp' - infile2 = '../test/test_merge_2.shp' - vectors = merge_vectors([infile1,infile2]) \ No newline at end of file + infile1 = os.path.join(os.path.dirname(griml.__file__),'test/test_merge_1.shp') + infile2 = os.path.join(os.path.dirname(griml.__file__),'test/test_merge_2.shp') + merge_vectors([infile1,infile2]) diff --git a/src/griml/metadata/add_metadata.py b/src/griml/metadata/add_metadata.py index 50e6d9a..8deb1e0 100644 --- a/src/griml/metadata/add_metadata.py +++ b/src/griml/metadata/add_metadata.py @@ -36,13 +36,7 @@ def add_metadata(iml, names, regions, outfile=None): if __name__ == "__main__": - # indir = "/home/pho/python_workspace/GrIML/other/iml_2017/merged_vectors/griml_2017_inventory.shp" - indir = "/home/pho/python_workspace/GrIML/other/iml_2017/inspected_vectors/lakes_all-0000000000-0000037888_filtered.shp" - - - infile_names = '/home/pho/python_workspace/GrIML/other/datasets/placenames/oqaasileriffik_placenames.shp' - infile_regions = '/home/pho/python_workspace/GrIML/other/datasets/drainage_basins/greenland_basins_polarstereo.shp' - - - outfile = "/home/pho/python_workspace/GrIML/other/iml_2017/metadata_vectors/griml_2017_inventory_final.shp" - add_metadata(indir, infile_names, infile_regions, outfile) + infile1 = os.path.join(os.path.dirname(griml.__file__),'test/test_merge_2.shp') + infile2 = os.path.join(os.path.dirname(griml.__file__),'test/test_placenames.shp') + infile3 = os.path.join(os.path.dirname(griml.__file__),'test/greenland_basins_polarstereo.shp') + add_metadata(infile1, infile2, infile3) diff --git a/src/griml/metadata/assign_names.py b/src/griml/metadata/assign_names.py index c151a48..08a1a17 100644 --- a/src/griml/metadata/assign_names.py +++ b/src/griml/metadata/assign_names.py @@ -84,15 +84,4 @@ def _compile_names(gdf): placenames.append(v['Alternativ']) else: placenames.append(None) - return placenames - - -if __name__ == "__main__": - - # Define file attributes - infile = '/home/pho/Desktop/python_workspace/GrIML/src/griml/test/test_merge_1.shp' - names = '/home/pho/Desktop/python_workspace/GrIML/other/datasets/placenames/oqaasileriffik_placenames_polarstereo.shp' - - - # Perform conversion - a = assign_names(infile, names) \ No newline at end of file + return placenames diff --git a/src/griml/metadata/assign_regions.py b/src/griml/metadata/assign_regions.py index 6458989..6c1aa52 100644 --- a/src/griml/metadata/assign_regions.py +++ b/src/griml/metadata/assign_regions.py @@ -68,19 +68,4 @@ def _get_nearest_polygon(gdfA, gdfB, gdfB_cols=['subregion'], distance=100000.0) gdf.loc[gdf['dist']>=distance, 'subregion'] = 'Unknown' gdf = gdf.drop(columns=['dist']) - return gdf - - -if __name__ == "__main__": - - # Define file attributes - infile = '/home/pho/Desktop/python_workspace/GrIML/src/griml/test/test_merge_1.shp' - regions = '/home/pho/python_workspace/GrIML/other/datasets/drainage_basins/greenland_basins_polarstereo.shp' - regions=gpd.read_file(regions) - - regions_dissolve = regions.dissolve(by='subregion') - - - # Perform conversion - a = assign_regions(infile, regions) - + return gdf diff --git a/src/griml/test/process_cli.py b/src/griml/test/process_cli.py index 915effc..0616591 100644 --- a/src/griml/test/process_cli.py +++ b/src/griml/test/process_cli.py @@ -24,6 +24,8 @@ def parse_griml_arguments(): help='File path to ice margin for spatial filtering') parser.add_argument('-n', '--names_file', type=str, required=True, help='File path to placenames file for metadata population') + parser.add_argument('-r', '--regions_file', type=str, required=True, + help='File path to regions/drainage basin file for metadata population') parser.add_argument('-p', '--proj', type=str, default='EPSG:3413', required=False, help='Projection (of input and output)') parser.add_argument('-s', '--steps', type=str, default='1111', @@ -46,7 +48,7 @@ def check_dir(d): os.mkdir(d) -def griml_process(): +def griml(): '''Perform processing workflow''' args = parse_griml_arguments() @@ -127,6 +129,6 @@ def griml_process(): print('Reading from ' + src) print('Writing to ' + str(dest)) - add_metadata(src, args.names_file, str(dest)) + add_metadata(src, args.names_file, args.regions_file, str(dest)) print('Finished') diff --git a/src/griml/test/process_example.py b/src/griml/test/process_example.py index c96a4d9..a558f25 100644 --- a/src/griml/test/process_example.py +++ b/src/griml/test/process_example.py @@ -79,7 +79,8 @@ print('Writing to ' + dest) infile_names = str(root_dir.joinpath('datasets/placenames/oqaasileriffik_placenames.shp')) +infile_basins = str(root_dir.joinpath('datasets/drainage_basins/greenland_basins_polarstereo.shp')) -add_metadata(src, infile_names, dest) +add_metadata(src, infile_names, infile_basins, dest) print('Finished')