Coverage for py_tools_ds/io/raster/reader.py: 0%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
3# py_tools_ds - A collection of geospatial data analysis tools that simplify standard
4# operations when handling geospatial raster and vector data as well as projections.
5#
6# Copyright (C) 2016-2021
7# - Daniel Scheffler (GFZ Potsdam, daniel.scheffler@gfz-potsdam.de)
8# - Helmholtz Centre Potsdam - GFZ German Research Centre for Geosciences Potsdam,
9# Germany (https://www.gfz-potsdam.de/)
10#
11# This software was developed within the context of the GeoMultiSens project funded
12# by the German Federal Ministry of Education and Research
13# (project grant code: 01 IS 14 010 A-C).
14#
15# Licensed under the Apache License, Version 2.0 (the "License");
16# you may not use this file except in compliance with the License.
17# You may obtain a copy of the License at
18#
19# http://www.apache.org/licenses/LICENSE-2.0
20#
21# Unless required by applicable law or agreed to in writing, software
22# distributed under the License is distributed on an "AS IS" BASIS,
23# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24# See the License for the specific language governing permissions and
25# limitations under the License.
27import multiprocessing
28import ctypes
29import numpy as np
30from osgeo import gdal
32from ...numeric.array import get_array_tilebounds
34__author__ = "Daniel Scheffler"
37shared_array = None
40def init_SharedArray_in_globals(dims):
41 rows, cols = dims
42 global shared_array
43 shared_array_base = multiprocessing.Array(ctypes.c_double, rows * cols)
44 shared_array = np.ctypeslib.as_array(shared_array_base.get_obj())
45 shared_array = shared_array.reshape(rows, cols)
48def fill_arr(argDict, def_param=shared_array):
49 pos = argDict.get('pos')
50 func = argDict.get('func2call')
51 args = argDict.get('func_args', [])
52 kwargs = argDict.get('func_kwargs', {})
54 (rS, rE), (cS, cE) = pos
55 shared_array[rS:rE + 1, cS:cE + 1] = func(*args, **kwargs)
58def gdal_read_subset(fPath, pos, bandNr):
59 (rS, rE), (cS, cE) = pos
60 ds = gdal.Open(fPath)
61 data = ds.GetRasterBand(bandNr).ReadAsArray(cS, rS, cE - cS + 1, rE - rS + 1)
62 del ds
63 return data
66def gdal_ReadAsArray_mp(fPath, bandNr, tilesize=1500):
67 ds = gdal.Open(fPath)
68 rows, cols = ds.RasterYSize, ds.RasterXSize
69 del ds
71 init_SharedArray_in_globals((rows, cols))
73 tilepos = get_array_tilebounds(array_shape=(rows, cols), tile_shape=[tilesize, tilesize])
74 fill_arr_argDicts = [{'pos': pos, 'func2call': gdal_read_subset, 'func_args': (fPath, pos, bandNr)} for pos in
75 tilepos]
77 with multiprocessing.Pool() as pool:
78 pool.map(fill_arr, fill_arr_argDicts)
79 pool.close()
80 pool.join()
82 return shared_array