using Z and M feature geometry to create line graph in Python?

3478
1
06-18-2014 09:07 AM
by Anonymous User
Not applicable
Original User: willfish

Hello, I am a Python (v2.6.5) noob trying to plot a series of 3D polylines into graphs. So far I've come up with:

 
    # Import system modules
    import arcpy, os, sys, string
    from arcpy import env

    print "Modules imported."

    # Set environment settings
    arcpy.env.overwriteOutput = True
    arcpy.env.workspace = r"X:\HaulRoad\SpatialData"
    arcpy.env.scratchWorkspace = r"X:\HaulRoad\SpatialData"

    print "Workspaces set."

    # Set local variables
    infc = "\HaulRd.gdb\Treatment\XS_2009BEBRklnTerr_interpZ_Rte_isect"
    graph_template = r"\XS\XS_Seg02.55.grf"  # template graph originally created with 3d Analyst profile tool, customized in Advanced Properties, then exported to grf format

    print "Local variables set."


    #create list of fields of interest from FC or table of interest
    fields = ['SegID','STA_calc','shape.Z','shape.M']


    #create empty list for cursor to populate
    list = []

    print "Table and empty list created."

    # use search cursor to get field value for a given record 
    rows = arcpy.SearchCursor(infc, "", "", "SegID; STA_calc")
    for row in rows:
        list.append(row.SegID)
        list.append(row.STA_calc)

    del row, rows

    print "Rows appended to temporary table.  List deleted."

    #remove duplicates from list
    list = dict.fromkeys(list)
    list = list.keys()

    print "Duplicates removed."

    #create a temporary memory variable
    memoryFC = "in_memory" + "\\" + "virtualFC"
    print "Memory Feature created."

    for n in list:
        arcpy.TableSelect_analysis(infc, memoryFC, "STA_calc = " + str(n))
        out_Seg = fields[0]
        out_STA = fields[1]
        out_graph_name = n
        out_graph_pdf = r"\XS\Script\XS_chart_Seg" + str(out_Seg) + str(out_STA) + ".pdf"
        graph_data = memoryFC

        # Create temporary feature layer 
        arcpy.MakeFeatureLayer_management(infc, "XS_lyr")

        # Create the graph from temporary feature layer
        graph = arcpy.Graph()

        # Specify the title of the Graph
        graph.graphPropsGeneral.title = "Segment " + out_Seg

        # Specify the subtitle of the Graph
        graph.graphPropsGeneral.subtitle = "Station " + out_STA

        # Specify the title of the left axis
        graph.graphAxis[0].title = "Elevation (ft)"

        # Specify the title of the bottom axis
        graph.graphAxis[2].title = "Station (ft)"

        # Add a vertical bar series to the graph
        graph.addSeriesLineVertical("XS_lyr", shape.Z, shape.M)

        # Output a graph, which is created in-memory
        arcpy.MakeGraph_management(graph_template, graph, out_graph_name)

        # Save the graph as an PDF
        arcpy.SaveGraph_management(out_graph_name, out_graph_pdf, "MAINTAIN_ASPECT_RATIO", 800, 600)

    #clean in-memory
    arcpy.Delete_management("in_memory")

    print "PDFs created and memory cleaned."


I get an error in line 82 where I am adding the vertical line series. I assume the problem is that I am trying to plot values based on feature geometry as opposed to values in the attribute table.

The source data is a polyline feature class with Z and M values in an Arc 10.0 SP4 file geodatabase.  The features display properly in ArcScene based on their own elevation values and I'm able to locate other features along the lines (routes), so I'm pretty sure the Z and M geometry is solid. 

Is what I'm attempting possible without some intermediate conversion (e.g. converting vertices to points)? 

Thank-you,
Will

PS.  I'm also trying to get chart titles and subtitles to dynamically update, but haven't gotten to testing that yet.
0 Kudos
1 Reply
XanderBakker
Esri Esteemed Contributor
Hi Will,

As far as I can see it is not possible to do this using the interface of ArcGIS. This normally means that you won't be able to do so using Python code. The easiest way would be to add the M and Z values as attribute. This way you can still use your code.

Another possibility is to use matplotlib (pyplot), but this requires an additional installation (http://matplotlib.org/downloads.html, which is free). To give you an idea of how the code would look, see the (simplified) sample below:

import arcpy
import os
import matplotlib.pyplot as plt
from arcpy import env

infc = "\HaulRd.gdb\Treatment\XS_2009BEBRklnTerr_interpZ_Rte_isect"
flds = ('SHAPE@Z','SHAPE@M')

x = [] # Z
y = [] # M
outFolder = r'C:\path\to\output\folder'
outFile = os.path.join(outFolder, "NameForThisProfile.png")

fig = plt.figure(figsize=(10, 5)) # size in inches

with arcpy.da.SearchCursor(infc, flds) as rows:
    for row in rows:
        x.append(row[0]) # Z
        y.append(row[1]) # M
plt.plot(x,y,'r',linewidth=1.0)
plt.plot(x,y,'ro',alpha=0.5)
plt.xlabel('x label text')
plt.ylabel('y label text')
plt.title('Give the graph a title')
fig.savefig(outFile, dpi=300)


Kind regards,

Xander
0 Kudos