Skip to main content

python package

sregion

Parsing of IVOA S_REGION strings

The STS-C formalism is described at http://www.ivoa.net/Documents/latest/STC-S.html, though it seems that it was never adopted as an official standard. Nevertheless, the s_region strings do seem to have been adopted as a sort of pseudostandard in IVOA-compliant datasets / databases.

astropy-regions would probably be a better place to put this, but I'm not interested in all of the full astropy coordinate compatibility for now.

Examples

>>> import numpy as np
>>> from sregion import SRegion

#
# Polygon string
#
>>> sr = SRegion('POLYGON 0.0 0.0 0.0 1.0 1.0 1.0 1.0 0.0')
>>> print(sr.area)
[1.0]
>>> print(sr.centroid)
[array([0.5, 0.5])]

#
# Circle string
#
>>> for i in range(4,10):
>>>     sr = SRegion('CIRCLE 10 10 1', ncircle=2**i)
>>>     print(f'ncircle={2**i:>3} {sr.area[0]/np.pi:.5f} {sr.centroid[0]}')
ncircle= 16 0.97450 [10. 10.]
ncircle= 32 0.99359 [10. 10.]
ncircle= 64 0.99839 [10. 10.]
ncircle=128 0.99960 [10. 10.]
ncircle=256 0.99990 [10. 10.]
ncircle=512 0.99997 [10. 10.]

# Circle with radius in angular units
>>> import astropy.units as u
>>> sr = SRegion('CIRCLE 10 10 1"', ncircle=256)
>>> print(f'{sr.sky_area(unit=u.arcsec**2)[0]:.5f}')
3.14128 arcsec2

#
# Box string
#
>>> sr = SRegion('BOX ICRS 90 10 7.5 7.5')
>>> print(sr.centroid)
[array([90., 10.])]
>>> print(sr.area)
[56.25]

# Box with width/height in angular units
>>> sr = SRegion("BOX ICRS 90 10 7.5' 7.5'")
>>> print(f"{sr.sky_area()[0]:.3f}")
56.250 arcmin2

#
# From WCS objects
#
>>> from astropy.wcs import WCS
>>> wcs = WCS()
>>> wcs.pixel_shape = [601,601]
>>> wcs.wcs.cdelt *= 0.1/3600
>>> wcs.wcs.crpix[1] = 300
>>> wcs.wcs.crval = [0,0]
>>> print(SRegion(wcs).sky_area())
[<Quantity 1. arcmin2>]

#
# From arrays
#
>>> x = np.array([0, 0, 1, 1])
>>> y = np.array([0, 1, 1, 0])
>>> sr = SRegion(np.array([x, y]).T)
>>> print(sr.area)
[1.0]
>>> print(sr.centroid)
[array([0.5, 0.5])]

# 
# To s_region string
#
>>> print(sr.s_region)
POLYGON 0.000000 0.000000 0.000000 1.000000 1.000000 1.000000 1.000000 0.000000

#
# To matplotlib path object(s)
#
>>> print(sr.path[0].contains_point([0.5, 0.5]))
True
>>> print(sr.path[0].contains_points([[0.5, 0.5], [2.0, 2.0]]))
[ True False]

#
# To matplotlib patch(es)
#
>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots(1,1,figsize=(2,2))
>>> for p in sr.patch(alpha=0.5, fc='r'):
>>>     ax.add_patch(p)
>>> ax.set_xlim(-1, 2)
>>> ax.set_ylim(*ax.get_xlim())
>>> ax.grid()

#
# To shapely polygons
# 
>>> sr.shapely
[<shapely.geometry.polygon.Polygon at 0x18055b910>]

#
# To DS9 region(s)
#
>>> for r in sr.region:
>>>    print(r)
polygon(0.000000,0.000000,0.000000,1.000000,1.000000,1.000000,1.000000,0.000000)

>>> sr.ds9_properties = 'color=red width=2'
>>> sr.label = 'my_group'
>>> for r in sr.region:
>>>    print(r)
polygon(0.000000,0.000000,0.000000,1.000000,1.000000,1.000000,1.000000,0.000000) # color=red width=2 text={my_group}

    

Region padding.

Metadata

Release files for sregion 1.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sregion 1.7
File Size Uploaded
sregion-1.7.tar.gz 17.0 kB Details

Release files / sregion-1.7.tar.gz

Download URL sregion-1.7.tar.gz
Size 17.0 kB
Tags Source
SHA-256 checksum
How to use checksums
e7570ce67df0beadc3f3c3ce44f49b9489148c86582ef758af5dd549d45c2a83
BLAKE2b-256 checksum
How to use checksums
22fc2e02c0b79173bdbba98d4beeca471bfa499bd1adedb76b3484f080b695aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release history Release notifications | RSS feed

This release

1.7 This release

1 release file

1.6

1 release file

1.5

1 release file

1.4.2

1 release file

1.4

1 release file

1.3.3

1 release file

1.3.2

1 release file

1.3.1

1 release file

1.2.2

1 release file

1.2

1 release file

1.1.1

1 release file

1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page