Select to view content in your preferred language

iterating rasters to input into weighted overlay

999
7
Jump to solution
10-16-2014 01:37 PM
AmyKlug
Frequent Contributor

I am preforming a weighted overlay. All of my raster's are in 1 folder. There are 4 different rasters that will be used in the weighted model (s,v,o,l)

1 run through will use 4 raster's with 0520a as the example code (I have 75 or so codes of rasters in 1 folder)

s0520a

v0520a

o0520a

l0520a

I need to find a way to select a code and loop through each 5 digit code code and add the s,v,o,l to the model then move on to the next code. If this was using a search cursor I would just set for r[0] but I'm not sure how to do that for looping through rasters. Also not sure how to create the output raster (send the code variable to the output name I suppose)

Thanks in advance!

Here is what I have so far:


import arcpy
arcpy.CheckOutExtension("spatial")


arcpy.overwriteOutput = True
inrcras = "C:\\Risk2014\\TestReclass\\"
arcpy.env.workspace = "C:\\Risk2014\\TestReclass"
rcras = sorted(arcpy.ListRasters())


outras = "C:\\Risk2014\\TestWeighted\\w"


for r in rcras:
    print r[4:] #code isolated for iterating
    if r[1] == "s":
        s = inrcras + r
    if r[1] == "v":
        v = inrcras + r
    if r[1] == "o":
        o = inrcras + r
    if r[1] == "l":
        l = inrcras + r
    print s, v, o, l
    #arcpy.gp.WeightedOverlay_sa("(s 25 VALUE (1 1; 2 2; 3 3; 4 4; 5 5; 6 6; 7 7; 8 8; 9 9; NODATA NODATA); v 25 VALUE (1 1; 2 2; 3 3; 4 4; 5 5; 6 6; 7 7; 8 8; 9 9; NODATA NODATA); o 25 VALUE (1 1; 2 2; 3 3; 4 4; 5 5; 6 6; 7 7; 8 8; 9 9; NODATA NODATA); l 25 VALUE (1 1; 2 2; 3 3; 4 4; 5 5; 6 6; 7 7; 8 8; 9 9; NODATA NODATA));1 9 1", outras + ?)
del r, rcras

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

Amy is this the kind of thing you are looking for?  I put in a set of fake rasters...I hope your sorting idea works since this is a bit of a cludge approach

l: C:\Risk2014\TestReclass\Dliu0Test
o: C:\Risk2014\TestReclass\Doiu0Test
v: C:\Risk2014\TestReclass\Dviu0Test
s: C:\Risk2014\TestReclass\Dsiu0Test
Weighted: C:\Risk2014\TestWeighted\w0Test

l: C:\Risk2014\TestReclass\rliu0520a
o: C:\Risk2014\TestReclass\roiu0520a
v: C:\Risk2014\TestReclass\rviu0520a
s: C:\Risk2014\TestReclass\rsiu0520a
Weighted: C:\Risk2014\TestWeighted\w0520a

import arcpy

arcpy.CheckOutExtension("spatial")

arcpy.overwriteOutput = True

reclass_path = "C:\\Risk2014\\TestReclass\\"

# change to arcpy.ListRasters() I put in a few fake ones

raster_list = sorted(['rsiu0520a','rviu0520a','roiu0520a','rliu0520a',

                      'Dsiu0Test','Dviu0Test','Doiu0Test','Dliu0Test'])

print raster_list

count = 0

for raster in raster_list:

  code = raster[-5:]

  val = raster[1]

  if val == "l":  l = reclass_path + raster

  elif val == "o":  o = reclass_path + raster

  elif val == "v":  v = reclass_path + raster

  elif val == "s":  s = reclass_path + raster

  count += 1

  if count > 3:

    #arcpy weighted overlay magicmagic

    out_ras ="C:\\Risk2014\\TestWeighted\\w" + code

    print("\n l: {0}\n o: {1}\n v: {2}\n s: {3}\n Weighted: {4}".format(l,o,v,s,out_ras))

    count = 0

View solution in original post

0 Kudos
7 Replies
DanPatterson_Retired
MVP Emeritus

is your indexing off?  I think you want to get the 1st character (?), if so it is index 0

>>> r = 's0520a'

>>> r[4:]

'0a'

>>> r[1]

'0'

>>> r[0]

's'

>>>

0 Kudos
AmyKlug
Frequent Contributor

No, my rasters are really named

rsiu0520a

rviu0520a

roiu0520a

rliu0520a

i shortened them to make it easier to understand but didn't think to change the code, my bad

0 Kudos
DanPatterson_Retired
MVP Emeritus

could you edit or restate your question so that we are now clear what you are asking

0 Kudos
AmyKlug
Frequent Contributor

I have 4 rasters for each code that need to go in to a wieghted overlay, each wieghted 25%. The code is the last 5 characters of the raster name and the 4 rasters are distinguished by s, v, o and l

Example

Code 0520a has 4 rasters

rsiu0520a

rviu0520a

roiu0520a

rliu0520a

In 1 folder are 75 codes each with 4 rasters so i need to grab the code, then each raster for that code, load them into weighted overlay, save output as code name with a w in the name (w0520a), and move on to the next code

Hope that makes sense

0 Kudos
DanPatterson_Retired
MVP Emeritus

Amy is this the kind of thing you are looking for?  I put in a set of fake rasters...I hope your sorting idea works since this is a bit of a cludge approach

l: C:\Risk2014\TestReclass\Dliu0Test
o: C:\Risk2014\TestReclass\Doiu0Test
v: C:\Risk2014\TestReclass\Dviu0Test
s: C:\Risk2014\TestReclass\Dsiu0Test
Weighted: C:\Risk2014\TestWeighted\w0Test

l: C:\Risk2014\TestReclass\rliu0520a
o: C:\Risk2014\TestReclass\roiu0520a
v: C:\Risk2014\TestReclass\rviu0520a
s: C:\Risk2014\TestReclass\rsiu0520a
Weighted: C:\Risk2014\TestWeighted\w0520a

import arcpy

arcpy.CheckOutExtension("spatial")

arcpy.overwriteOutput = True

reclass_path = "C:\\Risk2014\\TestReclass\\"

# change to arcpy.ListRasters() I put in a few fake ones

raster_list = sorted(['rsiu0520a','rviu0520a','roiu0520a','rliu0520a',

                      'Dsiu0Test','Dviu0Test','Doiu0Test','Dliu0Test'])

print raster_list

count = 0

for raster in raster_list:

  code = raster[-5:]

  val = raster[1]

  if val == "l":  l = reclass_path + raster

  elif val == "o":  o = reclass_path + raster

  elif val == "v":  v = reclass_path + raster

  elif val == "s":  s = reclass_path + raster

  count += 1

  if count > 3:

    #arcpy weighted overlay magicmagic

    out_ras ="C:\\Risk2014\\TestWeighted\\w" + code

    print("\n l: {0}\n o: {1}\n v: {2}\n s: {3}\n Weighted: {4}".format(l,o,v,s,out_ras))

    count = 0

0 Kudos
AmyKlug
Frequent Contributor

Thanks, That helped a lot (I still need to get the count thing down). I had to change the raster names to iu0520arc so they would sort. I would still like to find a way to select (set) for one code (and not need to sort) for times when a raster for a code might be missing but the only way I know how to do that is with a search cursor in a table and create dictionary's.


import arcpy
from arcpy.sa import *
arcpy.CheckOutExtension("spatial")


arcpy.overwriteOutput = True
reclass_path = "C:\\Risk2014\\TestReclass\\"
arcpy.env.workspace = "C:\\Risk2014\\TestReclass"
raster_list = sorted(arcpy.ListRasters())


#outras = "C:\\Risk2014\\TestWeighted\\w"
#for raster in raster_list:
    #front, back = raster[2:], raster[:2]
    #print front, back
    #arcpy.Rename_management(raster, front + back)
#del raster


count = 0


for raster in raster_list:
    code, val =  raster[2:7], raster[8]
    #print code, val
    if val == "s": 
        s = reclass_path + raster
    if val == "v": 
        v = reclass_path + raster
    if val == "o": 
        o = reclass_path + raster
    if val == "l": 
        l = reclass_path + raster
    count += 1
    if count > 3:
        out_ras = "C:\\Risk2014\\TestWeighted\\w" + code
        remap = RemapValue([[1,1], [2,2], [3,3], [4,4], [5,5], [6,6], [7,7], [8,8], [9,9], ["NODATA","NODATA"]])
        mwt = WOTable([[str(s), 25, "VALUE", remap],[str(v), 25, "VALUE", remap],[str(o), 25, "VALUE", remap],[str(l), 25, "VALUE", remap]],[1,9,1])
        out_overlay = WeightedOverlay(mwt)
        out_overlay.save(out_ras)
        count = 0
del raster, raster_list


DanPatterson_Retired
MVP Emeritus

Glad it worked out Amy

0 Kudos