26 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() ....
Kadir Şahbaz's user avatar
26 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 = ...
Rodrigo E. Principe's user avatar
14 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 ...
BERA's user avatar
  • 71.5k
11 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 ...
Igor Franzoni Okuyama's 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 [...
J. Monticolo's user avatar
  • 15.7k
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 ...
crmackey's user avatar
  • 8,484
7 votes

Loop through fields in QGIS calculator with condition: count fields with values >0

You can use the expression: array_length(array:= array_filter(array:= array_foreach(array:=generate_series( 1,10,1), expression:=attributes()[concat('Species', @element)]), expression:=@element > 0)...
BERA's user avatar
  • 71.5k
6 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\...
BERA's user avatar
  • 71.5k
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 ...
Rob Marty's user avatar
  • 369
6 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(): # ...
Germán Carrillo's user avatar
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'...
Dan C's user avatar
  • 12.2k
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 ...
xunilk's user avatar
  • 29.9k
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 ...
Erik's user avatar
  • 16.1k
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 ...
Kevin Reid's user avatar
  • 10.3k
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 ...
radouxju's user avatar
  • 49.6k
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(): ...
Kadir Şahbaz's 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 ...
M Bain's user avatar
  • 1,997
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 ...
BERA's user avatar
  • 71.5k
6 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 (...
Kadir Şahbaz's user avatar
6 votes
Accepted

Create series of heatmaps for each year in QGIS

You can automate the task: Create a model for your workflow (find the model I created for this task here to download) Run your model in batch mode, running it once for each year you wish. Create ...
Babel's user avatar
  • 70.6k
6 votes
Accepted

Choose all the files of a folder as an input to run a process in the model designer in QGIS

You can execute the model as a batch process. Press Run model and: I select all files with 72 in the name in this folder: The tricky part is to name the outputs. This expression replaces the input ...
BERA's user avatar
  • 71.5k
5 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(), ...
mgri's user avatar
  • 16.1k
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 = ...
Francisco Puga's 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 ...
BERA's user avatar
  • 71.5k
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 ...
J. Monticolo's user avatar
  • 15.7k
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 ...
fatih_dur's user avatar
  • 4,983
5 votes

Collect results of iterative processes in QGIS graphical modeler

I faced the same problem with the modeller in QGIS and find some workaround but no solution inside the modeller. The only way to get an iterative algorithm is to use Vector Features like you do. Then ...
Corentin Lemaitre's user avatar
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()....
Kadir Şahbaz's user avatar
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, ...
Midavalo's user avatar
  • 29.7k
4 votes

ogr2ogr apply sqlite dialect to multiple feature classes within a fgdb

You can load your filegdb to python and then you can do something like this import sys for g in inputGDB: sys.call(Your shell command goes here) By using sys.call() can execute your shell ...
Asad Abbas's user avatar
  • 1,634

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