1 | """ |
---|
2 | Author: John Jakeman |
---|
3 | Created: 12/12/2005 |
---|
4 | Program used to convert a commer seperated .xya file consiting of |
---|
5 | X and Y corrdinates in degrees and an elevation in meters into an .xya file |
---|
6 | in which all quantities are measured in meters |
---|
7 | """ |
---|
8 | |
---|
9 | from Numeric import array, zeros, Float, asarray |
---|
10 | |
---|
11 | # Open .xya file to be converted |
---|
12 | filename = 'cairns.xya' |
---|
13 | fid = open(filename) |
---|
14 | |
---|
15 | # Skip first line |
---|
16 | line = fid.readline() |
---|
17 | |
---|
18 | # Read remaining lines |
---|
19 | lines = fid.readlines() |
---|
20 | fid.close() |
---|
21 | |
---|
22 | r_earth = 6.378135E+06 |
---|
23 | # Origin of cartesian system is the point (lat_origin, long_origin) |
---|
24 | lat_origin = 145.0 |
---|
25 | long_origin = -24.0 |
---|
26 | |
---|
27 | from math import cos, pi |
---|
28 | |
---|
29 | # fields[0]: latitude |
---|
30 | # fields[1]: longitude |
---|
31 | # fields[2]: elevation |
---|
32 | |
---|
33 | # Open .xya file to be used to store new coordinates in meters |
---|
34 | filename = 'cairns_test.xya' |
---|
35 | fid = open(filename, "w") |
---|
36 | fid.write('elevation\n') |
---|
37 | |
---|
38 | |
---|
39 | for i, line in enumerate(lines): |
---|
40 | fields = line.split(',') |
---|
41 | |
---|
42 | # Evalulates distance in X-direction from origin for points in |
---|
43 | # Western Hemispehere |
---|
44 | if float(fields[0]) < 0: |
---|
45 | X = (360.0+float(fields[0])-lat_origin)/360.0*abs(cos(float(fields[1])*pi/180.0))*r_earth*2.0*pi |
---|
46 | |
---|
47 | elif float(fields[0]) < lat_origin: # Only needed when lat_origin > 0 |
---|
48 | X = (360.0-float(fields[0])-lat_origin)/360.0*abs(cos(float(fields[1])*pi/180.0))*r_earth*2.0*pi |
---|
49 | |
---|
50 | # Evaluates distance from origin in X-direction for points in |
---|
51 | # Eastern Hemisphere |
---|
52 | else: |
---|
53 | X = (float(fields[0])-lat_origin)/360.0*abs(cos(float(fields[1])*pi/180.0))*r_earth*2.0*pi |
---|
54 | |
---|
55 | # Calulates distance in Y-direction from a point to origin |
---|
56 | Y = abs((float(fields[1])-long_origin)/360.0*2*pi*r_earth) |
---|
57 | if Y < 0 : |
---|
58 | Y = 2*pi*r_earth + Y |
---|
59 | |
---|
60 | # Write X and Y corrdinates and corresponding elevation to .xya file |
---|
61 | fid.write(repr(X) + ', ') |
---|
62 | fid.write(repr(Y) + ', ') |
---|
63 | fid.write(fields[2]) |
---|
64 | |
---|
65 | fid.close() |
---|