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,459
10 votes
Accepted

Invalid Input Data Error 000368 Running ArcPy tools in for loop

SelectLayerByLocation requires a Layer to select features, you can't select features directly on a feature class, which is what your script is trying to do. The reason it worked in ArcMap is because ...
Midavalo's user avatar
  • 29.8k
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
9 votes
Accepted

Counting in for-loop using ArcPy Cursor?

In theory (because I don't know arcpy), simply use the standard function enumerate (GeoNet: Enumeration of a cursor) for i, row in enumerate(cur): row[0] = i cur.updateRow(row)
gene's user avatar
  • 55.1k
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
  • 74.8k
9 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
8 votes

Counting in for-loop using ArcPy Cursor?

The easiest option would be to reference the OID in the attributes using the OID@ token in a SearchCursor. import arcpy shp = r'X:\path\to\your\shapefile.shp' with arcpy.da.SearchCursor(shp, ["OID@"...
Aaron's user avatar
  • 51.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
  • 74.8k
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

Update Cursor: Unable to populate specific field

You have misunderstood how the updateRow method works. It is a method of the cursor as a whole and takes a row object as its argument: fromUpdateCursor_4Depth.updateRow(updateFromRow) You have to ...
Richard Fairhurst'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
  • 42.3k
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
  • 74.8k
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
  • 74.8k
5 votes
Accepted

Designing Update Cursor that updates rows found between two values?

This code should do the work you need to get done. Make sure the table is sorted as you expect. There is a GP tool Sort that can do that for you. import arcpy fc = r'C:\ArcGIS\Default.gdb\wells' ...
Alex Tereshenkov's user avatar
5 votes
Accepted

error trying to create two variables Cannot acquire a lock

You're tripping over your own feet here.. All cursors need to be removed or they will lock the data; until you free the cursor you could still go back to it at any stage. In your code you're using a ...
Michael Stimson's user avatar
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.7k
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

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.8k
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.3k
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,206
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.7k
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.4k
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,417
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

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