pygplates.PolylineOnSphere
- class pygplates.PolylineOnSphere
- Bases: - GeometryOnSphere- Represents a polyline on the surface of the unit length sphere. Polylines are equality ( - ==,- !=) comparable (but not hashable - cannot be used as a key in a- dict). See- PointOnSpherefor an overview of equality in the presence of limited floating-point precision.- A polyline instance is both: - a sequence of - points- see- get_points, and
- a sequence of - segments(between adjacent points) - see- get_segments().
 - In addition a polyline instance is directly iterable over its points (without having to use - get_points):- polyline = pygplates.PolylineOnSphere(points) for point in polyline: ... - …and so the following operations for accessing the points are supported: - Operation - Result - len(polyline)- number of vertices in polyline - for p in polyline- iterates over the vertices p of polyline - p in polyline- Trueif p is equal to a vertex of polyline- p not in polyline- Falseif p is equal to a vertex of polyline- polyline[i]- the vertex of polyline at index i - polyline[i:j]- slice of polyline from i to j - polyline[i:j:k]- slice of polyline from i to j with step k Since a PolylineOnSphere is immutable it contains no operations or methods that modify its state (such as adding or removing points). This is similar to other immutable types in python such as- str.So instead of modifying an existing polyline you will need to create a new- PolylineOnSphereinstance as the following example demonstrates:- # Get a list of points from an existing PolylineOnSphere 'polyline'. points = list(polyline) # Modify the points list somehow. points[0] = pygplates.PointOnSphere(...) points.append(pygplates.PointOnSphere(...)) # 'polyline' now references a new PolylineOnSphere instance. polyline = pygplates.PolylineOnSphere(points) - A PolylineOnSphere can also be pickled. - Changed in version 0.42: Added pickle support. - __init__(...)
- A PolylineOnSphere object can be constructed in more than one way… - __init__(points)
- Create a polyline from a sequence of (x,y,z) or (latitude,longitude) points. - param points:
- A sequence of (x,y,z) points, or (latitude,longitude) points (in degrees). 
- type points:
- any sequence of - PointOnSphereor- LatLonPointor tuple (float,float,float) or tuple (float,float)
- raises:
- InvalidLatLonError if any latitude or longitude is invalid 
- raises:
- ViolatedUnitVectorInvariantError if any (x,y,z) is not unit magnitude 
- raises:
- InvalidPointsForPolylineConstructionError if sequence has less than two points or if any two points (adjacent in the points sequence) are antipodal to each other (on opposite sides of the globe) 
 - Note - The sequence must contain at least two points in order to be a valid polyline, otherwise InvalidPointsForPolylineConstructionError will be raised. - During creation, a - GreatCircleArcis created between each adjacent pair of points in points - see- get_segments().- It is not an error for adjacent points in the sequence to be coincident. In this case each - GreatCircleArcbetween two such adjacent points will have zero length (- GreatCircleArc.is_zero_length()will return- True) and will have no rotation axis (- GreatCircleArc.get_rotation_axis()will raise an error). However if two such adjacent points are antipodal (on opposite sides of the globe) then InvalidPointsForPolylineConstructionError will be raised.- The following example shows a few different ways to create a - polyline:- points = [] points.append(pygplates.PointOnSphere(...)) points.append(pygplates.PointOnSphere(...)) points.append(pygplates.PointOnSphere(...)) polyline = pygplates.PolylineOnSphere(points) points = [] points.append((lat1,lon1)) points.append((lat2,lon2)) points.append((lat3,lon3)) polyline = pygplates.PolylineOnSphere(points) points = [] points.append([x1,y1,z1]) points.append([x2,y2,z2]) points.append([x3,y3,z3]) polyline = pygplates.PolylineOnSphere(points) - If you have latitude/longitude values but they are not a sequence of tuples or if the latitude/longitude order is swapped then the following examples demonstrate how you could restructure them: - # Flat lat/lon array. points = numpy.array([lat1, lon1, lat2, lon2, lat3, lon3]) polyline = pygplates.PolylineOnSphere(zip(points[::2],points[1::2])) # Flat lon/lat list (ie, different latitude/longitude order). points = [lon1, lat1, lon2, lat2, lon3, lat3] polyline = pygplates.PolylineOnSphere(zip(points[1::2],points[::2])) # Separate lat/lon arrays. lats = numpy.array([lat1, lat2, lat3]) lons = numpy.array([lon1, lon2, lon3]) polyline = pygplates.PolylineOnSphere(zip(lats,lons)) # Lon/lat list of tuples (ie, different latitude/longitude order). points = [(lon1, lat1), (lon2, lat2), (lon3, lat3)] polyline = pygplates.PolylineOnSphere([(lat,lon) for lon, lat in points]) 
- __init__(geometry, [allow_one_point=True])
- Create a polyline from a - GeometryOnSphere.- param geometry:
- The point, multi-point, polyline or polygon geometry to convert from. 
- type geometry:
- param allow_one_point:
- Whether geometry is allowed to be a - PointOnSphereor a- MultiPointOnSpherecontaining only a single point - if allowed then that single point is duplicated since a PolylineOnSphere requires at least two points - default is- True.
- type allow_one_point:
- bool 
- raises:
- InvalidPointsForPolylineConstructionError if geometry is a - PointOnSphere(and allow_one_point is- False), or a- MultiPointOnSpherewith one point (and allow_one_point is- False), or if any two consecutive points in a- MultiPointOnSphereare antipodal to each other (on opposite sides of the globe)
 - If allow_one_point is - Truethen geometry can be- PointOnSphere,- MultiPointOnSphere,- PolylineOnSphereor- PolygonOnSphere. However if allow_one_point is- Falsethen geometry must be a- PolylineOnSphere, or a- PolygonOnSphere, or a- MultiPointOnSpherecontaining at least two points to avoid raising InvalidPointsForPolylineConstructionError.- During creation, a - GreatCircleArcis created between each adjacent pair of geometry points - see- get_segments().- It is not an error for adjacent points in a geometry sequence to be coincident. In this case each - GreatCircleArcbetween two such adjacent points will have zero length (- GreatCircleArc.is_zero_length()will return- True) and will have no rotation axis (- GreatCircleArc.get_rotation_axis()will raise an error). However if two such adjacent points are antipodal (on opposite sides of the globe) then InvalidPointsForPolylineConstructionError will be raised- To create a PolylineOnSphere from any geometry type: - polyline = pygplates.PolylineOnSphere(geometry) - To create a PolylineOnSphere from any geometry containing at least two points: - try: polyline = pygplates.PolylineOnSphere(geometry, allow_one_point=False) except pygplates.InvalidPointsForPolylineConstructionError: ... # Handle failure to convert 'geometry' to a PolylineOnSphere. - Note If geometry is a polygon then only its exterior ring is converted (interior rings are ignored).And the last point in the created polyline matches the first point to ensure the polyline forms a closed ring.
 
 - Methods - __init__(...)- A PolylineOnSphere object can be constructed in more than one way... - clone()- Create a duplicate of this geometry (derived) instance. - distance(geometry1, geometry2, ...)- [staticmethod] Returns the (minimum) distance between two geometries (in radians). - Returns the total arc length of this polyline (in radians). - Returns the centroid of this polyline. - get_points()- Returns a read-only sequence of - pointsin this geometry.- Returns a read-only sequence of - segmentsin this polyline.- join(geometries, ...)- Joins geometries that have end points closer than a distance threshold. - rotation_interpolate(from_polyline, ...)- [staticmethod] Interpolates between two polylines about a rotation pole. - to_lat_lon_array()- Returns the sequence of points, in this geometry, as a numpy array of (latitude,longitude) pairs (in degrees). - to_lat_lon_list()- Returns the sequence of points, in this geometry, as (latitude,longitude) tuples (in degrees). - to_lat_lon_point_list()- Returns the sequence of points, in this geometry, as - lat lon points.- to_tessellated(tessellate_radians)- Returns a new polyline that is a tessellated version of this polyline. - to_uniform_points(point_spacing_radians, ...)- Returns a sequence of points uniformly spaced along this polyline. - to_xyz_array()- Returns the sequence of points, in this geometry, as a numpy array of (x,y,z) triplets. - to_xyz_list()- Returns the sequence of points, in this geometry, as (x,y,z) cartesian coordinate tuples. - get_arc_length()
- Returns the total arc length of this polyline (in radians). - Return type:
- float 
 
 - get_centroid()
- Returns the centroid of this polyline. - Return type:
 - The centroid is calculated as a weighted average of the mid-points of the - great circle arcsof this polyline with weighting proportional to the individual arc lengths.
 - get_segments()
- Returns a read-only sequence of - segmentsin this polyline.- Return type:
- a read-only sequence of - GreatCircleArc
 - The following operations for accessing the great circle arcs in the returned read-only sequence are supported: - Operation - Result - len(seq)- number of segments of the polyline - for s in seq- iterates over the segments s of the polyline - s in seq- Trueif s is an segment of the polyline- s not in seq- Falseif s is an segment of the polyline- seq[i]- the segment of the polyline at index i - seq[i:j]- slice of segments of the polyline from i to j - seq[i:j:k]- slice of segments of the polyline from i to j with step k - Note - Between each adjacent pair of - pointsthere is an- segmentsuch that the number of points exceeds the number of segments by one.- The following example demonstrates some uses of the above operations: - polyline = pygplates.PolylineOnSphere(points) ... segments = polyline.get_segments() for segment in segments: if not segment.is_zero_length(): segment_midpoint_direction = segment.get_arc_direction(0.5) first_segment = segments[0] last_segment = segments[-1] - Note - The returned sequence is read-only and cannot be modified. - Note - If you want a modifiable sequence consider wrapping the returned sequence in a - listusing something like- segments = list(polyline.get_segments())but note that modifying the- list(eg, appending a new segment) will not modify the original polyline.
 - static join(geometries[, distance_threshold_radians][, polyline_conversion=PolylineConversion.ignore_non_polyline])
- Joins geometries that have end points closer than a distance threshold. - Parameters:
- geometries (sequence (eg, - listor- tuple) of- GeometryOnSphere) – the geometries to join
- distance_threshold_radians (float) – optional closeness distance threshold in radians for joining to occur (if not specified then end point equality is used) 
- polyline_conversion (PolylineConversion.convert_to_polyline, PolylineConversion.ignore_non_polyline or PolylineConversion.raise_if_non_polyline) – whether to raise error, convert to - PolylineOnSphereor ignore those geometries in geometries that are not- PolylineOnSphere- defaults to PolylineConversion.ignore_non_polyline
 
- Returns:
- a list of joined polylines 
- Return type:
- list of - PolylineOnSphere
- Raises:
- GeometryTypeError if polyline_conversion is PolylineConversion.raise_if_non_polyline and any geometry in geometries is not a - PolylineOnSphere
 - All pairs of geometries are tested for joining and only those with end points closer than distance_threshold_radians radians are joined. Each joined polyline is further joined if possible until there are no more possibilities for joining (or there is a single joined polyline that is a concatenation of all geometries in geometries - depending on polyline_conversion). - Note - If distance_threshold_radians is not specified then the end points must be equal (rather than separated by less than a threshold distance). - When determining if two geometries A and B can be joined the closest pair of end points (one from A and one from B) decides which end of each geometry can be joined, provided their distance is less than distance_threshold_radians radians. If a third geometry C also has an end point close enough to A then the closest of B and C is joined to A. - Two geometries A and B are joined by prepending or appending a (possibly reversed) copy of the points in geometry B to a copy of the points in geometry A. Hence the joined polyline will always have points ordered in the same direction as geometry A (only the points from geometry B are reversed if necessary). So geometries earlier in the geometries sequence determine the direction of joined polylines. - Join three polylines if their end points are within 3 degrees of another: - # If all three polylines join then the returned list will have one joined polyline. # If only two polylines join then the returned list will have two polylines (one original and one joined). # If no polylines join then the returned list will have the three original polylines. joined_polylines = pygplates.PolylineOnSphere.join((polyline1, polyline2, polyline3), math.radians(3)) - Other geometries besides - PolylineOnSpherecan be joined if polyline_conversion is PolylineConversion.convert_to_polyline. This is useful for joining nearby points into polylines for example:- # If all points are close enough then the returned list will have one joined polyline, # otherwise there will be multiple polylines each representing a subset of the points. # If none of the points are close to each other then the returned list will have degenerate # polylines that each look like a point (each polyline has two identical points). joined_polylines = pygplates.PolylineOnSphere.join( points, math.radians(3), pygplates.PolylineConversion.convert_to_polyline) 
 - static rotation_interpolate(from_polyline, to_polyline, rotation_pole, interpolate[, minimum_latitude_overlap_radians=0][, maximum_latitude_non_overlap_radians=0][, maximum_distance_threshold_radians][, flatten_longitude_overlaps=FlattenLongitudeOverlaps.no][, polyline_conversion=PolylineConversion.ignore_non_polyline])
- [staticmethod] Interpolates between two polylines about a rotation pole. - Parameters:
- from_polyline ( - GeometryOnSphere) – the polyline to interpolate from
- to_polyline ( - GeometryOnSphere) – the polyline to interpolate to
- rotation_pole ( - PointOnSphereor- LatLonPointor tuple (latitude,longitude), in degrees, or tuple (x,y,z)) – the rotation axis to interpolate around
- interpolate (float, or list of float) – if a single number then interpolate is the interval spacing, in radians, between from_polyline and to_polyline at which to generate interpolated polylines - otherwise if a sequence of numbers (eg, list or tuple) then interpolate is the sequence of interpolate ratios, in the range [0,1], at which to generate interpolated polylines (with 0 meaning from_polyline and 1 meaning to_polyline) 
- minimum_latitude_overlap_radians (float - defaults to zero) – required amount of latitude overlap of polylines 
- maximum_latitude_non_overlap_radians (float - defaults to zero) – allowed non-overlapping latitude region 
- maximum_distance_threshold_radians (float - default is no threshold detection) – maximum distance (in radians) between from_polyline and to_polyline - if specified and if exceeded then - Noneis returned
- flatten_longitude_overlaps (FlattenLongitudeOverlaps.no, FlattenLongitudeOverlaps.use_from or FlattenLongitudeOverlaps.use_to - defaults to FlattenLongitudeOverlaps.no) – whether or not to ensure from_polyline and to_polyline do not overlap in longitude (in North pole reference frame of rotation_pole) and how to correct the overlap 
- polyline_conversion (PolylineConversion.convert_to_polyline, PolylineConversion.ignore_non_polyline or PolylineConversion.raise_if_non_polyline) – whether to raise error, convert to - PolylineOnSphereor ignore from_polyline and to_polyline if they are not- PolylineOnSphere(ignoring equates to returning- None) - defaults to PolylineConversion.ignore_non_polyline
 
- Returns:
- list of interpolated polylines - or - Noneif polylines do not have overlapping latitude ranges or if maximum distance threshold exceeded or if either polyline is not a- PolylineOnSphere(and polyline_conversion is PolylineConversion.ignore_non_polyline)
- Return type:
- list of - PolylineOnSphereor None
- Raises:
- GeometryTypeError if from_polyline or to_polyline are not of type - PolylineOnSphere(and polyline_conversion is PolylineConversion.raise_if_non_polyline)
 - If interpolate is a single number then it is the distance interval spacing, in radians, between from_polyline and to_polyline at which to generate interpolated polylines. Also modified versions of from_polyline and to_polyline are returned along with the interpolated polylines. - If interpolate is a sequence of numbers (eg, list or tuple) then it is the sequence of interpolate ratios, in the range [0,1], at which to generate interpolated polylines (with 0 meaning from_polyline and 1 meaning to_polyline and values between meaning interpolated polylines). - The points in the returned polylines are ordered from closest (latitude) to rotation_pole to furthest (which may be different than the order in the original polylines). The modified versions of polylines from_polyline and to_polyline, and hence all interpolated polylines, have monotonically decreasing latitudes (in North pole reference frame of rotation_pole) starting with the northmost polyline end-point and (monotonically) decreasing southward such that subsequent points have latitudes lower than, or equal to, all previous points as shown in the following diagram: - /| / | / | ___ | | | /| | |/ | |__ | | | ===> | | /| | |/ | |__ | | \ \ \ \ | | | / | |/ |__ - The modified versions of polylines from_polyline and to_polyline are also clipped to have a common overlapping latitude range (with a certain amount of non-overlapping allowed if max_latitude_non_overlap_radians is non-zero). - minimum_latitude_overlap_radians specifies the amount that from_polyline and to_polyline must overlap in latitude (North pole reference frame of rotation_pole), otherwise - Noneis returned. Note that this also means if the range of latitudes of either polyline is smaller than the minimum overlap then- Noneis returned. The following diagram shows the original latitude overlapping polylines on the left and the resultant interpolated polylines on the right clipped to the latitude overlapping range (in rotation_pole reference frame):- |_ | | | | | | |_ |_ ===> |_ |_ |_ | | | | | | | | | | |_ | - However problems can arise if rotation_pole is placed such that one, or both, the original polylines (from_polyline and to_polyline) strongly overlaps itself (in rotation_pole reference frame) causing the monotonically-decreasing-latitude requirement to severely distort its geometry. The following diagram shows the original polylines in the top of the diagram and the resultant interpolated polylines in the bottom of the diagram: - \ \ ______ \ ____| |____ \ __| |__ \ / \ \ \ \ | | | | | | || || \/ ______________________________ \ \ \ | | | | | | | | |- If maximum_latitude_non_overlap_radians is non-zero then an extra range of non-overlapping latitudes at the North and South (in rotation_pole reference frame) of from_polyline and to_polyline is allowed. The following diagram shows the original latitude overlapping polylines on the left and the resultant interpolated polylines on the right with a limited amount of non-overlapping interpolation from the North end of one polyline and from the South end of the other (in rotation_pole reference frame): - | | | | | | | | | | | | |_ |_ ===> |_ |_ |_ | | | | | | | | | | | | | | | | | - If flatten_longitude_overlaps is FlattenLongitudeOverlaps.use_from or FlattenLongitudeOverlaps.use_to then this function ensures the longitudes of each point pair of from_polyline and to_polyline (in North pole reference frame of rotation_pole) at the same latitude don’t overlap. For those point pairs where overlap occurs, the points in from_polyline are copied to the corresponding (same latitude) points in to_polyline if FlattenLongitudeOverlaps.use_from is used (and vice versa if FlattenLongitudeOverlaps.use_to is used). This essentially removes or flattens overlaps in longitude. The following diagram shows the original longitude overlapping polylines on the left and the resultant interpolated polylines on the right (in rotation_pole reference frame) after longitude flattening with FlattenLongitudeOverlaps.use_from: - from to \ / \ | / \ / \ | / \ / \|/ . . / \ \ / \ \ / \ \ | | | | | ===> | | | | \ / / \ / / \ / / . . / \ /|\ / \ / | \ / \ / | \ / \ / | \ - Returns - Noneif:- the polylines do not overlap by at least minimum_latitude_overlap_radians radians (where North pole is rotation_axis), or 
- any corresponding pair of points (same latitude) of the polylines are separated by a distance of more than max_distance_threshold_radians (if specified). 
 - Note - All returned polylines have the same number of points. - Note - Corresponding points in returned polylines (points at same indices) have the same latitude (in North pole reference frame of rotation_pole) except those points in the non-overlapping latitude ranges (if maximum_latitude_non_overlap_radians is specified). - To interpolate polylines with a spacing of 2 minutes (with a minimum required latitude overlap of 1 degree and with an allowed latitude non-overlap of up to 3 degrees and with no distance threshold and with no longitude overlaps flattened): - interpolated_polylines = pygplates.PolylineOnSphere.rotation_interpolate( from_polyline, to_polyline, rotation_pole, math.radians(2.0/60), math.radians(1), math.radians(3)) - To interpolate polylines at interpolate ratios between 0 and 1 at 0.1 intervals (with a minimum required latitude overlap of 1 degree and with an allowed latitude non-overlap of up to 3 degrees and with no distance threshold and with no longitude overlaps flattened): - interpolated_polylines = pygplates.PolylineOnSphere.rotation_interpolate( from_polyline, to_polyline, rotation_pole, range(0, 1.01, 0.1), math.radians(1), math.radians(3)) - An easy way to test whether two polylines can possibly be interpolated without actually interpolating anything is to specify an empty list of interpolate ratios: - if pygplates.PolylineOnSphere.rotation_interpolate( from_polyline, to_polyline, rotation_pole, [], ...) is not None: # 'from_polyline' and 'to_polyline' can be interpolated (ie, they overlap # and don't exceed the maximum distance threshold) ... 
 - to_tessellated(tessellate_radians)
- Returns a new polyline that is a tessellated version of this polyline. - Parameters:
- tessellate_radians (float) – maximum tessellation angle (in radians) 
- Return type:
- Raises:
- ValueError if tessellate_radians is negative or zero 
 - Adjacent points (in the returned tessellated polyline) are separated by no more than tessellate_radians on the globe. - Create a polyline tessellated to 2 degrees: - tessellated_polyline = polyline.to_tessellated(math.radians(2)) - Note - Since a PolylineOnSphere is immutable it cannot be modified. Which is why a new (tessellated) PolylineOnSphere is returned. - Note - The distance between adjacent points (in the tessellated polyline) will not be exactly uniform. This is because each - segmentin the original polyline is tessellated to the nearest integer number of points (that keeps that segment under the threshold) and hence each original segment will have a slightly different tessellation angle.- See also 
 - to_uniform_points(point_spacing_radians[, first_point_spacing_radians=0.0][, return_segment_informations=False])
- Returns a sequence of points uniformly spaced along this polyline. - Parameters:
- point_spacing_radians (float) – spacing between points (in radians) 
- first_point_spacing_radians (float) – Spacing of first uniform point from this polyline’s first vertex (in radians). By default the first uniform point coincides with this polyline’s first vertex. Ideally this is non-negative (but, for example, if it’s slightly negative then the first uniform point will be slightly off the first arc near its start point but still on its great circle). 
- return_segment_informations (bool) – whether to also return information about the polyline segment that each uniform point is on - default is - False
 
- Returns:
- list of points, or (if return_segment_informations is - True) a 2-tuple containing a list of points and a list of segment informations (which are 2-tuples identifying the index of the- segmentcontaining the point, and where the point is located on that segment in the range [0,1])
- Return type:
- list of - PointOnSphere, or tuple (list of- PointOnSphere, list of tuple (int, float)) if return_segment_informations is- True
- Raises:
- ValueError if point_spacing_radians is negative or zero 
 - Note - The distance (along the polyline) between the last uniform point and the last vertex of the polyline can be less than point_spacing_radians (since the length of the polyline minus first_point_spacing_radians might not be an integer multiple of point_spacing_radians). - Note If first_point_spacing_radians is greater than the- polyline's lengththen no uniform points will be generated.And if the polyline length is zero and first_point_spacing_radians is zero then a single uniform point will be generated.- Create points uniformly spaced by 1 degree along a polyline starting 0.5 degrees from the first polyline vertex: - uniform_points = polyline.to_uniform_points( math.radians(1), first_point_spacing_radians = math.radians(0.5)) - Next, we extend the above example by associating a segment normal (from great circle arc) with each uniform point: - uniform_points, uniform_point_segment_informations = polyline.to_uniform_points( math.radians(1), first_point_spacing_radians = math.radians(0.5), return_segment_informations = True) # Each uniform point is on a segment, so retrieve a segment normal for each point. # We end up with a list of normals (with a list length equal to the number of uniform points). polyline_segments = polyline.get_segments() uniform_point_normals = [polyline_segments[segment_index].get_great_circle_normal() for segment_index, _ in uniform_point_segment_informations] - See also - Added in version 0.47.