Vector layer
Import
from pyqgis_wrapper.layer import VectorLayerBuilder, VectorLayerManager
VectorLayerBuilder()
¶
Bases: pyqgis_wrapper.layer.base.LayerBuilder[qgis.core.QgsVectorLayer]
Class that handle layer creation. Either form an existing layer with various degree of copying (fields, feature) or from scratch
It can be written to a file or stay in memory
Source code in pyqgis_wrapper/layer/vector_layer.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
get_geometry_type
property
¶
Get the geometry type
Returns:
| Type | Description |
|---|---|
QgsWkbTypes
|
Type of geometry used for the layer creation |
get_fields
property
¶
Fields getter. Allow to add the fields to QgsFeature that'll be added to the created layer
Returns:
| Type | Description |
|---|---|
QgsFields
|
Fields of the created layer |
set_geometry(geom_type)
¶
Set the geometry type for the output layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
geom_type
|
typing.Union[str, int]
|
Accepts (case don't matter): 'Point', 'MultiPoint', 'LineString', 'MultiLineString', 'Polygon', 'MultiPolygon' Accepts following enum/WkbType : 1: QgsWkbTypes.Point, 2: QgsWkbTypes.LineString, 3: QgsWkbTypes.Polygon, 4: QgsWkbTypes.MultiPoint, 5: QgsWkbTypes.MultiLineString, 6: QgsWkbTypes.MultiPolygon, |
required |
Returns:
| Type | Description |
|---|---|
VectorLayerBuilder
|
self |
Source code in pyqgis_wrapper/layer/vector_layer.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
set_fields(fields)
¶
Set fields for the output layer
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fields
|
qgis.core.QgsFields
|
Fields to be set |
required |
Returns:
| Type | Description |
|---|---|
VectorLayerBuilder
|
self |
Source code in pyqgis_wrapper/layer/vector_layer.py
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | |
set_request(request)
¶
Spatial or attribute request to create a subset of feature when calling from_layer with copy_feat = True
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
qgis.core.QgsFeatureRequest
|
description |
required |
Returns:
| Type | Description |
|---|---|
VectorLayerBuilder
|
self |
Source code in pyqgis_wrapper/layer/vector_layer.py
224 225 226 227 228 229 230 231 232 233 234 235 236 | |
from_layer(layer, copy_feat=True, copy_fields=True, request=None)
¶
Duplicate a layer at various degree If you want more flexibility in layer creation you should call configure instead
Once you set the wanted parameters you can call build method to create the layer
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layer
|
qgis.core.QgsVectorLayer
|
Layer to duplicate |
required |
copy_feat
|
bool
|
Copy feature in the new layer, defaults to True |
True
|
copy_fields
|
bool
|
Copy fields in the new layer, defaults to True
You can get the QgsFields object with the |
True
|
request
|
typing.Optional[qgis.core.QgsFeatureRequest]
|
Spatial or attributary request to select a subset of features to copy, defaults to None |
None
|
Returns:
| Type | Description |
|---|---|
VectorLayerBuilder
|
self |
Source code in pyqgis_wrapper/layer/vector_layer.py
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | |
configure(crs, geometry, fields=None)
¶
Configure layer creation from scratch
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
crs
|
typing.Union[str, int, qgis.core.QgsCoordinateReferenceSystem]
|
Crs of the layer to create |
required |
geometry
|
str
|
Geometry type of the layer to create Accepts (case don't matter): 'Point', 'MultiPoint', 'LineString', 'MultiLineString', 'Polygon', 'MultiPolygon' |
required |
fields
|
typing.Optional[qgis.core.QgsFields]
|
Fields to add to the new layer, defaults to None |
None
|
Returns:
| Type | Description |
|---|---|
VectorLayerBuilder
|
self |
Source code in pyqgis_wrapper/layer/vector_layer.py
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 | |
build()
¶
Build a layer form the parameters set by from_layer or configure method. If a path was provided the new layer will be saved on a file
Returns:
| Type | Description |
|---|---|
VectorLayerBuilder
|
self |
Source code in pyqgis_wrapper/layer/vector_layer.py
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 | |
save(path, layer_name=None, extension='gpkg', provider='ogr', overwrite=False, append_layer=False)
¶
Writes layer to a file
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
typing.Union[str, pathlib.Path]
|
Output path to write to |
required |
layer_name
|
typing.Optional[str]
|
Name of the layer. If None it'll be infered by the file name. If append_layer is True and layer name already exists or is None then a random one will be created, defaults to None The layer name is ignored if the extension does not support multi layer |
None
|
extension
|
str
|
The file extension to use when saving the layer, defaults to "gpkg". If the path includes an extension, it will take precedence over this parameter. Accepted extensions : 'gpkg', 'shp', '000', 'csv', 'dgn', 'dxf', 'fgb', 'gdb', 'geojson', 'geojsonl', 'geojsons', 'gml', 'gpx', 'gxt', 'ili', 'itf', 'json', 'kml', 'ods', 'sql', 'sqlite', 'tab', 'txt', 'xlsx', 'xml', 'xtf' |
'gpkg'
|
provider
|
str
|
Provider used to load the QgsVectorLayer. Defaults to 'ogr'.
Most extension is handled by the default provider. However, if the
specified provider does not match the file format (e.g., using "ogr" for a format
that requires a different provider), the resulting layer will be invalid and an
error will be raised when calling the |
'ogr'
|
overwrite
|
bool
|
If True will erase existing file or layer, defaults to False |
False
|
append_layer
|
bool
|
If True will append the layer to an existing file if the format allows it, defaults to False |
False
|
Returns:
| Type | Description |
|---|---|
VectorLayerBuilder
|
self |
Source code in pyqgis_wrapper/layer/vector_layer.py
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 | |
VectorLayerManager(edit_session=False)
¶
Handles
- adding and removing fields in a QgsVectorLayer,
- modifying attributes
- adding or removing feature
Support working directly on the dataprovider or with the edit session. Does not own the logic, instead delegate to lower level manager. Please note that working directly on the provider provide less safeguarding and do not keep track of previous changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edit_session
|
bool
|
Use edit session from QGIS or work directly on the
file, defaults to False. Can be changed by calling |
False
|
Source code in pyqgis_wrapper/layer/vector_layer.py
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 | |
edit_session
property
writable
¶
Getter for edit_session
Returns:
| Type | Description |
|---|---|
bool
|
edit_session value |
VectorLayerBaseManager(edit_session=False)
¶
Bases: abc.ABC
Base handler for QgsVectorLayer
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edit_session
|
bool
|
Use edit session from QGIS or work directly on the
file, defaults to False. Can be changed by calling |
False
|
Source code in pyqgis_wrapper/layer/vector_layer.py
583 584 585 586 587 588 589 590 | |
edit_session
property
writable
¶
Getter for edit_session
Returns:
| Type | Description |
|---|---|
bool
|
edit_session value |
exit_edit_mode(layer, strategy=EditStrategy.ROLLBACK)
¶
Attempts to exit edit mode on the given layer using the provided strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layer
|
qgis.core.QgsVectorLayer
|
Layer to check |
required |
strategy
|
pyqgis_wrapper.layer.vector_layer.EditStrategy
|
Either ROLLBACK or COMMIT |
pyqgis_wrapper.layer.vector_layer.EditStrategy.ROLLBACK
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the layer edit state has changed |
Source code in pyqgis_wrapper/layer/vector_layer.py
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 | |
VectorLayerFieldManager(edit_session=False)
¶
Bases: pyqgis_wrapper.layer.vector_layer.VectorLayerBaseManager
Handles adding and removing fields in a QgsVectorLayer, Support working directly on the dataprovider or with the edit session.
Source code in pyqgis_wrapper/layer/vector_layer.py
583 584 585 586 587 588 589 590 | |
add(layer, field_input)
¶
Add a QgsField or QgsFields to the given QgsVectorLayer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layer
|
qgis.core.QgsVectorLayer
|
The layer to add the field(s) to |
required |
field_or_fields
|
typing.Union[qgis.core.QgsField, qgis.core.QgsFields]
|
A single QgsField or multiple QgsFields |
required |
Returns:
| Type | Description |
|---|---|
Union[int, Dict[str, int]]
|
Index of the added field, or dict of {field names: indexes} |
Source code in pyqgis_wrapper/layer/vector_layer.py
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 | |
delete(layer, field_names)
¶
Delete one or more fields from the given QgsVectorLayer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layer
|
qgis.core.QgsVectorLayer
|
The layer to delete the field(s) from |
required |
field_names
|
typing.Union[str, typing.List[str]]
|
Name of the field or list of field names to delete |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the field(s) were successfully deleted |
Source code in pyqgis_wrapper/layer/vector_layer.py
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 | |
VectorLayerFeatureManager(edit_session=False)
¶
Bases: pyqgis_wrapper.layer.vector_layer.VectorLayerBaseManager
Handles adding and removing feature in a QgsVectorLayer, Support working directly on the dataprovider or with the edit session.
Source code in pyqgis_wrapper/layer/vector_layer.py
583 584 585 586 587 588 589 590 | |
add(layer, feature_input)
¶
Add a feature or an iterable of feature to the layer
Example : - Deleted feature n°1. - Added feature with a setId(1) -> Internal id is feature n°2
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layer
|
qgis.core.QgsVectorLayer
|
Layer to add the feature to |
required |
feature_input
|
typing.Union[qgis.core.QgsFeature, typing.Iterable[qgis.core.QgsFeature]]
|
Feature(s) that will be added |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if successfully added |
Source code in pyqgis_wrapper/layer/vector_layer.py
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 | |
delete(layer, fid_input)
¶
Delete a feature or a list of feature by their ids.
Please be aware that if you delete a feature the next feature that'll be added will have an internal id incremented by one.
Example : - Deleted feature n°1. - Added feature with a setId(1) -> Internal id is feature n°2
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layer
|
qgis.core.QgsVectorLayer
|
Layer to delete the features from |
required |
fid_input
|
typing.Union[int, typing.List[int]]
|
Feature id or a list of features id to be deleted from the layer |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if successfully deleted |
Source code in pyqgis_wrapper/layer/vector_layer.py
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 | |
update(layer, feature_input)
¶
Update a feature or an iterable of features in a layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layer
|
qgis.core.QgsVectorLayer
|
Layer to update the feature(s) from. |
required |
feature_input
|
typing.Union[qgis.core.QgsFeature, typing.Iterable[qgis.core.QgsFeature]]
|
Feature or iterable of features with matching feature.id() to update |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if successfully updated. |
Source code in pyqgis_wrapper/layer/vector_layer.py
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 | |
VectorLayerAttributeManager(edit_session=False)
¶
Bases: pyqgis_wrapper.layer.vector_layer.VectorLayerBaseManager
Handles adding and removing attribute for specific feature in a QgsVectorLayer, Support working directly on the dataprovider or with the edit session.
Source code in pyqgis_wrapper/layer/vector_layer.py
583 584 585 586 587 588 589 590 | |
update(layer, attr_map)
¶
Update attributes from feature(s).
In edit session QGIS will cast str, float and bool to int if able
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layer
|
qgis.core.QgsVectorLayer
|
Layer containing the features whose attributes will be updated |
required |
attr_map
|
typing.Dict[int, typing.Dict[int, typing.Any]]
|
Dictionary in the form {fid: {field_idx: new_value, ...}, ...} |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if successfully updated |
Source code in pyqgis_wrapper/layer/vector_layer.py
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 | |
VectorLayerGeometryManager(edit_session=False)
¶
Bases: pyqgis_wrapper.layer.vector_layer.VectorLayerBaseManager
Handles adding and removing geometry for specific feature in a QgsVectorLayer, Support working directly on the dataprovider or with the edit session.
Source code in pyqgis_wrapper/layer/vector_layer.py
583 584 585 586 587 588 589 590 | |
update(layer, geom_map)
¶
Update geometries from feature(s)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layer
|
qgis.core.QgsVectorLayer
|
Layer containing the features whose geometries will be updated |
required |
attr_map
|
typing.Dict[int, qgis.core.QgsGeometry]
|
Dictionary in the form {fid: QgsGeometry, ...} |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if successfully updated |
Source code in pyqgis_wrapper/layer/vector_layer.py
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 | |
EditStrategy
¶
Bases: str, enum.Enum
Class to store edit strategy and eventually apply it.