subsetting.py 10.1 KB
Newer Older
1
2
3
4
5
# -*- coding: utf-8 -*-

import warnings
import numpy as np
from shapely.geometry import box, Polygon
6
7
8
9
10
11
from py_tools_ds.geo.coord_calc import get_corner_coordinates, calc_FullDataset_corner_positions
from py_tools_ds.geo.coord_grid import snap_bounds_to_pixGrid
from py_tools_ds.geo.coord_trafo import mapXY2imXY, transform_any_prj, imXY2mapXY
from py_tools_ds.geo.projection import prj_equal
from py_tools_ds.geo.vector.topology import get_overlap_polygon
from py_tools_ds.numeric.array import get_outFillZeroSaturated
12

13
14

__author__ = 'Daniel Scheffler'
15
16
17


def _clip_array_at_mapPos(arr, mapBounds, arr_gt, band2clip=None, fillVal=0):
18
    # type: (any, tuple, tuple, int, int) -> (np.ndarray, tuple)
19
20
21
22
23
24
25
26
27
28
29
30
    """
    NOTE: asserts that mapBounds have the same projection like the coordinates in arr_gt

    :param arr:
    :param mapBounds:   xmin, ymin, xmax, ymax
    :param arr_gt:
    :param band2clip:   band index of the band to be returned (full array if not given)
    :param fillVal:
    :return:
    """

    # assertions
31
    assert isinstance(arr_gt, (tuple, list))
32
33
34
    assert isinstance(band2clip, int) or band2clip is None

    # get array metadata
35
36
37
    rows, cols = arr.shape[:2]
    bands = arr.shape[2] if arr.ndim == 3 else 1
    arr_dtype = arr.dtype
38
    ULxy, LLxy, LRxy, URxy = get_corner_coordinates(gt=arr_gt, rows=rows, cols=cols)
39
    arrBounds = ULxy[0], LRxy[1], LRxy[0], ULxy[1]
40
41

    # snap mapBounds to the grid of the array
42
    mapBounds = snap_bounds_to_pixGrid(mapBounds, arr_gt)
43
44
45
    xmin, ymin, xmax, ymax = mapBounds

    # get out_gt and out_prj
46
    out_gt = list(arr_gt)
47
48
49
    out_gt[0], out_gt[3] = xmin, ymax

    # get image area to read
50
51
    cS, rS = [int(i) for i in mapXY2imXY((xmin, ymax), arr_gt)]  # UL
    cE, rE = [int(i) - 1 for i in mapXY2imXY((xmax, ymin), arr_gt)]  # LR
52
53
54

    if 0 <= rS <= rows - 1 and 0 <= rE <= rows - 1 and 0 <= cS <= cols - 1 and 0 <= cE <= cols - 1:
        """requested area is within the input array"""
55
        if bands == 1:
56
57
58
59
60
61
            out_arr = arr[rS:rE + 1, cS:cE + 1]
        else:
            out_arr = arr[rS:rE + 1, cS:cE + 1, band2clip] if band2clip is not None else arr[rS:rE + 1, cS:cE + 1, :]
    else:
        """requested area is not completely within the input array"""
        # create array according to size of mapBounds + fill with nodata
62
63
        tgt_rows = int(abs((ymax - ymin) / arr_gt[5]))
        tgt_cols = int(abs((xmax - xmin) / arr_gt[1]))
64
65
66
67
68
69
70
        tgt_bands = bands if band2clip is None else 1
        tgt_shape = (tgt_rows, tgt_cols, tgt_bands) if tgt_bands > 1 else (tgt_rows, tgt_cols)

        try:
            fillVal = fillVal if fillVal is not None else get_outFillZeroSaturated(arr)[0]
            out_arr = np.full(tgt_shape, fillVal, arr_dtype)
        except MemoryError:
71
            raise MemoryError('Calculated target dimensions are %s. Check your inputs!' % str(tgt_shape))
72
73
74
75
76

        # calculate image area to be read from input array
        overlap_poly = get_overlap_polygon(box(*arrBounds), box(*mapBounds))['overlap poly']
        assert overlap_poly, 'The input array and the requested geo area have no spatial overlap.'
        xmin_in, ymin_in, xmax_in, ymax_in = overlap_poly.bounds
77
78
79
        cS_in, rS_in = [int(i) for i in mapXY2imXY((xmin_in, ymax_in), arr_gt)]
        cE_in, rE_in = [int(i) - 1 for i in
                        mapXY2imXY((xmax_in, ymin_in), arr_gt)]  # -1 because max values do not represent pixel origins
80
81
82
83
84
85

        # read a subset of the input array
        if bands == 1:
            data = arr[rS_in:rE_in + 1, cS_in:cE_in + 1]
        else:
            data = arr[rS_in:rE_in + 1, cS_in:cE_in + 1, band2clip] if band2clip is not None else \
86
                arr[rS_in:rE_in + 1, cS_in:cE_in + 1, :]
87
88

        # calculate correct area of out_arr to be filled and fill it with read data from input array
89
90
91
        cS_out, rS_out = [int(i) for i in mapXY2imXY((xmin_in, ymax_in), out_gt)]
        # -1 because max values do not represent pixel origins
        cE_out, rE_out = [int(i) - 1 for i in mapXY2imXY((xmax_in, ymin_in), out_gt)]
92
93

        # fill newly created array with read data from input array
94
95
        if tgt_bands == 1:
            out_arr[rS_out:rE_out + 1, cS_out:cE_out + 1] = data if data.ndim == 2 else data[:, :, 0]
96
        else:
97
            out_arr[rS_out:rE_out + 1, cS_out:cE_out + 1, :] = data
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

    return out_arr, out_gt


def get_array_at_mapPosOLD(arr, arr_gt, arr_prj, mapBounds, mapBounds_prj, band2get=None, fillVal=0):
    # FIXME mapBounds_prj should not be handled as target projection
    """

    :param arr:
    :param arr_gt:
    :param arr_prj:
    :param mapBounds:       xmin, ymin, xmax, ymax
    :param mapBounds_prj:
    :param band2get:        band index of the band to be returned (full array if not given)
    :param fillVal:
    :return:
    """

116
    # [print(i,'\n') for i in [arr, arr_gt, arr_prj, mapBounds, mapBounds_prj]]
117
118
119
120
    # check if requested bounds have the same projection like the array
    samePrj = prj_equal(arr_prj, mapBounds_prj)

    if samePrj:
121
        out_prj = arr_prj
122
123
124
        out_arr, out_gt = _clip_array_at_mapPos(arr, mapBounds, arr_gt, band2clip=band2get, fillVal=fillVal)

    else:
125
126
        # calculate requested corner coordinates in the same projection like the input array
        # (bounds are not sufficient due to projection rotation)
127
128
129
        xmin, ymin, xmax, ymax = mapBounds
        ULxy, URxy, LRxy, LLxy = (xmin, ymax), (xmax, ymax), (xmax, ymin), (xmin, ymin)
        ULxy, URxy, LRxy, LLxy = [transform_any_prj(mapBounds_prj, arr_prj, *xy) for xy in [ULxy, URxy, LRxy, LLxy]]
130
        mapBounds_arrPrj = Polygon([ULxy, URxy, LRxy, LLxy]).buffer(arr_gt[1]).bounds
131
132
133
134
135
136

        # read subset of input array as temporary data (that has to be reprojected later)
        temp_arr, temp_gt = _clip_array_at_mapPos(arr, mapBounds_arrPrj, arr_gt, band2clip=band2get, fillVal=fillVal)

        # eliminate no data area for faster warping
        try:
137
138
139
140
141
            oneBandArr = np.all(np.where(temp_arr == fillVal, 0, 1), axis=2) \
                if len(temp_arr.shape) > 2 else np.where(temp_arr == fillVal, 0, 1)
            corners = [(i[1], i[0]) for i in
                       calc_FullDataset_corner_positions(oneBandArr, assert_four_corners=False)]
            bounds = [int(i) for i in Polygon(corners).bounds]
142
143
            cS, rS, cE, rE = bounds

144
            temp_arr = temp_arr[rS:rE + 1, cS:cE + 1]
145
            temp_gt[0], temp_gt[3] = [int(i) for i in imXY2mapXY((cS, rS), temp_gt)]
146
        except Exception:
147
148
149
            warnings.warn('Could not eliminate no data area for faster warping. '
                          'Result will not be affected but processing takes a bit longer..')

150
151
152
        # from matplotlib import pyplot as plt
        # plt.figure()
        # plt.imshow(temp_arr[:,:])
153
154
155
156

        # calculate requested geo bounds in the target projection, snapped to the output array grid
        mapBounds = snap_bounds_to_pixGrid(mapBounds, arr_gt)
        xmin, ymin, xmax, ymax = mapBounds
157
        out_gt = list(arr_gt)
158
159
        out_gt[0], out_gt[3] = xmin, ymax
        out_rows = int(abs((ymax - ymin) / arr_gt[5]))
160
161
162
        # FIXME using out_gt and outRowsCols is a workaround for not beeing able to pass output extent in the OUTPUT
        # FIXME projection
        out_cols = int(abs((xmax - xmin) / arr_gt[1]))
163
164

        # reproject temporary data to target projection (the projection of mapBounds)
165
        from py_tools_ds.geo.raster.reproject import warp_ndarray
166
167
        out_arr, out_gt, out_prj = warp_ndarray(temp_arr, temp_gt, arr_prj, mapBounds_prj,
                                                in_nodata=fillVal, out_nodata=fillVal, out_gt=out_gt,
168
169
                                                outRowsCols=(out_rows, out_cols), outExtent_within=True,
                                                rsp_alg=0)  # FIXME resampling alg
170
171
172
173
174
175

    return out_arr, out_gt, out_prj


def get_array_at_mapPos(arr, arr_gt, arr_prj, out_prj, mapBounds, mapBounds_prj=None, out_gsd=None, band2get=None,
                        fillVal=0, rspAlg='near', progress=True):
176
    # type: (any, tuple, str, str, tuple, str, tuple, int, int, str, bool) -> (np.ndarray, tuple, str)
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
    """

    :param arr:
    :param arr_gt:
    :param arr_prj:
    :param out_prj:         output projection as WKT string
    :param mapBounds:       xmin, ymin, xmax, ymax
    :param mapBounds_prj:   the projection of the given map bounds (default: output projection)
    :param out_gsd:         (X,Y)
    :param band2get:        band index of the band to be returned (full array if not given)
    :param fillVal:
    :param rspAlg:          <str> Resampling method to use. Available methods are:
                            near, bilinear, cubic, cubicspline, lanczos, average, mode, max, min, med, q1, q2
    :param progress:
    :return:
    """

    # check if reprojection is needed
    mapBounds_prj = mapBounds_prj if mapBounds_prj else out_prj
196
    samePrj = prj_equal(arr_prj, out_prj)
197
198
199
200
201
202
203
204

    if samePrj:
        # output array is requested in the same projection like input array => no reprojection needed

        # mapBounds are expected to have the same projection like the input array
        if not prj_equal(arr_prj, mapBounds_prj):
            xmin, ymin, xmax, ymax = mapBounds
            (xmin, ymin), (xmax, ymax) = \
205
                [transform_any_prj(mapBounds_prj, arr_prj, X, Y) for X, Y in [(xmin, ymin), (xmax, ymax)]]
206
207
            mapBounds = xmin, ymin, xmax, ymax

208
        out_prj = arr_prj
209
210
211
212
213
214
215
216
        out_arr, out_gt = _clip_array_at_mapPos(arr, mapBounds, arr_gt, band2clip=band2get, fillVal=fillVal)

    else:
        # output array is requested in another projection => reprojection needed

        # calculate requested geo bounds in the target projection, snapped to the output array grid
        mapBounds = snap_bounds_to_pixGrid(mapBounds, arr_gt)

217
        arr = arr[:, :, band2get] if band2get else arr[:]  # also converts GeoArray to numpy.ndarray
218

219
        from py_tools_ds.geo.raster.reproject import warp_ndarray
220
221
222
223
224
        out_arr, out_gt, out_prj = \
            warp_ndarray(arr, arr_gt, arr_prj, out_prj=out_prj, out_bounds=mapBounds, out_bounds_prj=mapBounds_prj,
                         in_nodata=fillVal, out_nodata=fillVal, rspAlg=rspAlg, out_gsd=out_gsd, progress=progress)

    return out_arr, out_gt, out_prj