Skip to content

Instantly share code, notes, and snippets.

@ragnarheidar
Last active February 10, 2016 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ragnarheidar/7b9a9f25078ac894dcfc to your computer and use it in GitHub Desktop.
Save ragnarheidar/7b9a9f25078ac894dcfc to your computer and use it in GitHub Desktop.
A python script tool that uses the ArcPy site-package to convert a polygon feature class to a specific text based output. Output includes parameters, number of vertices in each feature and X and Y coordinates of each vertex for each feature. The output is specific for the avalanche simulation software SAMOS.
# -*- coding: utf-8 -*-
""""
Tool Name: Starting Zones to SAMOS
Source Name: StartingZonesToSamos.py
Version: ArcGIS 10.3.1
Author: Icelandic Meteorology Office/Ragnar H. Thrastarson
Created: 2015-07-20
Description: A python script tool that uses the ArcPy site-package
to convert a polygon feature class to a specific text based
output. Output includes parameters, number of vertices in each
feature and X and Y coordinates of each vertex for each feature.
The output is specific for the avalanche simulation software SAMOS.
Parameters
1 Input feature class. Avalanche Starting zone, must be polygon
2 Output feature class. Text based output with REL extension
3 Snow depth in m
4 Snow density in kg/m3
5 Type of starting zone
"""
import arcpy
InputFC = arcpy.GetParameterAsText(0) # input feature class, tool parameter needs to be set to polygon only
OutputFile = arcpy.GetParameterAsText(1) # output file is a text file with or without the REL extension
Parameter1 = arcpy.GetParameterAsText(2) # Snow depth in m
Parameter2 = arcpy.GetParameterAsText(3) # Snow density in kg/m3
Parameter3 = arcpy.GetParameterAsText(4) # Type of starting zone
# define function
def FeatureToSamos():
with open(OutputFile, 'w') as f: # create and open output file in write mode
for row in arcpy.da.SearchCursor(InputFC, ["SHAPE@"]): # for each feature in feature class
f.write(str(Parameter1.replace(",", ".")) + " " + str(Parameter2.replace(",", ".")) + " " + str(Parameter3.replace(",", ".")) + "\n") # write parameters in first line and move to next line
for part in row[0]: # for each feature
vert_count = len(part) # create a variable with the number of vertices
f.write(str(vert_count) + "\n") # write the number of vertices and move to next line
for pnt in part: # for each node
f.write("{0} {1}".format(pnt.X, pnt.Y) + "\n") # write the coordinates of each node and move to next line
f.close() # save and close output file
# test if output file has REL extension
if OutputFile.lower()[-4:] == ".rel": # if yes, run function
FeatureToSamos()
else:
OutputFile = OutputFile + ".rel" # if no, add the REL extension and run function
FeatureToSamos()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment