• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pytroll / pyresample / 4653744392

pending completion
4653744392

Pull #511

github

GitHub
Merge edb481dd6 into 7ca8789a3
Pull Request #511: Bump pypa/gh-action-pypi-publish from 1.8.4 to 1.8.5

12270 of 13075 relevant lines covered (93.84%)

3.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

53.57
/pyresample/utils/rasterio.py
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
#
4
# Copyright (C) 2019-2021 Pyresample developers
5
#
6
# This program is free software: you can redistribute it and/or modify it under
7
# the terms of the GNU Lesser General Public License as published by the Free
8
# Software Foundation, either version 3 of the License, or (at your option) any
9
# later version.
10
#
11
# This program is distributed in the hope that it will be useful, but WITHOUT
12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
14
# details.
15
#
16
# You should have received a copy of the GNU Lesser General Public License along
17
# with this program.  If not, see <http://www.gnu.org/licenses/>.
18
"""Utilities for working with rasterio objects."""
4✔
19
from . import proj4_str_to_dict
4✔
20

21

22
def _get_area_def_from_gdal(dataset, area_id=None, name=None, proj_id=None, projection=None):
4✔
23
    from pyresample.geometry import AreaDefinition
×
24

25
    # a: width of a pixel
26
    # b: row rotation (typically zero)
27
    # c: x-coordinate of the upper-left corner of the upper-left pixel
28
    # d: column rotation (typically zero)
29
    # e: height of a pixel (typically negative)
30
    # f: y-coordinate of the of the upper-left corner of the upper-left pixel
31
    c, a, b, f, d, e = dataset.GetGeoTransform()
×
32
    if not (b == d == 0):
×
33
        raise ValueError('Rotated rasters are not supported at this time.')
×
34
    area_extent = (c, f + e * dataset.RasterYSize, c + a * dataset.RasterXSize, f)
×
35

36
    if projection is None:
×
37
        from osgeo import osr
×
38
        proj = dataset.GetProjection()
×
39
        if proj != '':
×
40
            sref = osr.SpatialReference(wkt=proj)
×
41
            projection = proj4_str_to_dict(sref.ExportToProj4())
×
42
        else:
43
            raise ValueError('The source raster is not gereferenced, please provide the value of proj_dict')
×
44

45
        if proj_id is None:
×
46
            proj_id = proj.split('"')[1]
×
47

48
    area_def = AreaDefinition(area_id, name, proj_id, projection,
×
49
                              dataset.RasterXSize, dataset.RasterYSize, area_extent)
50
    return area_def
×
51

52

53
def _get_area_def_from_rasterio(dataset, area_id, name, proj_id=None, projection=None):
4✔
54
    from pyresample.geometry import AreaDefinition
4✔
55

56
    a, b, c, d, e, f, _, _, _ = dataset.transform
4✔
57
    if not (b == d == 0):
4✔
58
        raise ValueError('Rotated rasters are not supported at this time.')
4✔
59

60
    if projection is None:
4✔
61
        projection = dataset.crs
4✔
62
        if projection is None:
4✔
63
            raise ValueError('The source raster is not gereferenced, please provide the value of proj_dict')
4✔
64

65
        if proj_id is None:
4✔
66
            proj_id = projection.wkt.split('"')[1]
4✔
67

68
    area_def = AreaDefinition(area_id, name, proj_id, projection,
4✔
69
                              dataset.width, dataset.height, dataset.bounds)
70
    return area_def
4✔
71

72

73
def get_area_def_from_raster(source, area_id=None, name=None, proj_id=None, projection=None):
4✔
74
    """Construct AreaDefinition object from raster.
75

76
    Parameters
77
    ----------
78
    source : str, Dataset, DatasetReader or DatasetWriter
79
        A file name. Also it can be ``osgeo.gdal.Dataset``,
80
        ``rasterio.io.DatasetReader`` or ``rasterio.io.DatasetWriter``
81
    area_id : str, optional
82
        ID of area
83
    name : str, optional
84
        Name of area
85
    proj_id : str, optional
86
        ID of projection
87
    projection : pyproj.CRS, dict, or string, optional
88
        Projection parameters as a CRS object or a dictionary or string of
89
        PROJ.4 parameters.
90

91
    Returns
92
    -------
93
    area_def : object
94
        AreaDefinition object
95
    """
96
    try:
4✔
97
        import rasterio
4✔
98
    except ImportError:
×
99
        rasterio = None
×
100
        try:
×
101
            from osgeo import gdal
×
102
        except ImportError:
×
103
            raise ImportError('Either rasterio or gdal must be available')
×
104

105
    cleanup_gdal = cleanup_rasterio = None
4✔
106
    if isinstance(source, str):
4✔
107
        if rasterio is not None:
4✔
108
            source = rasterio.open(source)
4✔
109
            cleanup_rasterio = True
4✔
110
        else:
111
            source = gdal.Open(source)
×
112
            cleanup_gdal = True
×
113

114
    try:
4✔
115
        if rasterio is not None and isinstance(source, (rasterio.io.DatasetReader, rasterio.io.DatasetWriter)):
4✔
116
            return _get_area_def_from_rasterio(source, area_id, name, proj_id, projection)
4✔
117
        return _get_area_def_from_gdal(source, area_id, name, proj_id, projection)
×
118
    finally:
119
        if cleanup_rasterio:
4✔
120
            source.close()
4✔
121
        elif cleanup_gdal:
×
122
            source = None
3✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc