Skip to content

Instantly share code, notes, and snippets.

@csmoore
Created April 4, 2014 00:36
Show Gist options
  • Save csmoore/9965706 to your computer and use it in GitHub Desktop.
Save csmoore/9965706 to your computer and use it in GitHub Desktop.
#------------------------------------------------------------------------------
# Copyright 2013 Esri
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#------------------------------------------------------------------------------
# TerrainCrossSection.py
# Description: Uses StackProfile tool then matplotlib to chart the Profile (Z vs M)
# (also known as a 3D Terrain Cross Section)
# Requirements: ArcGIS Geoprocessing + 3D + matplotlib
# Additional: Credit: Elise Acheson - Shown at: Esri Dev Summit 2014
# -----------------------------------------------------------------------------
import matplotlib.pyplot as plt
import os
import arcpy
# Set up parameters
curdir = arcpy.env.scratchFolder
filename = "chart.png"
out_filepath = os.path.join(curdir, filename)
out_filepath_clean = os.path.normcase(out_filepath)
inputLine = arcpy.GetParameter(0)
inputRaster = arcpy.GetParameter(1)
outputTable = "in_memory\\StackOutTable"
# Call StackProfile tool
arcpy.ddd.StackProfile(inputLine, inputRaster, outputTable, "#")
# Put measure and elevation data into two python arrays
m = []
z = []
with arcpy.da.SearchCursor(outputTable, ["FIRST_DIST", "FIRST_Z"]) as cursor:
for row in cursor:
m.append(row[0])
z.append(row[1])
# Matplotlib chart code
plt.plot(m, z)
plt.grid(True) # show a grid
plt.xlabel('Distance (m)') # x label text
plt.ylabel('Elevation (m)') # y label text
plt.title('Profile') # y label text
plt.savefig(out_filepath_clean, bbox_inches='tight')
arcpy.SetParameterAsText(2, out_filepath_clean)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment