Giter Site home page Giter Site logo

amckinstry / spherical_geometry Goto Github PK

View Code? Open in Web Editor NEW

This project forked from spacetelescope/spherical_geometry

0.0 1.0 0.0 8.81 MB

A Python package for handling spherical polygons that represent arbitrary regions of the sky

Home Page: https://spacetelescope.github.io/spherical_geometry/spherical_geometry

Python 88.20% C 11.80%

spherical_geometry's Introduction

User documentation

.. currentmodule:: spherical_geometry

The spherical_geometry library is a Python package for handling spherical polygons that represent arbitrary regions of the sky.

Requirements

  • Python 3.5 or later
  • Numpy 1.10.0 or later
  • astropy 0.4 or later
  • qd-library 2.3.7 or later (optional: if not available, the bundled version will be used). To force using the system-installed version, build with setup.py build --use-system-qd.

Coordinate representation

Coordinates in world space are traditionally represented by right ascension and declination (ra and dec), or longitude and latitude. While these representations are convenient, they have discontinuities at the poles, making operations on them trickier at arbitrary locations on the sky sphere. Therefore, all internal operations of this library are done in 3D vector space, where coordinates are represented as (x, y, z) vectors. The spherical_geometry.vector module contains functions to convert between (ra, dec) and (x, y, z) representations.

While any (x, y, z) triple represents a vector and therefore a location on the sky sphere, a distinction must be made between normalized coordinates that fall exactly on the unit sphere, and unnormalized coordinates which do not. A normalized coordinate is defined as a vector whose length is 1, i.e.:

\sqrt{x^2 + y^2 + z^2} = 1

To prevent unnecessary recomputation, many methods in this library assume that the vectors passed in are already normalized. If this is not the case, spherical_geometry.vector.normalize_vector can be used to normalize an array of vectors.

When not working in Cartesian vectors, the library allows the user to work in either degrees or radians. All methods that require or return an angular value have a degrees keyword argument. When degrees is True, these measurements are in degrees, otherwise they are in radians.

Spherical polygons

Spherical polygons are arbitrary areas on the sky sphere enclosed by great circle arcs. They are represented by the ~spherical_geometry.polygon.SphericalPolygon class.

Representation

The points defining the polygon are available from the ~polygon.SphericalPolygon.points property. It is a Nx3 array where each row is an (x, y, z) vector, normalized. The polygon points are explicitly closed, i.e., the first and last points are the same.

Where is the inside?

The edges of a polygon serve to separate the “inside” from the “outside” area. On a traditional 2D planar surface, the “inside” is defined as the finite area and the “outside” is the infinite area. However, since the surface of a sphere is cyclical, i.e., it wraps around on itself, the a spherical polygon actually defines two finite areas. To specify which should be considered the “inside” vs. the “outside”, the definition of the polygon also has an “inside point” which is just any point that should be considered inside of the polygon.

In the following image, the inside point (marked with the red dot) declares that the area of the polygon is the green region, and not the white region.

inside.png

The inside point of the the polygon can be obtained from the ~polygon.SphericalPolygon.inside property.

What is the orientation?

The correctness of several of the algorithms using polygons depends on a consistent orientation of the points defining it. That is, the points should have a clockwise order. When creating a new spherical polygon, the order of the points defining a polygon will be reversed if they are not in clockwise order. The method `SphericalPolygon.is_clockwise`is used to est if the points are in clockwise order. It takes two successive sides and computes the normal vector to the sides. If the normal vector points inward towards the center of the sphere, the two sides are counter clockwise. If the normal vector points outward, the two sides are clockwise. The code determines the orientation by computing the triple product of the two sides with the vertex of the the two sides. Summing the triple product over all the sides gives the predominant orientation of the points in the polygon.

Disjoint Polygons

If a polygon is the result of the intersection of polygons, it may be disjoint. Disjoint polygons are represented as a list of spherical polygons. The library handles the details of this internally. However, the user must be aware that several of the properties of polygons are generators and return the value for a single polygon at a time. To access all the values of a proeprty, either use a for loop, or coerce the property to a list. The properties which are generators are:

  • SphericalPolygon.points: The points defining each polygon
  • SphericalPolygon.inside : The inside point of each polygon

If the intersection of two polygons generates disjoint polygons the code computes a new interior point for the disjoint polygons.

Creating spherical polygons

.. currentmodule:: spherical_geometry.polygon

SphericalPolygon objects have 5 different constructors:

  • SphericalPolygon: Takes an array of (x, y, z) points, or a list of disjoint SphericalPolygon instances.
  • SphericalPolygon.from_radec: Takes an array of (ra, dec) points and an inside point.
  • SphericalPolygon.from_cone: Creates a polygon from a cone on the sky shere. Takes (ra, dec, radius).
  • SphericalPolygon.from_wcs: Creates a polygon from the footprint of a FITS image using its WCS header keywords. Takes a FITS filename or a astropy.io.fits.Header object.
  • SphericalPolygon.convex_hull: Creates a polygon that is the convex hull of a list of points.

Operations on Spherical Polygons

Once one has a SphericalPolygon object, there are a number of operations available:

  • ~SphericalPolygon.contains_point: Determines if the given point is inside the polygon.
  • ~SphericalPolygon.intersects_poly: Determines if one polygon intersects with another.
  • ~SphericalPolygon.area: Determine the area of a polygon.
  • ~SphericalPolygon.union and ~SphericalPolygon.multi_union: Return a new polygon that is the union of two or more polygons.
  • ~SphericalPolygon.intersection and ~SphericalPolygon.multi_intersection: Return a new polygon that is the intersection of two or more polygons.
  • ~SphericalPolygon.overlap: Determine how much a given polygon overlaps another.
  • ~SphericalPolygon.to_radec: Convert (x, y, z) points in the polygon to (ra, dec) points.
  • ~SphericalPolygon.draw: Plots the polygon using matplotlib’s Basemap toolkit. This feature is rather bare and intended primarily for debugging purposes.

Great circle arcs

.. currentmodule:: spherical_geometry.great_circle_arc

As seen above, great circle arcs are used to define the edges of the polygon. The spherical_geometry.great_circle_arc module contains a number of functions that are useful for dealing with them.

  • length: Returns the angular distance between two points on the sphere.
  • intersection: Returns the intersection point between two great circle arcs.
  • intersects: Determines if two great circle arcs intersect.
  • intersects_point: Determines if a point is along the great circle arc.
  • angle: Calculate the angle between two great circle arcs.
  • midpoint: Calculate the midpoint along a great circle arc.

spherical_geometry's People

Contributors

bernie-simon avatar astrofrog avatar mdboom avatar eteq avatar embray avatar larrybradley avatar pllim avatar jhunkeler avatar mwcraig avatar cdeil avatar bsipocz avatar kbarbary avatar keflavich avatar mcara avatar wkerzendorf avatar nden avatar crawfordsm avatar willmycroft avatar hamogu avatar

Watchers

Alastair McKinstry avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.