From 747c6944017a812c7c7438cd0e85d2544fbc4a7c Mon Sep 17 00:00:00 2001 From: Daniel Teske Date: Fri, 25 Mar 2016 21:54:59 +0100 Subject: [PATCH 1/5] bugfix: force accurate statistics to prevent wrong min/max limitations --- python/raster_isobands/isobands_gdal.py | 2 +- python/raster_isobands/isobands_matplotlib.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/python/raster_isobands/isobands_gdal.py b/python/raster_isobands/isobands_gdal.py index c871dc7..44bfd58 100644 --- a/python/raster_isobands/isobands_gdal.py +++ b/python/raster_isobands/isobands_gdal.py @@ -26,7 +26,7 @@ def isobands(in_file, band, out_file, out_format, layer_name, attr_name, xsize_in = band_in.XSize ysize_in = band_in.YSize - stats = band_in.GetStatistics(True, True) + stats = band_in.GetStatistics(False, True) if min_level == None: min_value = stats[0] diff --git a/python/raster_isobands/isobands_matplotlib.py b/python/raster_isobands/isobands_matplotlib.py index 3758f78..84b10eb 100644 --- a/python/raster_isobands/isobands_matplotlib.py +++ b/python/raster_isobands/isobands_matplotlib.py @@ -58,7 +58,7 @@ def isobands(in_file, band, out_file, out_format, layer_name, attr_name, raster_values = band_in.ReadAsArray(0, 0, xsize_in, ysize_in) - stats = band_in.GetStatistics(True, True) + stats = band_in.GetStatistics(False, True) if min_level == None: min_value = stats[0] min_level = offset + interval * floor((min_value - offset)/interval) From 920247ee40c079288d79e984a00eb854be357adb Mon Sep 17 00:00:00 2001 From: Daniel Teske Date: Fri, 25 Mar 2016 22:00:40 +0100 Subject: [PATCH 2/5] workaround to make it run on systems without Xwindows --- python/raster_isobands/isobands_matplotlib.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/python/raster_isobands/isobands_matplotlib.py b/python/raster_isobands/isobands_matplotlib.py index 84b10eb..8144961 100644 --- a/python/raster_isobands/isobands_matplotlib.py +++ b/python/raster_isobands/isobands_matplotlib.py @@ -16,6 +16,9 @@ from os.path import exists from os import remove from argparse import ArgumentParser + +import matplotlib +matplotlib.use('Agg') #workaround to make it run on systems without Xwindows import matplotlib.pyplot as plt From abd1fec4017069f7ab0eb303ce3cdc8a03ebfafd Mon Sep 17 00:00:00 2001 From: Daniel Teske Date: Fri, 25 Mar 2016 22:04:11 +0100 Subject: [PATCH 3/5] introduce parameter -fl for fixed levels --- python/raster_isobands/isobands_matplotlib.py | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/python/raster_isobands/isobands_matplotlib.py b/python/raster_isobands/isobands_matplotlib.py index 8144961..297f101 100644 --- a/python/raster_isobands/isobands_matplotlib.py +++ b/python/raster_isobands/isobands_matplotlib.py @@ -24,7 +24,7 @@ def isobands(in_file, band, out_file, out_format, layer_name, attr_name, - offset, interval, min_level = None): + offset, interval, fixed_levels, min_level = None): ''' The method that calculates the isobands ''' @@ -61,17 +61,18 @@ def isobands(in_file, band, out_file, out_format, layer_name, attr_name, raster_values = band_in.ReadAsArray(0, 0, xsize_in, ysize_in) - stats = band_in.GetStatistics(False, True) - if min_level == None: - min_value = stats[0] - min_level = offset + interval * floor((min_value - offset)/interval) - - max_value = stats[1] - #Due to range issues, a level is added - max_level = offset + interval * (1 + ceil((max_value - offset)/interval)) - - levels = arange(min_level, max_level, interval) - + if fixed_levels != None: + levels = fixed_levels + else: + stats = band_in.GetStatistics(False, True) + if min_level == None: + min_value = stats[0] + min_level = offset + interval * floor((min_value - offset)/interval) + + max_value = stats[1] + #Due to range issues, a level is added + max_level = offset + interval * (1 + ceil((max_value - offset)/interval)) + levels = arange(min_level, max_level, interval) contours = plt.contourf(x_grid, y_grid, raster_values, levels) @@ -123,6 +124,9 @@ def isobands(in_file, band, out_file, out_format, layer_name, attr_name, PARSER.add_argument("-i", help="The interval (default 0)", type=float, default = 0.0, metavar = 'interval') + PARSER.add_argument("-fl", + help="name one or more 'fixed levels' to extract (default 0)", + type=float, nargs="+", metavar = 'fixed levels') PARSER.add_argument("-nln", help="The out layer name (default bands)", default = 'bands', metavar = 'layer_name') @@ -135,4 +139,4 @@ def isobands(in_file, band, out_file, out_format, layer_name, attr_name, ARGS = PARSER.parse_args() isobands(ARGS.src_file, ARGS.b, ARGS.out_file, ARGS.f, ARGS.nln, ARGS.a, - ARGS.off, ARGS.i) + ARGS.off, ARGS.i, ARGS.fl) From 3ee141f2d3267109e283e8cf274d5723304d1e09 Mon Sep 17 00:00:00 2001 From: Daniel Teske Date: Fri, 25 Mar 2016 22:28:03 +0100 Subject: [PATCH 4/5] commit after tests --- python/raster_isobands/isobands_gdal.py | 0 python/raster_isobands/isobands_matplotlib.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 python/raster_isobands/isobands_gdal.py mode change 100644 => 100755 python/raster_isobands/isobands_matplotlib.py diff --git a/python/raster_isobands/isobands_gdal.py b/python/raster_isobands/isobands_gdal.py old mode 100644 new mode 100755 diff --git a/python/raster_isobands/isobands_matplotlib.py b/python/raster_isobands/isobands_matplotlib.py old mode 100644 new mode 100755 From 0aa65813ff2363168d468f4e4427d755c397254e Mon Sep 17 00:00:00 2001 From: Daniel Teske Date: Fri, 25 Mar 2016 22:41:39 +0100 Subject: [PATCH 5/5] update documentation --- python/raster_isobands/Readme.md | 3 +++ python/raster_isobands/isobands_matplotlib.py | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/python/raster_isobands/Readme.md b/python/raster_isobands/Readme.md index c39d84a..2663759 100644 --- a/python/raster_isobands/Readme.md +++ b/python/raster_isobands/Readme.md @@ -10,6 +10,7 @@ There are two scripts, that are used exacly the same and produce the same result ``` Usage: isobands_matplotlib.py [-h] [-b band] [-off offset] [-i interval] + [-fl fixed levels [fixed levels ...]] [-nln layer_name] [-a attr_name] [-f formatname] src_file out_file Calculates the isobands from a raster into a vector file @@ -22,6 +23,8 @@ There are two scripts, that are used exacly the same and produce the same result -b band The band in the source file to process (default 1) -off offset The offset to start the isobands (default 0) -i interval The interval (default 0) + -fl fixed levels [fixed levels ...] + List of fixed levels (float) -nln layer_name The out layer name (default bands) -a attr_name The out layer attribute name (default h) -f formatname The output file format name (default ESRI Shapefile) diff --git a/python/raster_isobands/isobands_matplotlib.py b/python/raster_isobands/isobands_matplotlib.py index 297f101..0c63360 100755 --- a/python/raster_isobands/isobands_matplotlib.py +++ b/python/raster_isobands/isobands_matplotlib.py @@ -125,7 +125,7 @@ def isobands(in_file, band, out_file, out_format, layer_name, attr_name, help="The interval (default 0)", type=float, default = 0.0, metavar = 'interval') PARSER.add_argument("-fl", - help="name one or more 'fixed levels' to extract (default 0)", + help="List of fixed levels (float)", type=float, nargs="+", metavar = 'fixed levels') PARSER.add_argument("-nln", help="The out layer name (default bands)",