473,387 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

nesting for statements?

I'm not what you'd call a "programmer" of any sort, so perhaps this
question may seem arcane and result in a plethora of "you idiot"
threads, but here goes:

ArcGIS 9.1 has a neat interface with python (2.1-2.4), allowing me to
do all sorts of spatial operations within python, namely, repetitive
functions.
In the below code, I'm trying to iterate thru multiple values of the
variable "Discretisation error factor" using a for statement that
temporarily populates "Disc", which is used as input in "Discretisation
error factor" in the gp.TopoToRaster._sa function.
For each iteration of "Discretisation error factor", I'm trying to name
the output file "outa", "outb", "out...."

Not quite sure how to implement that. There are lots of examples on
nested for loops out there, but nothing on combing that with a output
file naming sheme.

The gp.TopoToRaster_sa function by itself without all the for
statements works fine.

Appreciate any help on this, other wise I have to manually interpolate
hundreds of LiDAR point clouds, each, 10 times for diff. values of DEF.

# TopoToRaster_sample.py
# Description: Interpolate a series of point features onto a
rectangular raster using TopoToRaster
# Requirements: None
# Author: ESRI
# Date: 12\\01\\03

# Import system modules
import sys, string, os, win32com.client

# Create the Geoprocessor object
from win32com.client import Dispatch
gp = Dispatch("esriGeoprocessing.GpDispatch.1")

# Check out any necessary licenses
gp.CheckOutExtension("spatial")

# Iterate 2 thru 4 in increments of 2 for DEF
# Name the "2" dem "outa" and the "4" dem "outb"
for x in range(2,4):
Disc = int(x)
names = ["a","b"]
for y in names:
Out_Dem = "out"+y
try:

# Process: Topo to Raster...
gp.TopoToRaster_sa("C:\\temp\\falls_lidar.shp Z PointElevation",
Out_Dem, # Variable for name of output raster.
This should increment name of output based on the for statement
"5", # Output raster cell size: each pixel is 5
feet by 5 feet
"2103763.27 813746.12 2111850.32 822518.65",
#extent of raster borders, SPF, NC, NAD83
"20", # #Grid Margin
"", #Smallest z value to be used in
interpolation (optional)
"", #Largest z value to be used in interpolation
(optional)
"NO_ENFORCE", #Drainage option
"SPOT", #Spot data option
"40", #Maximum number of iterations (optional)
"", #Roughness penalty (optional)
Disc, #Discretisation error factor: This should
increment DEF based on the for statement
"0", #Vertical standard error (optional)
"", #Tolerance 1 (optional)
"" #Tolerance 2 (optional)
)
except:
print "ERROR OCCURED"
print gp.GetMessages()

Nov 27 '05 #1
3 1465
tp******@gmail.com wrote:
I'm not what you'd call a "programmer" of any sort, so perhaps this
question may seem arcane and result in a plethora of "you idiot"
threads, but here goes:

ArcGIS 9.1 has a neat interface with python (2.1-2.4), allowing me to
do all sorts of spatial operations within python, namely, repetitive
functions.
In the below code, I'm trying to iterate thru multiple values of the
variable "Discretisation error factor" using a for statement that
temporarily populates "Disc", which is used as input in "Discretisation
error factor" in the gp.TopoToRaster._sa function.
For each iteration of "Discretisation error factor", I'm trying to name
the output file "outa", "outb", "out...."

Not quite sure how to implement that. There are lots of examples on
nested for loops out there, but nothing on combing that with a output
file naming sheme.

The gp.TopoToRaster_sa function by itself without all the for
statements works fine.

Appreciate any help on this, other wise I have to manually interpolate
hundreds of LiDAR point clouds, each, 10 times for diff. values of DEF.

# TopoToRaster_sample.py
# Description: Interpolate a series of point features onto a
rectangular raster using TopoToRaster
# Requirements: None
# Author: ESRI
# Date: 12\\01\\03

# Import system modules
import sys, string, os, win32com.client

# Create the Geoprocessor object
from win32com.client import Dispatch
gp = Dispatch("esriGeoprocessing.GpDispatch.1")

# Check out any necessary licenses
gp.CheckOutExtension("spatial")

# Iterate 2 thru 4 in increments of 2 for DEF
# Name the "2" dem "outa" and the "4" dem "outb"
for x in range(2,4):
Disc = int(x)
names = ["a","b"]
for y in names:
Out_Dem = "out"+y
try:

# Process: Topo to Raster...
gp.TopoToRaster_sa("C:\\temp\\falls_lidar.shp Z PointElevation",
Out_Dem, # Variable for name of output raster.
This should increment name of output based on the for statement
"5", # Output raster cell size: each pixel is 5
feet by 5 feet
"2103763.27 813746.12 2111850.32 822518.65",
#extent of raster borders, SPF, NC, NAD83
"20", # #Grid Margin
"", #Smallest z value to be used in
interpolation (optional)
"", #Largest z value to be used in interpolation
(optional)
"NO_ENFORCE", #Drainage option
"SPOT", #Spot data option
"40", #Maximum number of iterations (optional)
"", #Roughness penalty (optional)
Disc, #Discretisation error factor: This should
increment DEF based on the for statement
"0", #Vertical standard error (optional)
"", #Tolerance 1 (optional)
"" #Tolerance 2 (optional)
)
except:
print "ERROR OCCURED"
print gp.GetMessages()


I think you want a filename generated for each pass through the loop. I
would suggest generating the filenames before you ever start the loop in
a list. Then, reference the filename list while you are in the loop.
You could also construct the filename while you are in the loop.

sor = 2 # start of range
eor = 4 # end of range

filenames = ['file' + chr((x-sor) + ord('a')) for x in range(sor, eor)]

for x in range(sor, eor):
print filenames[x - sor]

If you do not want to create all of the filenames in memory at one time,
you could generated them during the loop. However, this will not work
well when you code is run on a system not using ASCII character encoding.

for x in range(sor, eor):
print 'file' + chr((x - sor) + ord('a'))

Something using string.ascii_lowercase would work better. My guess is
that on the mainframe using EBCDIC character encoding that
string.ascii_lowercase still contains the LATIN SMALL LETTER characters,
just as it does on ASCII-based systems.
Nov 27 '05 #2
Hi. Thanks for the tip. However, implementing that example, the script
will only generate the second output "file", (or it's overwriting the
first one), so all I get when run is "fileb".

# Import system modules
import sys, string, os, win32com.client

# Create the Geoprocessor object
from win32com.client import Dispatch
gp = Dispatch("esriGeoprocessing.GpDispatch.1")

# Check out any necessary licenses
gp.CheckOutExtension("spatial")

# Iterate 2 thru 4 in increments of 2 for DEF
# Name the "2" dem "outa" and the "4" dem "outb"
sor = 2 # start of range
eor = 4 # end of range

filenames = ['file' + chr((x-sor) + ord('a')) for x in range(sor, eor)]

for x in range(sor, eor):
Out_Dem = filenames[x - sor]
try:

# Process: Topo to Raster...
gp.TopoToRaster_sa("C:\\temp\\test.shp Z PointElevation",
"C:\\temp\\"+Out_Dem, # Variable for name of
output raster. This should increment name of output based on the for
statement
"5", # Output raster cell size: each pixel is 5
feet by 5 feet
"2103763.27 813746.12 2111850.32 822518.65",
#extent of raster borders, SPF, NC, NAD83
"20", # #Grid Margin
"", #Smallest z value to be used in
interpolation (optional)
"", #Largest z value to be used in interpolation
(optional)
"NO_ENFORCE", #Drainage option
"SPOT", #Spot data option
"40", #Maximum number of iterations (optional)
"", #Roughness penalty (optional)
"0" #Discretisation error factor: This should
increment DEF based on the for statement
"0", #Vertical standard error (optional)
"", #Tolerance 1 (optional)
"" #Tolerance 2 (optional)
)
except:
print "ERROR OCCURED"
print gp.GetMessages()

Nov 28 '05 #3
tp******@gmail.com wrote:
Hi. Thanks for the tip. However, implementing that example, the script
will only generate the second output "file", (or it's overwriting the
first one), so all I get when run is "fileb".


I think your looping code has the structure

for x in ["a", "b", "c"]:
pass
print x

This will print x once after the loop has completed. By then the value of x
is "c". To fix it, move the code from /after/ the loop /into/ the loop:

for x in ["a", "b", "c"]:
print x

In the code you give that would mean that you have to indent the

try:
# ...
except:
# ...

statement one more level.

Inside that loop you want a filename and an integer value in lockstep. The
simplest way to get that is zip():
for Disc, suffix in zip(xrange(2, 6, 2), "abcdefghijklmnopqrstuvwxyz"): .... Out_Dem = "out" + suffix
.... print Disc, Out_Dem
....
2 outa
4 outb

Again, you have to replace the print statement by your try...except. If you
fear that you will eventually run out of suffices, here is a function that
"never" does:
def gen_suffix(chars): .... for c in chars:
.... yield c
.... for c in gen_suffix(chars):
.... for d in chars:
.... yield c + d
.... for i, s in zip(range(20), gen_suffix("abc")):

.... print i, s
....
0 a
1 b
2 c
3 aa
4 ab
5 ac
6 ba
7 bb
8 bc
9 ca
10 cb
11 cc
12 aaa
13 aab
14 aac
15 aba
16 abb
17 abc
18 aca
19 acb

That said, reading the tutorial or an introductory book on Python never
hurts...

Peter

Nov 29 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: JustSomeGuy | last post by:
I have a need to make an applicaiton that uses a variable number of nested for loops. for now I'm using a fixed number: for (z=0; z < Z; ++z) for (y=0; y < Y; ++y) for (x=0; x < X; ++x)
13
by: Philip WATTS | last post by:
I am trying to carry out multiple checks on some input data. I am doing this by the running the data through a number of functions. i.e. I have an onclick that calls a function, which in turn calls...
0
by: Wolfgang Schwanke | last post by:
Dear usenet, I'm having the following small problem. I've been ask to add some Quicktime panoramas to a website. The author of the panoramas has made two versions of each: One in MOV format,...
8
by: Hardrock | last post by:
I encountered some difficulty in implementing dynamic loop nesting. I.e. the number of nesting in a for(...) loop is determined at run time. For example void f(int n) { For(i=0; i<=K; i++)...
8
by: Howard Kaikow | last post by:
I got bored today, so I decided to rewrite the code in KB article 316383 to decrease the number of object references. This resulted in a number of nested With ... End With. The original code had...
22
by: Technoid | last post by:
Is it possible to have a conditional if structure nested inside a conditional switch structure? switch(freq) { case 1: CASENAME if (variable==1) { do some code }
4
by: kl.vanw | last post by:
I would like to count the nesting level in template classes. How can I make the following work? #include <assert.h> template <class T> class A { public: A() { // what goes here?
6
by: stephen.cunliffe | last post by:
Hi, I'm looking for opinion/facts/arguments on the correct nesting of UL, OL, & LI elements. For example, this is what I want (unordered list): * Item 1 * Item 2 * Item 3
11
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
Can switch statements be nested? I've got a large routine that runs off of a switch statement. If one of the switches in switch #1 is true, that enters switch statement #2. Some of the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.