Hot answers tagged

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,191
12 votes
Accepted

Simplify repetitive tasks in QGIS Graphical Modeler

The way I would set your task up would be to create a custom script which provides greater flexibility than the modeler but can still provide a similar interface to its users. You can create one from: ...
Joseph's user avatar
  • 75.2k
10 votes
Accepted

GDAL utility in Bash for loop (Ubuntu)

Linux (ubuntu as you call it) uses a shell (probably bash in this case) which has a different syntax to windows so you want something like: for i in se70*.tif do gdalinfo $i done
Ian Turton's user avatar
  • 80.4k
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.6k
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

Looping through raster layers in folder with PyQGIS?

There's a couple of things to notice: In your algorithm, you are using rstr (the path of the rasters) as the input instead of the actual rasters which you have defined as lyr. This probably depends ...
Joseph's user avatar
  • 75.2k
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
  • 54.4k
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
  • 68.7k
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.5k
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
  • 68.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

Using Loop for calculating NDVI in R?

There's no need for a for (or any other kind of) loop since such stuff is entirely included in the raster package. If you want to calculate the NDVI, then basically all you need is ## required ...
fdetsch's user avatar
  • 5,183
7 votes
Accepted

Update Cursor for automation of tedious data entry which keeps crashing ArcMap

Never, never, never use embedded cursor loops. Never! It is bad for performance, memory, data safety, etc. It is very, very bad. See my blog on Turbo Charging Data Manipulation with Python Cursors ...
Richard Fairhurst'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
  • 40.7k
5 votes
Accepted

Problems with nested loops - How to start from 0?

This is because you create a selection on your gridDivision layer. The cursor then only iterates through selected features. Clear your selection after your cursor. # Go through all lines feature ...
Emil Brundage's user avatar
5 votes
Accepted

Not getting correct result from for loop

You said that the datatype of the DB column is a number, but in your if and elif statements you compare that number to a string (e.g. 4 == '4') which will return False. Maybe you get the desired ...
TobsenB's user avatar
  • 902
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

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

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.2k
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.6k
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
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,156
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.2k
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
  • 9,963

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