From 73142b15ef4d76bcbfa6794bf062b665b6853767 Mon Sep 17 00:00:00 2001 From: Cameron Poole Date: Sun, 18 Sep 2016 17:34:07 +0800 Subject: [PATCH 1/6] Added get map bbox function --- frisson/frisson.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/frisson/frisson.py b/frisson/frisson.py index 8a1972f..a863dd1 100644 --- a/frisson/frisson.py +++ b/frisson/frisson.py @@ -138,4 +138,16 @@ def georeference(input_filename, output_filename, control_points, opts): def get_map_bbbox(filename): - pass + """ + $GDAL_PATH/gdalinfo $INPUT_WARPED_TIF + $DO_STUFF_TO_PARSE "Lower Left" "Upper Right" + :param filename: + :return: + """ + args = [ + path.join(environ["GDAL_HOME"], "gdalinfo"), + filename, + "Lower Left", + "Upper Right" + ] + return call(args) \ No newline at end of file From 74b2053f5c6b4d52ae735ea16898251d9bef7460 Mon Sep 17 00:00:00 2001 From: Cameron Poole Date: Sun, 18 Sep 2016 17:35:22 +0800 Subject: [PATCH 2/6] Create test bbox method will need to get output however --- tests/unit/test_frisson.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_frisson.py b/tests/unit/test_frisson.py index c56a557..0eb2ff9 100644 --- a/tests/unit/test_frisson.py +++ b/tests/unit/test_frisson.py @@ -7,7 +7,8 @@ create_mask_vectors, virtual_georeference, _points_to_string, - georeference + georeference, + get_map_bbbox ) @@ -73,6 +74,10 @@ def test_georeference(self): opts) ) + def test_map_bbox(self): + self.assertEquals(get_map_bbbox("tests/test_output_referenced.tif"), + 0) + @classmethod def tearDownClass(self): test_files = [ From 8cf7edf5fcd79dc20b2cf94fd9f1707f14ecf6b1 Mon Sep 17 00:00:00 2001 From: Cameron Poole Date: Sun, 18 Sep 2016 21:21:46 +0800 Subject: [PATCH 3/6] Fixed up bounding box --- frisson/frisson.py | 20 +++++++++++++------- tests/unit/test_frisson.py | 14 ++++++++------ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/frisson/frisson.py b/frisson/frisson.py index a863dd1..29e7574 100644 --- a/frisson/frisson.py +++ b/frisson/frisson.py @@ -1,6 +1,6 @@ # from osgeo import gdal # from osgeo import ogr -from subprocess import call +from subprocess import call, check_output from os import environ, path @@ -120,12 +120,12 @@ def georeference(input_filename, output_filename, control_points, opts): assert "RESAMPLE_OPTION" in opts assert "MASK_OPTIONS" in opts args = [ - path.join(environ["GDAL_HOME"], "gdal_warp"), + path.join(environ["GDAL_HOME"], "gdalwarp"), "-r", opts["RESAMPLE_OPTION"], - "-srcnodata"] + opts["MASK_OPTIONS"] + [ "-dstalpha", - "s_srs", + "-srcnodata"] + opts["MASK_OPTIONS"] + [ + "-s_srs", "EPSG:4326", "-co", "TILED=YES", @@ -134,6 +134,7 @@ def georeference(input_filename, output_filename, control_points, opts): input_filename, output_filename ] + print(" ".join(args)) return call(args) @@ -141,13 +142,18 @@ def get_map_bbbox(filename): """ $GDAL_PATH/gdalinfo $INPUT_WARPED_TIF $DO_STUFF_TO_PARSE "Lower Left" "Upper Right" + This is such a shitty hack but I think it's best to replace a lot of the functions with ogr. + So for consistancy I will keep it as is till we discuss further. :param filename: :return: """ args = [ path.join(environ["GDAL_HOME"], "gdalinfo"), filename, - "Lower Left", - "Upper Right" ] - return call(args) \ No newline at end of file + bbox = [] + output = check_output(args) + for line in output.split("\n"): + if "Upper Right" in line or "Lower Left" in line: + bbox.append(line) + return bbox \ No newline at end of file diff --git a/tests/unit/test_frisson.py b/tests/unit/test_frisson.py index 0eb2ff9..e1ea12e 100644 --- a/tests/unit/test_frisson.py +++ b/tests/unit/test_frisson.py @@ -65,18 +65,19 @@ def test_gcp_string(self): ) def test_georeference(self): - opts = {"MASK_OPTIONS":["17", "17", "17"], - "RESAMPLE_OPTION":"near"} + opts = {"MASK_OPTIONS":["'17 17 17'"], + "RESAMPLE_OPTION": "near"} self.assertEquals( georeference("tests/test_output.vrt", "tests/test_output_referenced.tif", self.gcp_string, - opts) + opts), 0 ) def test_map_bbox(self): self.assertEquals(get_map_bbbox("tests/test_output_referenced.tif"), - 0) + ['Lower Left (-9186967.204, 2427630.502) (Invalid angle,Invalid angle)', + 'Upper Right (-9186247.194, 2428339.250) (Invalid angle,Invalid angle)']) @classmethod def tearDownClass(self): @@ -86,8 +87,9 @@ def tearDownClass(self): "tests/test_output.shp", "tests/test_output.shx", "tests/test_output.vrt", + "test_output_referenced.tif", + "test_output_referenced.tif.aux.xml" ] for tf in test_files: if path.isfile(tf): - pass - #remove(tf) + remove(tf) From 2db8a43f3aa449a13a0764ed25a3f8b3d0ed9b3c Mon Sep 17 00:00:00 2001 From: Cameron Poole Date: Thu, 29 Sep 2016 18:37:09 +0800 Subject: [PATCH 4/6] Added add_mask function --- frisson/frisson.py | 35 +++++++++++++++++++++++++++-------- tests/unit/test_frisson.py | 3 ++- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/frisson/frisson.py b/frisson/frisson.py index 29e7574..187b27b 100644 --- a/frisson/frisson.py +++ b/frisson/frisson.py @@ -49,6 +49,31 @@ def create_mask_vectors(output_filename, input_filename): return call(args) +def add_mask(input_filename, output_filename, mask_vectors): + """ + $GDAL_PATH/gdal_rasterize -i -burn 17 -b 1 -b 2 -b 3 \ + $VECTOR_FILE -l $NAME_OF_VECTOR_FILE_LAYER $TARGET_TIF + + """ + args = [path.join(environ["GDAL_HOME"], "gdal_rasterize"), + "-i", + "-burn", + "17", + "-b", + "1", + "-b", + "2", + "-b", + "3", + "-of", + "-l", + mask_vectors, + input_filename, + output_filename, + ] + return call(args) + + def _points_to_string(gcp_points): """ Converts a list of GCP points to a gdal_translate gcp argument string @@ -67,17 +92,11 @@ def _points_to_string(gcp_points): return gcp_s -def add_mask(input_filename, output_filename, mask_vectors): - """ - $GDAL_PATH/gdal_translate -a_srs '+init=epsg:4326' -of VRT \ - $CONVERTED_TIF $VIRTUAL_WARPED.vrt $MAGIC_GCP_STRING - """ - pass - - def virtual_georeference(input_filename, output_filename, control_points): """ Performs the virtual georeferencing using gdal translate + $GDAL_PATH/gdal_translate -a_srs '+init=epsg:4326' -of VRT \ + $CONVERTED_TIF $VIRTUAL_WARPED.vrt $MAGIC_GCP_STRING :param input_filename: :param output_filename: :param control_points: should be a list of tuples (x,y,long,lat) diff --git a/tests/unit/test_frisson.py b/tests/unit/test_frisson.py index e1ea12e..b2f2b03 100644 --- a/tests/unit/test_frisson.py +++ b/tests/unit/test_frisson.py @@ -92,4 +92,5 @@ def tearDownClass(self): ] for tf in test_files: if path.isfile(tf): - remove(tf) + #remove(tf) + pass From fb592b0f542966024b5ca26621af9564e3ac9ffd Mon Sep 17 00:00:00 2001 From: Cameron Poole Date: Fri, 30 Sep 2016 07:56:59 +0800 Subject: [PATCH 5/6] Fixed bad gcp points casusing flipped raster --- frisson/frisson.py | 22 ++++++++++++------ ...{cons 1636 item 3706.tif => test_1636.tif} | Bin 2 files changed, 15 insertions(+), 7 deletions(-) rename tests/{cons 1636 item 3706.tif => test_1636.tif} (100%) diff --git a/frisson/frisson.py b/frisson/frisson.py index 187b27b..95711a1 100644 --- a/frisson/frisson.py +++ b/frisson/frisson.py @@ -19,6 +19,7 @@ def convert_to_tiff(filename, output_filename): "-co", "PHOTOMETRIC=RGB", "-co", "PROFILE=BASELINE", output_filename] + print(" ".join(args)) return call(args) @@ -33,11 +34,13 @@ def add_overviews(filename): "16", "32", "64"] + print(" ".join(args)) return call(args) -def create_mask_vectors(output_filename, input_filename): +def create_tile_index(output_filename, input_filename): """ + Takes a warped raster and returns a shapefile $GDAL_PATH / gdaltindex - write_absolute_path $OUTPUT_INDEX_SHAPEFILE $INPUT_FOLDER / *.$EXT :return: """ @@ -46,10 +49,11 @@ def create_mask_vectors(output_filename, input_filename): output_filename, input_filename ] + print(" ".join(args)) return call(args) -def add_mask(input_filename, output_filename, mask_vectors): +def add_mask(vector_file, layer_name, output_filename): """ $GDAL_PATH/gdal_rasterize -i -burn 17 -b 1 -b 2 -b 3 \ $VECTOR_FILE -l $NAME_OF_VECTOR_FILE_LAYER $TARGET_TIF @@ -67,10 +71,11 @@ def add_mask(input_filename, output_filename, mask_vectors): "3", "-of", "-l", - mask_vectors, - input_filename, + layer_name, + vector_file, output_filename, ] + print(" ".join(args)) return call(args) @@ -110,6 +115,7 @@ def virtual_georeference(input_filename, output_filename, control_points): input_filename, output_filename, ] + print(" ".join(args)) return call(args) # NOTE need to check resample option is either @@ -137,13 +143,14 @@ def georeference(input_filename, output_filename, control_points, opts): :return: """ assert "RESAMPLE_OPTION" in opts - assert "MASK_OPTIONS" in opts + # Mask options not necessary + #assert "MASK_OPTIONS" in opts args = [ path.join(environ["GDAL_HOME"], "gdalwarp"), "-r", opts["RESAMPLE_OPTION"], - "-dstalpha", - "-srcnodata"] + opts["MASK_OPTIONS"] + [ + # "-dstalpha", + # "-srcnodata"] + opts["MASK_OPTIONS"] + [ "-s_srs", "EPSG:4326", "-co", @@ -175,4 +182,5 @@ def get_map_bbbox(filename): for line in output.split("\n"): if "Upper Right" in line or "Lower Left" in line: bbox.append(line) + print(" ".join(args)) return bbox \ No newline at end of file diff --git a/tests/cons 1636 item 3706.tif b/tests/test_1636.tif similarity index 100% rename from tests/cons 1636 item 3706.tif rename to tests/test_1636.tif From ac89f2ee91d6895493c927f99ed3d67119a1ca66 Mon Sep 17 00:00:00 2001 From: Cameron Poole Date: Thu, 13 Oct 2016 18:34:35 +0800 Subject: [PATCH 6/6] Fixed bounding box test --- tests/unit/test_frisson.py | 59 ++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/tests/unit/test_frisson.py b/tests/unit/test_frisson.py index b2f2b03..79274fa 100644 --- a/tests/unit/test_frisson.py +++ b/tests/unit/test_frisson.py @@ -4,11 +4,12 @@ from frisson.frisson import ( convert_to_tiff, add_overviews, - create_mask_vectors, + create_tile_index, virtual_georeference, _points_to_string, georeference, - get_map_bbbox + get_map_bbbox, + add_mask ) @@ -16,26 +17,22 @@ class TestPrintPoint(unittest.TestCase): @classmethod def setUpClass(cls): cls.gcp_points = [ - [12895157.1042, -3756156.6509, 1989.1230, -2999.8393], - [12898106.0028, -3756364.7068, 4083.4782, -3103.3251], - [12898161.6258, -3754995.9836, 4108.2269, -2151.3432] + [1991.5184954376359201,3000.20271501503793843, 115.83883775684209638,-31.94554601339920907], + [3211.51960671375672973, 3078.3775435045758968, 115.85439400036874247, -31.94675219309830183], + [2542.09731250744516728, -1049.31538631206467471, 115.84633294756488908, -31.92436589497924615], ] cls.gcp_string = [ - ["-gcp", "12895157.1042", - "-3756156.6509", - "1989.123", "-2999.8393"], - ["-gcp", "12898106.0028", "-3756364.7068", - "4083.4782", "-3103.3251"], - ["-gcp", "12898161.6258", "-3754995.9836", - "4108.2269", "-2151.3432"] + ['-gcp', '1991.51849544', '3000.20271502', '115.838837757', '-31.9455460134'], + ['-gcp', '3211.51960671', '3078.3775435', '115.854394', '-31.9467521931'], + ['-gcp', '2542.09731251', '-1049.31538631', '115.846332948', '-31.924365895'] ] def test_input_exists(self): - self.assertTrue(path.isfile("tests/cons 1636 item 3706.tif"), "Test File doesn't exist") + self.assertTrue(path.isfile("tests/test_1636.tif"), "Test File doesn't exist") # @pytest.mark.xfail def test_convert_to_tiff(self): - self.assertEquals(convert_to_tiff("tests/cons 1636 item 3706.tif", + self.assertEquals(convert_to_tiff("tests/test_1636.tif", "tests/test_output.tif"), 0, "Couldn't convert tiff") @@ -48,10 +45,12 @@ def test_convert_overview(self): self.assertEquals(add_overviews("tests/test_output.tif"), 0, "Couldn't create overview") - def test_shapefile_index(self): - # TODO: Not 100% on this one - self.assertEquals(create_mask_vectors("tests/test_output.shp", "tests/test_output.tif"), 0, - "Couldn't create shapefile index") + def test_add_mask(self): + # TODO: Or this one + if path.exists("tests/test_mask.gml"): + self.assertEquals(add_mask("tests/test_mask.gml", "features", "tests/test_output_clipped.tif"), 0, + "Couldn't add mask index") + return True def test_virtual_georeference(self): self.assertEquals(virtual_georeference("tests/test_output.tif", @@ -59,25 +58,36 @@ def test_virtual_georeference(self): self.gcp_points), 0, "Couldn't add mask") + #TODO: Create georeference with masking + def test_gcp_string(self): self.assertEquals(_points_to_string(self.gcp_points), self.gcp_string ) def test_georeference(self): - opts = {"MASK_OPTIONS":["'17 17 17'"], - "RESAMPLE_OPTION": "near"} + output = "tests/test_output_referenced.tif" + if path.exists(output): + remove(output) + opts = {"RESAMPLE_OPTION": "average"} self.assertEquals( georeference("tests/test_output.vrt", - "tests/test_output_referenced.tif", + output, self.gcp_string, opts), 0 ) + def test_tile_index(self): + # TODO: Not 100% on this one + if path.exists("tests/test_output_referenced.tif"): + self.assertEquals(create_tile_index("tests/test_output.shp", "tests/test_output_referenced.tif"), 0, + "Couldn't create shapefile index") + return True + def test_map_bbox(self): self.assertEquals(get_map_bbbox("tests/test_output_referenced.tif"), - ['Lower Left (-9186967.204, 2427630.502) (Invalid angle,Invalid angle)', - 'Upper Right (-9186247.194, 2428339.250) (Invalid angle,Invalid angle)']) + ['Lower Left ( 115.8136615, -31.9342700) (115d48\'49.18"E, 31d56\' 3.37"S)', + 'Upper Right ( 115.8265317, -31.9282996) (115d49\'35.51"E, 31d55\'41.88"S)']) @classmethod def tearDownClass(self): @@ -87,10 +97,11 @@ def tearDownClass(self): "tests/test_output.shp", "tests/test_output.shx", "tests/test_output.vrt", + "tests/test_output_masked.tif", "test_output_referenced.tif", "test_output_referenced.tif.aux.xml" ] for tf in test_files: if path.isfile(tf): #remove(tf) - pass + pass \ No newline at end of file