-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoordinates.py
74 lines (63 loc) · 2.5 KB
/
coordinates.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
from grand.coordinates import CartesianPoint, CartesianPoints, GeodeticPoint, \
GeodeticPoints
import numpy
# A nx3 numpy array can be viewed as a coordinates type.
array = numpy.ones((2, 3))
points = array.view(CartesianPoints)
# Note that this is equivalent to a cast. I.e. array and points refer to the
# same data. See e.g. below.
assert(points.x[0] == 1)
points.z = (0, 2)
assert(array[1,2] == 2)
# New coordinates data are created with the constructor syntax. The constructor
# takes a single argument that can be:
#
# 1. An `int` specifying the size of the coordinates. The data are created empty
# i.e. with random values.
#
# 2. An nx3 numpy ndarray. The data of the array are copied.
#
# 3. An other coordinates object. The data are copied and converted to the
# new parametrization if needed, e.g. from Cartesian to geodetic.
#
# Note that in all three cases a new data array is created, I.e. not a
# reference.
#
# The example below creates an empty array of 10 geodetic points and sets its
# data to equal values. Then a new array of cartesian points is created from
# the latter.
geodetic = GeodeticPoints(10)
geodetic.latitude = 45
geodetic.longitude = 3
geodetic.height = 0
cartesian = CartesianPoints(geodetic)
print(cartesian.x[0]) # <= this is the corresponding ECEF x coordinates
# Note if a coordinates object is viewed as another coordinates object then
# the data are not converted. Thus, this is usually wrong.
cartesian = geodetic.view(CartesianPoints)
print(cartesian.x[0]) # <= this is still the latitude
# Similar operations are available for scalar coordinates instances. Though,
# whenever possible it is more efficient CPU wise to deal with coordinates
# packed as arrays.
# A 1d numpy.ndarray of size 3 can be viewed as a scalar coordinates object.
array = numpy.ones(3)
point = array.view(CartesianPoint)
# Note that as previously array and point refer to the same data. See e.g.
# below.
assert(point.x == 1)
point.z = 2
assert(array[2] == 2)
# New scalar coordinates are created with the constructor syntax. The
# constructor has two syntaxes.
#
# 1. Zero to three arguments are provided specifing the coordinates values.
# Unspecified coordinates are initialised to 0.
#
# 2. A coordinates object is provided as single argument. The data are copied
# and converted if needed.
#
# The example below creates an geodetic point and converts it to Cartesian
# (ECEF) coordinates.
geodetic = GeodeticPoint(45, 3, 0)
cartesian = CartesianPoint(geodetic)
print(cartesian.x)