Skip to content

Commit 1d937b2

Browse files
Merge pull request #291 from FZJ-IEK3-VSA/285-deal-with-rasters-without-srs-in-warp
285 deal with rasters without srs in warp
2 parents 2c1f642 + 2fcdf8e commit 1d937b2

File tree

9 files changed

+133
-58
lines changed

9 files changed

+133
-58
lines changed

.readthedocs.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ build:
1616
# - "jupyter-book config sphinx . --toc docs/_toc.yml --config docs/_config.yml"
1717
- echo "Start creation of config.py"
1818
# - jupyter-book config sphinx --toc docs/_toc.yaml --config docs/_config.yaml .readthedocs.yaml
19-
- conda run --name latest pip install -e . --no-deps
20-
- conda run --name latest jupyter-book config sphinx .
19+
- conda run --name $READTHEDOCS_VERSION pip install -e . --no-deps
20+
- conda run --name $READTHEDOCS_VERSION jupyter-book config sphinx .
2121
- echo "Creation of config.py is terminated"
2222
- cat conf.py
2323
- echo "conf.py content printed"
@@ -27,7 +27,7 @@ build:
2727
- ls -lR
2828
- echo $READTHEDOCS_OUTPUT
2929
# by the time of testing $READTHEDOCS_OUTPUT=_readthedocs
30-
- conda run --name latest sphinx-build -vvv -T -b html --jobs auto -D language=en . $READTHEDOCS_OUTPUT/html
30+
- conda run --name $READTHEDOCS_VERSION sphinx-build -vvv -T -b html --jobs auto -D language=en . $READTHEDOCS_OUTPUT/html
3131
- ls -lR
3232
conda:
3333
environment: requirements-dev.yml

geokit/__init__.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,39 @@
1313
ogr.UseExceptions()
1414
gdal.SetConfigOption("OGR_GEOMETRY_ACCEPT_UNCLOSED_RING", "YES")
1515

16+
import sys
17+
18+
# This allows to change warning filter from the command line
19+
if not sys.warnoptions:
20+
import os, warnings
21+
22+
# Suppress matplotlib deprecation warnings from pyparsing
23+
warnings.filterwarnings(
24+
action="ignore",
25+
message=".*'oneOf'.*deprecated.*",
26+
category=DeprecationWarning,
27+
module="matplotlib._fontconfig_pattern",
28+
)
29+
warnings.filterwarnings(
30+
action="ignore",
31+
message=".*'parseString'.*deprecated.*",
32+
category=DeprecationWarning,
33+
module="matplotlib._fontconfig_pattern",
34+
)
35+
warnings.filterwarnings(
36+
action="ignore",
37+
message=".*'resetCache'.*deprecated.*",
38+
category=DeprecationWarning,
39+
module="matplotlib._fontconfig_pattern",
40+
)
41+
warnings.filterwarnings(
42+
action="ignore",
43+
message=".*'enablePackrat'.*deprecated.*",
44+
category=DeprecationWarning,
45+
module="matplotlib._mathtext",
46+
)
47+
48+
1649
if not "GDAL_DATA" in _environ:
1750
from os.path import isdir as _isdir
1851
from sys import executable as _executable

geokit/core/extent.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,6 @@ def _quickRaster(
10231023
dy: numeric,
10241024
dtype: geokit_c_data_types_literal | None = None,
10251025
noData: numeric | None | bool = None,
1026-
fill: None | numeric | bool = None,
10271026
data: np.ndarray | None = None,
10281027
scale: numeric | None = None,
10291028
offset: numeric | None = None,
@@ -1064,7 +1063,6 @@ def _quickRaster(
10641063
srs=self.srs,
10651064
dtype=dtype,
10661065
noData=noData,
1067-
fill=fill,
10681066
data=data,
10691067
scale=scale,
10701068
offset=offset,
@@ -1644,7 +1642,6 @@ def rasterMosaic(
16441642
dx=raster_info.pixelWidth,
16451643
dy=raster_info.pixelHeight,
16461644
noData=raster_info.noData,
1647-
fill=raster_info.noData,
16481645
scale=raster_info.scale,
16491646
offset=raster_info.offset,
16501647
dtype=raster_info.data_type_name_str,

geokit/core/raster.py

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ def createRaster(
190190
191191
srs : Anything acceptable to geokit.srs.loadSRS(); optional
192192
The srs of the point to create
193-
* If not given, longitude/latitude is assumed
194-
* srs MUST be given as a keyword argument
193+
* If not given, no srs will be assigned to the created raster. Please be
194+
aware that some operations may not work correctly if no SRS is given.
195195
* If 'bounds' is an Extent object, the bounds' internal srs will override
196196
this input
197197
@@ -335,6 +335,11 @@ def createRaster(
335335
if srs is not None:
336336
rasterSRS = SRS.loadSRS(srs)
337337
raster.SetProjection(rasterSRS.ExportToWkt())
338+
else:
339+
warnings.warn(
340+
message="No srs given when creating raster. Please be aware that some operations may not work correctly.",
341+
category=UserWarning,
342+
)
338343

339344
# Fill the raster will zeros, null values, or initial values (if given)
340345
band: gdal.Band = raster.GetRasterBand(raster_band_index)
@@ -1965,7 +1970,7 @@ def drawRaster(
19651970
vmax: float | None = None,
19661971
cmap="viridis",
19671972
cbargs=None,
1968-
cutlineFillValue=-9999,
1973+
noData: numeric | None = None,
19691974
zorder=0,
19701975
resampleAlg: Literal[
19711976
"near",
@@ -2012,13 +2017,9 @@ def drawRaster(
20122017
cutline : str or ogr.Geometry; optional
20132018
The cutline to limit the drawn data too
20142019
* If a string is given, it must be a path to a vector file
2015-
* Values outside of the cutline are given the value 'cutlineFillValue'
2020+
* Values outside of the cutline are given the noData value of the raster
20162021
* Requires a warp
20172022
2018-
cutlineFillValue : numeric; optional
2019-
The value to give to values outside a cutline
2020-
* Has no effect when cutline is not given
2021-
20222023
figsize : (int, int); optional
20232024
The figure size to create when generating a new axis
20242025
* If resultign figure looks weird, altering the figure size is your best
@@ -2054,6 +2055,9 @@ def drawRaster(
20542055
20552056
cbargs : dict; optional
20562057
2058+
noData : numeric; optional
2059+
Replaces all previous noData values with this value in the output raster.
2060+
20572061
resampleAlg : str, optional
20582062
The resampleAlg passed on to a call of warp() if needed, by default "med"
20592063
@@ -2123,8 +2127,7 @@ def drawRaster(
21232127
pixelWidth=xres,
21242128
srs=srs,
21252129
bounds=bounds,
2126-
fill=cutlineFillValue,
2127-
noData=None,
2130+
noData=noData,
21282131
resampleAlg=resampleAlg,
21292132
**warp_kwargs,
21302133
)
@@ -2133,8 +2136,6 @@ def drawRaster(
21332136

21342137
# Read the Data
21352138
data = extractMatrix(source).astype(float)
2136-
if cutlineFillValue is not None:
2137-
data[data == cutlineFillValue] = np.nan
21382139

21392140
data[data == info.noData] = np.nan
21402141

@@ -2427,8 +2428,7 @@ def warp(
24272428
srs: srs_input | None = None,
24282429
bounds: tuple[numeric, numeric, numeric, numeric] | None = None,
24292430
dtype: None | geokit_c_data_types_literal = None,
2430-
noData=None,
2431-
fill: numeric | None = None,
2431+
noData: numeric | None = None,
24322432
overwrite: bool = True,
24332433
meta: None | dict[str, str] = None,
24342434
**kwargs,
@@ -2488,15 +2488,11 @@ def warp(
24882488
- a String such as "Byte", "UInt16", or "Double"
24892489
24902490
noData : numeric; optional
2491-
The no-data value to apply to the output raster
2491+
Replaces all previous noData values with this value in the output raster.
24922492
24932493
meta: dict; optional: contains a key value pair that is passed to the
24942494
output gdal.dataset using the SetMetadataItem method.
24952495
2496-
fill : numeric; optional
2497-
The fill data to place into the new raster before warping occurs
2498-
* Does not play a role when writing a file to disk
2499-
25002496
**kwargs:
25012497
* All keyword arguments are passed on to a call to gdal.WarpOptions
25022498
* Use these to fine-tune the warping procedure
@@ -2570,12 +2566,6 @@ def warp(
25702566
pass
25712567
else:
25722568
raise GeoKitRasterError("noData must be a numeric or boolean value but got: %s" % str(type(noDataRead)))
2573-
if isinstance(fill, (numeric, bool)):
2574-
list_of_numbers.append(fill)
2575-
elif fill is None:
2576-
pass
2577-
else:
2578-
raise GeoKitRasterError("fill must be a numeric boolean or None value but got: %s" % str(type(fill)))
25792569

25802570
list_of_datatypes = []
25812571
if isinstance(dtype, str):
@@ -2675,7 +2665,6 @@ def warp(
26752665
dy=pixelHeight,
26762666
dtype=gdal_data_type_string,
26772667
noData=noDataRead,
2678-
fill=fill,
26792668
)
26802669

26812670
# Do a warp

geokit/core/util.py

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -423,17 +423,44 @@ def _check_fill_return_code(fill_return_code):
423423

424424
def quickRaster(
425425
bounds: tuple[numeric, numeric, numeric, numeric],
426-
srs: osr.SpatialReference,
426+
srs: osr.SpatialReference | None,
427427
dx: numeric,
428428
dy: numeric,
429429
dtype: geokit_c_data_types_literal | None = None,
430430
noData: None | numeric | bool = None,
431-
fill: None | numeric | bool = None,
432431
data: np.ndarray | None = None,
433432
scale: numeric | None = None,
434433
offset: numeric | None = None,
435-
):
436-
"""GeoKit internal for quickly creating a raster datasource."""
434+
) -> gdal.Dataset:
435+
"""
436+
GeoKit internal for quickly creating a raster datasource.
437+
438+
Parameters
439+
----------
440+
bounds : tuple[numeric, numeric, numeric, numeric]
441+
The bounds to create the raster for
442+
srs : osr.SpatialReference | None
443+
The SRS to assign to the raster. Please be aware that some operations may not work correctly if no SRS is given.
444+
dx : numeric
445+
The pixel width in x direction.
446+
dy : numeric
447+
The pixel height in y direction.
448+
dtype : geokit_c_data_types_literal | None, optional
449+
The GDAL C datatype to use for the raster band. by default None
450+
noData : None | numeric | bool, optional
451+
The value to use for no data in the raster band, by default None
452+
data : np.ndarray | None, optional
453+
The data array to write to the raster band, by default None
454+
scale : numeric | None, optional
455+
The scale factor to apply to the raster band, by default None
456+
offset : numeric | None, optional
457+
The offset to apply to the raster band, by default None
458+
459+
Returns
460+
-------
461+
A raster dataset as gdal.Dataset
462+
463+
"""
437464
# bounds = fitBoundsTo(bounds, dx, dy)
438465

439466
# Make a raster dataset and pull the band/maskBand objects
@@ -452,11 +479,6 @@ def quickRaster(
452479
else:
453480
list_of_scalars.append(noData)
454481

455-
if fill is None:
456-
pass
457-
else:
458-
list_of_scalars.append(fill)
459-
460482
list_of_datatype_strings = []
461483
if dtype is None:
462484
list_of_gdal_data_type_strings = []
@@ -476,20 +498,24 @@ def quickRaster(
476498
raster.SetGeoTransform((originX, abs(dx), 0, originY, 0, -1 * abs(dy)))
477499

478500
# Set the SRS
479-
raster.SetProjection(srs.ExportToWkt())
501+
if srs is not None:
502+
raster.SetProjection(srs.ExportToWkt())
503+
else:
504+
warnings.warn(
505+
message="No srs given when creating raster. Please be aware that some operations may not work correctly.",
506+
category=UserWarning,
507+
)
480508

481509
# get the band
482510
band: gdal.Band = raster.GetRasterBand(1)
483511

484512
# set optionals
485513
if not noData is None:
486514
band.SetNoDataValue(noData)
487-
if fill is None and data is None:
515+
516+
if data is None:
488517
no_data_fill_return_code = band.Fill(noData)
489518
_check_fill_return_code(fill_return_code=no_data_fill_return_code)
490-
if not fill is None:
491-
fill_return_code = band.Fill(fill)
492-
_check_fill_return_code(fill_return_code=fill_return_code)
493519

494520
if not scale is None:
495521
band.SetScale(scale)

geokit/core/vector.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,7 +1598,6 @@ def rasterize(
15981598
compress=True,
15991599
noData=None,
16001600
overwrite: bool = True,
1601-
fill=None,
16021601
**kwargs,
16031602
) -> gdal.Dataset | str:
16041603
"""Rasterize a vector datasource onto a raster context.
@@ -1666,11 +1665,6 @@ def rasterize(
16661665
raster
16671666
* Must be the same datatype as the 'dtype' input (or that which is derived)
16681667
1669-
fill : numeric; optional
1670-
The initial value given to all pixels in the created raster band
1671-
- numeric
1672-
* Must be the same datatype as the 'dtype' input (or that which is derived)
1673-
16741668
overwrite : bool
16751669
A flag to overwrite a pre-existing output file
16761670
* If set to False and an 'output' is specified which already exists,
@@ -1735,8 +1729,6 @@ def rasterize(
17351729
]
17361730
list_of_numbers.append(value)
17371731

1738-
if isinstance(fill, (numeric, bool)):
1739-
list_of_numbers.append(fill)
17401732
if isinstance(noData, (numeric, bool)):
17411733
list_of_numbers.append(noData)
17421734

@@ -1756,7 +1748,6 @@ def rasterize(
17561748
dy=pixelHeight,
17571749
dtype=minimum_data_type_string,
17581750
noData=noData,
1759-
fill=fill,
17601751
)
17611752

17621753
# Do rasterize

geokit/data/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
gebco_tiles.zip
33
iho.zip
44
turbine_locations_shapefile.zip
5-
5+
turbine_locations shapefile.zip
66
# Folder
77
gebco_tiles/
88
iho/

test/test_01_util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def test_fitBoundsTo():
122122

123123
def test_quickRaster():
124124
load_srs = loadSRS(source=4326)
125-
new_raster = geokit.core.util.quickRaster(bounds=(0, 0, 4, 4), srs=load_srs, dx=1, dy=1, noData=-9999, fill=-9999)
125+
new_raster = geokit.core.util.quickRaster(bounds=(0, 0, 4, 4), srs=load_srs, dx=1, dy=1, noData=-9999)
126126
extracted_raster = extractMatrix(source=new_raster)
127127
raster_for_comparison = np.array(
128128
[

0 commit comments

Comments
 (0)