I have a '2015' folder within which I have: 1) a 'Daily Min' 2) a 'Daily Max' and 3) a 'RasterCon' folders. 'Daily Min' and 'Daily Max' have shapefiles in them that I convert to raster and run a Con statement on and output to the 'RasterCon' folder. The Con rasters have values of 0 - 1.
In 'RasterCon', there are 2 files for each day (Day1ConMin, Day1ConMax, Day2ConMin, Day2ConMax). I want to run Cell Statistics sum function on each day's files - Day 1 Min/Day 1 Max, Day 2 Min/Day 2 Max. Ideally, the values should be 0 - 2. The last piece of my code is supposed to do this but does not return any output. Where am I going wrong?
import arcpy
import os
from arcpy import env
from arcpy.sa import *
#Set workspace to Max Temps folder and allow for overwrite
env.overwriteOutput = True
env.workspace = r"C:\Users\uma.bhandaram\Desktop2015円\DailyMax2015"
outputFolder = r"C:\Users\uma.bhandaram\Desktop2015円\RasterCon2015"
#Get Max Temp shapefiles and convert them to Raster
shpFileList = arcpy.ListFiles("*.shp")
for shpFile in shpFileList:
shpFileName = os.path.splitext (shpFile)[0]
rastFile = shpFileName + ".tif"
arcpy.PointToRaster_conversion(shpFile, "Max_2015", rastFile, "MOST_FREQUENT", "", 0.05)
#Get raster files and run through Raster Calculator to see if Max threshold has been met
rastFileList = arcpy.ListFiles("*.tif")
for rastFile in rastFileList:
rastFileName = os.path.splitext (rastFile) [0]
MaxRstIn = Raster(rastFile)
MaxRstOut = Con(MaxRstIn, 1, 0, "VALUE <=85")
output_raster_path = os.path.join(outputFolder, "Con" + rastFile)
MaxRstOut.save(output_raster_path)
#Set workspace to Min Temps folder and allow overwrite
env.overwriteOutput = True
env.workspace = r"C:\Users\uma.bhandaram\Desktop2015円\DailyMin2015"
outputFolder = r"C:\Users\uma.bhandaram\Desktop2015円\RasterCon2015"
#Loop through Min Temp shapefiles and convert them to Raster
shpFileList = arcpy.ListFiles("*.shp")
for shpFile in shpFileList:
shpFileName = os.path.splitext (shpFile)[0]
rastFile = shpFileName + ".tif"
arcpy.PointToRaster_conversion(shpFile, "Min_2015", rastFile, "MOST_FREQUENT", "", 0.05)
#Loop through raster files and run through Raster Calculator to see if Min threshold has been met
rastFileList = arcpy.ListFiles("*.tif")
for rastFile in rastFileList:
rastFileName = os.path.splitext (rastFile) [0]
MinRstIn = Raster(rastFile)
MinRstOut = Con(MinRstIn, 1, 0, "VALUE >=45")
output_raster_path = os.path.join(outputFolder, "Con" + rastFile)
MaxRstOut.save(output_raster_path)
#Loop through corresponding Min and Max rasters and calculate Cell Statistics (SUM)
#This will identify which days met both Min and Max thresholds (SUM = 2)
MainFolder = r"C:\Users\uma.bhandaram\Desktop2015円\RasterCon2015"
for (path, dirs, files) in os.walk(MainFolder):
for dir in dirs:
env.workspace = os.path.join(path, dir)
listRstrs = arcpy.ListRasters("Con.tif")
for raster in listRstrs:
rasterlist = []
rasterlist.append(raster)
outCellStats = CellStatistics(rasterlist, "SUM", "DATA")
outCellStats.save(os.path.join(path, dir) + "Check.tif")
-
If the output is created, what cell values do you get instead of the expected sum?GISGe– GISGe2016年11月30日 08:21:07 +00:00Commented Nov 30, 2016 at 8:21
-
The 'Daily Min' and 'Daily Max' rasters have values between 45 - 52 and 82 - 92, respectively. Then, these are converted to have values of 0 - 1, if thresholds were met. The Cell Stats should sum these and give values 0 - 2 but instead it outputs the original raster values of 45 - 52 and 82 - 92Uma– Uma2016年11月30日 14:38:49 +00:00Commented Nov 30, 2016 at 14:38
1 Answer 1
The input rasters created in the first step (outputs of Point to Raster) are saved in a workspace examined by os.walk()
in the second step, so they will be used too to calculate the statistics. You should try to:
- print the raster names in the
listRstrs
list to better understand what's going on - use different workspaces for the input rasters (outputs of Point to Raster) and the reclassified rasters (outputs of Con) created in step 1, or
- use a wildcard in
listRstrs = arcpy.ListRasters("*")
to only include rasters with "Con" in their name.
-
How would I set a different workspace for the Con rasters? The input rasters for these are the Point To Rasters - don't they all need to be in the same workspace? When I use a wildcard, the script runs but there are no outputs so it seems the os.walk() is not doing what I thought it was.Uma– Uma2016年11月30日 19:46:42 +00:00Commented Nov 30, 2016 at 19:46
-
In your modified code, the wildcard won't work: only the rasters named 'Con.tif' will be selected, while they are named 'Con' + rastFile.tif. Instead you should use a star to select everything that starts with Con:
listRstrs = arcpy.ListRasters("Con*")
- provided the workspace doesn't contain unwanted rasters with a name also starting with Con. But, as I said, using the wildcard in one option, using separate workspaces is another one (no need to use both a priori). The way you've defined the workspaces now should solve the issue without the need for a wildcard in the ListRasters function.GISGe– GISGe2016年12月01日 07:20:48 +00:00Commented Dec 1, 2016 at 7:20