From c3986e523adc266758772b3221e73154a072ef17 Mon Sep 17 00:00:00 2001 From: Andres Chamorro Date: Sat, 24 Feb 2024 09:32:46 -0500 Subject: [PATCH 1/6] remove seaborn.set() --- src/GOSTrocks/rasterMisc.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/GOSTrocks/rasterMisc.py b/src/GOSTrocks/rasterMisc.py index 89f07e2..3924b3d 100644 --- a/src/GOSTrocks/rasterMisc.py +++ b/src/GOSTrocks/rasterMisc.py @@ -16,9 +16,6 @@ from rasterio.io import MemoryFile from contextlib import contextmanager -import seaborn as sns -sns.set(font_scale=1.5, style="whitegrid") - curPath = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0])) if not curPath in sys.path: sys.path.append(curPath) From da18c004695e45c2f76a9232f7c9024e40f8218a Mon Sep 17 00:00:00 2001 From: Andres Chamorro Date: Sat, 24 Feb 2024 09:38:48 -0500 Subject: [PATCH 2/6] small fix to clipRaster --- src/GOSTrocks/rasterMisc.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/GOSTrocks/rasterMisc.py b/src/GOSTrocks/rasterMisc.py index 3924b3d..0290a73 100644 --- a/src/GOSTrocks/rasterMisc.py +++ b/src/GOSTrocks/rasterMisc.py @@ -136,7 +136,7 @@ def project_raster(srcRst, dstCrs, output_raster=''): return([dstRst, kwargs]) -def clipRaster(inR, inD, outFile='', crop=True): +def clipRaster(inR, inD, outFile=None, crop=True): ''' Clip input raster :param inR: rasterio object to clip @@ -163,12 +163,12 @@ def getFeatures(gdf): tD = gpd.GeoDataFrame([[1]], geometry=[box(*inD.total_bounds)]) coords = getFeatures(tD) - out_img, out_transform = mask(inR, shapes=coords, crop=True) + out_img, out_transform = mask(inR, shapes=coords, crop=True, all_touched=True) out_meta.update({"driver": "GTiff", "height": out_img.shape[1], "width": out_img.shape[2], "transform": out_transform}) - if outFile != '': + if outFile: with rasterio.open(outFile, "w", **out_meta) as dest: dest.write(out_img) return([out_img, out_meta]) From c98c91ef1c2b71663117911583d136a1c48728b8 Mon Sep 17 00:00:00 2001 From: Andres Chamorro Date: Sat, 24 Feb 2024 09:59:38 -0500 Subject: [PATCH 3/6] add nodata option to rasterizeDataFrame() --- src/GOSTrocks/rasterMisc.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/GOSTrocks/rasterMisc.py b/src/GOSTrocks/rasterMisc.py index 0290a73..c37e382 100644 --- a/src/GOSTrocks/rasterMisc.py +++ b/src/GOSTrocks/rasterMisc.py @@ -173,7 +173,8 @@ def getFeatures(gdf): dest.write(out_img) return([out_img, out_meta]) -def rasterizeDataFrame(inD, outFile='', idField='', templateRaster='', templateMeta = '', nCells=0, res=0, mergeAlg="REPLACE", re_proj=False): +def rasterizeDataFrame(inD, outFile, idField='', templateRaster='', templateMeta = '', + nCells=0, res=0, mergeAlg="REPLACE", re_proj=False, nodata=np.nan): """ Convert input geopandas dataframe into a raster file :param inD: input data frame to rasterize @@ -249,16 +250,17 @@ def rasterizeDataFrame(inD, outFile='', idField='', templateRaster='', templateM else: crs = inD.crs cMeta = {'count':1, 'crs': crs, 'dtype':inD['VALUE'].dtype, 'driver':'GTiff', - 'transform':nTransform, 'height':height, 'width':width} + 'transform':nTransform, 'height':height, 'width':width, 'nodata':nodata} shapes = ((row.geometry,row.VALUE) for idx, row in inD.iterrows()) - burned = features.rasterize(shapes=shapes, out_shape=(cMeta['height'], cMeta['width']), transform=nTransform, dtype=cMeta['dtype'], merge_alg=mAlg) - try: - with rasterio.open(outFile, 'w', **cMeta) as out: - out.write_band(1, burned) - return({'meta':cMeta, 'vals': burned}) - except: - print("Error writing raster") - return({'meta':cMeta, 'vals': burned}) + burned = features.rasterize(shapes=shapes, out_shape=(cMeta['height'], cMeta['width']), + transform=nTransform, dtype=cMeta['dtype'], merge_alg=mAlg, fill=nodata) + if outFile: + try: + with rasterio.open(outFile, 'w', **cMeta) as out: + out.write_band(1, burned) + except: + print("Error writing raster") + return({'meta':cMeta, 'vals': burned}) def polygonizeArray(data, curRaster): """ Convert input array (data) to a geodataframe From d1fe6436906c243a01faa39b4aa0fc3f5337a0f2 Mon Sep 17 00:00:00 2001 From: Andres Chamorro Date: Sat, 24 Feb 2024 10:10:23 -0500 Subject: [PATCH 4/6] add get_utm() to misc.py --- src/GOSTrocks/misc.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/GOSTrocks/misc.py b/src/GOSTrocks/misc.py index 66628b8..4cd9f40 100644 --- a/src/GOSTrocks/misc.py +++ b/src/GOSTrocks/misc.py @@ -5,9 +5,11 @@ import numpy as np from math import ceil -from shapely.geometry import Point, Polygon +from shapely.geometry import Point, Polygon, box -wgs84 = {'init':'epsg:4326'} +from pyproj import CRS +from pyproj.aoi import AreaOfInterest +from pyproj.database import query_utm_crs_info def loggingInfo(lvl = logging.INFO): """ Set logging settings to info (default) and print useful information @@ -17,7 +19,6 @@ def loggingInfo(lvl = logging.INFO): """ logging.basicConfig(format='%(asctime)s:%(levelname)s: %(message)s', level=lvl) - def tPrint(s): '''prints the time along with the message''' print("%s\t%s" % (time.strftime("%H:%M:%S"), s)) @@ -212,4 +213,22 @@ def project_UTM(inD): letter = '7' outUTM = '32%s%s' % (letter, ll_utm[2]) return(inD.to_crs({'init': 'epsg:%s' % outUTM})) + +def get_utm(gdf, bbox=True): + + if bbox: + center = box(*gdf.total_bounds).centroid + else: + center = gdf.unary_union.centroid + utm_crs_list = query_utm_crs_info( + datum_name="WGS 84", + area_of_interest=AreaOfInterest( + west_lon_degree=center.x, + south_lat_degree=center.y, + east_lon_degree=center.x, + north_lat_degree=center.y + ), + ) + utm_crs = CRS.from_epsg(utm_crs_list[0].code) + return(utm_crs) From 8127c760a2bea85351573d48bedf413cb3eeac64 Mon Sep 17 00:00:00 2001 From: Andres Chamorro Date: Sat, 24 Feb 2024 10:19:42 -0500 Subject: [PATCH 5/6] fix GOSTrocks import statements --- src/GOSTrocks/ghslMisc.py | 2 +- src/GOSTrocks/osmMisc.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/GOSTrocks/ghslMisc.py b/src/GOSTrocks/ghslMisc.py index 13cd64d..b6ff96c 100644 --- a/src/GOSTrocks/ghslMisc.py +++ b/src/GOSTrocks/ghslMisc.py @@ -6,7 +6,7 @@ import xarray as xr import matplotlib.pyplot as plt -from GOSTRocks.misc import tPrint +from GOSTrocks.misc import tPrint curPath = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0])) if not curPath in sys.path: diff --git a/src/GOSTrocks/osmMisc.py b/src/GOSTrocks/osmMisc.py index eb37271..340116f 100644 --- a/src/GOSTrocks/osmMisc.py +++ b/src/GOSTrocks/osmMisc.py @@ -5,7 +5,7 @@ import pandas as pd from shapely.geometry import box -from GOSTRocks import misc +from GOSTrocks import misc # Highway features are reclassified to 4 OSMLR classes for simplification and standardization # https://mapzen.com/blog/osmlr-2nd-technical-preview/ From 2f653ba5b6e53c7c0df1960a01ef785063ba21c8 Mon Sep 17 00:00:00 2001 From: Andres Chamorro Date: Sat, 24 Feb 2024 10:21:16 -0500 Subject: [PATCH 6/6] readme fix --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 34836eb..0d9a277 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Future releases can be built from source, but pip will contain the most recent s ## Contributing -Please refer to the World Bank's Github [Contributing](docs/Contributing.md) guidelines. +Please refer to the World Bank's Github [Contributing](docs/CONTRIBUTING.md) guidelines. ## Code of Conduct