0

I cannot perform the Con tool over all the raster images with same date in two folders. This is the script I have worked upon so far.

import arcpy, os
from arcpy.sa import *

# Set the current workspace 
location_a = "D:/datasets/regression_interpolation_rasters/airtemp_snowmask_regression_rasters"
location_b = "D:/datasets/regression_interpolation_rasters/regression_rasters"
location = "D:/datasets/regression_interpolation_rasters/snowmask_rasters"

# get the names of rasters 
arcpy.env.workspace = location_a
snow_raster = arcpy.ListRasters("", "TIF")
#print(type(snow_raster))
for snow_ras in snow_raster:
    snow = Raster(snow_ras)
    #print(type(snow))
    snow_name = snow_ras.split("_")
    snow_values = snow_name[2]
    #print(snow_values)

# get the names of rasters 
arcpy.env.workspace = location_b
airtemp_raster = arcpy.ListRasters("", "TIF")
#print(airtemp_raster)
for airtemp_ras in airtemp_raster:
    airtemp = Raster(airtemp_ras)
    airtemp_name = airtemp_ras.split("_")
    airtemp_values = airtemp_name[0]
    #print(airtemp_values)

    if airtemp_values == snow_values:
        output_raster = Con(snow_raster==1, 0, airtemp_raster)  
        output = os.path.join(location,airtemp_ras.split("_")[0] +'_airtemp'+ '.tif')
        output_raster.save(output)
            #print("yes")
    else:
        print("action cannot take place")

All it is showing the else statement even though the dates match.The placement of the dates vary but the format of dates are similar.These are the images in location_b

enter image description here

These are the images in location_a

enter image description here

I have revised my code to:

import arcpy, os
from arcpy.sa import *

# Set the current workspace
location_a = "D:/datasets/regression_interpolation_rasters/airtemp_snowmask_regression_rasters"
location_b = "D:/datasets/regression_interpolation_rasters/interpolation_rasters_for_testing"
location = "D:/datasets/regression_interpolation_rasters/snowmask_rasters"

# get the names of rasters
snow_raster_dict = dict()
arcpy.env.workspace = location_a
snow_raster_list = arcpy.ListRasters("", "TIF")
#print(type(snow_raster))

for snow_name in snow_raster_list:
    snow_raster = Raster(snow_name)
    #print(type(snow))
    snow_name_parts = snow_name.split("_")
    snow_date = snow_name_parts[2]
    snow_raster_dict[snow_date] = snow_raster
    #print(snow_values) ##22 line##
    
# get the names of rasters ##14 line##
arcpy.env.workspace = location_b
airtemp_raster_list = arcpy.ListRasters("", "TIF")
#print(airtemp_raster)

for airtemp_name in airtemp_raster_list:
    airtemp_raster = Raster(airtemp_name)
    airtemp_name_parts = airtemp_name.split("_")
    airtemp_date = airtemp_name_parts[0]
    #print(airtemp_values)
    try:
        snow_date == airtemp_date
        output_raster = Con(snow_raster==1, 0, airtemp_raster)  
        output = os.path.join(location,airtemp_name.split("_")[0] +'_airtemp'+ '.tif')
        output_raster.save(output)
            #print("yes")
    except KeyError:
        print(f"no snow raster found for {airtemp_date}")

After editing the code images whose dates match are being stored but somehow all the images have the same pixel value of 1 replaced by 0 which is incorrect. What I am trying to achieve is producing an image where the pixel values of 1 are converted to 0 and the pixel values of 0 are replaced with the pixel values of another image using the images with the same dates.

2
  • 1
    docs.python.org/3/library/…
    – BERA
    Sep 6, 2022 at 17:00
  • One of the single most powerful features of Python is its native support for dictionaries. Walking a list to do comparisons is just asking for list offset trouble. I suggest you learn to use dictionary lookups instead.
    – Vince
    Sep 6, 2022 at 17:53

3 Answers 3

0

After you spit your file names and extract the dates you have your list indices mixed up.

snow_values = snow_name[2] for location_A should be snow_values = snow_name[0]

and

snow_values = snow_name[0] for locationBB should be snow_values = snow_name[2]

Given your comment then: airtemp_raster is a list of rasters in a workspace and not an individual raster layer...

`output_raster = Con(snow_raster==1, 0, airtemp_raster)`   

should be

output_raster = Con(snow_raster==1, 0, airtemp)  
2
  • I corrected the labeling of the raster images in the screenshot. The screenshot labelling was wrong, which has been corrected now. Sep 6, 2022 at 21:30
  • I have altered my answer.
    – GBG
    Sep 6, 2022 at 22:21
0

Looping through both folders is pretty inefficient. I would propose the following logic (which only uses a single loop):

  1. For each image in folder A, extract the date into a variable
  2. Construct the path to the corresponding image in folder B based on the date (regardless of whether it actually exists)
  3. Check whether the constructed path exists with os.path.exists() or arcpy.Exists()
  4. If step 3 returns True, then run your Con()
0

I have managed to gather the code with some additional help.Here is the code.

import arcpy, os
from arcpy.sa import *
from arcpy import env

# Set the current workspace
location_a = "D:/datasets/planetscope/binary_snow_images_converted"
location_b = "D:/datasets/regression_interpolation_rasters/linear_regression_interpolation_rasters_without_snowmask"
location = "D:/datasets/regression_interpolation_rasters/regression_rasters_after_snowmask"
arcpy.env.extent = "D:/datasets/DEM ALTA_VALTELLINA LOMBARDY REGION/clip_dtm.tif"
arcpy.env.snapRaster = "D:/datasets/DEM ALTA_VALTELLINA LOMBARDY REGION/clip_dtm.tif"
arcpy.env.cellSize = "D:/datasets/regression_interpolation_rasters/linear_regression_interpolation_rasters_without_snowmask/2017-10-01_airtemp.tif"

# get the names of rasters
snow_raster_dict = dict()
all_snow_rasters = []
arcpy.env.workspace = location_a
snow_raster_list = arcpy.ListRasters("", "TIF")


for snow_name in snow_raster_list:
    snow_raster = Raster(snow_name)
    snow_name_parts = snow_name.split("_")
    snow_date = snow_name_parts[2]
    snow_raster_dict[snow_date] = snow_raster
    
    # get the names of rasters ##26 line##
arcpy.env.workspace = location_b
airtemp_raster_list = arcpy.ListRasters("", "TIF")


for airtemp_name in airtemp_raster_list:
    airtemp_raster = Raster(airtemp_name)
    airtemp_name_parts = airtemp_name.split("_")
    airtemp_date = airtemp_name_parts[0]
    try:
        snow_raster = snow_raster_dict[airtemp_date]
        output_raster = Con(snow_raster==1, 0, airtemp_raster)  
        output = os.path.join(location,airtemp_name.split("_")[0] +'_airtemp'+ '.tif')
        output_raster.save(output)
    except KeyError:
        print(f"no snow raster found for {airtemp_date}")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.