22 votes

Iterating over map layers using PyQGIS

For QGIS 3, QgsMapLayerRegistry's functionality has been moved to QgsProject. So, for iterating over map layers, you should use that structure: layers = QgsProject.instance().mapLayers() ....
user avatar
21 votes
Accepted

Mosaicking Image Collection by Date (day) in Google Earth Engine

var poly = /* color: #d63000 */ee.Geometry.Polygon( [[[-76.0803, 10.8656], [-76.0913, 7.7436], [-73.1909, 7.7545], [-73.3776, 9.4273], [-75.2124, 10.9304]]]) var start = ...
user avatar
16 votes

Iterating over map layers using PyQGIS

For QGIS 2.6 here is the code to identify each layer and group them: #make the desired groups for layers toc = self.iface.legendInterface() root = QgsProject.instance().layerTreeRoot() group1 = root....
user avatar
13 votes
Accepted

Iterating features and buffering using GeoPandas?

Look at the answer to Updating value in iterrow for pandas: The rows you get back from iterrows are copies that are no longer connected to the original data frame, so edits don't change your ...
user avatar
  • 54k
9 votes

Mosaicking Image Collection by Date (day) in Google Earth Engine

Here is another solution that does not involve iterating through days, but maps through unique dates (after removing seconds, microseconds...) that appear in the image collection. It also adds the ...
user avatar
8 votes
Accepted

Iterating through fields and generating formatted string output

Is this code faster (tested on a 300 features memory layer) : layer = iface.activeLayer() # Get name and type of attributes in layer intfieldsnames = [i.name() for i in layer.fields() if i.type() in [...
user avatar
7 votes

Repeating steps based on different field using ModelBuilder?

There is not an Iterate Fields tool in ModelBuilder. I can think of two possible workarounds: Modify the model to run as a Python script. Define a list of the fields you want to use, and define a ...
user avatar
  • 8,926
7 votes
Accepted

Any way to clear selections from features in a map only if they have selected features?

Yes, each Layer has a method called getSelectionSet(), which returns a list of selected OIDs for the layer. >>> for lyr in arcpy.mapping.ListLayers(mxd): ... if lyr.isFeatureLayer and ...
user avatar
  • 8,384
6 votes
Accepted

repeating steps in GRASS r.mapcalc

You can call r.mapcalc in for loop. In shell it could look like this: X=5 # initialize first map r.mapcalc "old = 10" # loop for I in `seq $X` do r.mapcalc "new = old + 10" --overwrite g.rename ...
user avatar
  • 731
6 votes

Automate & set layers for PDF maps

You are describing the exact functionality of Data-Driven Pages (DDP), a built-in feature of ArcGIS. You can implement them either in a Python script or in ArcMap. If you've never used them before, I'...
user avatar
  • 12k
6 votes
Accepted

Reading value of point (x, y) in raster and changing its value to another raster using PyQGIS?

To do that is preferable to use a QgsRasterBlock object to get raster values and python GDAL module to write resulting raster values in a new raster. In this case you only need raster1. Complete code ...
user avatar
  • 26.9k
6 votes
Accepted

Iterate over ImageCollection returning pandas dataframe using earth engine & python

Found a solution, by making a list of the scene IDs from the imageCollection and iterating over the list. Then in a loop I import the individual images instead of mapping/iterating over the ...
user avatar
  • 367
6 votes
Accepted

Iterating clip over multiple layers using QGIS?

You can use the Graphical Modeller (Processing menu) to build the desired process. Add as many layers as you like, add your grid, then switch tabs and add a clip-process for each vector-layer and set ...
user avatar
  • 13.2k
6 votes
Accepted

Returning image for each iteration using Google Earth Engine?

Furthermore it appears only 3 items are stored in the list which makes me assume the iteration is more complex than a literal iteration where all images are calculated one by one? You've got some of ...
user avatar
  • 9,046
6 votes
Accepted

Problems with for iteration in arcpy

In your script, calculate field will compute the values of group for ALL the row (or the selected rows if you used a layer) at each iteration. Therefore the GROUP values for all rows are overwritten ...
user avatar
  • 48.7k
6 votes

Iterate through a list of polygons' centroids in QGIS

In your code, polycent is always set the last polygon's centroid. So you must add centroids to a list. Change your code into that: polycents = [] # empty list for feature in layer.getFeatures(): ...
user avatar
6 votes

Using RECURSIVE in Virtual Layer

I don't think you need recursive, it looks like you want subtotals for different categories of "DIST_KM" Does this give something close to want you expect: SELECT COUNT(*) as anzahl, SUM(FLUX) AS ...
user avatar
  • 1,732
6 votes

Iterating through fields and generating formatted string output

You can also use Refactor fields. Takes ~15 s for a feature class with 44.000 features with 15 integer fields: lyr = QgsProject.instance().mapLayersByName('sksNaturvardsavtal')[0] #List all fields ...
user avatar
  • 54k
5 votes

Iterate through features to use selection as input for ExtractByMask

The SearchCursor will return geometries with the SHAPE@ token which can be used as extracting features etc.: SHAPE@ —A geometry object for the feature. import arcpy feature_class = r'C:\test.gdb\...
user avatar
  • 54k
5 votes

Iterating over features in vector layer

Check that the layer is valid. If the layer, or the path is not valid qgis will no raise an error, it will return a QgsVectorLayer object where you can call the methods but with mostly no-op. layer = ...
user avatar
5 votes

Iterating raster pairs using ArcPy?

To get started with a Python script you can try code below. ListRasters will sort your rasters by name so if they are named like you say, it should work. Otherwise there are other ways of sorting the ...
user avatar
  • 54k
5 votes
Accepted

Using RECURSIVE in Virtual Layer

With RECURSIVE query, you have to do a generate_series (PostgreSQL function not supported by SQLite), which create you a number series from conf.start to conf.stop by conf.step. Then, retrieve this ...
user avatar
5 votes
Accepted

arcpy.SelectByAttribute 'Parameters are not valid' error

As the exception states, SelectLayerByAttribute_management expects a layer (which is not the same thing as feature class -fc variable in your case, that you are passing, which is just the data) and ...
user avatar
  • 4,828
5 votes
Accepted

Shortest path (point to point) in iteration, safe for the case there is no route

Try something like that: n=0 for f1 in layer1: p1 = f1.geometry().asPoint() x1, y1 = p1.x(), p1.y() coord1= f"{x1},{y1}" for f2 in layer2: p2 = f2.geometry()....
user avatar
5 votes

Iterate over features in a model

I don't know if it is possible using actual options in Modeler or Processing window, but you can make a script doing that. from qgis.PyQt.QtCore import QCoreApplication from qgis.core import (...
user avatar
4 votes

Iterating over map layers using PyQGIS

Visible layers You can iterate visible layers in this way: # Iterate visible layers (warning: only spatial ones) def iterate_visible_layers(): for layer in iface.mapCanvas().layers(): # ...
user avatar
4 votes
Accepted

Obtaining feature extent while iterating using PyQGIS?

Assuming that layer is your polygon layer, you may use this code: for feature in layer.getFeatures(): bbox = feature.geometry().boundingBox() bbox_extent = '%f,%f,%f,%f' % (bbox.xMinimum(), ...
user avatar
  • 15.6k
4 votes
Accepted

Looping and Adding All Feature Classes From File Geodatabase Feature Dataset into Current Map

Take a look at arcpy.ListFeatureClasses() and how that works in a `for loop. See the Code Sample at the bottom of that page. Then as you loop through the feature classes you just use your same code, ...
user avatar
  • 29.2k
4 votes

Problems with for iteration in arcpy

The problem is in arcpy.CalculateField_management. It overwrite ALL of the field rows when you run it. Using selection will solve this problem but it may cause perfomance problems. To avoid them use ...
user avatar
  • 5,914
4 votes

Problems with for iteration in arcpy

As Mr Che and radouxju have said calculate field does all the rows, and that you should be using an update cursor but I would like to add that you're using an older style of cursor.. the arcpy.da ...
user avatar

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