Automate creation of Cost Distance Raster with Python

2870
4
Jump to solution
05-20-2016 12:15 AM
ThomasWirth
New Contributor

Hi everybody

I need some help on the automation processin which I try to create several cost distance raster. In a polygon feature class I have several records and I should create for each record an individual cost distance raster. So far script looks like this:

rCost = "U:\Daten\WWFgis\CH\Projekte\Raumplanung\Landschaften\Test.gdb\F_Offen_Final"
fcStart = r"U:\Daten\WWFgis\CH\Projekte\Raumplanung\Landschaften\Test.gdb\Startorte"
cursor = arcpy.SearchCursor(fcStart)
count = 1
for row in cursor:
    strcount = str(count)
    CD = CostDistance(fcStart,rCost, "100000")
    CD.save("U:\Daten\WWFgis\CH\Projekte\Raumplanung\Landschaften\Cost_Distance.gdb\C"+strcount)
    count +=1

It perfectly runs as many times I have records in the feature class, but the result is always the same for every cost distance raster as all records of the feature were selected for the creation of the cost distance raster instead of only one and the next one in the next loop.

Any suggestions?

Thomas

0 Kudos
1 Solution

Accepted Solutions
NeilAyres
MVP Frequent Contributor

At the moment your loop is not doing anything but using fcStart & rCost repeatedly.

If you want to create cost distances for each geometry in fcStart, you must read the geometry object.

btw, please use the advanced editor and python syntax for posting code.

And switch to using the da.cursor methods.

So something like this :

with arcpy.da.SearchCursor(fcStart, ["SHAPE@"]) as Cur:
    for row in Cur:
        polyGeom = row[0]
        CD = CostDistance(polyGeom, rCost, "100000")

View solution in original post

4 Replies
FC_Basson
MVP Regular Contributor

Your Cost Distance calculation is on the whole Feature Class in every loop and not just the cursor feature:

CD = CostDistance(fcStart,rCost, "100000")

Isolate the cursor feature by creating a temporary layer for each cursor row and run the Cost Distance on that layer.

0 Kudos
NeilAyres
MVP Frequent Contributor

At the moment your loop is not doing anything but using fcStart & rCost repeatedly.

If you want to create cost distances for each geometry in fcStart, you must read the geometry object.

btw, please use the advanced editor and python syntax for posting code.

And switch to using the da.cursor methods.

So something like this :

with arcpy.da.SearchCursor(fcStart, ["SHAPE@"]) as Cur:
    for row in Cur:
        polyGeom = row[0]
        CD = CostDistance(polyGeom, rCost, "100000")
ThomasWirth
New Contributor

Thank you very much. Now it works as i hoped for.

0 Kudos
NeilAyres
MVP Frequent Contributor

Thomas,

could you mark this question as answered...

0 Kudos