From ac8bb9058f1ea3b3a8605f402652052fe6876824 Mon Sep 17 00:00:00 2001 From: Severin Ryberg Date: Wed, 18 Jul 2018 23:33:26 +0200 Subject: [PATCH 1/8] added .shape to LocationSet --- geokit/core/location.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/geokit/core/location.py b/geokit/core/location.py index 33e81a7..0276b6b 100644 --- a/geokit/core/location.py +++ b/geokit/core/location.py @@ -274,8 +274,11 @@ def __init__(s, locations, srs=4326, _skip_check=False): s._lats = None s._bounds4326 = None s.count = len(s._locations) - + s.shape = (s.count,) + def __len__(s): return s.count + + def __getitem__(s,i): return s._locations[i] def __repr__(s): out = " , Lon , Lat\n" From 103497a03438cbda547e8776d9b4b28836edd6f2 Mon Sep 17 00:00:00 2001 From: Sev Date: Wed, 1 Aug 2018 15:40:56 +0200 Subject: [PATCH 2/8] added bo --- bo.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 bo.txt diff --git a/bo.txt b/bo.txt new file mode 100644 index 0000000..e69de29 From 43536286e7ade7d9cd08e6bbda0413517e681f1b Mon Sep 17 00:00:00 2001 From: Severin Ryberg Date: Mon, 13 Aug 2018 09:36:45 +0200 Subject: [PATCH 3/8] Added percent padding to Extent --- geokit/core/extent.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/geokit/core/extent.py b/geokit/core/extent.py index 6980992..b1719ce 100755 --- a/geokit/core/extent.py +++ b/geokit/core/extent.py @@ -262,7 +262,7 @@ def __str__(s): return "(%.5f,%.5f,%.5f,%.5f)"%s.xyXY - def pad(s, pad): + def pad(s, pad, percent=False): """Pad the extent in all directions Parameters: @@ -271,6 +271,10 @@ def pad(s, pad): The amount to pad in all directions * In units of the extent's srs * Can also accept a negative padding + + percent : bool, optional + If True, the padding values are understood to be a percentage of the + unpadded extent Returns: -------- @@ -287,6 +291,10 @@ def pad(s, pad): xpad = pad ypad = pad + if percent: + xpad = xpad/100*(s.xMax-s.xMin) /2 + ypad = ypad/100*(s.yMax-s.yMin) /2 + # Pad the extent return Extent(s.xMin-xpad, s.yMin-ypad, s.xMax+xpad, s.yMax+ypad, srs=s.srs) From 7450d787f3f9906d8894482d716d57acd23e6a61 Mon Sep 17 00:00:00 2001 From: Severin Ryberg Date: Mon, 13 Aug 2018 10:56:20 +0200 Subject: [PATCH 4/8] outputType is no longer given when rasterizing a vector --- geokit/__init__.py | 2 +- geokit/core/vector.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/geokit/__init__.py b/geokit/__init__.py index a6261c2..a05c02e 100644 --- a/geokit/__init__.py +++ b/geokit/__init__.py @@ -40,7 +40,7 @@ from geokit.core.raster import drawRaster # import the special algorithms -#import geokit.algorithms +import geokit.algorithms # Add useful paths for testing and stuff from collections import OrderedDict as _OrderedDict diff --git a/geokit/core/vector.py b/geokit/core/vector.py index 51d1f63..2fa42ab 100644 --- a/geokit/core/vector.py +++ b/geokit/core/vector.py @@ -962,7 +962,7 @@ def rasterize(source, pixelWidth, pixelHeight, srs=None, bounds=None, where=None # Do rasterize tmp = gdal.Rasterize( output, source, outputBounds=bounds, xRes=pixelWidth, yRes=pixelHeight, - outputSRS=srs, outputType=getattr(gdal, dtype), noData=noData, where=where, + outputSRS=srs, noData=noData, where=where, creationOptions=co, targetAlignedPixels=aligned, **kwargs) if not isRaster(tmp): raise GeoKitRegionMaskError("Rasterization failed!") From fa7718845fc1ade42e5f74623cdbd843de2e235d Mon Sep 17 00:00:00 2001 From: Severin Ryberg Date: Wed, 22 Aug 2018 15:31:54 +0200 Subject: [PATCH 5/8] Fixed createRaster bug --- geokit/core/raster.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/geokit/core/raster.py b/geokit/core/raster.py index 2531f5e..1a450c1 100644 --- a/geokit/core/raster.py +++ b/geokit/core/raster.py @@ -152,7 +152,7 @@ def createRaster( bounds, output=None, pixelWidth=100, pixelHeight=100, dtype=No raise GeoKitRasterError("Output file already exists: %s" %output) # Ensure bounds is okay - bounds = fitBoundsTo(bounds, pixelWidth, pixelHeight) + # bounds = fitBoundsTo(bounds, pixelWidth, pixelHeight) ## Make a raster dataset and pull the band/maskBand objects originX = bounds[0] From 4699c142aafcca9c42a135f40359d1f087097c42 Mon Sep 17 00:00:00 2001 From: Severin Ryberg Date: Wed, 22 Aug 2018 15:37:19 +0200 Subject: [PATCH 6/8] same bug fix in quickRaster --- geokit/core/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/geokit/core/util.py b/geokit/core/util.py index 05572b1..bd05a86 100644 --- a/geokit/core/util.py +++ b/geokit/core/util.py @@ -351,7 +351,7 @@ def fitBoundsTo(bounds, dx, dy): def quickRaster(bounds, srs, dx, dy, dType="GDT_Byte", noData=None, fill=None, data=None, header=''): """GeoKit internal for quickly creating a raster datasource""" - bounds = fitBoundsTo(bounds, dx, dy) + #bounds = fitBoundsTo(bounds, dx, dy) ## Make a raster dataset and pull the band/maskBand objects originX = bounds[0] From ee4b05b8a9074c894f0e987face147a06334279c Mon Sep 17 00:00:00 2001 From: Severin Ryberg Date: Wed, 5 Sep 2018 09:25:33 +0200 Subject: [PATCH 7/8] Added automatic raster scaling --- geokit/core/raster.py | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/geokit/core/raster.py b/geokit/core/raster.py index 1a450c1..d5e430f 100644 --- a/geokit/core/raster.py +++ b/geokit/core/raster.py @@ -61,7 +61,7 @@ def gdalType(s): #################################################################### # Raster writer -def createRaster( bounds, output=None, pixelWidth=100, pixelHeight=100, dtype=None, srs='europe_m', compress=True, noData=None, overwrite=True, fill=None, data=None, meta=None, **kwargs): +def createRaster( bounds, output=None, pixelWidth=100, pixelHeight=100, dtype=None, srs='europe_m', compress=True, noData=None, overwrite=True, fill=None, data=None, meta=None, scale=0, offset=0, **kwargs): """Create a raster file NOTE: @@ -135,6 +135,16 @@ def createRaster( bounds, output=None, pixelWidth=100, pixelHeight=100, dtype=No * array dimensions must fit raster dimensions as calculated by the bounds and the pixel resolution + scale : numeric; optional + The scaling value given to apply to all values + - numeric + * Must be the same datatye as the 'dtype' input (or that which is derived) + + offset : numeric; optional + The offset value given to apply to all values + - numeric + * Must be the same datatye as the 'dtype' input (or that which is derived) + Returns: -------- * If 'output' is None: gdal.Dataset @@ -194,6 +204,8 @@ def createRaster( bounds, output=None, pixelWidth=100, pixelHeight=100, dtype=No # Fill the raster will zeros, null values, or initial values (if given) band = raster.GetRasterBand(1) + if not scale is None: band.SetScale(scale) + if not offset is None: band.SetOffset(offset) if( not noData is None): band.SetNoDataValue(noData) @@ -367,8 +379,12 @@ def extractMatrix(source, bounds=None, boundsSRS='latlon', maskBand=False, autoc ywin=None # get Data - if maskBand: data = mb.ReadAsArray(xoff=xoff, yoff=yoff, win_xsize=xwin, win_ysize=ywin) - else: data = sourceBand.ReadAsArray(xoff=xoff, yoff=yoff, win_xsize=xwin, win_ysize=ywin) + if maskBand: + data = mb.ReadAsArray(xoff=xoff, yoff=yoff, win_xsize=xwin, win_ysize=ywin) + else: + data = sourceBand.ReadAsArray(xoff=xoff, yoff=yoff, win_xsize=xwin, win_ysize=ywin) + if dsInfo.scale != 1.0: data = data*dsInfo.scale + if dsInfo.offset != 0.0: data = data+dsInfo.offset # Correct 'nodata' values if autocorrect: @@ -546,7 +562,7 @@ def isFlipped(source): if( dy<0 ): return True else: return False -RasterInfo = namedtuple("RasterInfo","srs dtype flipY yAtTop bounds xMin yMin xMax yMax dx dy pixelWidth pixelHeight noData, xWinSize, yWinSize, meta, source") +RasterInfo = namedtuple("RasterInfo","srs dtype flipY yAtTop bounds xMin yMin xMax yMax dx dy pixelWidth pixelHeight noData, xWinSize, yWinSize, meta, source, scale, offset") def rasterInfo(sourceDS): """Returns a named tuple containing information relating to the input raster @@ -566,6 +582,8 @@ def rasterInfo(sourceDS): dx:The raster's pixelWidth dy: The raster's pixelHeight noData: The noData value used by the raster + scale: The scale value used by the raster + offset: The offset value used by the raster xWinSize: The width of the raster is pixels yWinSize: The height of the raster is pixels meta: The raster's meta data ) @@ -581,6 +599,8 @@ def rasterInfo(sourceDS): sourceBand = sourceDS.GetRasterBand(1) output['dtype'] = sourceBand.DataType output['noData'] = sourceBand.GetNoDataValue() + output['scale'] = sourceBand.GetScale() + output['offset'] = sourceBand.GetOffset() xSize = sourceBand.XSize ySize = sourceBand.YSize @@ -765,11 +785,12 @@ def loadPoint(pt,s): for xi,yi,ib in zip(xStarts, yStarts, inBounds): if not ib: - data = np.zeros((window,window)) - data[:,:] = np.nan + data = np.empty((window,window)) else: # Open and read from raster data = band.ReadAsArray(xoff=xi, yoff=yi, win_xsize=window, win_ysize=window) + if info.scale != 1.0: data = data*info.scale + if info.offset != 0.0: data = data+info.offset # Look for nodata if not info.noData is None: From bd3fb17f05014237c84bb9e4d6f6f775209c142d Mon Sep 17 00:00:00 2001 From: Severin Ryberg Date: Wed, 5 Sep 2018 15:34:00 +0200 Subject: [PATCH 8/8] Fixed default raster scaling --- geokit/core/location.py | 4 ++++ geokit/core/raster.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/geokit/core/location.py b/geokit/core/location.py index 0276b6b..1d04d9d 100644 --- a/geokit/core/location.py +++ b/geokit/core/location.py @@ -258,6 +258,10 @@ def __init__(s, locations, srs=4326, _skip_check=False): if not _skip_check: if isinstance(locations, ogr.Geometry) or isinstance(locations, Location): s._locations = np.array([Location.load(locations, srs=srs), ]) + elif isinstance(locations, LocationSet): + s._locations = locations[:] + elif isinstance(locations, pd.DataFrame): + s._locations = LocationSet( locations["geom"] )[:] else: try: # Try loading all locations one at a time s._locations = np.array([Location.load(l, srs=srs) for l in locations]) diff --git a/geokit/core/raster.py b/geokit/core/raster.py index d5e430f..dbb2614 100644 --- a/geokit/core/raster.py +++ b/geokit/core/raster.py @@ -61,7 +61,7 @@ def gdalType(s): #################################################################### # Raster writer -def createRaster( bounds, output=None, pixelWidth=100, pixelHeight=100, dtype=None, srs='europe_m', compress=True, noData=None, overwrite=True, fill=None, data=None, meta=None, scale=0, offset=0, **kwargs): +def createRaster( bounds, output=None, pixelWidth=100, pixelHeight=100, dtype=None, srs='europe_m', compress=True, noData=None, overwrite=True, fill=None, data=None, meta=None, scale=1, offset=0, **kwargs): """Create a raster file NOTE: