Class: QgsLineString

class qgis.core.QgsLineString

Bases: QgsCurve

QgsLineString(points: Iterable[QgsPoint]) Construct a linestring from a vector of points. Z and M type will be set based on the type of the first point in the vector.

New in version 3.0.

QgsLineString(x: Iterable[float], y: Iterable[float], z: Iterable[float] = [], m: Iterable[float] = [], is25DType: bool = False) Construct a linestring from arrays of coordinates. If the z or m arrays are non-empty then the resultant linestring will have z and m types accordingly. This constructor is more efficient then calling setPoints() or repeatedly calling addVertex()

If the z vector is filled, then the geometry type will either be a LineStringZ(M) or LineString25D depending on the is25DType argument. If is25DType is True (and the m vector is unfilled) then the created Linestring will be a LineString25D type. Otherwise, the LineString will be LineStringZ (or LineStringZM) type.

New in version 3.0.

QgsLineString(p1: QgsPoint, p2: QgsPoint) Constructs a linestring with a single segment from p1 to p2.

New in version 3.2.

QgsLineString(points: Iterable[QgsPointXY]) Construct a linestring from list of points. This constructor is more efficient then calling setPoints() or repeatedly calling addVertex()

New in version 3.0.

QgsLineString(segment: QgsLineSegment2D) Construct a linestring from a single 2d line segment.

New in version 3.2.

QgsLineString(QgsLineString)

Line string geometry type, with support for z-dimension and m-values.

Methods

addMValue

param mValue

addToPainterPath

param path

addVertex

Adds a new vertex to the end of the line string.

addZValue

param zValue

append

Appends the contents of another line string to the end of this line string.

asGml2

param doc

asGml3

param doc

asQPolygonF

rtype

QPolygonF

asWkb

rtype

QByteArray

asWkt

param precision

calculateBoundingBox

rtype

QgsRectangle

centroid

rtype

QgsPoint

childCount

childGeometry

Returns pointer to child geometry (for geometries with child geometries - i.e.

childPoint

clear

clearCache

clone

rtype

QgsLineString

close

Closes the line string by appending the first point to the end of the line, if it is not already closed.

closestSegment

param pt

convertTo

param type

createEmptyWithSameType

rtype

QgsLineString

curveSubstring

param startDistance

curveToLine

Returns a new line string geometry corresponding to a segmentized approximation of the curve.

deleteVertex

param position

dimension

rtype

int

draw

param p

drawAsPolygon

param p

dropMValue

rtype

bool

dropZValue

rtype

bool

endPoint

rtype

QgsPoint

equals

param other

extend

Extends the line geometry by extrapolating out the start or end of the line by a specified distance.

fromBezierCurve

Returns a new linestring created by segmentizing the bezier curve between start and end, with the specified control points.

fromQPolygonF

Returns a new linestring from a QPolygonF polygon input.

fromWkb

param wkb

fromWkt

param wkt

geometryType

rtype

str

hasChildGeometries

Returns whether the geometry has any child geometries (False for point / curve, True otherwise)

insertVertex

param position

interpolatePoint

param distance

isEmpty

rtype

bool

length

rtype

float

length3D

Returns the length in 3D world of the line string.

mAt

Returns the m-coordinate of the specified node in the line string.

moveVertex

param position

nCoordinates

rtype

int

numPoints

rtype

int

pointAt

param node

pointN

Returns the point at the specified index.

points

rtype

List[QgsPoint]

removeDuplicateNodes

param epsilon

reversed

rtype

QgsLineString

segmentLength

param startVertex

setMAt

Sets the m-coordinate of the specified node in the line string.

setPoints

Resets the line string to match the specified list of points.

setXAt

Sets the x-coordinate of the specified node in the line string.

setYAt

Sets the y-coordinate of the specified node in the line string.

setZAt

Sets the z-coordinate of the specified node in the line string.

setZMTypeFromSubGeometry

Updates the geometry type based on whether sub geometries contain z or m values.

snappedToGrid

param hSpacing

startPoint

rtype

QgsPoint

sumUpArea

rtype

float

swapXy

toCurveType

Returns the geometry converted to the more generic curve type QgsCompoundCurve

transform

transform(self, t: QTransform, zTranslate: float = 0, zScale: float = 1, mTranslate: float = 0, mScale: float = 1)

vertexAngle

param vertex

xAt

Returns the x-coordinate of the specified node in the line string.

yAt

Returns the y-coordinate of the specified node in the line string.

zAt

Returns the z-coordinate of the specified node in the line string.

addMValue(self, mValue: float = 0) → bool
Parameters

mValue (float = 0) –

Return type

bool

addToPainterPath(self, path: QPainterPath)
Parameters

path (QPainterPath) –

addVertex(self, pt: QgsPoint)

Adds a new vertex to the end of the line string.

Parameters

pt (QgsPoint) – vertex to add

addZValue(self, zValue: float = 0) → bool
Parameters

zValue (float = 0) –

Return type

bool

append(self, line: QgsLineString)

Appends the contents of another line string to the end of this line string.

Parameters

line (QgsLineString) – line to append. Ownership is not transferred.

asGml2(self, doc: QDomDocument, precision: int = 17, ns: str = '', axisOrder: QgsAbstractGeometry.AxisOrder = QgsAbstractGeometry.AxisOrder.XY) → QDomElement
Parameters
  • doc (QDomDocument) –

  • precision (int = 17) –

  • ns (str = '') –

  • axisOrder (QgsAbstractGeometry.AxisOrder = QgsAbstractGeometry.AxisOrder.XY) –

Return type

QDomElement

asGml3(self, doc: QDomDocument, precision: int = 17, ns: str = '', axisOrder: QgsAbstractGeometry.AxisOrder = QgsAbstractGeometry.AxisOrder.XY) → QDomElement
Parameters
  • doc (QDomDocument) –

  • precision (int = 17) –

  • ns (str = '') –

  • axisOrder (QgsAbstractGeometry.AxisOrder = QgsAbstractGeometry.AxisOrder.XY) –

Return type

QDomElement

asQPolygonF(self) → QPolygonF
Return type

QPolygonF

asWkb(self) → QByteArray
Return type

QByteArray

asWkt(self, precision: int = 17) → str
Parameters

precision (int = 17) –

Return type

str

calculateBoundingBox(self)QgsRectangle
Return type

QgsRectangle

centroid(self)QgsPoint
Return type

QgsPoint

childCount(self) → int
childGeometry(self, index: int)QgsAbstractGeometry

Returns pointer to child geometry (for geometries with child geometries - i.e. geom. collection / polygon)

Note

used for vertex_iterator implementation

New in version 3.0.

childPoint(self, index: int)QgsPoint
clear(self)
clearCache(self)
clone(self)QgsLineString
Return type

QgsLineString

close(self)

Closes the line string by appending the first point to the end of the line, if it is not already closed.

closestSegment(self, pt: QgsPoint, epsilon: float = 4 * DBL_EPSILON) → Tuple[float, QgsPoint, QgsVertexId, int]
Parameters
  • pt (QgsPoint) –

  • epsilon (float = 4*DBL_EPSILON) –

Return type

Tuple[float, QgsPoint, QgsVertexId, int]

convertTo(self, type: QgsWkbTypes.Type) → bool
Parameters

type (QgsWkbTypes.Type) –

Return type

bool

createEmptyWithSameType(self)QgsLineString
Return type

QgsLineString

curveSubstring(self, startDistance: float, endDistance: float)QgsLineString
Parameters
  • startDistance (float) –

  • endDistance (float) –

Return type

QgsLineString

curveToLine(self, tolerance: float = M_PI_2 / 90, toleranceType: QgsAbstractGeometry.SegmentationToleranceType = QgsAbstractGeometry.MaximumAngle)QgsLineString

Returns a new line string geometry corresponding to a segmentized approximation of the curve.

Parameters
  • tolerance (float = M_PI_2/90) – segmentation tolerance

  • toleranceType (QgsAbstractGeometry.SegmentationToleranceType = QgsAbstractGeometry.MaximumAngle) – maximum segmentation angle or maximum difference between approximation and curve*

Return type

QgsLineString

deleteVertex(self, position: QgsVertexId) → bool
Parameters

position (QgsVertexId) –

Return type

bool

dimension(self) → int
Return type

int

draw(self, p: QPainter)
Parameters

p (QPainter) –

drawAsPolygon(self, p: QPainter)
Parameters

p (QPainter) –

dropMValue(self) → bool
Return type

bool

dropZValue(self) → bool
Return type

bool

endPoint(self)QgsPoint
Return type

QgsPoint

equals(self, other: QgsCurve) → bool
Parameters

other (QgsCurve) –

Return type

bool

extend(self, startDistance: float, endDistance: float)

Extends the line geometry by extrapolating out the start or end of the line by a specified distance. Lines are extended using the bearing of the first or last segment in the line.

New in version 3.0.

Parameters
  • startDistance (float) –

  • endDistance (float) –

fromBezierCurve(start: QgsPoint, controlPoint1: QgsPoint, controlPoint2: QgsPoint, end: QgsPoint, segments: int = 30)QgsLineString

Returns a new linestring created by segmentizing the bezier curve between start and end, with the specified control points.

The segments parameter controls how many line segments will be present in the returned linestring.

Any z or m values present in the input coordinates will be interpolated along with the x and y values.

New in version 3.10.

Parameters
Return type

QgsLineString

fromQPolygonF(polygon: QPolygonF)QgsLineString

Returns a new linestring from a QPolygonF polygon input.

New in version 3.10.

Parameters

polygon (QPolygonF) –

Return type

QgsLineString

fromWkb(self, wkb: QgsConstWkbPtr) → bool
Parameters

wkb (QgsConstWkbPtr) –

Return type

bool

fromWkt(self, wkt: str) → bool
Parameters

wkt (str) –

Return type

bool

geometryType(self) → str
Return type

str

hasChildGeometries(self) → bool

Returns whether the geometry has any child geometries (False for point / curve, True otherwise)

Note

used for vertex_iterator implementation

New in version 3.0.

insertVertex(self, position: QgsVertexId, vertex: QgsPoint) → bool
Parameters
Return type

bool

interpolatePoint(self, distance: float)QgsPoint
Parameters

distance (float) –

Return type

QgsPoint

isEmpty(self) → bool
Return type

bool

length(self) → float
Return type

float

length3D(self) → float

Returns the length in 3D world of the line string. If it is not a 3D line string, return its 2D length.

See also

length()

New in version 3.10.

Return type

float

mAt(self, index: int) → float

Returns the m-coordinate of the specified node in the line string.

An IndexError will be raised if no point with the specified index exists.

If the LineString does not have a m-dimension then nan will be returned.

Indexes can be less than 0, in which case they correspond to positions from the end of the line. E.g. an index of -1 corresponds to the last point in the line.

Parameters

index (int) –

Return type

float

moveVertex(self, position: QgsVertexId, newPos: QgsPoint) → bool
Parameters
Return type

bool

nCoordinates(self) → int
Return type

int

numPoints(self) → int
Return type

int

pointAt(self, node: int, point: QgsPoint) → Tuple[bool, QgsVertexId.VertexType]
Parameters
Return type

Tuple[bool, QgsVertexId.VertexType]

pointN(self, i: int)QgsPoint

Returns the point at the specified index. An IndexError will be raised if no point with the specified index exists.

Indexes can be less than 0, in which case they correspond to positions from the end of the line. E.g. an index of -1 corresponds to the last point in the line.

Parameters

i (int) –

Return type

QgsPoint

points(self) → List[QgsPoint]
Return type

List[QgsPoint]

removeDuplicateNodes(self, epsilon: float = 4 * DBL_EPSILON, useZValues: bool = False) → bool
Parameters
  • epsilon (float = 4*DBL_EPSILON) –

  • useZValues (bool = False) –

Return type

bool

reversed(self)QgsLineString
Return type

QgsLineString

segmentLength(self, startVertex: QgsVertexId) → float
Parameters

startVertex (QgsVertexId) –

Return type

float

setMAt(self, index: int, m: float)

Sets the m-coordinate of the specified node in the line string. The corresponding node must already exist in line string and the line string must have m-dimension.

An IndexError will be raised if no point with the specified index exists.

Indexes can be less than 0, in which case they correspond to positions from the end of the line. E.g. an index of -1 corresponds to the last point in the line.

See also

mAt()

Parameters
  • index (int) –

  • m (float) –

setPoints(self, points: Iterable[QgsPoint])

Resets the line string to match the specified list of points. The line string will inherit the dimensionality of the first point in the list.

Parameters

points (Iterable[QgsPoint]) – new points for line string. If empty, line string will be cleared.

setXAt(self, index: int, x: float)

Sets the x-coordinate of the specified node in the line string. The corresponding node must already exist in line string.

An IndexError will be raised if no point with the specified index exists.

Indexes can be less than 0, in which case they correspond to positions from the end of the line. E.g. an index of -1 corresponds to the last point in the line.

See also

xAt()

Parameters
  • index (int) –

  • x (float) –

setYAt(self, index: int, y: float)

Sets the y-coordinate of the specified node in the line string. The corresponding node must already exist in line string.

An IndexError will be raised if no point with the specified index exists.

Indexes can be less than 0, in which case they correspond to positions from the end of the line. E.g. an index of -1 corresponds to the last point in the line.

See also

yAt()

Parameters
  • index (int) –

  • y (float) –

setZAt(self, index: int, z: float)

Sets the z-coordinate of the specified node in the line string. The corresponding node must already exist in line string and the line string must have z-dimension.

An IndexError will be raised if no point with the specified index exists.

Indexes can be less than 0, in which case they correspond to positions from the end of the line. E.g. an index of -1 corresponds to the last point in the line.

See also

zAt()

Parameters
  • index (int) –

  • z (float) –

setZMTypeFromSubGeometry(self, subggeom: QgsAbstractGeometry, baseGeomType: QgsWkbTypes.Type)

Updates the geometry type based on whether sub geometries contain z or m values.

snappedToGrid(self, hSpacing: float, vSpacing: float, dSpacing: float = 0, mSpacing: float = 0)QgsLineString
Parameters
  • hSpacing (float) –

  • vSpacing (float) –

  • dSpacing (float = 0) –

  • mSpacing (float = 0) –

Return type

QgsLineString

startPoint(self)QgsPoint
Return type

QgsPoint

sumUpArea(self) → float
Return type

float

swapXy(self)
toCurveType(self)QgsCompoundCurve

Returns the geometry converted to the more generic curve type QgsCompoundCurve

Return type

QgsCompoundCurve

Returns

the converted geometry. Caller takes ownership*

transform(self, ct: QgsCoordinateTransform, d: QgsCoordinateTransform.TransformDirection = QgsCoordinateTransform.ForwardTransform, transformZ: bool = False)

transform(self, t: QTransform, zTranslate: float = 0, zScale: float = 1, mTranslate: float = 0, mScale: float = 1)

Parameters
  • ct (QgsCoordinateTransform) –

  • d (QgsCoordinateTransform.TransformDirection = QgsCoordinateTransform.ForwardTransform) –

  • transformZ (bool = False) –

vertexAngle(self, vertex: QgsVertexId) → float
Parameters

vertex (QgsVertexId) –

Return type

float

xAt(self, index: int) → float

Returns the x-coordinate of the specified node in the line string.

An IndexError will be raised if no point with the specified index exists.

Indexes can be less than 0, in which case they correspond to positions from the end of the line. E.g. an index of -1 corresponds to the last point in the line.

Parameters

index (int) –

Return type

float

yAt(self, index: int) → float

Returns the y-coordinate of the specified node in the line string.

An IndexError will be raised if no point with the specified index exists.

Indexes can be less than 0, in which case they correspond to positions from the end of the line. E.g. an index of -1 corresponds to the last point in the line.

Parameters

index (int) –

Return type

float

zAt(self, index: int) → float

Returns the z-coordinate of the specified node in the line string.

An IndexError will be raised if no point with the specified index exists.

If the LineString does not have a z-dimension then nan will be returned.

Indexes can be less than 0, in which case they correspond to positions from the end of the line. E.g. an index of -1 corresponds to the last point in the line.

Parameters

index (int) –

Return type

float