def test_rasterize_num_cells(prep): """ Tests rasterize_shapefile default using num cells """ mask_name = 'mask_50.msk' new_mask_grid = path.join(prep.tgrid.write, mask_name) rasterize_shapefile(prep.shapefile_path, new_mask_grid, x_num_cells=50, y_num_cells=50) # compare msk prep.compare_masks(mask_name)
def test_rasterize_num_cells_wkt(prep, get_wkt): """ Tests rasterize_shapefile default using num cells and wkt """ mask_name = 'mask_50_wkt.msk' new_mask_grid = path.join(prep.tgrid.write, mask_name) rasterize_shapefile(prep.shapefile_path, new_mask_grid, x_num_cells=50, y_num_cells=50, raster_nodata=0, raster_wkt_proj=get_wkt) # compare msk prep.compare_masks(mask_name)
def test_rasterize_num_cells_utm(prep): """ Tests rasterize_shapefile default using num cells and utm """ mask_name = 'mask_50_utm.msk' new_mask_grid = path.join(prep.tgrid.write, mask_name) rasterize_shapefile(prep.shapefile_path, new_mask_grid, x_num_cells=50, y_num_cells=50, raster_nodata=0, convert_to_utm=True) # compare msk prep.compare_masks(mask_name)
def test_rasterize_cell_size_ascii_utm(prep): """ Tests rasterize_shapefile using cell size to ascii in utm """ gazar.log_to_console(False) log_file = path.join(prep.tgrid.write, 'gazar.log') gazar.log_to_file(filename=log_file, level='DEBUG') mask_name = 'mask_cell_size_ascii_utm.msk' new_mask_grid = path.join(prep.tgrid.write, mask_name) gr = rasterize_shapefile(prep.shapefile_path, x_cell_size=1000, y_cell_size=1000, raster_nodata=0, as_gdal_grid=True, convert_to_utm=True) gr.to_grass_ascii(new_mask_grid, print_nodata=False) # compare msk prep.compare_masks(mask_name) gazar.log_to_file(False) # compare log_to_file compare_log_file = path.join(prep.tgrid.compare, 'gazar.log') with open(log_file) as lgf, open(compare_log_file) as clgf: assert lgf.read() == clgf.read()
def test_rasterize_cell_size_ascii(prep): """ Tests rasterize_shapefile default using cell size to ascii """ mask_name = 'mask_cell_size_ascii.msk' new_mask_grid = path.join(prep.tgrid.write, mask_name) gr = rasterize_shapefile(prep.shapefile_path, x_cell_size=0.01, y_cell_size=0.01, raster_nodata=0, as_gdal_grid=True) gr.to_grass_ascii(new_mask_grid, print_nodata=False) # compare msk prep.compare_masks(mask_name)
def test_rasterize_num_cells_wkt_ascii(prep, get_wkt): """ Tests rasterize_shapefile default using num cells """ mask_name = 'mask_50_wkt_ascii.msk' new_mask_grid = path.join(prep.tgrid.write, mask_name) gr = rasterize_shapefile(prep.shapefile_path, x_num_cells=50, y_num_cells=50, raster_nodata=0, raster_wkt_proj=get_wkt, as_gdal_grid=True) gr.to_grass_ascii(new_mask_grid, print_nodata=False) # compare msk prep.compare_masks(mask_name)
def test_rasterize_num_cells_utm_ascii(prep): """ Tests rasterize_shapefile default using num cells and utm to ascii """ mask_name = 'mask_50_utm_ascii.msk' new_mask_grid = path.join(prep.tgrid.write, mask_name) gr = rasterize_shapefile(prep.shapefile_path, x_num_cells=50, y_num_cells=50, raster_nodata=0, convert_to_utm=True, as_gdal_grid=True) gr.to_grass_ascii(new_mask_grid, print_nodata=False) # compare msk prep.compare_masks(mask_name)
def generateFromWatershedShapefile(self, shapefile_path, cell_size, out_raster_path=None): ''' Generates a mask from a watershed_shapefile Example:: from gsshapy.orm import ProjectFile, WatershedMaskFile from gsshapy.lib import db_tools as dbt from os import path, chdir gssha_directory = '/gsshapy/tests/grid_standard/gssha_project' shapefile_path = 'watershed_boundary.shp' # Create Test DB sqlalchemy_url, sql_engine = dbt.init_sqlite_memory() # Create DB Sessions db_session = dbt.create_session(sqlalchemy_url, sql_engine) # Instantiate GSSHAPY object for reading to database project_manager = ProjectFile() # read project file project_manager.readInput(directory=gssha_directory, projectFileName='grid_standard.prj', session=db_session) # generate watershed mask watershed_mask = WatershedMaskFile(session=db_session, project_file=project_manager) watershed_mask.generateFromWatershedShapefile(shapefile_path, x_num_cells=50, y_num_cells=50, ) # write out updated parameters project_manager.writeInput(session=db_session, directory=gssha_directory, name='grid_standard') ''' if not self.projectFile: raise ValueError("Must be connected to project file ...") # match elevation grid if exists match_grid = None try: match_grid = self.projectFile.getGrid(use_mask=False) except ValueError: pass # match projection if exists wkt_projection = None try: wkt_projection = self.projectFile.getWkt() except ValueError: pass if out_raster_path is None: project_path = '' project_path_card = self.projectFile.getCard('PROJECT_PATH') if project_path_card: project_path = project_path_card.value.strip('"').strip("'") out_raster_path = os.path.join(project_path, '{0}.{1}'.format(self.projectFile.name, self.extension), ) gr = rasterize_shapefile(shapefile_path, x_cell_size=cell_size, y_cell_size=cell_size, match_grid=match_grid, raster_nodata=0, as_gdal_grid=True, raster_wkt_proj=wkt_projection, convert_to_utm=True, ) gr.to_grass_ascii(out_raster_path, print_nodata=False) self.filename = out_raster_path # update project file cards self.projectFile.setCard('WATERSHED_MASK', out_raster_path, add_quotes=True) self.projectFile.setCard('GRIDSIZE', str((gr.geotransform[1] - gr.geotransform[-1])/2.0)) self.projectFile.setCard('ROWS', str(gr.y_size)) self.projectFile.setCard('COLS', str(gr.x_size)) # write projection file if does not exist if wkt_projection is None: proj_file = ProjectionFile() proj_file.projection = gr.wkt proj_file.projectFile = self.projectFile proj_path = "{0}_prj.pro".format(os.path.splitext(out_raster_path)[0]) gr.write_prj(proj_path) self.projectFile.setCard('#PROJECTION_FILE', proj_path, add_quotes=True) # read raster into object self._load_raster_text(out_raster_path)
def generateFromWatershedShapefile(self, shapefile_path, cell_size, out_raster_path=None, load_raster_to_db=True): """ Generates a mask from a watershed_shapefile Example:: from gsshapy.orm import ProjectFile, WatershedMaskFile from gsshapy.lib import db_tools as dbt gssha_directory = '/gsshapy/tests/grid_standard/gssha_project' shapefile_path = 'watershed_boundary.shp' # Create Test DB sqlalchemy_url, sql_engine = dbt.init_sqlite_memory() # Create DB Sessions db_session = dbt.create_session(sqlalchemy_url, sql_engine) # Instantiate GSSHAPY object for reading to database project_manager = ProjectFile() # read project file project_manager.readInput(directory=gssha_directory, projectFileName='grid_standard.prj', session=db_session) # generate watershed mask watershed_mask = WatershedMaskFile(session=db_session, project_file=project_manager) watershed_mask.generateFromWatershedShapefile(shapefile_path, x_num_cells=50, y_num_cells=50, ) # write out updated parameters project_manager.writeInput(session=db_session, directory=gssha_directory, name='grid_standard') """ if not self.projectFile: raise ValueError("Must be connected to project file ...") # match elevation grid if exists match_grid = None try: match_grid = self.projectFile.getGrid(use_mask=False) except ValueError: pass # match projection if exists wkt_projection = None try: wkt_projection = self.projectFile.getWkt() except ValueError: pass if out_raster_path is None: out_raster_path = '{0}.{1}'.format(self.projectFile.name, self.extension) # make sure paths are absolute as the working directory changes shapefile_path = os.path.abspath(shapefile_path) # make sure the polygon is valid check_watershed_boundary_geometry(shapefile_path) gr = rasterize_shapefile(shapefile_path, x_cell_size=cell_size, y_cell_size=cell_size, match_grid=match_grid, raster_nodata=0, as_gdal_grid=True, raster_wkt_proj=wkt_projection, convert_to_utm=True) with tmp_chdir(self.projectFile.project_directory): gr.to_grass_ascii(out_raster_path, print_nodata=False) self.filename = out_raster_path # update project file cards self.projectFile.setCard('WATERSHED_MASK', out_raster_path, add_quotes=True) self.projectFile.setCard('GRIDSIZE', str((gr.geotransform[1] - gr.geotransform[-1])/2.0)) self.projectFile.setCard('ROWS', str(gr.y_size)) self.projectFile.setCard('COLS', str(gr.x_size)) # write projection file if does not exist if wkt_projection is None: proj_file = ProjectionFile() proj_file.projection = gr.wkt proj_file.projectFile = self.projectFile proj_path = "{0}_prj.pro".format(os.path.splitext(out_raster_path)[0]) gr.write_prj(proj_path) self.projectFile.setCard('#PROJECTION_FILE', proj_path, add_quotes=True) # read raster into object if load_raster_to_db: self._load_raster_text(out_raster_path)