openquake.hazardlib.geo.utils contains functions that are common
to several geographical primitives and some other low-level spatial operations.
OrthographicProjection(west, east, north, south)¶
Callable object to compute orthographic projections. See the docstring of get_orthographic_projection.
Return the spherical coordinates for coordinates in Cartesian space.
This function does an opposite to
Parameters: vectors – Array of 3d vectors in Cartesian space. Returns: Tuple of three arrays of the same shape as
vectorsrepresenting longitude (decimal degrees), latitude (decimal degrees) and depth (km) in specified order.
Given a list of
Pointobjects, return a new list with adjacent duplicate points removed.
Return True if two longitude values define line crossing international date line.
>>> cross_idl(-45, 45) False >>> cross_idl(-180, -179) False >>> cross_idl(180, 179) False >>> cross_idl(45, -45) False >>> cross_idl(0, 0) False >>> cross_idl(-170, 170) True >>> cross_idl(170, -170) True >>> cross_idl(-180, 180) True
Return the distance between two longitude values as an angular measure. Parameters represent two longitude values in degrees.
Returns: Float, the angle between
lon2in degrees. Value is positive if
lon2is on the east from
lon1and negative otherwise. Absolute value of the result doesn’t exceed 180 for valid parameters values.
get_middle_point(lon1, lat1, lon2, lat2)¶
Given two points return the point exactly in the middle lying on the same great circle arc.
Parameters are point coordinates in degrees.
Returns: Tuple of longitude and latitude of the point in the middle.
get_orthographic_projection(west, east, north, south)¶
Create and return a projection object for a given bounding box.
Returns: callable OrthographicProjection object that can perform both forward and reverse projection (converting from longitudes and latitudes to x and y values on 2d-space and vice versa). The call takes three arguments: first two are numpy arrays of longitudes and latitudes or abscissae and ordinates of points to project and the third one is a boolean that allows to choose what operation is requested – is it forward or reverse one.
Truevalue given to third positional argument (or keyword argument “reverse”) indicates that the projection of points in 2d space back to earth surface is needed. The default value for “reverse” argument is
False, which means forward projection (degrees to kilometers).
ValueErrorin forward projection mode if any of the target points is further than 90 degree (along the great circle arc) from the projection center.
Parameters are given as floats, representing decimal degrees (first two are longitudes and last two are latitudes). They define a bounding box in a spherical coordinates of the collection of points that is about to be projected. The center point of the projection (coordinates (0, 0) in Cartesian space) is set to the middle point of that bounding box. The resulting projection is defined for spherical coordinates that are not further from the bounding box center than 90 degree on the great circle arc.
The result projection is of type Orthographic. This projection is prone to distance, area and angle distortions everywhere outside of the center point, but still can be used for checking shapes: verifying if line intersects itself (like in
line_intersects_itself()) or if point is inside of a polygon (like in
openquake.hazardlib.geo.polygon.Polygon.discretize()). It can be also used for measuring distance to an extent of around 700 kilometers (error doesn’t exceed 1 km up until then).
Given a collection of points find and return the bounding box, as a pair of longitudes and a pair of latitudes.
Parameters define longitudes and latitudes of a point collection respectively in a form of lists or numpy arrays.
Returns: A tuple of four items. These items represent western, eastern, northern and southern borders of the bounding box respectively. Values are floats in decimal degrees. Raises: ValueError – If points collection has the longitudinal extent of more than 180 degrees (it is impossible to define a single hemisphere bound to poles that would contain the whole collection).
line_intersects_itself(lons, lats, closed_shape=False)¶
Trueif line of points intersects itself. Line with the last point repeating the first one considered intersecting itself.
The line is defined by lists (or numpy arrays) of points’ longitudes and latitudes (depth is not taken into account).
Parameters: closed_shape – If
Truethe line will be checked twice: first time with its original shape and second time with the points sequence being shifted by one point (the last point becomes first, the first turns second and so on). This is useful for checking that the sequence of points defines a valid
Get unit vector for a given one.
Parameters: vector – Numpy vector as coordinates in Cartesian space, or an array of such. Returns: Numpy array of the same shape and structure where all vectors are normalized. That is, each coordinate component is divided by its vector’s length.
This fits an n-dimensional plane to a set of points. See http://stackoverflow.com/questions/12299540/plane-fitting-to-4-or-more-xyz-points
Parameters: points – An instance of :class:~numpy.ndarray. The number of columns must be equal to three. Returns: A point on the plane and the normal to the plane.
point_to_polygon_distance(polygon, pxx, pyy)¶
Calculate the distance to polygon for each point of the collection on the 2d Cartesian plane.
- polygon – Shapely “Polygon” geometry object.
- pxx – List or numpy array of abscissae values of points to calculate the distance from.
- pyy – Same structure as
pxx, but with ordinate values.
Numpy array of distances in units of coordinate system. Points that lie inside the polygon have zero distance.
spherical_to_cartesian(lons, lats, depths)¶
Return the position vectors (in Cartesian coordinates) of list of spherical coordinates.
For equations see: http://mathworld.wolfram.com/SphericalCoordinates.html.
Parameters are components of spherical coordinates in a form of scalars, lists or numpy arrays.
Nonein which case it’s considered zero for all points.
numpy.arrayof 3d vectors representing points’ coordinates in Cartesian space. The array has the same shape as parameter arrays. In particular it means that if
latsare scalars, the result is a single 3d vector. Vector of length
1represents distance of 1 km.
triangle_area(e1, e2, e3)¶
Get the area of triangle formed by three vectors.
Parameters are three three-dimensional numpy arrays representing vectors of triangle’s edges in Cartesian space.
Returns: Float number, the area of the triangle in squared units of coordinates, or numpy array of shape of edges with one dimension less.
Uses Heron formula, see http://mathworld.wolfram.com/HeronsFormula.html.