def test_with_projection(self): wktsrs = """PROJCS["Moon2000_Mercator180", GEOGCS["GCS_Moon_2000", DATUM["Moon_2000", SPHEROID["Moon_2000_IAU_IAG",1737400.0,0.0]], PRIMEM["Reference_Meridian",0.0], UNIT["Degree",0.017453292519943295]], PROJECTION["Mercator_1SP"], PARAMETER["False_Easting",0.0], PARAMETER["False_Northing",0.0], PARAMETER["Central_Meridian",180.0], PARAMETER["latitude_of_origin",0.0], UNIT["Meter",1.0]]""" io_gdal.array_to_raster(self.arr, 'test.tif', projection=wktsrs) expected_srs = """PROJCS["Moon2000_Mercator180", GEOGCS["GCS_Moon_2000", DATUM["Moon_2000", SPHEROID["Moon_2000_IAU_IAG",1737400,0]], PRIMEM["Reference_Meridian",0], UNIT["Degree",0.017453292519943295]], PROJECTION["Mercator_2SP"], PARAMETER["central_meridian",180], PARAMETER["false_easting",0], PARAMETER["false_northing",0], PARAMETER["standard_parallel_1",0], UNIT["Meter",1]]""" dataset = io_gdal.GeoDataset('test.tif') test_srs = dataset.spatial_reference.__str__() self.assertEqual(test_srs.split(), expected_srs.split())
def run_algorithm(self, module, func=None): """Run method that performs all the real work""" # Gets the current layer layer = self.iface.activeLayer() layer_path = layer.dataProvider().dataSourceUri() if module == crism_algs: img = crism_open(layer_path) else: # Opens the image using plio for reading img = m3_open(layer_path) # Applies the algorithm specified modified_img = getattr(module, str(func))(img) # Stores the name of the image file new_filename = (str(func) + '_' + str(layer_path.split('/')[-1].split('.')[0]) + '.tif') # Creates the new filepath for the image new_filepath = os.path.join(str(PyhatDialog.img_outpath), new_filename) try: img.spatial_reference array_to_raster(modified_img, new_filepath, bittype='GDT_Float32', projection=img.spatial_reference) except: # Writes the tiff to the user specified location array_to_raster(modified_img, new_filepath, bittype='GDT_Float32') # Grabs the new tiff and adds it into QGIS self.iface.addRasterLayer(new_filepath, new_filename) return 0
def run_algos(module, img, filepath, crism=False): """ Parameters ---------- module : Name of python module you want to run functions out of img : Full path to M3 image img_tiff filepath: Path to where you want the new tiffs to be generated Returns ------- : tiff image """ # Grabs all functions in a module package_funcs = inspect.getmembers(module, inspect.isfunction) if crism: img_tiff = crism_open(img) # Makes a readable img else: img_tiff = m3_open(img) for function in package_funcs: print(function) # If a callable function, call it with the img specified above array_to_raster(function[1](img_tiff), filepath + str(img).split('/')[-1].split('.')[0] + '_' + str(function[0]) + '.tiff', bittype='GDT_Float32')
def run_algorithm(self, module, func=None): """Run method that performs all the real work""" # Gets the current layer layer = self.iface.activeLayer() layer_path = layer.dataProvider().dataSourceUri() if module == crism_algs: img = crism_open(layer_path) else: # Opens the image using plio for reading img = m3_open(layer_path) # Applies the algorithm specified modified_img = getattr(module, str(func))(img) # Stores the name of the image file base_with_ext = os.path.basename(layer_path) base = os.path.splitext(base_with_ext)[0] # get base without extension new_filename = (str(func) + '_' + base + '.tif') # Creates the new filepath for the image new_filepath = os.path.join(str(PyhatDialog.img_outpath), new_filename) try: img.spatial_reference array_to_raster(modified_img, new_filepath, bittype='GDT_Float32', projection=img.spatial_reference) except: # Writes the tiff to the user specified location array_to_raster(modified_img, new_filepath, bittype='GDT_Float32') # Grabs the new tiff and adds it into QGIS self.iface.addRasterLayer(new_filepath, new_filename) # Selects original layer to be active instead of new layer self.iface.setActiveLayer(layer) return 0
def run_algorithm(self, module, func=None): """Run method that performs all the real work""" # Gets the current layer layer = self.iface.activeLayer() layer_path = layer.dataProvider().dataSourceUri() if module == crism_algs: img = crism_open(layer_path) else: # Opens the image using plio for reading img = m3_open(layer_path) # Applies the algorithm specified modified_img = getattr(module, str(func))(img) # Stores the name of the image file new_filename = (str(func) + '_' + str(layer_path.split('/')[-1].split('.')[0]) + '.tif') # Creates the new filepath for the image new_filepath = os.path.join(str(PyhatDialog.img_outpath), new_filename) try: img.spatial_reference img_tiff = array_to_raster(modified_img, new_filepath, bittype='GDT_Float32', geotransform=img.geotransform, projection=img.spatial_reference) except: # Writes the tiff to the user specified location img_tiff = array_to_raster(modified_img, new_filepath, bittype='GDT_Float32', geotransform=img.geotransform) # Grabs the new tiff and adds it into QGIS self.iface.addRasterLayer(new_filepath, new_filename) return 0
def test_with_no_data_value(self): no_data_value = 0.0 io_gdal.array_to_raster(self.arr, 'test.tif', ndv=no_data_value) dataset = io_gdal.GeoDataset('test.tif') self.assertEqual(dataset.no_data_value, no_data_value)
def test_with_geotrasform(self): gt = (-464400.0, 3870.0, 0.0, -506970.0, 0.0, -3870.0) io_gdal.array_to_raster(self.arr, 'test.tif', geotransform=gt) dataset = io_gdal.GeoDataset('test.tif') self.assertEqual(gt, dataset.geotransform)
def test_write_ndarray(self): io_gdal.array_to_raster(self.arr, 'test.tif') self.assertTrue(os.path.exists('test.tif')) os.remove('test.tif')