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()