The website uses cookies. By using this site, you agree to our use of cookies as described in the Privacy Policy.
I Agree
zhang
13 articles
My Web Markups - zhang
  • renderer = layer.renderer()
  • Single Symbol Renderer
  • vector layer is being rendered, the appearance of the data is given by renderer and symbols associated with the layer.
  • writer = QgsVectorFileWriter.create
  • Write to an ESRI Shapefile format dataset using UTF-8 text encoding
  • error = QgsVectorFileWriter.writeAsVectorFormatV2(layer, "testdata/my_new_file.gpkg", transform_context, save_options)
  • transform_context = QgsProject.instance().transformContext()
  • There are several ways to generate a vector layer dataset:
  • The QgsVectorLayerUtils class
  • create spatial index
  • Imagine, for instance, that you are writing an interpolation algorithm, and that for a given location you need to know the 10 closest points from a points layer
  • Spatial indexes
  • layer.updateFields()
  • undo functionality:
  • To find out whether a layer is in editing mode, use the isEditable() method
  • The first one will commit all your changes to the data source, while the second one will discard them and will not modify the data source at all.
  • commitChanges() or rollBack() methods.
  • To start the editing mode, use the startEditing() method.
  • the changes you make are not written until you commit them — they stay in layer’s in-memory editing buffer.
  • Favor QgsVectorLayerEditUtils class for geometry-only edits
  • change feature’s geometry or to change some attributes.
  • delete some features
  • initialize the feature passing a QgsFields object
  • Create some QgsFeature instances and pass a list of them to provider’s addFeatures() method. It will return two values: result (True or False) and list of added features
  • it might be necessary to force a redraw of the map canvas in order to see the changes you’ve done
  • In case you would like to do only temporary changes, skip to the next section that explains how to do modifications with editing buffer.
  • By using any of the following methods for vector layer editing, the changes are directly committed to the underlying data store
  • Most vector data providers support editing of layer data. Sometimes they support just a subset of possible editing actions
  • With setLimit() you can limit the number of requested features.
  • If you only need selected features,
  • selection = layer.selectedFeatures()
  • iterate over a given subset of features in a layer
  • attributes can be referred to by index. This is a bit faster than using the name.
  • clear the selection
  • Attributes can be referred to by their name
  • To add features to the selected features list for a given layer,
  • To change the selection color
  • select using an expression
  • To select all the features, the selectAll() method
  • In QGIS desktop, features can be selected in different ways: the user can click on a feature, draw a rectangle on the map canvas or use an expression filter.
  • retrieve every feature with its geometry and attributes
  • showing some information about each feature
  • vlayer.fields()
  • You can retrieve information about the fields associated with a vector layer by calling fields()
44 annotations