diff --git a/frisson/frisson.py b/frisson/frisson.py index 8a1972f..95711a1 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 @@ -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,6 +49,33 @@ def create_mask_vectors(output_filename, input_filename): output_filename, input_filename ] + print(" ".join(args)) + return call(args) + + +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 + + """ + args = [path.join(environ["GDAL_HOME"], "gdal_rasterize"), + "-i", + "-burn", + "17", + "-b", + "1", + "-b", + "2", + "-b", + "3", + "-of", + "-l", + layer_name, + vector_file, + output_filename, + ] + print(" ".join(args)) return call(args) @@ -67,17 +97,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) @@ -91,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 @@ -118,14 +143,15 @@ 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"], "gdal_warp"), + path.join(environ["GDAL_HOME"], "gdalwarp"), "-r", opts["RESAMPLE_OPTION"], - "-srcnodata"] + opts["MASK_OPTIONS"] + [ - "-dstalpha", - "s_srs", + # "-dstalpha", + # "-srcnodata"] + opts["MASK_OPTIONS"] + [ + "-s_srs", "EPSG:4326", "-co", "TILED=YES", @@ -134,8 +160,27 @@ def georeference(input_filename, output_filename, control_points, opts): input_filename, output_filename ] + print(" ".join(args)) return call(args) def get_map_bbbox(filename): - pass + """ + $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, + ] + bbox = [] + output = check_output(args) + 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 diff --git a/tests/unit/test_frisson.py b/tests/unit/test_frisson.py index c56a557..79274fa 100644 --- a/tests/unit/test_frisson.py +++ b/tests/unit/test_frisson.py @@ -4,10 +4,12 @@ from frisson.frisson import ( convert_to_tiff, add_overviews, - create_mask_vectors, + create_tile_index, virtual_georeference, _points_to_string, - georeference + georeference, + get_map_bbbox, + add_mask ) @@ -15,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") @@ -47,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", @@ -58,21 +58,37 @@ 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) + 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 ( 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): test_files = [ @@ -81,8 +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): - pass #remove(tf) + pass \ No newline at end of file