Skip to content

Commit 2851540

Browse files
committed
feat: add support of Regions in from_astropy_regions
1 parent 57f7e6b commit 2851540

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818

1919
### Added
2020

21+
* Add support of `regions.Regions` [#163]
2122
* Creation of a MOC from a zone (defined by min/max ra and dec)`MOC.from_zone`
2223
* Creation of a single MOC from a lot of small cones is faster with the new option in
2324
`MOC.from_cones`: the keyword 'union_strategy' can now take the value 'small_cones'.

python/mocpy/moc/moc.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1844,7 +1844,7 @@ def from_astropy_regions(cls, region, max_depth: int):
18441844
The supported sky regions are `~regions.CircleSkyRegion`,
18451845
`~regions.CircleAnnulusSkyRegion`, `~regions.EllipseSkyRegion`,
18461846
`~regions.RectangleSkyRegion`, `~regions.PolygonSkyRegion`,
1847-
`~regions.PointSkyRegion`.
1847+
`~regions.PointSkyRegion`, `~regions.Regions`.
18481848
max_depth : int
18491849
The maximum HEALPix cell resolution of the MOC. Should be comprised between
18501850
0 and 29.
@@ -1853,6 +1853,14 @@ def from_astropy_regions(cls, region, max_depth: int):
18531853
-------
18541854
`~mocpy.moc.MOC`
18551855
1856+
Notes
1857+
-----
1858+
- For the `~regions.Regions`, the returned MOC will be the union of all the regions.
1859+
- For the `~regions.PolygonSkyRegion` and the `~regions.RectangleSkyRegion`, the MOC
1860+
will consider the sides to follow great circles on the sky sphere while in
1861+
astropy-regions the sides follow straight lines in the projected space (depending on
1862+
a given WCS, see issue https://github.com/astropy/regions/issues/564).
1863+
18561864
Examples
18571865
--------
18581866
>>> from astropy.coordinates import SkyCoord
@@ -1869,6 +1877,7 @@ def from_astropy_regions(cls, region, max_depth: int):
18691877
regions.RectangleSkyRegion,
18701878
regions.PolygonSkyRegion,
18711879
regions.PointSkyRegion,
1880+
regions.Regions,
18721881
)
18731882
if isinstance(region, regions.CircleSkyRegion):
18741883
center = region.center.icrs
@@ -1927,6 +1936,13 @@ def from_astropy_regions(cls, region, max_depth: int):
19271936
return cls.from_polygon_skycoord(region.vertices, max_depth=max_depth)
19281937
if isinstance(region, regions.PointSkyRegion):
19291938
return cls.from_skycoords(region.center, max_norder=max_depth)
1939+
if isinstance(region, regions.Regions):
1940+
mocs = [
1941+
cls.from_astropy_regions(reg, max_depth=max_depth) for reg in region
1942+
]
1943+
if len(mocs) == 1:
1944+
return mocs[0]
1945+
return mocs[0].union(*mocs[1:]) # fastest multi-union
19301946

19311947
raise ValueError(
19321948
"'from_astropy_regions' does not support this region type."

python/mocpy/tests/test_moc.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -689,14 +689,18 @@ def test_from_astropy_regions():
689689
# polygons
690690
vertices = SkyCoord([1, 2, 2], [1, 1, 2], unit="deg", frame="fk5")
691691
polygon = regions.PolygonSkyRegion(vertices)
692-
moc = MOC.from_astropy_regions(polygon, max_depth=10)
693-
assert all(moc.contains_skycoords(vertices))
692+
moc_polygon = MOC.from_astropy_regions(polygon, max_depth=10)
693+
assert all(moc_polygon.contains_skycoords(vertices))
694694
# points
695695
point = SkyCoord("+23h20m48.3s +61d12m06s")
696696
region_point = regions.PointSkyRegion(point)
697-
moc = MOC.from_astropy_regions(region_point, max_depth=10)
698-
assert moc.max_order == 10
699-
assert moc.contains_skycoords(point)
697+
moc_point = MOC.from_astropy_regions(region_point, max_depth=10)
698+
assert moc_point.max_order == 10
699+
assert moc_point.contains_skycoords(point)
700+
# multi regions
701+
multi_region = regions.Regions([region_point, polygon])
702+
moc = MOC.from_astropy_regions(multi_region, max_depth=10)
703+
assert moc == moc_polygon + moc_point
700704

701705

702706
# TODO: IMPROVE THE ALGO

0 commit comments

Comments
 (0)