Skip to main content
16 votes
Accepted

Masking NetCDF time series data from shapefile using Python

Here is an example of using rioxarray to mask out data with a shapefile: https://corteva.github.io/rioxarray/stable/examples/clip_geom.html import geopandas import rioxarray import xarray from ...
snowman2's user avatar
  • 7,527
10 votes
Accepted

Creating multiple buffers in a loop in GEE

You don't need the loop. You can generate a distance list and use map function on it. var point = ee.Geometry.Point([-46.633286, -23.550510]); // generate a buffer distance list var distance_list = ...
Kadir Şahbaz's user avatar
10 votes
Accepted

QGIS Python loop integer with select by expression

You can use the following script structure: layer = iface.activeLayer() end_number = 5 # OS1, OS2, OS3, OS4, OS5 attribute_name = "attribute_name" # field name containing OS values for i ...
Kadir Şahbaz's user avatar
9 votes
Accepted

Iterate through list of field names and calculate fields with PyQGIS?

The QgsFeatureIterator is closed after returning all features. Example: >>layer = iface.activeLayer() >>feats = layer.getFeatures() >>[f['GRID_ID'] for f in feats] ['AP7', 'AP8', '...
Bera's user avatar
  • 77.7k
8 votes
Accepted

Looping if condition in ArcPy?

Try using an UpdateCursor: import arcpy stands = r'D:\mountaine\database.mdb\testPoly' arcpy.MakeFeatureLayer_management(stands,'pormap') with arcpy.da.UpdateCursor("pormap", [ 'TEXTSTR', 'NUMBR', '...
Richard Morgan's user avatar
8 votes
Accepted

Looping with a range of integers for 'numeric' inputs in QGIS Python Console

The problem is dys/dayss is datatype np.int64 when it should be int: import numpy as np dayss = np.arange(1, 367, 1) print(type(dayss[0])) <class 'numpy.int64'> Try int(dys) in your paramms ...
Bera's user avatar
  • 77.7k
8 votes

Looping with a range of integers for 'numeric' inputs in QGIS Python Console

Since you populate dayss list using NumPy arange method, type of items in the list is not int, instead, it is <class 'numpy.int32'>. Just add dys = int(dys) to for loop. for dys in dayss: ...
Kadir Şahbaz's user avatar
7 votes
Accepted

Using PostGIS to find the overall difference between two (large) polygon datasets

Expanding on this answer, we can build a query that will process each geometry of table 1, then, for each of them (via the lateral join), union all intersecting geometries from the 2nd layer and ...
JGH's user avatar
  • 43.1k
7 votes

Looping every features pair with PyQGIS

You can use zip and indexing: layer = iface.activeLayer() featurelist = [f for f in layer.getFeatures()] #List all features for featurepair in zip(featurelist[::2], featurelist[1::2]): f1, f2 = ...
Bera's user avatar
  • 77.7k
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
  • 77.7k
6 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.3k
5 votes
Accepted

Iterate through shapefile and output an image for each feature using sf in R

The steps to perform this task using sf would be: library('sf') # Load sf package library('dplyr') # Load dplyr package # Load 2016 Census shape file for Canada (Step 1) can_cd_2016 <- st_read('...
Werner's user avatar
  • 221
5 votes
Accepted

Handling multiple-extent-problem to create raster stack in R?

You can manage multi-extent-problem resampling your data before mask() function. This work for aligned and non-aligned pixels (for non-aligned, choose wisely method argument). Also, you can use ...
aldo_tapia's user avatar
  • 13.8k
5 votes
Accepted

Checking whether file already exists using while loop with ArcPy?

Use arcpy.Exists() if arcpy.Exists(r"MyFeatureClass"): print "Feature Class Exists" else: print "Feature Class Doesn't Exist" Additionally, look into arcpy.CreateUniqueName() to generate a ...
Midavalo's user avatar
  • 29.9k
5 votes
Accepted

How to create a list of spatial objects and merge them in R

Are they multiple layers of a single (3D) raster? brick(filez) Are they disparate windows from a single layer parent (2D) raster? Reduce(merge, lapply(filez, raster)) Are they disparate 2D ...
mdsumner's user avatar
  • 8,246
5 votes

Looping several raster files in R

300 rasters, huh? Sounds like a problem for parallel! In R, it is more common to use apply functions than your typical for loop. Using apply functions you can run processes in parallel pretty easily ...
JMT2080AD's user avatar
  • 655
5 votes
Accepted

Raster calculation loop - R

Just use a for loop with a little trick to iterate. First, as always is recommended, a reproducible example: library(raster) data('lsat',package='RStoolbox') names(lsat)[1:4] <- c('blue','green',...
aldo_tapia's user avatar
  • 13.8k
5 votes

Looping if condition in ArcPy?

I think you have a misunderstanding of cursors and calculate field, these methods are mutually exclusive (if you use one don't use the other). The answer by Richard Morgan is correct, however there ...
Michael Stimson's user avatar
5 votes
Accepted

ReduceRegion Loop GEE

You will have to map over the image collection and apply reduceRegion on each image: var imageCollection = byMonthYearDoce.map(function(image){ var dictionary = image.reduceRegion({ reducer: ee....
Kuik's user avatar
  • 10.1k
5 votes
Accepted

Remove Images from Image Collection in Google Earth Engine?

In almost all cases, you are better off using filters. My understanding is that it's significantly more efficient. But if you really need to, you can filter out an element in map() by returning null ...
Daniel Wiell's user avatar
  • 14.8k
5 votes
Accepted

Printing zip lists whose data comes from input user in a loop

You assign zip(lats,lons) to pair_coordinates in every loop. Therefore, when you exit out of the loop, pair_coordinates and pair_coordinates_list always contain the coordinates of the last region you ...
Kadir Şahbaz's user avatar
5 votes
Accepted

Comparing polygons from zip lists inside a loop, whose data comes from user in Python

When assigning a value to poly, use this: poly[regions - 1] = geometry.Polygon(.... Result of print(poly[dividend].intersection(poly[divisor])):
Kadir Şahbaz's user avatar
5 votes
Accepted

How to subset a raster stack by year with a loop in R?

I'd like to show you an alternative approach using terra instead of raster since this a little bit more elegant from my point of view: # list files available, I created 12 files beforehand files <- ...
dimfalk's user avatar
  • 1,419
5 votes

Looping area calculations for multiple rasters in R

You can do this by reading the rasters into a list object then iterating through the resulting list, defining a function that operates on each raster. This creates the list object containing rasters. ...
Jeffrey Evans's user avatar
5 votes

Looping every features pair with PyQGIS

In most latest QGIS versions like QGIS 3.34.1 Prizren​, it should come with the pairwise() function from the itertool module. Since Python 3.10. # imports from itertools import pairwise from qgis.core ...
Taras's user avatar
  • 33.9k
4 votes

I want to do SpatialLines programatically

I am sure that one could write a function that would do this using lapply but I am not sure that it is worth the trouble. In this case, I am not sure that an apply like approach would buy you much. ...
Jeffrey Evans's user avatar
4 votes

Looping through 16 million records using ArcPy?

You could write the Objectid and result of the calculation (cate_2) to a csv file. Then join the csv to your original file, populate a field, to preserve the result. This way you aren't updating the ...
klewis's user avatar
  • 7,495
4 votes

Create loop in ModelBuilder in ArcGIS?

You can use the Iterate Feature Classes tool within ModelBuilder. This will iterate through your feature classes as long as they are all within the same workspace. It is a ModelBuilder only tool ...
Stephen Ruhl's user avatar
4 votes

How to create a list of spatial objects and merge them in R

I think your problem is because your list contains only the names of the rasters and not the rasters themselves. First get the rasters and then you can apply do.call: namez1.get <- lapply(namez1, ...
Sébastien Rochette's user avatar
4 votes
Accepted

Looping for NDVI in different file paths using R?

First, your formula is not right. You can't use (x-y/x+y)... Is the same than x - (y/x) + y. So correct this first. I'll create some example data: library(raster) r <- raster() userpath <- '/...
aldo_tapia's user avatar
  • 13.8k

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