From c5fdc1cff53ce9b80da7844ed37a7ff62f00bf75 Mon Sep 17 00:00:00 2001 From: AlexanderJuestel Date: Mon, 22 Jul 2024 20:36:08 +0200 Subject: [PATCH] Format web.py --- gemgis/web.py | 456 +++++++++++++++++++++++++++++++------------------- 1 file changed, 280 insertions(+), 176 deletions(-) diff --git a/gemgis/web.py b/gemgis/web.py index fd74dedf..96b20a09 100644 --- a/gemgis/web.py +++ b/gemgis/web.py @@ -33,8 +33,7 @@ ############################### -def load_wms(url: str, - version: str = '1.3.0'): # -> owslib.wms.WebMapService: +def load_wms(url: str, version: str = "1.3.0"): # -> owslib.wms.WebMapService: """Loading a WMS Service by URL Parameters @@ -78,47 +77,52 @@ def load_wms(url: str, from owslib.wms import WebMapService except ModuleNotFoundError: raise ModuleNotFoundError( - 'owslib package is not installed. Use pip install owslib to install the latest version') + "owslib package is not installed. Use pip install owslib to install the latest version" + ) # Trying to import requests but returning error if requests is not installed try: import requests except ModuleNotFoundError: raise ModuleNotFoundError( - 'requests package is not installed. Use pip install requests to install the latest version') + "requests package is not installed. Use pip install requests to install the latest version" + ) # Checking if url is of type string if not isinstance(url, str): - raise TypeError('URL must be of type string') + raise TypeError("URL must be of type string") # Checking that the version is provided as string if not isinstance(version, str): - raise TypeError('The WMS Service version must be provided as string') + raise TypeError("The WMS Service version must be provided as string") # Requesting the WMS Service or returning an error if a module may be missing try: - wms = WebMapService(url, - version=version) + wms = WebMapService(url, version=version) return wms except requests.exceptions.SSLError: - print("GemGIS: SSL Error, potentially related to missing module - try:\n\n pip install -U openssl \n\n") + print( + "GemGIS: SSL Error, potentially related to missing module - try:\n\n pip install -U openssl \n\n" + ) raise -def load_as_map(url: str, - version: str, - layer: str, - style: str, - crs: Union[str, dict], - bbox: List[Union[float, int]], - size: List[int], - filetype: str, - transparent: bool = True, - save_image: bool = False, - path: str = None, - overwrite_file: bool = False, - create_directory: bool = False): # -> owslib.util.ResponseWrapper: +def load_as_map( + url: str, + version: str, + layer: str, + style: str, + crs: Union[str, dict], + bbox: List[Union[float, int]], + size: List[int], + filetype: str, + transparent: bool = True, + save_image: bool = False, + path: str = None, + overwrite_file: bool = False, + create_directory: bool = False, +): # -> owslib.util.ResponseWrapper: """Loading a portion of a WMS as array Parameters @@ -197,62 +201,66 @@ def load_as_map(url: str, # Trying to import owslib but returning error if owslib is not installed try: from owslib import util + __all__ = [util] except ModuleNotFoundError: raise ModuleNotFoundError( - 'owslib package is not installed. Use pip install owslib to install the latest version') + "owslib package is not installed. Use pip install owslib to install the latest version" + ) # Checking if the url is of type string if not isinstance(url, str): - raise TypeError('URL must be of type string') + raise TypeError("URL must be of type string") # Checking that the version is provided as string if not isinstance(version, str): - raise TypeError('The WMS Service version must be provided as string') + raise TypeError("The WMS Service version must be provided as string") # Checking if the layer name is of type string if not isinstance(layer, str): - raise TypeError('Layers must be of type string') + raise TypeError("Layers must be of type string") # Checking if the style is of type string if not isinstance(style, str): - raise TypeError('Style must be of type string') + raise TypeError("Style must be of type string") # Checking if the crs is of type string or dict if not isinstance(crs, (str, dict)): - raise TypeError('CRS must be of type str or dict') + raise TypeError("CRS must be of type str or dict") # Checking if bbox is of type list if not isinstance(bbox, list): - raise TypeError('Bbox must be of type list') + raise TypeError("Bbox must be of type list") # Checking the length of the bbox list if len(bbox) != 4: - raise ValueError('Provide minx, maxx, miny, and maxy values for the bounding box') + raise ValueError( + "Provide minx, maxx, miny, and maxy values for the bounding box" + ) # Checking if size is of type list if not isinstance(size, list): - raise TypeError('Size must be of type list') + raise TypeError("Size must be of type list") # Checking the length of the size list if len(size) != 2: - raise ValueError('Provide only a x- and y-value for the size') + raise ValueError("Provide only a x- and y-value for the size") # Checking if file type is of type string if not isinstance(filetype, str): - raise TypeError('File type must be of type string') + raise TypeError("File type must be of type string") # Checking if the transparency is of type book if not isinstance(transparent, bool): - raise TypeError('transparent must be of type bool') + raise TypeError("transparent must be of type bool") # Checking if save_image is of type bool if not isinstance(save_image, bool): - raise TypeError('Save_image must be of type bool') + raise TypeError("Save_image must be of type bool") # Checking is path is of type string if not isinstance(path, (str, type(None))): - raise TypeError('Path must be of type string') + raise TypeError("Path must be of type string") if isinstance(path, str): # Getting the absolute path @@ -271,49 +279,60 @@ def load_as_map(url: str, if create_directory: os.makedirs(path_dir) else: - raise LookupError('Directory not found. Pass create_directory=True to create a new directory') + raise LookupError( + "Directory not found. Pass create_directory=True to create a new directory" + ) if not overwrite_file: if os.path.exists(path): raise FileExistsError( - "The file already exists. Pass overwrite_file=True to overwrite the existing file") + "The file already exists. Pass overwrite_file=True to overwrite the existing file" + ) # Loading WMS Service wms = load_wms(url, version=version) # Creating map object - wms_map = wms.getmap(layers=[layer], styles=[style], srs=crs, bbox=tuple([bbox[0], bbox[2], bbox[1], bbox[3]]), - size=tuple(size), format=filetype, - transparent=transparent) + wms_map = wms.getmap( + layers=[layer], + styles=[style], + srs=crs, + bbox=tuple([bbox[0], bbox[2], bbox[1], bbox[3]]), + size=tuple(size), + format=filetype, + transparent=transparent, + ) # Saving an image if save_image is true and a path is provided if save_image: if isinstance(path, str): - out = open(path, 'wb') + out = open(path, "wb") out.write(wms_map.read()) out.close() else: - raise ValueError('Path is missing') + raise ValueError("Path is missing") else: if isinstance(path, str): - raise ValueError('Save_image was set to False') + raise ValueError("Save_image was set to False") return wms_map -def load_as_array(url: str, - version: str, - layer: str, - style: str, - crs: Union[str, dict], - bbox: List[Union[float, int]], - size: List[int], - filetype: str, - transparent: bool = True, - save_image: bool = False, - path: str = None, - overwrite_file: bool = False, - create_directory: bool = False) -> np.ndarray: +def load_as_array( + url: str, + version: str, + layer: str, + style: str, + crs: Union[str, dict], + bbox: List[Union[float, int]], + size: List[int], + filetype: str, + transparent: bool = True, + save_image: bool = False, + path: str = None, + overwrite_file: bool = False, + create_directory: bool = False, +) -> np.ndarray: """Loading a portion of a WMS as array Parameters @@ -400,68 +419,74 @@ def load_as_array(url: str, # Trying to import owslib but returning error if owslib is not installed try: from owslib import util + __all__ = [util] except ModuleNotFoundError: raise ModuleNotFoundError( - 'owslib package is not installed. Use pip install owslib to install the latest version') + "owslib package is not installed. Use pip install owslib to install the latest version" + ) # Trying to import matplotlib but returning error if matplotlib is not installed try: import matplotlib.pyplot as plt except ModuleNotFoundError: - raise ModuleNotFoundError('Matplotlib package is not installed. Use pip install matplotlib to install the latest version') + raise ModuleNotFoundError( + "Matplotlib package is not installed. Use pip install matplotlib to install the latest version" + ) # Checking if the url is of type string if not isinstance(url, str): - raise TypeError('URL must be of type string') + raise TypeError("URL must be of type string") # Checking that the version is provided as string if not isinstance(version, str): - raise TypeError('The WMS Service version must be provided as string') + raise TypeError("The WMS Service version must be provided as string") # Checking if the layer name is of type string if not isinstance(layer, str): - raise TypeError('Layers must be of type string') + raise TypeError("Layers must be of type string") # Checking if the style is of type string if not isinstance(style, str): - raise TypeError('Style must be of type string') + raise TypeError("Style must be of type string") # Checking if the crs is of type string or dict if not isinstance(crs, (str, dict)): - raise TypeError('CRS must be of type str or dict') + raise TypeError("CRS must be of type str or dict") # Checking if bbox is of type list if not isinstance(bbox, list): - raise TypeError('Bbox must be of type list') + raise TypeError("Bbox must be of type list") # Checking the length of the bbox list if len(bbox) != 4: - raise ValueError('Provide minx, maxx, miny and maxy values for the bounding box') + raise ValueError( + "Provide minx, maxx, miny and maxy values for the bounding box" + ) # Checking if size is of type list if not isinstance(size, list): - raise TypeError('Size must be of type list') + raise TypeError("Size must be of type list") # Checking the length of the size list if len(size) != 2: - raise ValueError('Provide only a x- and y-value for the size') + raise ValueError("Provide only a x- and y-value for the size") # Checking if file type is of type string if not isinstance(filetype, str): - raise TypeError('File type must be of type string') + raise TypeError("File type must be of type string") # Checking if the transparency is of type book if not isinstance(transparent, bool): - raise TypeError('transparent must be of type bool') + raise TypeError("transparent must be of type bool") # Checking if save_image is of type bool if not isinstance(save_image, bool): - raise TypeError('Save_image must be of type bool') + raise TypeError("Save_image must be of type bool") # Checking is path is of type string if not isinstance(path, (str, type(None))): - raise TypeError('Path must be of type string') + raise TypeError("Path must be of type string") if isinstance(path, str): # Getting the absolute path @@ -480,25 +505,30 @@ def load_as_array(url: str, if create_directory: os.makedirs(path_dir) else: - raise LookupError('Directory not found. Pass create_directory=True to create a new directory') + raise LookupError( + "Directory not found. Pass create_directory=True to create a new directory" + ) if not overwrite_file: if os.path.exists(path): raise FileExistsError( - "The file already exists. Pass overwrite_file=True to overwrite the existing file") + "The file already exists. Pass overwrite_file=True to overwrite the existing file" + ) # Creating WMS map object - wms_map = load_as_map(url=url, - version=version, - layer=layer, - style=style, - crs=crs, - bbox=bbox, - size=size, - filetype=filetype, - transparent=transparent, - save_image=save_image, - path=path) + wms_map = load_as_map( + url=url, + version=version, + layer=layer, + style=style, + crs=crs, + bbox=bbox, + size=size, + filetype=filetype, + transparent=transparent, + save_image=save_image, + path=path, + ) # Converting WMS map object to array maps = io.BytesIO(wms_map.read()) @@ -548,21 +578,24 @@ def load_wfs(url: str): # -> owslib.wfs.WebFeatureService: try: from owslib import util from owslib.wfs import WebFeatureService + __all__ = [util] except ModuleNotFoundError: raise ModuleNotFoundError( - 'owslib package is not installed. Use pip install owslib to install the latest version') + "owslib package is not installed. Use pip install owslib to install the latest version" + ) # Trying to import requests but returning error if requests is not installed try: import requests except ModuleNotFoundError: raise ModuleNotFoundError( - 'requests package is not installed. Use pip install requests to install the latest version') + "requests package is not installed. Use pip install requests to install the latest version" + ) # Checking if url is of type string if not isinstance(url, str): - raise TypeError('URL must be of type string') + raise TypeError("URL must be of type string") # Requesting the WMS Service or returning an error if a module may be missing try: @@ -571,14 +604,15 @@ def load_wfs(url: str): # -> owslib.wfs.WebFeatureService: return wfs except requests.exceptions.SSLError: - print("GemGIS: SSL Error, potentially related to missing module - try:\n\n pip install -U openssl \n\n") + print( + "GemGIS: SSL Error, potentially related to missing module - try:\n\n pip install -U openssl \n\n" + ) raise -def load_as_gpd(url: str, - typename: str = None, - outputformat: str = None - ) -> gpd.geodataframe.GeoDataFrame: +def load_as_gpd( + url: str, typename: str = None, outputformat: str = None +) -> gpd.geodataframe.GeoDataFrame: """Requesting data from a WFS Service Parameters @@ -621,29 +655,32 @@ def load_as_gpd(url: str, # Trying to import owslib but returning error if owslib is not installed try: from owslib import util + __all__ = [util] except ModuleNotFoundError: raise ModuleNotFoundError( - 'owslib package is not installed. Use pip install owslib to install the latest version') + "owslib package is not installed. Use pip install owslib to install the latest version" + ) # Trying to import requests but returning error if requests is not installed try: import requests except ModuleNotFoundError: raise ModuleNotFoundError( - 'requests package is not installed. Use pip install requests to install the latest version') + "requests package is not installed. Use pip install requests to install the latest version" + ) # Checking that the url is of type string if not isinstance(url, str): - raise TypeError('URL must be of type string') + raise TypeError("URL must be of type string") # Checking that the typename is of type string or None if not isinstance(typename, (str, type(None))): - raise TypeError('Name of the feature must be of type string') + raise TypeError("Name of the feature must be of type string") # Checking that the outputformat is of type string if not isinstance(outputformat, (str, type(None))): - raise TypeError('The output format must be of type string') + raise TypeError("The output format must be of type string") # Loading the wfs layer wfs = load_wfs(url=url) @@ -652,19 +689,26 @@ def load_as_gpd(url: str, if not typename: layer = list(wfs.contents)[0] else: - raise ValueError('No layer available') + raise ValueError("No layer available") # If the output format is not provided, take the last if not outputformat: - if wfs.getOperationByName('GetFeature').formatOptions == ['{http://www.opengis.net/wfs}GML2']: - outputformat = 'xml/gml2' + if wfs.getOperationByName("GetFeature").formatOptions == [ + "{http://www.opengis.net/wfs}GML2" + ]: + outputformat = "xml/gml2" # Specify the parameters for fetching the data - params = dict(service='WFS', version=wfs.version, request='GetFeature', - typeName=layer, outputFormat=outputformat) + params = dict( + service="WFS", + version=wfs.version, + request="GetFeature", + typeName=layer, + outputFormat=outputformat, + ) # Parse the URL with parameters - q = requests.Request('GET', url, params=params).prepare().url + q = requests.Request("GET", url, params=params).prepare().url # Read data from request feature = gpd.read_file(q) @@ -715,14 +759,16 @@ def load_wcs(url: str): # -> owslib.wcs.WebCoverageService: try: from owslib import util from owslib.wcs import WebCoverageService + __all__ = [util] except ModuleNotFoundError: raise ModuleNotFoundError( - 'owslib package is not installed. Use pip install owslib to install the latest version') + "owslib package is not installed. Use pip install owslib to install the latest version" + ) # Checking if URL is of type string if not isinstance(url, str): - raise TypeError('URL must be of type string') + raise TypeError("URL must be of type string") # Loading the WCS Layer wcs = WebCoverageService(url) @@ -730,12 +776,14 @@ def load_wcs(url: str): # -> owslib.wcs.WebCoverageService: return wcs -def create_request(wcs_url: str, - version: str, - identifier: str, - form: str, - extent: List[Union[float, int]], - name: str = 'test.tif') -> str: +def create_request( + wcs_url: str, + version: str, + identifier: str, + form: str, + extent: List[Union[float, int]], + name: str = "test.tif", +) -> str: """Creating URL to request data from WCS Server Parameters @@ -792,48 +840,76 @@ def create_request(wcs_url: str, # Trying to import owslib but returning error if owslib is not installed try: from owslib import util + __all__ = [util] except ModuleNotFoundError: raise ModuleNotFoundError( - 'owslib package is not installed. Use pip install owslib to install the latest version') + "owslib package is not installed. Use pip install owslib to install the latest version" + ) # Checking that the URL is of type string if not isinstance(wcs_url, str): - raise TypeError('URL must be of type string') + raise TypeError("URL must be of type string") # Checking that the version number is of type string if not isinstance(version, str): - raise TypeError('WCS Version must be of type string') + raise TypeError("WCS Version must be of type string") # Checking that the identifier is of type string if not isinstance(identifier, str): - raise TypeError('Layer Name/Identifier must be of type string') + raise TypeError("Layer Name/Identifier must be of type string") # Checking that the format is of type string if not isinstance(form, str): - raise TypeError('Download format must be of type string') + raise TypeError("Download format must be of type string") # Checking that the extent is of type list if not isinstance(extent, list): - raise TypeError('Extent must be provided as list of minx, maxx, miny, maxy') + raise TypeError("Extent must be provided as list of minx, maxx, miny, maxy") # Checking the length of the extent if len(extent) != 4: - raise ValueError('Extent must be provided as list of minx, maxx, miny, maxy') + raise ValueError("Extent must be provided as list of minx, maxx, miny, maxy") # Create URL for Request - url = wcs_url + '?' + 'REQUEST=GetCoverage' + '&' + 'SERVICE=WCS' + '&' + 'VERSION=' + str(version) + '&' + \ - 'COVERAGEID=' + identifier + '&' + 'FORMAT=' + form + '&' + \ - 'SUBSET=x(' + str(extent[0]) + ',' + str(extent[1]) + ')' + '&' + \ - 'SUBSET=y(' + str(extent[2]) + ',' + str(extent[3]) + ')' + '&' + 'OUTFILE=' + name + url = ( + wcs_url + + "?" + + "REQUEST=GetCoverage" + + "&" + + "SERVICE=WCS" + + "&" + + "VERSION=" + + str(version) + + "&" + + "COVERAGEID=" + + identifier + + "&" + + "FORMAT=" + + form + + "&" + + "SUBSET=x(" + + str(extent[0]) + + "," + + str(extent[1]) + + ")" + + "&" + + "SUBSET=y(" + + str(extent[2]) + + "," + + str(extent[3]) + + ")" + + "&" + + "OUTFILE=" + + name + ) return url -def load_as_file(url: str, - path: str, - overwrite_file: bool = False, - create_directory: bool = False): +def load_as_file( + url: str, path: str, overwrite_file: bool = False, create_directory: bool = False +): """Executing WCS request and downloading file into specified folder Parameters @@ -882,24 +958,28 @@ def load_as_file(url: str, # Trying to import owslib but returning error if owslib is not installed try: from owslib import util + __all__ = [util] except ModuleNotFoundError: raise ModuleNotFoundError( - 'owslib package is not installed. Use pip install owslib to install the latest version') + "owslib package is not installed. Use pip install owslib to install the latest version" + ) # Trying to import urllib but returning error if urllib is not installed try: import urllib except ModuleNotFoundError: - raise ModuleNotFoundError('urllib package is not installed. Use pip install urllib to install the latest version') + raise ModuleNotFoundError( + "urllib package is not installed. Use pip install urllib to install the latest version" + ) # Checking that the url is of type string if not isinstance(url, str): - raise TypeError('URL must be of type string') + raise TypeError("URL must be of type string") # Checking that the path is of type string if not isinstance(path, str): - raise TypeError('Path must be of type string') + raise TypeError("Path must be of type string") # Getting the absolute path path = os.path.abspath(path=path) @@ -916,25 +996,30 @@ def load_as_file(url: str, if create_directory: os.makedirs(path_dir) else: - raise LookupError('Directory not found. Pass create_directory=True to create a new directory') + raise LookupError( + "Directory not found. Pass create_directory=True to create a new directory" + ) if not overwrite_file: if os.path.exists(path): raise FileExistsError( - "The file already exists. Pass overwrite_file=True to overwrite the existing file") + "The file already exists. Pass overwrite_file=True to overwrite the existing file" + ) # Executing request and downloading files to the specified folder urllib.request.urlretrieve(url, path) -def load_as_files(wcs_url: str, - version: str, - identifier: str, - form: str, - extent: List[Union[float, int]], - size: int, - path: str = '', - create_directory: bool = False): +def load_as_files( + wcs_url: str, + version: str, + identifier: str, + form: str, + extent: List[Union[float, int]], + size: int, + path: str = "", + create_directory: bool = False, +): """Executing WCS requests and downloading files into specified folder Parameters @@ -992,44 +1077,48 @@ def load_as_files(wcs_url: str, # Trying to import owslib but returning error if owslib is not installed try: from owslib import util + __all__ = [util] except ModuleNotFoundError: raise ModuleNotFoundError( - 'owslib package is not installed. Use pip install owslib to install the latest version') + "owslib package is not installed. Use pip install owslib to install the latest version" + ) # Trying to import tqdm but returning error if tqdm is not installed try: from tqdm import tqdm except ModuleNotFoundError: - raise ModuleNotFoundError('tqdm package is not installed. Use pip install tqdm to install the latest version') + raise ModuleNotFoundError( + "tqdm package is not installed. Use pip install tqdm to install the latest version" + ) # Checking that the URL is of type string if not isinstance(wcs_url, str): - raise TypeError('URL must be of type string') + raise TypeError("URL must be of type string") # Checking that the version number is of type string if not isinstance(version, str): - raise TypeError('WCS Version must be of type string') + raise TypeError("WCS Version must be of type string") # Checking that the identifier is of type string if not isinstance(identifier, str): - raise TypeError('Layer Name/Identifier must be of type string') + raise TypeError("Layer Name/Identifier must be of type string") # Checking that the format is of type string if not isinstance(form, str): - raise TypeError('Download format must be of type string') + raise TypeError("Download format must be of type string") # Checking that the extent is of type list if not isinstance(extent, list): - raise TypeError('Extent must be provided as list of minx, maxx, miny, maxy') + raise TypeError("Extent must be provided as list of minx, maxx, miny, maxy") # Checking the length of the extent if len(extent) != 4: - raise ValueError('Extent must be provided as list of minx, maxx, miny, maxy') + raise ValueError("Extent must be provided as list of minx, maxx, miny, maxy") # Checking that the provided size of each tile is of type int if not isinstance(size, int): - raise TypeError('Tile size must be provided as int') + raise TypeError("Tile size must be provided as int") # Calculating the x Extent x = extent[1] - extent[0] @@ -1038,41 +1127,56 @@ def load_as_files(wcs_url: str, y = extent[3] - extent[2] # Printing the extent and number of tiles that are going to be downloaded - print('Extent X: ', x, ' m') - print('Extent Y: ', y, ' m') - print('Number of tiles in X directions: ', int(x / size)) - print('Number of tiles in Y directions: ', int(y / size)) - print('Total Number of Tiles: ', int(x / size) * int(y / size)) + print("Extent X: ", x, " m") + print("Extent Y: ", y, " m") + print("Number of tiles in X directions: ", int(x / size)) + print("Number of tiles in Y directions: ", int(y / size)) + print("Total Number of Tiles: ", int(x / size) * int(y / size)) # Loop through each tile and download data for i in tqdm(range(int(x / size))): for j in range(int(y / size)): # Download data only if the tile does not exist yet - if not os.path.exists(path + 'tile_%d_%d_%d_%d.tif' % - (extent[0] + i * size, - extent[0] + (i + 1) * size, - extent[2] + j * size, - extent[2] + (j + 1) * size)): + if not os.path.exists( + path + + "tile_%d_%d_%d_%d.tif" + % ( + extent[0] + i * size, + extent[0] + (i + 1) * size, + extent[2] + j * size, + extent[2] + (j + 1) * size, + ) + ): # Create URL request - url = create_request(wcs_url=wcs_url, - version=version, - identifier=identifier, - form=form, - extent=[extent[0] + i * size, - extent[0] + (i + 1) * size, - extent[2] + j * size, - extent[2] + (j + 1) * size], - name=path) + url = create_request( + wcs_url=wcs_url, + version=version, + identifier=identifier, + form=form, + extent=[ + extent[0] + i * size, + extent[0] + (i + 1) * size, + extent[2] + j * size, + extent[2] + (j + 1) * size, + ], + name=path, + ) print(url) # Load file - load_as_file(url=url, - path=path + 'tile_%d_%d_%d_%d.tif' % (extent[0] + i * size, - extent[0] + (i + 1) * size, - extent[2] + j * size, - extent[2] + (j + 1) * size), - create_directory=create_directory) + load_as_file( + url=url, + path=path + + "tile_%d_%d_%d_%d.tif" + % ( + extent[0] + i * size, + extent[0] + (i + 1) * size, + extent[2] + j * size, + extent[2] + (j + 1) * size, + ), + create_directory=create_directory, + ) else: - print('All tiles have already been downloaded') + print("All tiles have already been downloaded") pass