Skip to main content
33 votes
Accepted

Creating temporary layer out of selection in QGIS

I think there are at least two methods: Select your features and run the "Extract selected features" tool from the Processing Toolbox (Ctrl+Alt+T). As described in this answer to a related ...
Joseph's user avatar
  • 75.9k
20 votes
Accepted

Maximum value of field of vector layer using PyQGIS

It is not necessary to get a complete list of field values. In QgsVectorLayer exists 'maximumValue' method. So, this works well and it's shorter: layer = iface.activeLayer() idx = layer....
xunilk's user avatar
  • 30k
17 votes
Accepted

Deleting all features of Vector layer using PyQGIS

You could use the following code which is heavily based on the answer from this post: How to delete selected features in QGIS using Python layer = iface.activeLayer() with edit(layer): for ...
Joseph's user avatar
  • 75.9k
16 votes
Accepted

Generating invalid Vector layer in QGIS

Providing an invalid path makes it invalid: layer = QgsVectorLayer("zxcxzcxc/home/bera/GIS/Data/testdata/ak_riks.shp", "testlayer_shp", "ogr") Or a providerlib that ...
Bera's user avatar
  • 74.3k
14 votes

Reprojecting vector layer in QGIS

To reproject a vector layer (or a batch of layers) (i.e. recalculate the coordinates values for a new reference system) you must go to Menu > Processing > Toolbox (this will open a side window). ...
r01f 51m035's user avatar
13 votes

Clipping Error 1: Cutline polygon is invalid

The original shapefile has geometry issues. I've run the Check validity with GEOS option selected and the output was "no erros". But I ran it again with QGIS selected and it detected errors. The ...
jgrocha's user avatar
  • 5,405
12 votes
Accepted

Filtering layers by geometry type using PyQGIS

You can simply use the isSpatial() method of the QgsVectorLayer class, to filter geometry and geometryless layers. Additionally, you should not use numbers to do such comparisons. Recommendations ...
Germán Carrillo's user avatar
12 votes
Accepted

Creating grid with PyQGIS

You use wrong TYPE code. You have to use 2 for rectangular grid. 3 means diamond. params = {'TYPE':2, ...} # 2: rectangle For more details about the algorithm try import processing processing....
Kadir Şahbaz's user avatar
12 votes
Accepted

Adding different types of geometry to layer in QGIS

If you mean adding them to a QGIS layer, the answer is 'No'. It is not possible. This is the nature of the QGIS layer. But somehow (maybe by a script) you may export layers, which have different ...
Kadir Şahbaz's user avatar
11 votes

Maximum value of field of vector layer using PyQGIS

The accepted answer does not work in QGIS3. Now one has to do: fieldname='id' layer=iface.activeLayer() idx=layer.fields().indexFromName(fieldname) print(layer.maximumValue(idx)) (I am setting ...
MortenSickel's user avatar
  • 3,824
11 votes
Accepted

Tool for exporting vector layer in QGIS

You can use "Vector General -> Save Vector Features to File" tool. In the tool window, you can set the coordinate precision using COORDINATE_PRECISION=NUMBER beneath "GDAL layer ...
Kadir Şahbaz's user avatar
10 votes

Deleting selected features using PyQGIS

There is also a possibility of using the deleteSelectedFeatures() method from the QgsVectorLayer class for deleting selected features: # choose an active layer layer = iface.activeLayer() # make own ...
Taras's user avatar
  • 33.2k
10 votes

Deleting all features of Vector layer using PyQGIS

In QGIS 3 one can use the truncate() method of the QgsVectorDataProvider class. Removes all features from the layer. This requires either the FastTruncate or DeleteFeatures capability. Providers with ...
Peter Petrik's user avatar
  • 1,465
10 votes

Assign a specific color to vector layer based on RGB codes in attribute table

In the layer styles panel if you click on simple marker, there is a small box to the right of the fill color combo box - if you click on it you can select edit from the menu and enter the expression ...
Ian Turton's user avatar
  • 82k
10 votes
Accepted

Changing Opacity of Vector layer in Google Earth Engine

It works like this: var shown = true; // true or false, 1 or 0 var opacity = 0.5; // number [0-1] var nameLayer = 'map'; // string var visParams = {color: 'red'}; // dictionary: Map.addLayer(...
Kuik's user avatar
  • 10.1k
10 votes

Saving temporary layer to shapefile using PyQGIS

Use writeAsVectorFormatV2 method of QgsVectorFileWriter class. # Attach libraries from qgis.core import * # attach main QGIS library from qgis.utils import * # attach main python library import os # ...
Kadir Şahbaz's user avatar
10 votes

Generating invalid Vector layer in QGIS

Using QgsVectorLayer without parameters generates an invalid vector layer. layer = QgsVectorLayer() print(layer) # OUT: # <QgsVectorLayer: '' (Invalid)> QgsProject.instance().addMapLayer(layer)
Kadir Şahbaz's user avatar
10 votes
Accepted

QGIS make the appearance of selected feature the same as defined in symbology

You can create a virtual layer that will display the selected feature. This virtual layer should be located above the main layer. Add a virtual field to the point layer, of type integer size 1 and ...
JGH's user avatar
  • 42.2k
10 votes
Accepted

Replica of point layer data from attribute value in QGIS

The following script does the job: layer = QgsProject.instance().mapLayersByName('Layer_Name')[0] name_field = 'Name' # not mandatory count_field = 'Count' # numerical field, mandatory # make new ...
Kadir Şahbaz's user avatar
9 votes
Accepted

Saving layer as shapefile using PyQGIS

Looking at the documentation suggests that it is expecting these parameters: Parameters: layer – layer to write fileName – file name to write to fileEncoding – encoding to use ...
Ian Turton's user avatar
  • 82k
9 votes

Exporting several layers as GeoPackages at the same time in QGIS

Another solution is to use the "Package layers" tool as a Batch Process.
Taras's user avatar
  • 33.2k
9 votes

Joining dangling end in the vector layer using QGIS

You can use "Snap Vertices to Nearest Points by Condition" Processing tool from ProcessX PlugIn. It is made for this purpose. Before running it, just extract the vertices of your lines via &...
MrXsquared's user avatar
  • 34.5k
8 votes

Openlayers3 Add click/hover action on ol.layer.Vector

for the hover interaction you can use this code: hoverInteraction = new ol.interaction.Select({ condition: ol.events.condition.pointerMove, layers:[yourLayer] //Setting layers to be hovered }...
Hicham Zouarhi's user avatar
8 votes

Convert Lon Lat to XY in Openlayers 3

I was looking for this function too, and I have found it in some of the examples on their site. You can use ol.proj.fromLonLat([19.062072, 47.473478]). http://openlayers.org/en/v3.13.1/apidoc/ol....
Isti115's user avatar
  • 193
8 votes
Accepted

Adding field in vector layer from attribute table in QGIS

QGIS has built-in table join capabilities. If you want to add fields from a table (data.shp) into another layer (pop.shp), then go this way: Right click on pop.shp -> Properties -> Joins -> "+" ...
Christophe P.'s user avatar
8 votes
Accepted

Exporting Vector layer using PyQGIS

The official doc for QgsVectorFileWriter.FieldValueConverter tells you that it's an "Interface to convert raw field values to their user-friendly value" The interface has 3 methods, clone, ...
ThomasG77's user avatar
  • 31k
8 votes
Accepted

PyQGIS: setting layer styles with QML file from URL

Instead of loadNamedStyle use the method importNamedStyle. It can directly receive a XML document as argument instead of a file path. Creating an XML document is simply using QDomDocument::setContent()...
CodeBard's user avatar
  • 3,561
8 votes

Adjusting coordinates from whole vector layer in QGIS

Solution You are using the Swiss national grid which exists in two versions: The coordinates in your layer are in the "old" style: CH1903 / LV03, corresponding to EPSG:21781 ( fundamental ...
Babel's user avatar
  • 72.1k
7 votes

Enabling "Snapping" in QGIS while drawing polygons?

Note that in the latest QGIS release snapping options is no longer visible by default. To use it right-click settings to activate snapping options.
Maarten 's user avatar
7 votes

Creating new empty vector layer with PyQGIS

In QGIS 3 one need to do: from qgis.core import QgsVectorLayer, QgsFeature, QgsField, QgsGeometry, QgsPointXY, QgsField, QgsProject vl = QgsVectorLayer("Point", "temporary_points",...
MortenSickel's user avatar
  • 3,824

Only top scored, non community-wiki answers of a minimum length are eligible