yirgacheffe 1.4.1__py3-none-any.whl → 1.6.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of yirgacheffe might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  from __future__ import annotations
2
- import os
3
2
  from math import ceil, floor
3
+ from pathlib import Path
4
4
  from typing import Any, Optional, Tuple, Union
5
5
  from typing_extensions import NotRequired
6
6
 
@@ -62,7 +62,7 @@ class RasteredVectorLayer(RasterLayer):
62
62
  @classmethod
63
63
  def layer_from_file( # type: ignore[override] # pylint: disable=W0221
64
64
  cls,
65
- filename: str,
65
+ filename: Union[Path,str],
66
66
  where_filter: Optional[str],
67
67
  scale: PixelScale,
68
68
  projection: str,
@@ -172,7 +172,7 @@ class VectorLayer(YirgacheffeLayer):
172
172
  @classmethod
173
173
  def layer_from_file_like(
174
174
  cls,
175
- filename: str,
175
+ filename: Union[Path,str],
176
176
  other_layer: YirgacheffeLayer,
177
177
  where_filter: Optional[str]=None,
178
178
  datatype: Optional[Union[int, DataType]] = None,
@@ -198,7 +198,7 @@ class VectorLayer(YirgacheffeLayer):
198
198
  layer,
199
199
  other_layer.pixel_scale,
200
200
  other_layer.projection,
201
- name=filename,
201
+ name=str(filename),
202
202
  datatype=datatype if datatype is not None else other_layer.datatype,
203
203
  burn_value=burn_value,
204
204
  anchor=(other_layer.area.left, other_layer.area.top),
@@ -208,14 +208,14 @@ class VectorLayer(YirgacheffeLayer):
208
208
  # a SIGSEGV when using the layers from it later, as some SWIG pointers outlive
209
209
  # the original object being around
210
210
  vector_layer._original = vectors
211
- vector_layer._dataset_path = filename
211
+ vector_layer._dataset_path = filename if isinstance(filename, Path) else Path(filename)
212
212
  vector_layer._filter = where_filter
213
213
  return vector_layer
214
214
 
215
215
  @classmethod
216
216
  def layer_from_file(
217
217
  cls,
218
- filename: str,
218
+ filename: Union[Path,str],
219
219
  where_filter: Optional[str],
220
220
  scale: PixelScale,
221
221
  projection: str,
@@ -223,9 +223,11 @@ class VectorLayer(YirgacheffeLayer):
223
223
  burn_value: Union[int,float,str] = 1,
224
224
  anchor: Tuple[float,float] = (0.0, 0.0)
225
225
  ) -> VectorLayer:
226
- vectors = ogr.Open(filename)
227
- if vectors is None:
228
- raise FileNotFoundError(filename)
226
+ try:
227
+ vectors = ogr.Open(filename)
228
+ except RuntimeError as exc:
229
+ # With exceptions on GDAL now returns the wrong (IMHO) exception
230
+ raise FileNotFoundError(filename) from exc
229
231
  layer = vectors.GetLayer()
230
232
  if where_filter is not None:
231
233
  layer.SetAttributeFilter(where_filter)
@@ -243,7 +245,7 @@ class VectorLayer(YirgacheffeLayer):
243
245
  layer,
244
246
  scale,
245
247
  projection,
246
- name=filename,
248
+ name=str(filename),
247
249
  datatype=datatype_arg,
248
250
  burn_value=burn_value,
249
251
  anchor=anchor
@@ -253,7 +255,7 @@ class VectorLayer(YirgacheffeLayer):
253
255
  # a SIGSEGV when using the layers from it later, as some SWIG pointers outlive
254
256
  # the original object being around
255
257
  vector_layer._original = vectors
256
- vector_layer._dataset_path = filename
258
+ vector_layer._dataset_path = filename if isinstance(filename, Path) else Path(filename)
257
259
  vector_layer._filter = where_filter
258
260
  return vector_layer
259
261
 
@@ -282,7 +284,7 @@ class VectorLayer(YirgacheffeLayer):
282
284
  self.burn_value = burn_value
283
285
 
284
286
  self._original = None
285
- self._dataset_path: Optional[str] = None
287
+ self._dataset_path: Optional[Path] = None
286
288
  self._filter: Optional[str] = None
287
289
 
288
290
  # work out region for mask
@@ -323,7 +325,7 @@ class VectorLayer(YirgacheffeLayer):
323
325
 
324
326
  def __getstate__(self) -> object:
325
327
  # Only support pickling on file backed layers (ideally read only ones...)
326
- if self._dataset_path is None or not os.path.isfile(self._dataset_path):
328
+ if self._dataset_path is None or not self._dataset_path.exists():
327
329
  raise ValueError("Can not pickle layer that is not file backed.")
328
330
  odict = self.__dict__.copy()
329
331
  del odict['_original']
@@ -357,7 +359,14 @@ class VectorLayer(YirgacheffeLayer):
357
359
  def datatype(self) -> DataType:
358
360
  return self._datatype
359
361
 
360
- def read_array_for_area(self, target_area: Area, x: int, y: int, width: int, height: int) -> Any:
362
+ def _read_array_for_area(
363
+ self,
364
+ target_area: Area,
365
+ x: int,
366
+ y: int,
367
+ width: int,
368
+ height: int,
369
+ ) -> Any:
361
370
  assert self._pixel_scale is not None
362
371
 
363
372
  if self._original is None:
@@ -397,8 +406,8 @@ class VectorLayer(YirgacheffeLayer):
397
406
  res = backend.promote(dataset.ReadAsArray(0, 0, width, height))
398
407
  return res
399
408
 
400
- def read_array_with_window(self, _x, _y, _width, _height, _window) -> Any:
409
+ def _read_array_with_window(self, _x, _y, _width, _height, _window) -> Any:
401
410
  assert NotRequired
402
411
 
403
412
  def read_array(self, x: int, y: int, width: int, height: int) -> Any:
404
- return self.read_array_for_area(self._active_area, x, y, width, height)
413
+ return self._read_array_for_area(self._active_area, x, y, width, height)
yirgacheffe/operators.py CHANGED
@@ -1,15 +1,19 @@
1
1
  import logging
2
2
  import math
3
3
  import multiprocessing
4
+ import os
4
5
  import sys
6
+ import tempfile
5
7
  import time
6
8
  import types
7
9
  from enum import Enum
8
10
  from multiprocessing import Semaphore, Process
9
11
  from multiprocessing.managers import SharedMemoryManager
10
- from typing import Callable, Optional
12
+ from pathlib import Path
13
+ from typing import Callable, Dict, Optional, Union
11
14
 
12
15
  import numpy as np
16
+ import numpy.typing as npt
13
17
  from osgeo import gdal
14
18
  from dill import dumps, loads # type: ignore
15
19
 
@@ -40,6 +44,9 @@ class LayerConstant:
40
44
  def _eval(self, _area, _index, _step, _target_window):
41
45
  return self.val
42
46
 
47
+ @property
48
+ def area(self):
49
+ return Area.world()
43
50
 
44
51
  class LayerMathMixin:
45
52
 
@@ -91,9 +98,9 @@ class LayerMathMixin:
91
98
  def _eval(self, area, index, step, target_window=None):
92
99
  try:
93
100
  window = self.window if target_window is None else target_window
94
- return self.read_array_for_area(area, 0, index, window.xsize, step)
101
+ return self._read_array_for_area(area, 0, index, window.xsize, step)
95
102
  except AttributeError:
96
- return self.read_array_for_area(area, 0, index, target_window.xsize if target_window else 1, step)
103
+ return self._read_array_for_area(area, 0, index, target_window.xsize if target_window else 1, step)
97
104
 
98
105
  def nan_to_num(self, nan=0, posinf=None, neginf=None):
99
106
  return LayerOperation(
@@ -346,25 +353,19 @@ class LayerOperation(LayerMathMixin):
346
353
  self.__dict__.update(state)
347
354
 
348
355
  @property
349
- def area(self) -> Optional[Area]:
356
+ def area(self) -> Area:
350
357
  # The type().__name__ here is to avoid a circular import dependancy
351
- lhs_area = self.lhs.area if not type(self.lhs).__name__ == "ConstantLayer" else None
358
+ lhs_area = self.lhs.area
352
359
  try:
353
- rhs_area = self.rhs.area if not type(self.rhs).__name__ == "ConstantLayer" else None # type: ignore[return-value] # pylint: disable=C0301
360
+ rhs_area = self.rhs.area
354
361
  except AttributeError:
355
362
  rhs_area = None
356
363
  try:
357
- other_area = self.other.area if not type(self.other).__name__ == "ConstantLayer" else None # type: ignore[return-value] # pylint: disable=C0301
364
+ other_area = self.other.area
358
365
  except AttributeError:
359
366
  other_area = None
360
367
 
361
- all_areas = []
362
- if lhs_area is not None:
363
- all_areas.append(lhs_area)
364
- if rhs_area is not None:
365
- all_areas.append(rhs_area)
366
- if other_area is not None:
367
- all_areas.append(other_area)
368
+ all_areas = [x for x in [lhs_area, rhs_area, other_area] if (x is not None) and (not x.is_world)]
368
369
 
369
370
  match self.window_op:
370
371
  case WindowOperation.NONE:
@@ -511,7 +512,7 @@ class LayerOperation(LayerMathMixin):
511
512
  res = chunk_max
512
513
  return res
513
514
 
514
- def save(self, destination_layer, and_sum=False, callback=None, band=1):
515
+ def save(self, destination_layer, and_sum=False, callback=None, band=1) -> Optional[float]:
515
516
  """
516
517
  Calling save will write the output of the operation to the provied layer.
517
518
  If you provide sum as true it will additionall compute the sum and return that.
@@ -608,7 +609,14 @@ class LayerOperation(LayerMathMixin):
608
609
  except AttributeError:
609
610
  pass
610
611
 
611
- def _parallel_save(self, destination_layer, and_sum=False, callback=None, parallelism=None, band=1):
612
+ def _parallel_save(
613
+ self,
614
+ destination_layer,
615
+ and_sum=False,
616
+ callback=None,
617
+ parallelism=None,
618
+ band=1
619
+ ) -> Optional[float]:
612
620
  assert (destination_layer is not None) or and_sum
613
621
  try:
614
622
  computation_window = self.window
@@ -648,7 +656,7 @@ class LayerOperation(LayerMathMixin):
648
656
  or (computation_window.ysize != destination_window.ysize):
649
657
  raise ValueError("Destination raster window size does not match input raster window size.")
650
658
 
651
- np_dtype = {
659
+ np_type_map : Dict[int, np.dtype] = {
652
660
  gdal.GDT_Byte: np.dtype('byte'),
653
661
  gdal.GDT_Float32: np.dtype('float32'),
654
662
  gdal.GDT_Float64: np.dtype('float64'),
@@ -659,7 +667,8 @@ class LayerOperation(LayerMathMixin):
659
667
  gdal.GDT_UInt16: np.dtype('uint16'),
660
668
  gdal.GDT_UInt32: np.dtype('uint32'),
661
669
  gdal.GDT_UInt64: np.dtype('uint64'),
662
- }[band.DataType]
670
+ }
671
+ np_dtype = np_type_map[band.DataType]
663
672
  else:
664
673
  band = None
665
674
  np_dtype = np.dtype('float64')
@@ -674,9 +683,13 @@ class LayerOperation(LayerMathMixin):
674
683
  with SharedMemoryManager() as smm:
675
684
 
676
685
  mem_sem_cast = []
677
- for i in range(worker_count):
686
+ for _ in range(worker_count):
678
687
  shared_buf = smm.SharedMemory(size=np_dtype.itemsize * self.ystep * computation_window.xsize)
679
- cast_buf = np.ndarray((self.ystep, computation_window.xsize), dtype=np_dtype, buffer=shared_buf.buf)
688
+ cast_buf : npt.NDArray = np.ndarray(
689
+ (self.ystep, computation_window.xsize),
690
+ dtype=np_dtype,
691
+ buffer=shared_buf.buf
692
+ )
680
693
  cast_buf[:] = np.zeros((self.ystep, computation_window.xsize), np_dtype)
681
694
  mem_sem_cast.append((shared_buf, Semaphore(), cast_buf))
682
695
 
@@ -746,7 +759,14 @@ class LayerOperation(LayerMathMixin):
746
759
 
747
760
  return total if and_sum else None
748
761
 
749
- def parallel_save(self, destination_layer, and_sum=False, callback=None, parallelism=None, band=1):
762
+ def parallel_save(
763
+ self,
764
+ destination_layer,
765
+ and_sum=False,
766
+ callback=None,
767
+ parallelism=None,
768
+ band=1
769
+ ) -> Optional[float]:
750
770
  if destination_layer is None:
751
771
  raise ValueError("Layer is required")
752
772
  return self._parallel_save(destination_layer, and_sum, callback, parallelism, band)
@@ -754,6 +774,50 @@ class LayerOperation(LayerMathMixin):
754
774
  def parallel_sum(self, callback=None, parallelism=None, band=1):
755
775
  return self._parallel_save(None, True, callback, parallelism, band)
756
776
 
777
+ def to_geotiff(
778
+ self,
779
+ filename: Union[Path,str],
780
+ and_sum: bool = False,
781
+ parallelism:Optional[int]=None
782
+ ) -> Optional[float]:
783
+ """Saves a calculation to a raster file, optionally also returning the sum of pixels.
784
+
785
+ Parameters
786
+ ----------
787
+ filename : Path
788
+ Path of the raster to save the result to.
789
+ and_sum : bool, default=False
790
+ If true then the function will also calculate the sum of the raster as it goes and return that value.
791
+ parallelism : int, optional, default=None
792
+ If passed, attempt to use multiple CPU cores up to the number provided.
793
+
794
+ Returns
795
+ -------
796
+ float, optional
797
+ Either returns None, or the sum of the pixels in the resulting raster if `and_sum` was specified.
798
+ """
799
+
800
+ # We want to write to a tempfile before we move the result into place, but we can't use
801
+ # the actual $TMPDIR as that might be on a different device, and so we use a file next to where
802
+ # the final file will be, so we just need to rename the file at the end, not move it.
803
+ if isinstance(filename, str):
804
+ filename = Path(filename)
805
+ target_dir = filename.parent
806
+
807
+ with tempfile.NamedTemporaryFile(dir=target_dir, delete=False) as tempory_file:
808
+ # Local import due to circular dependancy
809
+ from yirgacheffe.layers.rasters import RasterLayer # type: ignore # pylint: disable=C0415
810
+ with RasterLayer.empty_raster_layer_like(self, filename=tempory_file.name) as layer:
811
+ if parallelism is None:
812
+ result = self.save(layer, and_sum=and_sum)
813
+ else:
814
+ result = self.parallel_save(layer, and_sum=and_sum, parallelism=parallelism)
815
+
816
+ os.makedirs(target_dir, exist_ok=True)
817
+ os.rename(src=tempory_file.name, dst=filename)
818
+
819
+ return result
820
+
757
821
  class ShaderStyleOperation(LayerOperation):
758
822
 
759
823
  def _eval(self, area, index, step, target_window=None):
yirgacheffe/window.py CHANGED
@@ -1,3 +1,4 @@
1
+ from __future__ import annotations
1
2
  import math
2
3
  import sys
3
4
  from collections import namedtuple
@@ -37,6 +38,17 @@ class Area:
37
38
  right: float
38
39
  bottom: float
39
40
 
41
+ @staticmethod
42
+ def world() -> Area:
43
+ """Creates an area that covers the entire planet.
44
+
45
+ Returns
46
+ -------
47
+ Area
48
+ An area where the extents are nan, but is_world returns true.
49
+ """
50
+ return Area(float("nan"), float("nan"), float("nan"), float("nan"))
51
+
40
52
  def __hash__(self):
41
53
  return (self.left, self.top, self.right, self.bottom).__hash__()
42
54
 
@@ -69,6 +81,17 @@ class Area:
69
81
  bottom=self.bottom - offset
70
82
  )
71
83
 
84
+ @property
85
+ def is_world(self) -> bool:
86
+ """Returns true if this is a global area, independant of projection.
87
+
88
+ Returns
89
+ -------
90
+ bool
91
+ True is the Area was created with `world` otherwise False.
92
+ """
93
+ return math.isnan(self.left)
94
+
72
95
  def overlaps(self, other) -> bool:
73
96
  """Check if this area overlaps with another area.
74
97
 
@@ -82,6 +105,10 @@ class Area:
82
105
  bool
83
106
  True if the two areas intersect, otherwise false.
84
107
  """
108
+
109
+ if self.is_world or other.is_world:
110
+ return True
111
+
85
112
  return (
86
113
  (self.left <= other.left <= self.right) or
87
114
  (self.left <= other.right <= self.right) or
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: yirgacheffe
3
- Version: 1.4.1
3
+ Version: 1.6.0
4
4
  Summary: Abstraction of gdal datasets for doing basic math operations
5
5
  Author-email: Michael Dales <mwd24@cam.ac.uk>
6
6
  License-Expression: ISC
@@ -53,12 +53,12 @@ The motivation for Yirgacheffe layers is to make working with gdal data slightly
53
53
  For example, if we wanted to do a simple [Area of Habitat](https://github.com/quantifyearth/aoh-calculator/) calculation, whereby we find the pixels where a species resides by combining its range polygon, its habitat preferences, and its elevation preferences, the code would be like this:
54
54
 
55
55
  ```python
56
- from yirgacheffe.layer import RasterLayer, VectorLayer
56
+ import yirgaceffe as yg
57
57
 
58
- habitat_map = RasterLayer.layer_from_file("habitats.tif")
59
- elevation_map = RasterLayer.layer_from_file('elevation.tif')
60
- range_polygon = VectorLayer.layer_from_file('species123.geojson', raster_like=habitat_map)
61
- area_per_pixel_map = RasterLayer.layer_from_file('area_per_pixel.tif')
58
+ habitat_map = yg.read_raster("habitats.tif")
59
+ elevation_map = yg.read_raster('elevation.tif')
60
+ range_polygon = yg.read_shape_like('species123.geojson', like=habitat_map)
61
+ area_per_pixel_map = yg.read_raster('area_per_pixel.tif')
62
62
 
63
63
  refined_habitat = habitat_map.isin([...species habitat codes...])
64
64
  refined_elevation = (elevation_map >= species_min) && (elevation_map <= species_max)
@@ -71,8 +71,8 @@ print(f'area for species 123: {aoh.sum()}')
71
71
  Similarly, you could save the result to a new raster layer:
72
72
 
73
73
  ```python
74
- with RasterLayer.empty_raster_layer_like(aoh, filename="result.tif") as result:
75
- aoh.save(result)
74
+ ...
75
+ aoh.to_geotiff("result.tif")
76
76
  ```
77
77
 
78
78
  Yirgacheffe will automatically infer if you want to do an intersection of maps or a union of the maps based on the operators you use (see below for a full table). You can explicitly override that if you want.
@@ -107,30 +107,31 @@ If you have set either the intersection window or union window on a layer and yo
107
107
 
108
108
  ### Direct access to data
109
109
 
110
- If doing per-layer operations isn't applicable for your application, you can read the pixel values for all layers (including VectorLayers) by calling `read_array` similarly to how you would for gdal. The data you access will be relative to the specified window - that is, if you've called either `set_window_for_intersection` or `set_window_for_union` then `read_array` will be relative to that and Yirgacheffe will clip or expand the data with zero values as necessary.
111
-
112
-
113
- ### Todo but not supported
114
-
115
- Yirgacheffe is work in progress, so things planned but not supported currently:
116
-
117
- * Dynamic pixel scale adjustment - all raster layers must be provided at the same pixel scale currently *NOW IN EXPERIMENTAL TESTING, SEE BELOW*
118
- * A fold operation
119
- * CUDA/Metal support via CUPY/MLX
120
- * Dispatching work across multiple CPUs *NOW IN EXPERIMENTAL TESTING, SEE BELOW*
121
-
110
+ If doing per-layer operations isn't applicable for your application, you can read the pixel values for all layers (including VectorLayers) by calling `read_array` similarly to how you would for GDAL. The data you access will be relative to the specified window - that is, if you've called either `set_window_for_intersection` or `set_window_for_union` then `read_array` will be relative to that and Yirgacheffe will clip or expand the data with zero values as necessary.
122
111
 
123
112
 
124
113
  ## Layer types
125
114
 
115
+ Note that as part of the move to the next major release, 2.0, we are adding simpler ways to create layers. Not all of those have been implemented yet, which is why this section has some inconsistencies. However, given many of the common cases are already covered, we present the new 2.0 style methods (`read_raster` and similar) here so you can write cleaner code today rather than making people wait for the final 2.0 release.
116
+
126
117
  ### RasterLayer
127
118
 
128
119
  This is your basic GDAL raster layer, which you load from a geotiff.
129
120
 
130
121
  ```python
122
+ from yirgaceffe.layers import RasterLayer
123
+
131
124
  with RasterLayer.layer_from_file('test1.tif') as layer:
132
- data = layer.read_array(0, 0, 10, 10)
133
- ...
125
+ total = layer.sum()
126
+ ```
127
+
128
+ The new 2.0 way of doing this is:
129
+
130
+ ```python
131
+ import yirgacheffe as yg
132
+
133
+ with yg.read_raster('test.tif') as layer:
134
+ total = layer.sum()
134
135
  ```
135
136
 
136
137
  You can also create empty layers ready for you to store results, either by taking the dimensions from an existing layer. In both these cases you can either provide a filename to which the data will be written, or if you do not provide a filename then the layer will only exist in memory - this will be more efficient if the layer is being used for intermediary results.
@@ -159,6 +160,7 @@ with RasterLayer.layer_from_file('test1.tif') as source:
159
160
  scaled = RasterLayer.scaled_raster_from_raster(source, PixelScale(0.0001, -0.0001), 'scaled.tif')
160
161
  ```
161
162
 
163
+ If the data is from a GeoTIFF that has a nodata value specified, then pixel values with that specified nodata value in them will be converted to NaN. You can override that by providing `ignore_nodata=True` as an optional argument to `layer_from_file` (or with the new 2.0 API, `read_raster`). You can find out if a layer has a nodata value by accessing the `nodata` property - it is None if there is no such value.
162
164
 
163
165
  ### VectorLayer
164
166
 
@@ -167,51 +169,30 @@ This layer will load vector data and rasterize it on demand as part of a calcula
167
169
  Because it will be rasterized you need to specify the pixel scale and map projection to be used when rasterising the data, and the common way to do that is by using one of your other layers.
168
170
 
169
171
  ```python
170
- with VectorLayer.layer_from_file('range.gpkg', 'id_no == 42', layer1.pixel_scale, layer1.projection) as layer:
172
+ from yirgaceffe import WGS_84_PROJECTION
173
+ from yirgaceffe.window import PixelScale
174
+ from yirgaceffe.layers import VectorLayer
175
+
176
+ with VectorLayer.layer_from_file('range.gpkg', PixelScale(0.001, -0.001), WGS_84_PROJECTION) as layer:
171
177
  ...
172
178
  ```
173
179
 
174
- This class was formerly called `DynamicVectorRangeLayer`, a name now deprecated.
175
-
176
-
177
- ### UniformAreaLayer
178
-
179
- In certain calculations you find you have a layer where all the rows of data are the same - notably geotiffs that contain the area of a given pixel do this due to how conventional map projections work. It's hugely inefficient to load the full map into memory, so whilst you could just load them as `Layer` types, we recommend you do:
180
+ The new 2.0 way of doing this is:
180
181
 
181
182
  ```python
182
- with UniformAreaLayer('area.tiff') as layer:
183
- ....
184
- ```
185
-
186
- Note that loading this data can still be very slow, due to how image compression works. So if you plan to use area.tiff more than once, we recommend use save an optimised version - this will do the slow uncompression once and then save a minimal file to speed up future processing:
183
+ import yirgacheffe as yg
187
184
 
188
- ```python
189
- if not os.path.exists('yirgacheffe_area.tiff'):
190
- UniformAreaLayer.generate_narrow_area_projection('area.tiff', 'yirgacheffe_area.tiff')
191
- area_layer = UniformAreaLayer('yirgacheffe_area.tiff')
185
+ with yg.read_shape('range.gpkg', (0.001, -0.001), WGS_84_PROJECTION) as layer:
186
+ ...
192
187
  ```
193
188
 
189
+ It is more common that when a shape file is loaded that its pixel size and projection will want to be made to match that of an existing raster (as per the opening area of habitat example). For that there is the following convenience method:
194
190
 
195
- ### ConstantLayer
196
-
197
- This is there to simplify code when you have some optional layers. Rather than littering your code with checks, you can just use a constant layer, which can be included in calculations and will just return an fixed value as if it wasn't there. Useful with 0.0 or 1.0 for sum or multiplication null layers.
198
191
 
199
192
  ```python
200
- try:
201
- area_layer = UniformAreaLayer('myarea.tiff')
202
- except FileDoesNotExist:
203
- area_layer = ConstantLayer(0.0)
204
- ```
205
-
206
-
207
- ### H3CellLayer
208
-
209
- If you have H3 installed, you can generate a mask layer based on an H3 cell identifier, where pixels inside the cell will have a value of 1, and those outside will have a value of 0.
210
-
211
- Becuase it will be rasterized you need to specify the pixel scale and map projection to be used when rasterising the data, and the common way to do that is by using one of your other layers.
212
-
213
- ```python
214
- hex_cell_layer = H3CellLayer('88972eac11fffff', layer1.pixel_scale, layer1.projection)
193
+ with yg.read_raster("test.tif") as raster_layer:
194
+ with yg.read_shape_like('range.gpkg', raster_layer) as shape_layer:
195
+ ...
215
196
  ```
216
197
 
217
198
  ### GroupLayer
@@ -240,6 +221,18 @@ with GroupLayer.layer_from_directory('.') as all_tiles:
240
221
  ...
241
222
  ```
242
223
 
224
+ The new 2.0 way of doing this is:
225
+
226
+ ```python
227
+ import yirgacheffe as yg
228
+
229
+ with yg.read_rasters(['tile_N10_E10.tif', 'tile_N20_E10.tif']) as all_tiles:
230
+ ...
231
+ ```
232
+
233
+ If any of the layers have a `nodata` value specified, then any pixel with that value will be masked out to allow data from other layers to be visible.
234
+
235
+
243
236
  ### TiledGroupLayer
244
237
 
245
238
  This is a specialisation of GroupLayer, which you can use if your layers are all the same size and form a grid, as is often the case with map tiles. In this case the rendering code can be optimised and this class is significantly faster that GroupLayer.
@@ -250,11 +243,59 @@ tile2 = RasterLayer.layer_from_file('tile_N20_E10.tif')
250
243
  all_tiles = TiledGroupLayer([tile1, tile2])
251
244
  ```
252
245
 
246
+ The new 2.0 way of doing this is:
247
+
248
+ ```python
249
+ import yirgacheffe as yg
250
+
251
+ with yg.read_rasters(['tile_N10_E10.tif', 'tile_N20_E10.tif'], tiled=True) as all_tiles:
252
+ ...
253
+ ```
254
+
253
255
  Notes:
254
256
 
255
257
  * You can have missing tiles, and these will be filled in with zeros.
256
258
  * You can have tiles that overlap, so long as they still conform to the rule that all tiles are the same size and on a grid.
257
259
 
260
+ ### ConstantLayer
261
+
262
+ This is there to simplify code when you have some optional layers. Rather than littering your code with checks, you can just use a constant layer, which can be included in calculations and will just return an fixed value as if it wasn't there. Useful with 0.0 or 1.0 for sum or multiplication null layers.
263
+
264
+ ```python
265
+ try:
266
+ area_layer = RasterLayer.layer_from_file('myarea.tiff')
267
+ except FileDoesNotExist:
268
+ area_layer = ConstantLayer(0.0)
269
+ ```
270
+
271
+ ### H3CellLayer
272
+
273
+ If you have H3 installed, you can generate a mask layer based on an H3 cell identifier, where pixels inside the cell will have a value of 1, and those outside will have a value of 0.
274
+
275
+ Becuase it will be rasterized you need to specify the pixel scale and map projection to be used when rasterising the data, and the common way to do that is by using one of your other layers.
276
+
277
+ ```python
278
+ hex_cell_layer = H3CellLayer('88972eac11fffff', layer1.pixel_scale, layer1.projection)
279
+ ```
280
+
281
+
282
+ ### UniformAreaLayer
283
+
284
+ In certain calculations you find you have a layer where all the rows of data are the same - notably geotiffs that contain the area of a given pixel do this due to how conventional map projections work. It's hugely inefficient to load the full map into memory, so whilst you could just load them as `Layer` types, we recommend you do:
285
+
286
+ ```python
287
+ with UniformAreaLayer('area.tiff') as layer:
288
+ ....
289
+ ```
290
+
291
+ Note that loading this data can still be very slow, due to how image compression works. So if you plan to use area.tiff more than once, we recommend use save an optimised version - this will do the slow uncompression once and then save a minimal file to speed up future processing:
292
+
293
+ ```python
294
+ if not os.path.exists('yirgacheffe_area.tiff'):
295
+ UniformAreaLayer.generate_narrow_area_projection('area.tiff', 'yirgacheffe_area.tiff')
296
+ area_layer = UniformAreaLayer('yirgacheffe_area.tiff')
297
+ ```
298
+
258
299
 
259
300
  ## Supported operations on layers
260
301
 
@@ -281,6 +322,24 @@ with RasterLayer.layer_from_file('test1.tif') as layer1:
281
322
  calc.save(result)
282
323
  ```
283
324
 
325
+
326
+ The new 2.0 way of doing these are:
327
+
328
+ ```python
329
+ with yg.read_raster('test1.tif') as layer1:
330
+ with yg.read_raster('test2.tif') as layer2:
331
+ result = layer1 + layer2
332
+ result.to_geotiff("result.tif")
333
+ ```
334
+
335
+ or
336
+
337
+ ```python
338
+ with yg.read_raster('test1.tif') as layer1:
339
+ result = layer1 * 42.0
340
+ result.to_geotiff("result.tif")
341
+ ```
342
+
284
343
  ### Boolean testing
285
344
 
286
345
  Testing for equality, less than, less than or equal, greater than, and greater than or equal are supported on layers, along with logical or and logical and, as per this example, where `elevation_upper` and `elevation_lower` are scalar values:
@@ -0,0 +1,25 @@
1
+ yirgacheffe/__init__.py,sha256=n0v998xPOMMS6hdAQ16UNzmJUdnN5612HRtdudwjYa4,302
2
+ yirgacheffe/_core.py,sha256=0E56yP63vUiwi0g6ntUzmKhOuWQWoh3sSD6Ui8SWWRI,4082
3
+ yirgacheffe/constants.py,sha256=uCWJwec3-ND-zVxYbsk1sdHKANl3ToNCTPg7MZb0j2g,434
4
+ yirgacheffe/operators.py,sha256=03GKaGXn7E2VfA2UKwTUOinNuB7GtAhIO5XZec4jsFg,31364
5
+ yirgacheffe/rounding.py,sha256=ggBG4lMyLMtHLW3dBxr3gBCcF2qhRrY5etZiFGlIoqA,2258
6
+ yirgacheffe/window.py,sha256=kR8sHQ6lcixpndOWC18wLRgbgS8y00tq4Fkh8PLESvM,8209
7
+ yirgacheffe/_backends/__init__.py,sha256=jN-2iRrHStnPI6cNL7XhwhsROtI0EaGfIrbF5c-ECV0,334
8
+ yirgacheffe/_backends/enumeration.py,sha256=pADawllxpW_hW-IVVvZpHWIKzvEMs9aaqfkZRD1zjnY,1003
9
+ yirgacheffe/_backends/mlx.py,sha256=2vOTMqHbQbeqt81Eq_8hxWDXZHaPsDpbXkALRVGEnnw,6130
10
+ yirgacheffe/_backends/numpy.py,sha256=cYO628s4-5K_-Bp3CrnHegzYSZfkt2QC8iE9oOOMtvA,4069
11
+ yirgacheffe/layers/__init__.py,sha256=mYKjw5YTcMNv_hMy7a6K4yRzIuNUbR8WuBTw4WIAmSk,435
12
+ yirgacheffe/layers/area.py,sha256=Qs5N5XMrQwk7StE_Rky94X7BhavRxgRmpC6mXLeu2HQ,3905
13
+ yirgacheffe/layers/base.py,sha256=AgFe1HoWNuJWm2z_CgEE5o-3j-5hdC8HuNpYIM_h8HE,12672
14
+ yirgacheffe/layers/constant.py,sha256=PiRiAgcvOE1Li-fFItt6P_cj8IOlwTf_xi5G_iaqHok,1440
15
+ yirgacheffe/layers/group.py,sha256=e3pyQb35Ma-CPqY0dB4xDjMk3m2DQzzKiMGLSbBMY7c,16292
16
+ yirgacheffe/layers/h3layer.py,sha256=Hy8kJF9-EQnLaRbZlj4TLSRfpq2-dJga8x4jiiPUHYo,9919
17
+ yirgacheffe/layers/rasters.py,sha256=stZOi0sojJqMybvtKpvzfXlFJwRUvW7mA77Q3B4nhzA,13296
18
+ yirgacheffe/layers/rescaled.py,sha256=uam0dsidKpP97gQBig34zXefyNruUbwAnQaeuN68KE4,3104
19
+ yirgacheffe/layers/vectors.py,sha256=4BCGRSzbkFfsGZwLsQS9WelMXV5hDCcLmGGW6mf6MjU,15839
20
+ yirgacheffe-1.6.0.dist-info/licenses/LICENSE,sha256=dNSHwUCJr6axStTKDEdnJtfmDdFqlE3h1NPCveqPfnY,757
21
+ yirgacheffe-1.6.0.dist-info/METADATA,sha256=21WbEajGkbV-TBafy5uAjYLmGW7U0Lr2ur2IG4DXvkM,22410
22
+ yirgacheffe-1.6.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
23
+ yirgacheffe-1.6.0.dist-info/entry_points.txt,sha256=j4KgHXbVGbGyfTySc1ypBdERpfihO4WNjppvCdE9HjE,52
24
+ yirgacheffe-1.6.0.dist-info/top_level.txt,sha256=9DBFlKO2Ld3hG6TuE3qOTd3Tt8ugTiXil4AN4Wr9_y0,12
25
+ yirgacheffe-1.6.0.dist-info/RECORD,,