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 | def convert_latlon_to_xycoords(lat,longt,origin): |
---|

10 | |
---|

11 | from math import cos, pi |
---|

12 | r_earth = 6.378135E+06 |
---|

13 | |
---|

14 | lat_origin,long_origin = origin |
---|

15 | # Evalulates distance in X-direction from origin for points in |
---|

16 | # Western Hemispehere |
---|

17 | if lat < 0: |
---|

18 | X = (360.0+lat-lat_origin)/360.0*abs(cos(longt*pi/180.0))*r_earth*2.0*pi |
---|

19 | |
---|

20 | elif lat < lat_origin: # Only needed when lat_origin > 0 |
---|

21 | X = (360.0-lat-lat_origin)/360.0*abs(cos(longt*pi/180.0))*r_earth*2.0*pi |
---|

22 | |
---|

23 | # Evaluates distance from origin in X-direction for points in |
---|

24 | # Eastern Hemisphere |
---|

25 | else: |
---|

26 | X = (lat-lat_origin)/360.0*abs(cos(longt*pi/180.0))*r_earth*2.0*pi |
---|

27 | # Calulates distance in Y-direction from a point to origin |
---|

28 | Y = abs((longt-long_origin)/360.0*2*pi*r_earth) |
---|

29 | if Y < 0 : |
---|

30 | Y = 2*pi*r_earth + Y |
---|

31 | |
---|

32 | return [X,Y] |
---|

33 | |
---|

34 | """ |
---|

35 | from Numeric import array, zeros, Float, asarray |
---|

36 | |
---|

37 | # Open .xya file to be converted |
---|

38 | filename = 'cairns_degrees.xya' |
---|

39 | fid = open(filename) |
---|

40 | |
---|

41 | # Skip first line |
---|

42 | line = fid.readline() |
---|

43 | |
---|

44 | # Read remaining lines |
---|

45 | lines = fid.readlines() |
---|

46 | fid.close() |
---|

47 | |
---|

48 | # Origin of cartesian system is the point (lat_origin, long_origin) |
---|

49 | lat_origin = 145.0 |
---|

50 | long_origin = -24.0 |
---|

51 | |
---|

52 | origin = lat_origin, long_origin |
---|

53 | |
---|

54 | # fields[0]: latitude |
---|

55 | # fields[1]: longitude |
---|

56 | # fields[2]: elevation |
---|

57 | |
---|

58 | # Open .xya file to be used to store new coordinates in meters |
---|

59 | filename = 'cairns_test.xya' |
---|

60 | fid = open(filename, "w") |
---|

61 | fid.write('elevation\n') |
---|

62 | |
---|

63 | |
---|

64 | for i, line in enumerate(lines): |
---|

65 | fields = line.split(',') |
---|

66 | |
---|

67 | X,Y = convert_latlon_to_xycoords(float(fields[0]),float(fields[1]),origin) |
---|

68 | |
---|

69 | # Write X and Y corrdinates and corresponding elevation to .xya file |
---|

70 | fid.write(repr(X) + ', ') |
---|

71 | fid.write(repr(Y) + ', ') |
---|

72 | fid.write(fields[2]) |
---|

73 | |
---|

74 | fid.close() |
---|

75 | """ |
---|