This repository has been archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateGraticule.py
127 lines (110 loc) · 4.77 KB
/
CreateGraticule.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# -----------------------------------------------
# Name: Create Graticule
# Purpose: Creates graticule of desired interval ranging from 1 second to 30 degrees
# Author: James M Roden
# Created: Feb 2017
# ArcGIS Version: 10.3
# Python Version 2.6
# PEP8
# -----------------------------------------------
try:
import arcpy
import sys
import traceback
def dms_to_dd(deg, mins, secs):
"""Convert degrees, minutes and seconds into decimal degrees
Keyword arguments:
deg -- Degrees
mins -- minutes
secs -- seconds
"""
return deg + (mins / 60.0) + (secs / 3600.0)
# arcpy environment settings
arcpy.env.workspace = r'in_memory'
arcpy.env.scratchWorkspace = r'in_memory'
arcpy.env.overwriteOutput = True
# ArcGIS tool parameters
degrees = int(arcpy.GetParameterAsText(0)) # degrees
minutes = int(arcpy.GetParameterAsText(1)) # minutes
seconds = int(arcpy.GetParameterAsText(2)) # seconds
out_feature_class = arcpy.GetParameterAsText(3) # Output feature class
# Variables prepared for GP
mxd = arcpy.mapping.MapDocument("CURRENT")
data_frame = mxd.activeDataFrame
sr = data_frame.spatialReference
arcpy.env.outputCoordinateSystem = sr
extent = data_frame.extent
lower_left = extent.lowerLeft
upper_right = extent.upperRight
interval_param = dms_to_dd(degrees, minutes, seconds)
arcpy.AddMessage("Extent calculated.")
# Construct coordinate variables for Fishnet. Round so that they fall on a line of lat and long.
origin_coord = str(round(lower_left.X)) + ' ' + str(round(lower_left.Y))
corner_coord = str(round(upper_right.X)) + ' ' + str(round(upper_right.Y))
orient_coord = str(round(lower_left.X)) + ' ' + str(round(lower_left.Y + 10)) # Add 10 to Y for orientation
fishnet = arcpy.CreateFishnet_management("fishnet", origin_coord, orient_coord, interval_param, interval_param,
corner_coord=corner_coord, geometry_type="POLYLINE")
arcpy.AddMessage("Fishnet created.")
# Create fields and perform field calculation
arcpy.AddField_management(fishnet, "LINE", "TEXT", field_length=20)
arcpy.AddField_management(fishnet, "DMS", "TEXT", field_length=50)
line_codeblock = """def line_orientation(line):
firstX = line.firstPoint.X
firstY = line.firstPoint.Y
lastX = line.lastPoint.X
lastY = line.lastPoint.Y
if abs(firstX - lastX) < abs(firstY - lastY):
return "Longitude"
else:
return "Latitude"
"""
dms_codeblock = ("""def dd_to_dms (orientation, shape):
if orientation == "Latitude":
dd = round(shape.firstPoint.Y, 3)
elif orientation == "Longitude":
dd = round(shape.firstPoint.X, 3)
degrees = int(dd)
minutes = int(60 * (dd - degrees))
seconds = 60 * (60 * (dd - degrees) - minutes)
if orientation == "Longitude":
if degrees < 0:
direction = "W"
elif degrees > 0:
direction = "E"
else:
direction = ""
elif orientation == "Latitude":
if degrees < 0:
direction = "S"
elif degrees > 0:
direction = "N"
else:
direction = ""
notation = str(abs(degrees)) + ":" + str(abs(minutes))"""
"""+ ":" + str(abs(seconds)) + " " + direction
return notation
""")
arcpy.CalculateField_management(fishnet, "LINE", "line_orientation (!Shape!)",
"PYTHON_9.3", line_codeblock)
arcpy.CalculateField_management(fishnet, "DMS", "dd_to_dms (!LINE!, !Shape!)",
"PYTHON_9.3", dms_codeblock)
arcpy.AddMessage("Fields calculated.")
# Densify so there are enough intermediate vertices so graticule lines 'curve' when projected.
densify_distance = interval_param/3.00
arcpy.Densify_edit(fishnet, 'DISTANCE', densify_distance)
arcpy.AddMessage("Densify complete.")
# Create output feature class
arcpy.CopyFeatures_management(fishnet, out_feature_class)
except:
tb = sys.exc_info()[2] # Traceback object
tbinfo = traceback.format_tb(tb)[0] # Traceback string
# Concatenate error information and return to GP window
pymsg = ('PYTHON ERRORS:\nTraceback info:\n' + tbinfo + '\nError Info: \n'
+ str(sys.exc_info()[1]))
msgs = 'ArcPy ERRORS:\n' + arcpy.GetMessage(2) + '\n'
arcpy.AddError(msgs)
print pymsg
finally:
# Delete in_memory
arcpy.Delete_management('in_memory')
# End of script