0

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.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jun 20, 2014 at 16:37
1
  • have you tried with " if pnt: " Commented Jun 20, 2014 at 19:23

1 Answer 1

3

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.

answered Jun 21, 2014 at 15:06
2
  • +1 as this is probably the most pythonic approach to determining if a string is numeric. See this SO post for more. Commented Jun 21, 2014 at 15:25
  • Nice one @Paul, that looks very familiar, it's probably where I got it from. I went looking for the link (but not very hard) to include it. Thank you. Commented Jun 21, 2014 at 15:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.