Class: QgsPointLocator

Defines the interface for querying point locations.

This class offers:

  • query nearest vertices / edges to a point

  • query vertices / edges in rectangle

  • query areas covering a point

Works with one layer.

Note

This is an abstract class, with methods which must be implemented by a subclass.

The following methods must be implemented: acceptMatch()

Class Hierarchy

Inheritance diagram of qgis.core.QgsPointLocator

Base classes

QObject

Methods

cachedGeometryCount

Returns how many geometries are cached in the index

destinationCrs

Gets destination CRS - may be an invalid QgsCoordinateReferenceSystem if not doing OTF reprojection

destroyIndex

edgesInRect

Find edges within a specified rectangle Optional filter may discard unwanted matches.

extent

Gets extent of the area point locator covers - if None then it caches the whole layer

hasIndex

Indicate whether the data have been already indexed

init

Prepare the index for queries.

isIndexing

Returns True if the point locator is currently indexing the data.

layer

Gets associated layer

nearestArea

Find nearest area to the specified point - up to distance specified by tolerance Optional filter may discard unwanted matches.

nearestCentroid

Find nearest centroid to the specified point - up to distance specified by tolerance Optional filter may discard unwanted matches.

nearestEdge

Find nearest edge to the specified point - up to distance specified by tolerance Optional filter may discard unwanted matches.

nearestLineEndpoints

Find nearest line endpoint (start or end vertex) to the specified point - up to distance specified by tolerance Optional filter may discard unwanted matches.

nearestMiddleOfSegment

Find nearest middle of segment to the specified point - up to distance specified by tolerance Optional filter may discard unwanted matches.

nearestVertex

Find nearest vertex to the specified point - up to distance specified by tolerance Optional filter may discard unwanted matches.

pointInPolygon

Find out if the point is in any polygons

rebuildIndex

setExtent

Configure extent - if not None, it will index only that area

setRenderContext

Configure render context - if not None, it will use to index only visible feature

verticesInRect

Find vertices within a specified rectangle This method is either blocking or non blocking according to relaxed parameter passed Optional filter may discard unwanted matches.

waitForIndexingFinished

If the point locator has been initialized relaxedly and is currently indexing, this methods waits for the indexing to be finished

Signals

initFinished

Emitted whenever index has been built and initialization is finished

Attributes

All

Area

Centroid

Edge

Invalid

LineEndpoint

MiddleOfSegment

Vertex

class qgis.core.QgsPointLocator[source]

Bases: QObject

__init__(layer: QgsVectorLayer | None, destinationCrs: QgsCoordinateReferenceSystem = QgsCoordinateReferenceSystem(), transformContext: QgsCoordinateTransformContext = QgsCoordinateTransformContext(), extent: QgsRectangle | None = None)

Construct point locator for a layer.

If a valid QgsCoordinateReferenceSystem is passed for destinationCrs then the locator will do the searches on data reprojected to the given CRS. For accurate reprojection it is important to set the correct transformContext if a destinationCrs is specified. This is usually taken from the current QgsProject.transformContext().

If extent is not None, the locator will index only a subset of the layer which falls within that extent.

Parameters:
All = 31
Area = 4
Centroid = 8
Edge = 2
Invalid = 0
LineEndpoint = 32
class Match

Bases: object

construct invalid match

QgsPointLocator.Match(t: QgsPointLocator.Type, vl: Optional[QgsVectorLayer], fid: int, dist: float, pt: QgsPointXY, vertexIndex: int = 0, edgePoints: Optional[QgsPointXY] = None)

QgsPointLocator.Match(a0: QgsPointLocator.Match)

distance(self) float

for vertex / edge match units depending on what class returns it (geom.cache: layer units, map canvas snapper: dest crs units)

Return type:

float

edgePoints(self)

Only for a valid edge match - obtain endpoints of the edge

featureId(self) int

The id of the feature to which the snapped geometry belongs.

Return type:

int

hasArea(self) bool

Returns True if the Match is an area

Return type:

bool

hasCentroid(self) bool

Returns True if the Match is a centroid

Return type:

bool

hasEdge(self) bool

Returns True if the Match is an edge

Return type:

bool

hasLineEndpoint(self) bool

Returns True if the Match is a line endpoint (start or end vertex).

Added in version 3.20.

Return type:

bool

hasMiddleSegment(self) bool

Returns True if the Match is the middle of a segment

Return type:

bool

hasVertex(self) bool

Returns True if the Match is a vertex

Return type:

bool

interpolatedPoint(self, destinationCrs: QgsCoordinateReferenceSystem = QgsCoordinateReferenceSystem()) QgsPoint

Convenient method to return a point on an edge with linear interpolation of the Z value. The parameter destinationCrs depends of where the instance of this Match is created (geom.cache: layer CRS, map canvas snapper: dest CRS)

Added in version 3.10.

Parameters:

destinationCrs (QgsCoordinateReferenceSystem = QgsCoordinateReferenceSystem())

Return type:

QgsPoint

isValid(self) bool
Return type:

bool

layer(self) QgsVectorLayer | None

The vector layer where the snap occurred. Will be None if the snap happened on an intersection.

Return type:

Optional[QgsVectorLayer]

point(self) QgsPointXY

for vertex / edge match coords depending on what class returns it (geom.cache: layer coords, map canvas snapper: dest coords)

Return type:

QgsPointXY

type(self) QgsPointLocator.Type
Return type:

QgsPointLocator.Type

vertexIndex(self) int

for vertex / edge match (first vertex of the edge)

Return type:

int

class MatchFilter

Bases: object

Interface that allows rejection of some matches in intersection queries (e.g. a match can only belong to a particular feature / match must not be a particular point). Implement the interface and pass its instance to QgsPointLocator or QgsSnappingUtils methods.

acceptMatch(self, match: QgsPointLocator.Match) bool
Parameters:

match (QgsPointLocator.Match)

Return type:

bool

MiddleOfSegment = 16
class Type

Bases: int

class Types
class Types(f: QgsPointLocator.Types | QgsPointLocator.Type)
class Types(a0: QgsPointLocator.Types)

Bases: object

Vertex = 1
cachedGeometryCount(self) int[source]

Returns how many geometries are cached in the index

Return type:

int

destinationCrs(self) QgsCoordinateReferenceSystem[source]

Gets destination CRS - may be an invalid QgsCoordinateReferenceSystem if not doing OTF reprojection

Return type:

QgsCoordinateReferenceSystem

destroyIndex(self)[source]
edgesInRect(self, rect: QgsRectangle, filter: QgsPointLocator.MatchFilter | None = None, relaxed: bool = False) List[QgsPointLocator.Match][source]

Find edges within a specified rectangle Optional filter may discard unwanted matches. This method is either blocking or non blocking according to relaxed parameter passed

Parameters:
Return type:

List[QgsPointLocator.Match]

edgesInRect(self, point: QgsPointXY, tolerance: float, filter: QgsPointLocator.MatchFilter | None = None, relaxed: bool = False) List[QgsPointLocator.Match][source]

Override of edgesInRect that construct rectangle from a center point and tolerance This method is either blocking or non blocking according to relaxed parameter passed

Parameters:
Return type:

List[QgsPointLocator.Match]

extent(self) QgsRectangle | None[source]

Gets extent of the area point locator covers - if None then it caches the whole layer

Return type:

Optional[QgsRectangle]

hasIndex(self) bool[source]

Indicate whether the data have been already indexed

Return type:

bool

init(self, maxFeaturesToIndex: int = -1, relaxed: bool = False) bool[source]

Prepare the index for queries. Does nothing if the index already exists. If the number of features is greater than the value of maxFeaturesToIndex, creation of index is stopped to make sure we do not run out of memory. If maxFeaturesToIndex is -1, no limits are used.

This method is either blocking or non blocking according to relaxed parameter passed in the constructor. if True, index building will be done in another thread and init() method returns immediately. initFinished() signal will be emitted once the initialization is over.

Returns False if the creation of index is blocking and has been prematurely stopped due to the limit of features, otherwise True

See also

QgsPointLocator

Parameters:
  • maxFeaturesToIndex (int = -1)

  • relaxed (bool = False)

Return type:

bool

signal initFinished(ok: bool)[source]

Emitted whenever index has been built and initialization is finished

Parameters:

ok (bool) – False if the creation of index has been prematurely stopped due to the limit of features, otherwise True

isIndexing(self) bool[source]

Returns True if the point locator is currently indexing the data. This method is useful if constructor parameter relaxed is True

See also

QgsPointLocator

Return type:

bool

layer(self) QgsVectorLayer | None[source]

Gets associated layer

Return type:

Optional[QgsVectorLayer]

nearestArea(self, point: QgsPointXY, tolerance: float, filter: QgsPointLocator.MatchFilter | None = None, relaxed: bool = False) QgsPointLocator.Match[source]

Find nearest area to the specified point - up to distance specified by tolerance Optional filter may discard unwanted matches. This will first perform a pointInPolygon and return first result. If no match is found and tolerance is not 0, it will return nearestEdge. This method is either blocking or non blocking according to relaxed parameter passed

Parameters:
Return type:

QgsPointLocator.Match

nearestCentroid(self, point: QgsPointXY, tolerance: float, filter: QgsPointLocator.MatchFilter | None = None, relaxed: bool = False) QgsPointLocator.Match[source]

Find nearest centroid to the specified point - up to distance specified by tolerance Optional filter may discard unwanted matches. This method is either blocking or non blocking according to relaxed parameter passed

Added in version 3.12.

Parameters:
Return type:

QgsPointLocator.Match

nearestEdge(self, point: QgsPointXY, tolerance: float, filter: QgsPointLocator.MatchFilter | None = None, relaxed: bool = False) QgsPointLocator.Match[source]

Find nearest edge to the specified point - up to distance specified by tolerance Optional filter may discard unwanted matches. This method is either blocking or non blocking according to relaxed parameter passed

Parameters:
Return type:

QgsPointLocator.Match

nearestLineEndpoints(self, point: QgsPointXY, tolerance: float, filter: QgsPointLocator.MatchFilter | None = None, relaxed: bool = False) QgsPointLocator.Match[source]

Find nearest line endpoint (start or end vertex) to the specified point - up to distance specified by tolerance Optional filter may discard unwanted matches. This method is either blocking or non blocking according to relaxed parameter passed

Added in version 3.20.

Parameters:
Return type:

QgsPointLocator.Match

nearestMiddleOfSegment(self, point: QgsPointXY, tolerance: float, filter: QgsPointLocator.MatchFilter | None = None, relaxed: bool = False) QgsPointLocator.Match[source]

Find nearest middle of segment to the specified point - up to distance specified by tolerance Optional filter may discard unwanted matches. This method is either blocking or non blocking according to relaxed parameter passed

Added in version 3.12.

Parameters:
Return type:

QgsPointLocator.Match

nearestVertex(self, point: QgsPointXY, tolerance: float, filter: QgsPointLocator.MatchFilter | None = None, relaxed: bool = False) QgsPointLocator.Match[source]

Find nearest vertex to the specified point - up to distance specified by tolerance Optional filter may discard unwanted matches. This method is either blocking or non blocking according to relaxed parameter passed

Parameters:
Return type:

QgsPointLocator.Match

pointInPolygon(self, point: QgsPointXY, relaxed: bool = False, filter: QgsPointLocator.MatchFilter | None = None) List[QgsPointLocator.Match][source]

Find out if the point is in any polygons

Parameters:
  • point (QgsPointXY) – The point to check polygons against, in map coordinates

  • relaxed (bool = False) – True if index build has to be non blocking

  • filter (Optional[QgsPointLocator.MatchFilter] = None) – since QGIS 3.36, Optional filter may discard unwanted matches.

Note

Parameters filter and relaxed are in reversed order compared to the rest of MatchList returning methods.

Return type:

List[QgsPointLocator.Match]

rebuildIndex(self, maxFeaturesToIndex: int = -1) bool[source]
Parameters:

maxFeaturesToIndex (int = -1)

Return type:

bool

setExtent(self, extent: QgsRectangle | None)[source]

Configure extent - if not None, it will index only that area

Parameters:

extent (Optional[QgsRectangle])

setRenderContext(self, context: QgsRenderContext | None)[source]

Configure render context - if not None, it will use to index only visible feature

Added in version 3.2.

Parameters:

context (Optional[QgsRenderContext])

verticesInRect(self, rect: QgsRectangle, filter: QgsPointLocator.MatchFilter | None = None, relaxed: bool = False) List[QgsPointLocator.Match][source]

Find vertices within a specified rectangle This method is either blocking or non blocking according to relaxed parameter passed Optional filter may discard unwanted matches.

Added in version 3.6.

Parameters:
Return type:

List[QgsPointLocator.Match]

verticesInRect(self, point: QgsPointXY, tolerance: float, filter: QgsPointLocator.MatchFilter | None = None, relaxed: bool = False) List[QgsPointLocator.Match][source]

Override of verticesInRect that construct rectangle from a center point and tolerance This method is either blocking or non blocking according to relaxed parameter passed

Added in version 3.6.

Parameters:
Return type:

List[QgsPointLocator.Match]

waitForIndexingFinished(self)[source]

If the point locator has been initialized relaxedly and is currently indexing, this methods waits for the indexing to be finished