Back to all posts

Script example: Save point data to a text file

This script example demonstrates how to save point x and y coordinates along with another point attribute to a text file. The text file has xyz format, where each row includes x, y, and z data of a point. The z value could be any attribute value of a point. This example may be used as a reference on how to export model data to external text files.

Example

# export point data to text file of xyz format
def export_xyz_file(junctions, z_field, out_xyz_file):
  with open(out_xyz_file, 'wt') as f:
    for junc in junctions:
      x, y = junc['X'], junc['Y']  # obtain the X and Y fields
      z = junc[z_field]            # obtain the Z field 
      row = '{0},{1},{2}'.format(x, y, z)
      f.write('{0}\n'.format(row))

file = r'D:\Test\test.xyz'         # new text file to be saved. If already exists, the file will be overwritten
junctions = pcpy.Map.Layer['Junctions'].get_entities() # get all junction entities
export_xyz_file(junctions, 'InvertElev', file)         # export points to the file

print 'done'