-
Notifications
You must be signed in to change notification settings - Fork 2
/
MapRotateNorth.py
171 lines (129 loc) · 4.73 KB
/
MapRotateNorth.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# MapRotateNorth.py
# Rotate map display frame north
# Steve Peaslee 2018-02-10
## ===================================================================================
class MyError(Exception):
pass
## ===================================================================================
def PrintMsg(msg, severity=0):
# prints message to screen if run as a python script
# Adds tool message to the geoprocessor
#
#Split the message on \n first, so that if it's multiple lines, a GPMessage will be added for each line
try:
for string in msg.split('\n'):
#Add a geoprocessing message (in case this is run as a tool)
if severity == 0:
arcpy.AddMessage(string)
elif severity == 1:
arcpy.AddWarning(string)
elif severity == 2:
arcpy.AddMessage(" ")
arcpy.AddError(string)
except:
pass
## ===================================================================================
def errorMsg():
try:
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
theMsg = tbinfo + "\n" + str(sys.exc_type)+ ": " + str(sys.exc_value)
PrintMsg(theMsg, 2)
except:
PrintMsg("Unhandled error in unHandledException method", 2)
pass
## ===================================================================================
def Number_Format(num, places=0, bCommas=True):
try:
# Format a number according to locality and given places
#locale.setlocale(locale.LC_ALL, "")
if bCommas:
theNumber = locale.format("%.*f", (places, num), True)
else:
theNumber = locale.format("%.*f", (places, num), False)
return theNumber
except:
errorMsg()
return "???"
## ===================================================================================
def OffsetAngle(a, b, c):
# Given 3 points [x, y, z], calculate the angle formed
# Create vectors from points
# Each point is a list [x, y, z]
#
try:
angle = 0
ba = [ aa-bb for aa,bb in zip(a,b) ]
bc = [ cc-bb for cc,bb in zip(c,b) ]
# Normalize vector
nba = math.sqrt ( sum ( (x**2.0 for x in ba) ) )
ba = [ x/nba for x in ba ]
nbc = math.sqrt ( sum ( (x**2.0 for x in bc) ) )
bc = [ x/nbc for x in bc ]
# Calculate scalar from normalized vectors
scale = sum ( (aa*bb for aa,bb in zip(ba,bc)) )
# calculate the angle in radian
radians = math.acos(scale)
# Get the sign
if (c[0] - a[0]) == 0:
s = 0
else:
s = ( c[0] - a[0] ) / abs(c[0] - a[0])
angle = s * ( -1.0 * round(math.degrees(radians), 1) )
return angle
# A-----C
# | /
# | /
# | /
# | /
# |/
# B
except MyError, e:
# Example: raise MyError, "This is an error message"
PrintMsg(str(e), 2)
return angle
except:
errorMsg()
return angle
## ===================================================================================
## ====================================== Main Body ==================================
# Import modules
import sys, os, math, traceback
from arcpy import env
try:
mxd = arcpy.mapping.MapDocument("CURRENT")
df = mxd.activeDataFrame
dfSR = df.spatialReference
rotation = df.rotation
extent = df.extent # XMin...
# Calculate center of display
xCntr = ( extent.XMin + extent.XMax ) / 2.0
yCntr = ( extent.YMin + extent.YMax ) / 2.0
dfPoint1 = arcpy.Point(xCntr, yCntr)
pointGeometry = arcpy.PointGeometry(dfPoint1, dfSR)
# Create same point but as Geographic WGS1984
# Designed to handle dataframe coordinate system datums: NAD1983 or WGS1984.
#
outputSR = arcpy.SpatialReference(4326) # GCS WGS 1984
env.geographicTransformations = "WGS_1984_(ITRF00)_To_NAD_1983"
pointGM = pointGeometry.projectAs(outputSR, "")
pointGM1 = pointGM.firstPoint
wgsX1 = pointGM1.X
wgsY2 = pointGM1.Y + 1.0
offsetPoint = arcpy.Point(wgsX1, wgsY2)
# Project north offset back to dataframe coordinate system
offsetGM = arcpy.PointGeometry(offsetPoint, outputSR)
dfOffset = offsetGM.projectAs(dfSR, "")
dfPoint2 = dfOffset.firstPoint
A = [dfPoint2.X, dfPoint2.Y, 0.0]
B = [xCntr, yCntr, 0.0]
C = [xCntr, (yCntr + 1000.0), 0.0]
angle = OffsetAngle(A, B, C)
PrintMsg(" \nOffset Angle: " + str(angle) + " degrees \n ", 0)
df.rotation = angle
arcpy.RefreshActiveView()
except MyError, e:
# Example: raise MyError, "This is an error message"
PrintMsg(str(e), 2)
except:
errorMsg()