Python script
The following Python script was written by me when I was working with the Northern Virginia Regional Park Authority mapping elevations at mileposts on the Washington & Old Dominion Railroad Trail. The purpose of the script was to automate certain tasks, such as deleting unnecessary fields, adding and populating needed fields, projecting the shapefile, and importing it into a geodatabase. Following each trip to the Washington & Old Dominion Railroad Trail to collect the data, I exported the data from my GPS device and ran this script on the resulting shapefile.
import arcpy, sys, os, traceback
from arcpy import env
arcpy.env.workspace = 'C:\\Users\\dvtarasov\\Desktop\\NorthernVA\\W&OD\\W&ODFinalUntouched11\\'
outpath = 'C:\\Users\\dvtarasov\\Desktop\\NorthernVA\\W&OD\\W&ODPresentation\\TrailFeatures.gdb'
elevations = "Elevations.shp"
highestpoint = "HighestPoint.shp"
infeet = "InFeet"
elevationsPrj = "ElevationsPrj.shp"
elevationsNew = outpath + os.sep + "Elevations"
metersExpression = "!altitude! * 0.3048"
dropFields = ["display", "symbol", "dist", "proximity", "color", "depth", "temp", "time", "wpt_class", "sub_class", "attrib",
"link", "state", "country", "address", "zip", "facility", "crossroad", "ete", "dtype", "filename", "magvar",
"geoidheigh", "desc_", "fix", "sat", "hdop", "vdop", "pdop", "ageofdgpsd", "dgpsid", "dir", "ltime"]
try:
# 1. Delete InFeet
arcpy.DeleteField_management(elevations, infeet)
print 'Field deleted.'
# 2. Append HighestPoint into Elevations
arcpy.Append_management(highestpoint, elevations, "TEST", "", "")
print 'Highest point shapefile appended to Elevations.shp.'
# 3. Project the shapefile
outCS = "C:\\Program Files (x86)\\ArcGIS\\Desktop10.0\\Coordinate Systems\\Projected Coordinate Systems\\State Plane\\NAD 1983 HARN (US Feet)\\NAD 1983 HARN StatePlane Virginia North FIPS 4501 (US Feet).prj"
transformation = "NAD_1983_HARN_To_WGS_1984"
arcpy.Project_management(elevations, elevationsPrj, outCS, transformation)
print 'Shapefile projected.'
# 4. Import the resulting shapefile into the geodatabase
arcpy.CopyFeatures_management(elevationsPrj, elevationsNew)
print 'Features copied.'
# 5. Delete all unnecessary fields
# The fields to be deleted are display, symbol, dist, proximity, color, depth, temp, time, wpt_class, sub_class, attrib, link,
# state, country, address, zip, facility, crossroad, ete, dtype, filename, magvar, geoidheigh, desc_, fix, sat, hdop, vdop, pdop,
# ageofdgpsd, dgpsid, ltime, and dir
arcpy.DeleteField_management(elevationsNew, dropFields)
print 'All unneeded fields deleted.'
# 6. Add a subtype field
arcpy.AddField_management(elevationsNew, "IsHighest", "SHORT")
print 'Subtype field added.'
arcpy.SetSubtypeField_management(elevationsNew, "IsHighest")
# 7. Add subtype codes
arcpy.AddSubtype_management(elevationsNew, "0", "other point")
arcpy.AddSubtype_management(elevationsNew, "1", "highest point")
arcpy.AddSubtype_management(elevationsNew, "2", "milepost")
arcpy.AddSubtype_management(elevationsNew, "3", "road crossing")
print 'Subtype codes added.'
# 8. Set the default subtype code
arcpy.SetDefaultSubtype_management(elevationsNew, "0")
print 'Default subtype code defined.'
# 9. Change the value of the IsHighest field for all records to 0
landmarks = arcpy.UpdateCursor(elevationsNew)
for landmark in landmarks:
landmark.IsHighest = 0
landmarks.updateRow(landmark)
del landmark, landmarks
# 10. Change the subtype value for the highest point
mileposts = arcpy.UpdateCursor(elevationsNew)
for milepost in mileposts:
if milepost.ident == "HighestPoint":
milepost.IsHighest = 1
mileposts.updateRow(milepost)
del milepost, mileposts
# 11. Rewrite the ident field for the trail's highest point to something more legible
summits = arcpy.UpdateCursor(elevationsNew)
for summit in summits:
if summit.ident == "HighestPoint":
summit.ident = "Trail's highest point"
summits.updateRow(summit)
del summit, summits
# 12. Convert the altitude of the trail's highest point from meters to feet
meters = arcpy.UpdateCursor(elevationsNew)
for meter in meters:
if meter.ident == "Trail's highest point":
meter.altitude *= 3.28084
meters.updateRow(meter)
del meter, meters
# 13. Add a meters field
arcpy.AddField_management(elevationsNew, "InMeters", "DOUBLE")
print 'Meters field added.'
# 14. Calculate the contents of the meters field
arcpy.CalculateField_management(elevationsNew, "InMeters", metersExpression, "PYTHON_9.3")
print 'Meters field calculated.'
# 15. Add a time mapped field
arcpy.AddField_management(elevationsNew, "TimeMapped", "TEXT")
# 16. Copy the contents of the Content field into the time mapped field and delete the Content field
mappingTimes = arcpy.UpdateCursor(elevationsNew)
for mappingTime in mappingTimes:
mappingTime.TimeMapped = mappingTime.comment
mappingTimes.updateRow(mappingTime)
del mappingTime, mappingTimes
arcpy.DeleteField_management(elevationsNew, "comment")
# 17. Delete Elevations.prj
arcpy.Delete_management("ElevationsPrj.shp")
print 'ElevationsPrj deleted.'
print 'All done!'
except:
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n " + str(sys.exc_type) + ": " + str(sys.exc_value) + "\n"
msgs = "ARCPY ERRORS:\n" + arcpy.GetMessages(2) + "\n"
arcpy.AddError(msgs)
arcpy.AddError(pymsg)
print msgs
print pymsg
arcpy.AddMessage(arcpy.GetMessages(1))
print arcpy.GetMessages(1)
from arcpy import env
arcpy.env.workspace = 'C:\\Users\\dvtarasov\\Desktop\\NorthernVA\\W&OD\\W&ODFinalUntouched11\\'
outpath = 'C:\\Users\\dvtarasov\\Desktop\\NorthernVA\\W&OD\\W&ODPresentation\\TrailFeatures.gdb'
elevations = "Elevations.shp"
highestpoint = "HighestPoint.shp"
infeet = "InFeet"
elevationsPrj = "ElevationsPrj.shp"
elevationsNew = outpath + os.sep + "Elevations"
metersExpression = "!altitude! * 0.3048"
dropFields = ["display", "symbol", "dist", "proximity", "color", "depth", "temp", "time", "wpt_class", "sub_class", "attrib",
"link", "state", "country", "address", "zip", "facility", "crossroad", "ete", "dtype", "filename", "magvar",
"geoidheigh", "desc_", "fix", "sat", "hdop", "vdop", "pdop", "ageofdgpsd", "dgpsid", "dir", "ltime"]
try:
# 1. Delete InFeet
arcpy.DeleteField_management(elevations, infeet)
print 'Field deleted.'
# 2. Append HighestPoint into Elevations
arcpy.Append_management(highestpoint, elevations, "TEST", "", "")
print 'Highest point shapefile appended to Elevations.shp.'
# 3. Project the shapefile
outCS = "C:\\Program Files (x86)\\ArcGIS\\Desktop10.0\\Coordinate Systems\\Projected Coordinate Systems\\State Plane\\NAD 1983 HARN (US Feet)\\NAD 1983 HARN StatePlane Virginia North FIPS 4501 (US Feet).prj"
transformation = "NAD_1983_HARN_To_WGS_1984"
arcpy.Project_management(elevations, elevationsPrj, outCS, transformation)
print 'Shapefile projected.'
# 4. Import the resulting shapefile into the geodatabase
arcpy.CopyFeatures_management(elevationsPrj, elevationsNew)
print 'Features copied.'
# 5. Delete all unnecessary fields
# The fields to be deleted are display, symbol, dist, proximity, color, depth, temp, time, wpt_class, sub_class, attrib, link,
# state, country, address, zip, facility, crossroad, ete, dtype, filename, magvar, geoidheigh, desc_, fix, sat, hdop, vdop, pdop,
# ageofdgpsd, dgpsid, ltime, and dir
arcpy.DeleteField_management(elevationsNew, dropFields)
print 'All unneeded fields deleted.'
# 6. Add a subtype field
arcpy.AddField_management(elevationsNew, "IsHighest", "SHORT")
print 'Subtype field added.'
arcpy.SetSubtypeField_management(elevationsNew, "IsHighest")
# 7. Add subtype codes
arcpy.AddSubtype_management(elevationsNew, "0", "other point")
arcpy.AddSubtype_management(elevationsNew, "1", "highest point")
arcpy.AddSubtype_management(elevationsNew, "2", "milepost")
arcpy.AddSubtype_management(elevationsNew, "3", "road crossing")
print 'Subtype codes added.'
# 8. Set the default subtype code
arcpy.SetDefaultSubtype_management(elevationsNew, "0")
print 'Default subtype code defined.'
# 9. Change the value of the IsHighest field for all records to 0
landmarks = arcpy.UpdateCursor(elevationsNew)
for landmark in landmarks:
landmark.IsHighest = 0
landmarks.updateRow(landmark)
del landmark, landmarks
# 10. Change the subtype value for the highest point
mileposts = arcpy.UpdateCursor(elevationsNew)
for milepost in mileposts:
if milepost.ident == "HighestPoint":
milepost.IsHighest = 1
mileposts.updateRow(milepost)
del milepost, mileposts
# 11. Rewrite the ident field for the trail's highest point to something more legible
summits = arcpy.UpdateCursor(elevationsNew)
for summit in summits:
if summit.ident == "HighestPoint":
summit.ident = "Trail's highest point"
summits.updateRow(summit)
del summit, summits
# 12. Convert the altitude of the trail's highest point from meters to feet
meters = arcpy.UpdateCursor(elevationsNew)
for meter in meters:
if meter.ident == "Trail's highest point":
meter.altitude *= 3.28084
meters.updateRow(meter)
del meter, meters
# 13. Add a meters field
arcpy.AddField_management(elevationsNew, "InMeters", "DOUBLE")
print 'Meters field added.'
# 14. Calculate the contents of the meters field
arcpy.CalculateField_management(elevationsNew, "InMeters", metersExpression, "PYTHON_9.3")
print 'Meters field calculated.'
# 15. Add a time mapped field
arcpy.AddField_management(elevationsNew, "TimeMapped", "TEXT")
# 16. Copy the contents of the Content field into the time mapped field and delete the Content field
mappingTimes = arcpy.UpdateCursor(elevationsNew)
for mappingTime in mappingTimes:
mappingTime.TimeMapped = mappingTime.comment
mappingTimes.updateRow(mappingTime)
del mappingTime, mappingTimes
arcpy.DeleteField_management(elevationsNew, "comment")
# 17. Delete Elevations.prj
arcpy.Delete_management("ElevationsPrj.shp")
print 'ElevationsPrj deleted.'
print 'All done!'
except:
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n " + str(sys.exc_type) + ": " + str(sys.exc_value) + "\n"
msgs = "ARCPY ERRORS:\n" + arcpy.GetMessages(2) + "\n"
arcpy.AddError(msgs)
arcpy.AddError(pymsg)
print msgs
print pymsg
arcpy.AddMessage(arcpy.GetMessages(1))
print arcpy.GetMessages(1)