Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a function to join/enclose areas. #306

Merged
merged 10 commits into from
Mar 12, 2021
Next Next commit
Add a function to join/enclose areas.
Add a function to calculate an area enclosing one or more other areas.
  • Loading branch information
gerritholl committed Oct 22, 2020
commit 4f6524b41b07362fb6f6362c61df3d7d7e24ebab
48 changes: 42 additions & 6 deletions pyresample/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
# -*- coding: utf-8 -*-
# pyresample, Resampling of remote sensing image data in python
#
# Copyright (C) 2010-2016
#
# Authors:
# Esben S. Nielsen
# Thomas Lavergne
# Adam Dybbroe
# Copyright (C) 2010-2020 Pyresample developers
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
Expand Down Expand Up @@ -2443,3 +2438,44 @@ def _dict_representer(dumper, data):

OrderedDumper.add_representer(OrderedDict, _dict_representer)
return yaml.dump(data, stream, OrderedDumper, **kwds)


def area_enclosure(*areas, area_id="joint-area"):
"""Return the smallest areadefinition enclosing one or more others.

From one or more AreaDefinition objects (most usefully at least
two), which shall differ only in extent, calculate the smallest
AreaDefinition that encloses all. Touches only the ``area_extent``;
projection and units must be identical in all input areas and will be
unchanged in the resulting area. When the input areas :math:`i=1..n`
have extent :math:`(a_i, b_i, c_i, d_i)`, the resulting area will have
extent :math:`(\\min_i{a_i}, \\min_i{b_i}, \\max_i{c_i}, \\max_i{d_i})`.

Args:
*areas (AreaDefinition): AreaDefinition objects to enclose.
area_id (Optional[str]): Name of joint area, defaults to "joint-area".
"""

first = None
if len(areas) == 0:
raise TypeError("Must pass at least one area, found zero.")
for area in areas:
if first is None:
first = area
largest_extent = list(area.area_extent)
else:
if not area.proj_dict == first.proj_dict:
raise ValueError("Inconsistent projections between areas")
if not np.isclose(area.resolution, first.resolution).all():
raise ValueError("Inconsistent resolution between areas")
largest_extent[0] = min(largest_extent[0], area.area_extent[0])
largest_extent[1] = min(largest_extent[1], area.area_extent[1])
largest_extent[2] = max(largest_extent[2], area.area_extent[2])
largest_extent[3] = max(largest_extent[3], area.area_extent[3])

return create_area_def(
area_id=area_id,
projection=first.proj_dict,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After #332, this should be changed to .crs. Actually, it could be done now and should still work. Passing PROJ dicts in is fine, using them after is not. Ah you also use proj_dict below to get the units. This shouldn't be necessary as create_area_def should determine the units from the CRS information.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I change first.proj_dict to first.crs the test fails with TypeError: Wrong type for projection: <class 'pyproj.crs.crs.CRS'>. Expected dict or string. in _get_proj_data, which verifies that the projection is either a dict or a string.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 I think in #332 that should work. Once some of my more recent PRs are merged maybe we can test this idea again.

Copy link
Collaborator Author

@gerritholl gerritholl Mar 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I wait for merger of #332 and then adapt the code here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merged just now. If you have the time to merge master into this PR and test some things that'd be great. Thanks.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced proj_dict by crs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed name to enclose_areas.

units=first.proj_dict["units"],
area_extent=largest_extent,
resolution=first.resolution)
61 changes: 55 additions & 6 deletions pyresample/test/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
# -*- coding: utf-8 -*-
# pyresample, Resampling of remote sensing image data in python
#
# Copyright (C) 2010-2016
#
# Authors:
# Esben S. Nielsen
# Thomas Lavergne
# Adam Dybbroe
# Copyright (C) 2010-2020 Pyresample developers
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
Expand All @@ -24,6 +19,7 @@
"""Test the geometry objects."""
import random
import sys
import pytest

import numpy as np

Expand Down Expand Up @@ -2385,3 +2381,56 @@ def test_aggregate(self):
np.testing.assert_allclose(res.area_extent, area.area_extent)
self.assertEqual(res.shape[0], area.shape[0] / 2)
self.assertEqual(res.shape[1], area.shape[1] / 4)


def test_area_enclosure():
from pyresample.geometry import (area_enclosure, create_area_def)
proj_dict = {'proj': 'geos', 'sweep': 'x', 'lon_0': 0, 'h': 35786023,
'x_0': 0, 'y_0': 0, 'ellps': 'GRS80', 'units': 'm',
'no_defs': None, 'type': 'crs'}
proj_dict_alt = {'proj': 'laea', 'lat_0': -90, 'lon_0': 0, 'a': 6371228.0,
'units': 'm'}

ar1 = create_area_def(
"test-area",
projection=proj_dict,
units="m",
area_extent=[0, 20, 100, 120],
shape=(10, 10))

ar2 = create_area_def(
"test-area",
projection=proj_dict,
units="m",
area_extent=[20, 40, 120, 140],
shape=(10, 10))

ar3 = create_area_def(
"test-area",
projection=proj_dict,
units="m",
area_extent=[20, 0, 120, 100],
shape=(10, 10))

ar4 = create_area_def(
"test-area",
projection=proj_dict_alt,
units="m",
area_extent=[20, 0, 120, 100],
shape=(10, 10))

ar5 = create_area_def(
"test-area",
projection=proj_dict,
units="m",
area_extent=[-50, -50, 50, 50],
shape=(100, 100))

ar_joined = area_enclosure(ar1, ar2, ar3)
np.testing.assert_allclose(ar_joined.area_extent, [0, 0, 120, 140])
with pytest.raises(ValueError):
area_enclosure(ar3, ar4)
with pytest.raises(ValueError):
area_enclosure(ar3, ar5)
with pytest.raises(TypeError):
area_enclosure()