[Qgis-us-user] raster calculator batch processing

Dani Varghese danivarghes at gmail.com
Wed Apr 1 01:05:58 PDT 2020


Dear Paulo



Thank you, I really appreciate it


Best
Dani Varghese



On Thu, Mar 26, 2020 at 8:36 PM Paulo Raposo <pauloj.raposo at gmail.com>
wrote:

> Hi again Dani,
>
> The following hopefully gives you an impression of how you can achieve
> this with a Python script. This is a simple case of looping (aka recursion)
> and doing something once for each input file. I haven't tested this, and
> you'd have to modify various bits of it to suit your case, but hopefully it
> gives you a logical impression of what you can do to get your job done.
>
> Cheers,
> P
>
>
>
>
> import os, subprocess
>
> def defineClippedFilename(aFileName):
>     # A function for getting output filenames.
>     # Given a file name, return a new name where '_clipped'
>     # has been appended, with the file extension remaining
>     # the same.
>     filename, extension = os.path.splitext(aFileName)
>     newFileName = filename + "_clipped" + extension
>     return newFileName
>
>
> # The filesystem path to the file you want to use as a 'cookie cutter'.
> pathToMaskFile = "/home/myhome/somefolder/file.shp"
>
> # The filesystem path to the folder containing your 180 rasters.
> containingFolder = "/home/myhome/somefolder"
>
> # A list of each of your 180 files you want clipped, as they're named
> inside containingFolder.
> inRasters = [
>         "File001.tif",
>         "File002.tif",
>         "File003,tif"
>         # ...etc.
> ]
>
> # Loop over the list of files in inRasters, clipping by the mask, and
> # creating an output file in the same folder.
> for rasterFileName in inRasters:
>
>     # Define the exact text strings for the input and output files for
> this step.
>     thisInRaster = os.path.join(containingFolder, rasterFileName)
>     outName = defineClippedFilename(rasterFileName)
>     thisOutRaster = os.path.join(containingFolder, outName)
>
>     # Use Python string formatting to substitute-in the right input and
> output paths.
>     # You may need to specify a full path for the gdalwarp executable
> file, if it's
>     # not on your system PATH already (e.g., this is likely on Windows).
>     commandString = "gdalwarp -cutline {} -crop_to_cutline {}
> {}".format(pathToMaskFile, thisInRaster, thisOutRaster)
>
>     # Make the call to the command-line gdalwarp tool
>     subprocess.call(commandString, terminal=True)
>
>     # Tell the user what's going on in terms of progress.
>     print("Finished {}".format(rasterFileName))
>
> On Tue, Mar 24, 2020 at 11:42 PM Paulo Raposo <pauloj.raposo at gmail.com>
> wrote:
>
>> Hi Dani,
>>
>> Michele's answer is great, and she's right that scripting is ideally
>> suited to this.
>>
>> Do you need to extract pixels by their value, or by their location? I'm
>> thinking that if it's by value, you can do this by making reclassified
>> copies of each raster where any pixel that isn't of the values you want is
>> reclassified to NoData. If by location, you have an "extract by mask"
>> situation.
>>
>> Are you okay with or experienced with programming? If so, the GDAL
>> utilities (come with QGIS) can do either case (see
>> https://gis.stackexchange.com/questions/245170/reclassifying-raster-using-gdal
>> or
>> https://gis.stackexchange.com/questions/45053/gdalwarp-cutline-along-with-shapefile
>> for examples close to these two generic situations) - it would be simply a
>> matter of looping over your 180 inputs. You could write a Python script,
>> for example, that calls the command line once (use the subprocess package
>> in Python) for each input file, perhaps after reading them all from the
>> directory where you're storing them.
>>
>> If all that makes no sense to you, no worries :) Batch processing in the
>> way Michele points to should get it done too.
>>
>> Cheers,
>> P
>>
>> On Tue, Mar 24, 2020 at 4:10 PM Michele M Tobias <mmtobias at ucdavis.edu>
>> wrote:
>>
>>> Hi Dani,
>>>
>>> Here’s one way to do it with QGIS:
>>> https://gis.stackexchange.com/questions/318175/batch-raster-calculator
>>> This might be a good case for using a programming language like R or Python
>>> to do the processing.
>>>
>>>
>>>
>>> Best,
>>>
>>> Michele
>>>
>>>
>>>
>>> *Michele Tobias, PhD*
>>>
>>> Geospatial Data Specialist
>>>
>>> DataLab: Data Science & Informatics
>>>
>>> Data & Digital Scholarship
>>>
>>> UC Davis Library
>>>
>>>
>>>
>>> 370 Shields Library
>>>
>>> (530)752-7532
>>>
>>> mmtobias at ucdavis.edu
>>>
>>> ORCID: 0000-0002-2954-8710 <https://orcid.org/0000-0002-2954-8710>
>>>
>>>
>>>
>>> Pronouns: she, her, hers
>>>
>>>
>>>
>>> *From:* Qgis-us-user <qgis-us-user-bounces at lists.osgeo.org> *On Behalf
>>> Of *Dani Varghese
>>> *Sent:* Tuesday, March 24, 2020 3:08 AM
>>> *To:* qgis-us-user at lists.osgeo.org
>>> *Subject:* [Qgis-us-user] raster calculator batch processing
>>>
>>>
>>>
>>> Greetings
>>>
>>> I have nearly 180 raster images (15 minutes interval- climate data) per
>>> day, and need to apply raster calculator to extract a range of pixel
>>> values. can anyone help me, how to do it with raster calculator batch
>>> processing (documents or steps or tutorials) or any other methods.
>>>
>>>
>>>
>>> Thanks in advance
>>>
>>>
>>>
>>> Best
>>>
>>> Dani
>>> _______________________________________________
>>> Qgis-us-user mailing list
>>> Qgis-us-user at lists.osgeo.org
>>> https://lists.osgeo.org/mailman/listinfo/qgis-us-user
>>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-us-user/attachments/20200401/a7eec564/attachment.html>


More information about the Qgis-us-user mailing list