Skip to content

Commit

Permalink
fix: add possibility to have width > height for ellipses and boxes
Browse files Browse the repository at this point in the history
  • Loading branch information
ManonMarchand committed Jul 18, 2024
1 parent b37715e commit 6fa2690
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [unreleased]

## Fixed

* `MOC.from_astropy_regions` now accepts `EllipseSkyRegion` and `RectangleSkyRegion` where
width > height.

## [0.16.1]

### Fixed
Expand Down
41 changes: 31 additions & 10 deletions notebooks/02-Creating_MOCs_from_astropy_regions.ipynb

Large diffs are not rendered by default.

28 changes: 22 additions & 6 deletions python/mocpy/moc/moc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1811,22 +1811,38 @@ def from_astropy_regions(cls, region, max_depth: int):
)
if isinstance(region, regions.EllipseSkyRegion):
center = region.center.icrs
if region.width < region.height:
a = region.height / 2.0
b = region.width / 2.0
angle = region.angle
else:
a = region.width / 2.0
b = region.height / 2.0
angle = region.angle + Angle("90d")
return cls.from_elliptical_cone(
center.ra,
center.dec,
a=region.height / 2,
b=region.width / 2,
pa=region.angle,
a=a,
b=b,
pa=angle,
max_depth=max_depth,
)
if isinstance(region, regions.RectangleSkyRegion):
center = region.center.icrs
if region.width < region.height:
a = region.height / 2.0
b = region.width / 2.0
angle = region.angle
else:
a = region.width / 2.0
b = region.height / 2.0
angle = region.angle + Angle("90d")
return cls.from_box(
center.ra,
center.dec,
a=region.width / 2,
b=region.height / 2,
angle=region.angle + Angle("90d"),
a=a,
b=b,
angle=angle,
max_depth=max_depth,
)
if isinstance(region, regions.PolygonSkyRegion):
Expand Down
9 changes: 9 additions & 0 deletions python/mocpy/tests/test_moc.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,15 @@ def test_from_astropy_regions():
moc = MOC.from_astropy_regions(ellipse, max_depth=9)
assert moc.contains_skycoords(center)
assert round(moc.largest_distance_from_coo_to_vertices(center).to("deg").value) == 3
# invert axes in the ellipse
ellipse_swapped = regions.EllipseSkyRegion(
center,
6 * u.deg,
3 * u.deg,
(-90 + 10) * u.deg,
)
moc_swapped = MOC.from_astropy_regions(ellipse_swapped, max_depth=9)
assert moc_swapped == moc
# rectangle
box = regions.RectangleSkyRegion(center, 12 * u.deg, 6 * u.deg, 10 * u.deg)
moc = MOC.from_astropy_regions(box, max_depth=8)
Expand Down

0 comments on commit 6fa2690

Please sign in to comment.