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 ...
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 = ...
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 ...
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', '...
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', '...
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 ...
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:
...
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 ...
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 = ...
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)...
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(), ...
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('...
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 ...
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 ...
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 ...
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 ...
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',...
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 ...
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....
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 ...
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 ...
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])):
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 <- ...
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.
...
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 ...
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.
...
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 ...
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 ...
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, ...
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 <- '/...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
loop × 379arcpy × 145
r × 66
raster × 55
python × 45
arcgis-desktop × 41
google-earth-engine × 31
pyqgis × 30
cursor × 29
qgis × 22
modelbuilder × 19
iteration × 19
iterator × 18
shapefile × 11
for-loop × 11
field-calculator × 10
spatial-analyst × 10
list × 10
arcmap × 9
arcgis-10.2 × 9
google-earth-engine-javascript-api × 9
function × 9
grass-gis × 9
arcgis-10.0 × 8
ndvi × 8