2

I have created a simple script like that, which is working fine

import arcpy
from arcpy.sa import *
arcpy.CheckOutExtension("spatial")
arcpy.env.workspace = r'C:/2017/wgs84/project'
direction = arcpy.ListRasters("*WIND*","TIF")
print direction
speed = arcpy.ListRasters("*SPEED*","TIF")
print speed
for k in range(len(direction)):
  outCon = Con(Raster(direction[k]) == 1, Raster(speed[k]), 0)
  outCon.save('speed0' + str(k) + '.TIF')
  print ('done')
print ('done final')

I have different wind direction files(rasters) in different folders and speed files in another one. This code works for what I need, only if I have pasted the speed files and the wind files in the same folder. What if I wanted to create a list from one folder and a list from another so as not having to copy paste every time?

I tried to define also a different workspace for direction and different for speed but apparently its not working(maybe the last env.workspace is "hold" in the memory?)

arcpy.env.workspace = r'C:/speed'
speed = arcpy.ListRasters("*SPEED*","TIF")
print speed
arcpy.env.workspace = r'C:/2017/wgs84/project'
direction = arcpy.ListRasters("*WIND*","TIF")
2

This will list the raster names without full path/workspace:

arcpy.env.workspace = r'C:/speed'
speed = arcpy.ListRasters("*SPEED*","TIF")

So when you change workspace afterwards, arcpy will not know where to find them.

Try adding full path to the raster lists using os.path.join and list comprehension:

import os
arcpy.env.workspace = r'C:/speed'
speed = arcpy.ListRasters("*SPEED*","TIF")
speed = [os.path.join(arcpy.env.workspace,r) for r in speed]

And then do the same for the wind rasters in the other folder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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