[GIS] excluding NaN records from FOR loop in ArcPy

arcpyloop

I am trying to create an array of Z and M values for vertices from a polyline (Z and M enabled) feature class. Some of the vertices do no have M values/geometry and I wish to exclude them from the array.

    import arcpy
    from arcpy import env
    ##import math   #another way I was testing....commented-out
    import numpy as np

    print "Modules imported."

    # Set environment settings
    arcpy.env.overwriteOutput = True
    arcpy.env.workspace = r"E:\Projects\HaulRd\SpatialData"
    arcpy.env.scratchWorkspace = r"E:\Projects\HaulRd\SpatialData\Scratch"
    arcpy.env.outputZFlag = "Same As Input"
    arcpy.env.outputMFlag = "Same As Input"

    print "Workspaces and environments set."

    # Set local variables
    infc = "HaulRd_XS_linesZM_test.shp"
    ##x=float('nan') # another way I was testing.....commented out

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

    # Create search cursor
    #
    rows = arcpy.SearchCursor(infc)


    # Enter for loop for each feature/row
    #
    for row in rows:
        # Create the geometry object
        #
        feat = row.getValue(shapefieldname)

        # Print the current feature's ID
        #
        print "Feature %i:" % row.getValue(desc.OIDFieldName)
        partnum = 0

        # Step through each part of the feature
        #
        for part in feat:
            # Print the part number
            #
            print "Part %i:" % partnum

            # Step through each vertex in the feature
            #
            for pnt in feat.getPart(partnum):

                #exclude records with 'nan' (i.e. null)
                if pnt[~np.isnan(pnt).any(1)]

                    # Print z,m coordinates of current point
                    #
                    print pnt.Z, pnt.M

                else:
                    # If pnt is None, this represents an interior ring
                    #
                    print "Interior Ring:"
            partnum += 1 

I have made a number of attempts based on content I found in:

But I can't seem to figure-out how the syntax (could also be a logical problem) fits into my code.

Eventually, my temporary array will be be used to feed creation of vertical line graphs (see related post:

Using Z and M feature geometry to create line graph in ArcPy/Python?)

I am using Python v2.6.5.

Best Answer

I'm particularly fond of the try..except approach, which although nasty is one of the best ways of testing if a string is a number:

try:
  value = int(stringVar)
except:
  value = -1 # fail value

a similar approach will work with Z and M values, try to convert to float and if it fails then it doesn't have that particular value.

try:
  dont_care = int(pnt.Z)
  has_z = True
except:
  has_z = False

try:
  also_dont_care = int(pnt.m)
  has_m = True
except:
  has_m = False

if (has_z and has_m):
  #do your operations on Z and M values here

If the values are truly float falues then they will convert to int types readily, just without the decimal places, NaN and Inf types don't convert to int and raise the value error exception. You could also try an arithmetic operation like divide; valid values can be divided by a constant (say 2) but NaN and Inf cannot be divided and will raise an exception. I don't think it matters what the derived values and exceptions are, just that they have failed.

Related Question