Howdy,
I'm a GIS guy trying to get my hands dirty with some Python. What I'm trying to do is iterate through a file (shapefile), reading specific fields from each row of the file's associated table. The table has thousands of rows, which is why a Python script is handy here. Each row contains about 10 fields, but I'm interested in using gp.searchcursor() to specifically pull out 4 of those fields (N, S, E, W coordinates). I then want to insert those 4 values into some XML code, and write this code to a text file. Each piece of code is about 20 lines, and this piece needs to be written for 3600 groups of coordinate values (so the script should output to the text file about 72000 lines of XML code).

So not only does each piece of XML code need to have its unique set of 4 coordinate values, but also a unique name (iterating poly1, poly2, poly3, etc... as you can see below). My question is, am I heading in the right direction here, and how much is wrong with my code so far? This is the biggest script I've ever written, even thought it's just one loop, so bear with me...

(in the code, "Left" "Right" "Top" "Bott" refer to the field names of N,S,E,W coordinates)

Thanks in advance,
CM

# Script to iterate N,S,E,W coordinates of extent rectangles in 60x60 grid
# and write iterations of 20 lines of code to Region.kml


# IMPORT ARCGISSCRIPTING MODULE
import arcgisscripting, os

# CREATE GEOPROCESSOR
gp = arcgisscripting.create(9.3)

# TARGET FEATURE CLASS AND VARIABLES
gp.Workspace = r'c:\craig\Working\07282010'
inputFC = 'grid_60_bound_cm.shp'

# OUTPUT FILE
Region = open(r'C:\Craig\Working\07282010\Region.kml','w')



# BEGIN THE LOOP
rows = gp.searchcursor(inputFC)
row = rows.next()

while row:
    left = row.getValue("Left")
    right = row.getValue("Right")
    top = row.getValue("Top")
    bott = row.getValue("Bott")
    polynum = 1
    poly = "poly" + polynum

        Region.write("  <NetworkLink>")
        Region.write("  <Region>")
        Region.write("     <LatLonAltBox>")
        Region.write("        <north>",top,"</north>")
        Region.write("        <south>",bott,"</south>")
        Region.write("        <east>",right,"</east>")
        Region.write("        <west>",left,"</west>")
        Region.write("                <minAltitude>0</minAltitude>")
        Region.write("                <maxAltitude>0</maxAltitude>")
        Region.write("      </LatLonAltBox>")
        Region.write("          <Lod>")
        Region.write("                <minLodPixels>1024</minLodPixels>")
        Region.write("                <maxLodPixels>-1</maxLodPixels>")
        Region.write("          </Lod>")
        Region.write("  </Region>")
        Region.write("      <name>",poly,"</name>")
        Region.write("      <Link>")
        Region.write("          <href>",poly,".kmz</href>")
        Region.write("          <viewRefreshMode>onRegion</viewRefreshMode>")
        Region.write("      </Link>")
        Region.write("  </NetworkLink>")      
        
    polynum = polynum + 1
    row = rows.next()

          
else:
    Region.close()

I would rather write it this way, using a multiline template and the string % operator

# Script to iterate N,S,E,W coordinates of extent rectangles in 60x60 grid
# and write iterations of 20 lines of code to Region.kml


# IMPORT ARCGISSCRIPTING MODULE
import arcgisscripting, os

template = """  <NetworkLink>
        <Region>
            <LatLonAltBox>
                <north>%(top)s</north>
                <south>%(bott)s</south>
                <east>%(right)s</east>
                <west>%(left)s</west>
                <minAltitude>0</minAltitude>
                <maxAltitude>0</maxAltitude>
            </LatLonAltBox>
            <Lod>
                <minLodPixels>1024</minLodPixels>
                <maxLodPixels>-1</maxLodPixels>
            </Lod>
        </Region>
        <name>%(poly)s</name>
        <Link>
            <href>%(poly)s.kmz</href>
            <viewRefreshMode>onRegion</viewRefreshMode>
        </Link>
        </NetworkLink>
"""

# CREATE GEOPROCESSOR
gp = arcgisscripting.create(9.3)

# TARGET FEATURE CLASS AND VARIABLES
gp.Workspace = r'c:\craig\Working\07282010'
inputFC = 'grid_60_bound_cm.shp'

# OUTPUT FILE
Region = open(r'C:\Craig\Working\07282010\Region.kml','w')

# BEGIN THE LOOP
for row in gp.searchcursor(inputFC):
    left = row.getValue("Left")
    right = row.getValue("Right")
    top = row.getValue("Top")
    bott = row.getValue("Bott")
    polynum = 1
    poly = "poly" + polynum
    Region.write(template % locals())
    polynum = polynum + 1
    row = rows.next()
Region.close()

Gribouillis,

Thanks for the quick reply.... I really like the way you set it up. However, now I am receiving an error: TypeError: 'geoprocessing cursor object' object is not iterable.

Anybody on here know ArcGIS geoprocessing scripts? I'm not sure if this error message has to do with version or what...

I found some documentation here http://www.geospatialtraining.com/Newsletter/Spring%202006/GeoprocessorCursorObjects.htm. You should rewrite the loop structure like this

rows = gp.searchcursor(inputFC)
row = rows.next()

while row:
  ...
  row = rows.next()
...

(or perhaps 'next' should be Next ?). You can still use the template trick. I wonder why they didn't make the searchcursor object iterable. It would be more pythonic.

Ok, I reset it to look like this

rows = gp.searchcursor(inputFC)
row = rows.next()

# BEGIN THE LOOP
while row:
    left = row.getValue("Left")
    right = row.getValue("Right")
    top = row.getValue("Top")
    bott = row.getValue("Bott")
    polynum = 1
    poly = "poly" + polynum
    Region.write(template % locals())
    polynum = polynum + 1
    row = rows.next()
Region.close()

Now this is dumb... obviously, you can't concatenate string and int (I remember this from college). How would I set up 'polynum' and 'poly' so that it will iterate through and name each set of XML code 'poly1', 'poly2', 'poly3', etc... ?

Nevermind, just moved polynum = 1 to the variables section, DUH.

Thanks!! It's all working now, and I have a gigantic text file..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.