Select to view content in your preferred language

How to get the MIN and Max from a while loop - python

9439
14
Jump to solution
08-03-2012 09:44 AM
DanielAbera
Occasional Contributor
Hi Eveyone,

I got the following python code to get the geometry(X, Y, Z) of a polyline feature. It can print all the values. But what I am looking is that, how do we get the MIN and MAX values for Z -value which is an elevation value in my case; and pass it as a variable. I am new to python and having trouble to figure it out. The code is as follows. Thanks in advance for your help.

Daniel A.

# Import native arcgisscripting module # import arcgisscripting  # Create the geoprocessor object # gp = arcgisscripting.create(9.3)  infc = gp.GetParameterAsText(0)    # Identify the geometry field # desc = gp.Describe(infc) shapefieldname = desc.ShapeFieldName  # Create search cursor # rows = gp.SearchCursor(infc) row = rows.Next()  # Enter while loop for each feature/row # while row:     # Create the geometry object     #     feat = row.GetValue(shapefieldname)      # Print the current multipoint's ID     #     print "Feature " + str(row.getvalue(desc.OIDFieldName)) + ":"     partnum = 0      # Count the number of points in the current multipart feature     #     partcount = feat.PartCount      # Enter while loop for each part in the feature (if a singlepart feature     # this will occur only once)     #     while partnum < partcount:         # Print the part number         #         print "Part " + str(partnum) + ":"         part = feat.GetPart(partnum)         pnt = part.Next()         pntcount = 0          # Enter while loop for each vertex         #         while pnt:             # Print x,y coordinates of current point             #             print pnt.x, pnt.y, pnt.z             pnt = part.Next()             pntcount += 1              # If pnt is null, either the part is finished or there is an              #   interior ring             #             if not pnt:                  pnt = part.Next()                 if pnt:                     print "Interior Ring:"         partnum += 1      row = rows.Next()
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
Did you create the empty list?  It's hard to see the bold in the pasted code before.  Here is the subset of it:

while row:     list = []

View solution in original post

0 Kudos
14 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Daniel,

Are you looking to return only the MIN and MAX Z values?  If so, you can create a list, append the Z values to the list, sort the list and then print out the min and max values.  Ex:  (lines in bold)
# Import native arcgisscripting module
#
import arcgisscripting

# Create the geoprocessor object
#
gp = arcgisscripting.create(9.3)

infc = gp.GetParameterAsText(0)
 

# Identify the geometry field
#
desc = gp.Describe(infc)
shapefieldname = desc.ShapeFieldName

# Create search cursor
#
rows = gp.SearchCursor(infc)
row = rows.Next()

# Enter while loop for each feature/row
#
while row:
    list = []
    # Create the geometry object
    #
    feat = row.GetValue(shapefieldname)

    # Print the current multipoint's ID
    #
    print "Feature " + str(row.getvalue(desc.OIDFieldName)) + ":"
    partnum = 0

    # Count the number of points in the current multipart feature
    #
    partcount = feat.PartCount

    # Enter while loop for each part in the feature (if a singlepart feature
    # this will occur only once)
    #
    while partnum < partcount:
        # Print the part number
        #
        print "Part " + str(partnum) + ":"
        part = feat.GetPart(partnum)
        pnt = part.Next()
        pntcount = 0

        # Enter while loop for each vertex
        #
        while pnt:
            # Print x,y coordinates of current point
            ##print pnt.x, pnt.y, pnt.z
            pnt = part.Next()
            pntcount += 1

            # If pnt is null, either the part is finished or there is an 
            #   interior ring
            #
            if not pnt: 
                pnt = part.Next()
                if pnt:
                    print "Interior Ring:"
        partnum += 1
    list.sort()
    min = list[0]
    max = list[-1]
    print min, max

    row = rows.Next()
0 Kudos
DanielAbera
Occasional Contributor
Hi Jake,

Thanks for the reply. I did try that.  I get an error message that float object has no attribute append.  How did u do the append. What i did is ptn.z.append(list).
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Did you create the empty list?  It's hard to see the bold in the pasted code before.  Here is the subset of it:

while row:     list = []
0 Kudos
JasonScheirer
Esri Alum
minval, maxval = None, None
cur = gp.SearchCursor(infc)
for row in iter(cur.next, None):
   colval = row.getValue(field_name)
   if minval is None or colval < minval:
      minval = colval
   if maxval is None or colval > maxval:
      maxval = colval

del row
del cur
0 Kudos
DanielAbera
Occasional Contributor
Did you create the empty list?  It's hard to see the bold in the pasted code before.  Here is the subset of it:

while row:
    list = []



Yes, i used it. May be can be more specific where i should put the append, because there are 3 while loops and which one to append ( pnt or pnt.z ).

thanks again
0 Kudos
DanielAbera
Occasional Contributor
minval, maxval = None, None
cur = gp.SearchCursor(infc)
for row in iter(cur.next, None):
   colval = row.getValue(field_name)
   if minval is None or colval < minval:
      minval = colval
   if maxval is None or colval > maxval:
      maxval = colval

del row
del cur


Hi Jason,

Thanks for the reply

can you rewrite this so that it can be used to extract shape field values ( X, Y, & Z)

Thanks,
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Yes, i used it. May be can be more specific where i should put the append, because there are 3 while loops and which one to append ( pnt or pnt.z ).

thanks again


Looks like I forgot to include the append portion:
while pnt:
    # Print x,y coordinates of current point
    ##print pnt.x, pnt.y, pnt.z
    list.append(pnt.z)
    pnt = part.Next()
    pntcount += 1



So, the entire code I was able to get working is:

# Import native arcgisscripting module
#
import arcgisscripting

# Create the geoprocessor object
#
gp = arcgisscripting.create(9.3)

infc = gp.GetParameterAsText(0)
 

# Identify the geometry field
#
desc = gp.Describe(infc)
shapefieldname = desc.ShapeFieldName

# Create search cursor
#
rows = gp.SearchCursor(infc)
row = rows.Next()

# Enter while loop for each feature/row
#
while row:
    list = []
    # Create the geometry object
    #
    feat = row.GetValue(shapefieldname)

    # Print the current multipoint's ID
    #
    print "Feature " + str(row.getvalue(desc.OIDFieldName)) + ":"
    partnum = 0

    # Count the number of points in the current multipart feature
    #
    partcount = feat.PartCount

    # Enter while loop for each part in the feature (if a singlepart feature
    # this will occur only once)
    #
    while partnum < partcount:
        # Print the part number
        #
        print "Part " + str(partnum) + ":"
        part = feat.GetPart(partnum)
        pnt = part.Next()
        pntcount = 0

        # Enter while loop for each vertex
        #
        while pnt:
            # Print x,y coordinates of current point
            ##print pnt.x, pnt.y, pnt.z
            list.append(pnt.z)
            pnt = part.Next()
            pntcount += 1

            # If pnt is null, either the part is finished or there is an 
            #   interior ring
            #
            if not pnt: 
                pnt = part.Next()
                if pnt:
                    print "Interior Ring:"
        partnum += 1
    list.sort()
    min = list[0]
    max = list[-1]
    print min, max

    row = rows.Next()

del row, rows
0 Kudos
DanielAbera
Occasional Contributor
Looks like I forgot to include the append portion:
while pnt:
    # Print x,y coordinates of current point
    ##print pnt.x, pnt.y, pnt.z
    list.append(pnt.z)
    pnt = part.Next()
    pntcount += 1



So, the entire code I was able to get working is:

# Import native arcgisscripting module
#
import arcgisscripting

# Create the geoprocessor object
#
gp = arcgisscripting.create(9.3)

infc = gp.GetParameterAsText(0)
 

# Identify the geometry field
#
desc = gp.Describe(infc)
shapefieldname = desc.ShapeFieldName

# Create search cursor
#
rows = gp.SearchCursor(infc)
row = rows.Next()

# Enter while loop for each feature/row
#
while row:
    list = []
    # Create the geometry object
    #
    feat = row.GetValue(shapefieldname)

    # Print the current multipoint's ID
    #
    print "Feature " + str(row.getvalue(desc.OIDFieldName)) + ":"
    partnum = 0

    # Count the number of points in the current multipart feature
    #
    partcount = feat.PartCount

    # Enter while loop for each part in the feature (if a singlepart feature
    # this will occur only once)
    #
    while partnum < partcount:
        # Print the part number
        #
        print "Part " + str(partnum) + ":"
        part = feat.GetPart(partnum)
        pnt = part.Next()
        pntcount = 0

        # Enter while loop for each vertex
        #
        while pnt:
            # Print x,y coordinates of current point
            ##print pnt.x, pnt.y, pnt.z
            list.append(pnt.z)
            pnt = part.Next()
            pntcount += 1

            # If pnt is null, either the part is finished or there is an 
            #   interior ring
            #
            if not pnt: 
                pnt = part.Next()
                if pnt:
                    print "Interior Ring:"
        partnum += 1
    list.sort()
    min = list[0]
    max = list[-1]
    print min, max

    row = rows.Next()

del row, rows




Thanks alot it works !!!
I just made a little change to get just the min and max, so i put the code in the main loop as follows.
0 Kudos
DanielAbera
Occasional Contributor
Thanks alot it works !!!
I just made a little change to get just the min and max, so i put the code in the main loop as follows.




# Identify the geometry field
#
desc = gp.Describe(infc)
shapefieldname = desc.ShapeFieldName

# Create search cursor
#
rows = gp.SearchCursor(infc)
row = rows.Next()

# Enter while loop for each feature/row
list = []
while row:
   
    # Create the geometry object
    #
    feat = row.GetValue(shapefieldname)

    # Print the current multipoint's ID
    #
    
    partnum = 0

    # Count the number of points in the current multipart feature
    #
    partcount = feat.PartCount

    # Enter while loop for each part in the feature (if a singlepart feature
    # this will occur only once)
    #
    while partnum < partcount:
        # Print the part number
        #
        
        part = feat.GetPart(partnum)
        pnt = part.Next()
        pntcount = 0

        # Enter while loop for each vertex
       
        while pnt:
            # Print x,y coordinates of current point
            ##print pnt.x, pnt.y, pnt.z
            list.append(pnt.z)
            pnt = part.Next()
            pntcount += 1

            # If pnt is null, either the part is finished or there is an 
            #   interior ring
            #
            if not pnt: 
                pnt = part.Next()
                if pnt:
                    print "Interior Ring:"
        partnum += 1
    row = rows.Next()
list.sort()
min = list[0]
max = list[-1]
print min, max
   

del row, rows
0 Kudos