File: utm2ll.c

package info (click to toggle)
drawmap 2.5-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,176 kB
  • ctags: 681
  • sloc: ansic: 13,087; makefile: 90; sh: 38
file content (143 lines) | stat: -rw-r--r-- 4,555 bytes parent folder | download | duplicates (4)
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
/*
 * =========================================================================
 * utm2ll - A program to convert UTM coordinates to latitude/longitude coordinates
 * Copyright (c) 2000  Fred M. Erickson
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 * =========================================================================
 *
 *
 * Program to use Redfearn's formulas to convert UTM coordinates
 * to latitude/longitude geographical coordinates.
 *
 * There aren't a lot of comments in this program because it is
 * basically a wrapper that calls the appropriate conversion function
 * in the file utilities.c.  See the comments there for a description
 * of the conversion process.
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include "drawmap.h"


void
license(void)
{
	fprintf(stderr, "This program is free software; you can redistribute it and/or modify\n");
	fprintf(stderr, "it under the terms of the GNU General Public License as published by\n");
	fprintf(stderr, "the Free Software Foundation; either version 2, or (at your option)\n");
	fprintf(stderr, "any later version.\n\n");

	fprintf(stderr, "This program is distributed in the hope that it will be useful,\n");
	fprintf(stderr, "but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
	fprintf(stderr, "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n");
	fprintf(stderr, "GNU General Public License for more details.\n\n");

	fprintf(stderr, "You should have received a copy of the GNU General Public License\n");
	fprintf(stderr, "along with this program; if not, write to the Free Software\n");
	fprintf(stderr, "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n");
}

main(int argc, char *argv[])
{
	double utm_x, utm_y, longitude, latitude;
	long zone;
	long dtype;
	struct datum datum;

	if ((argc != 4) && (argc != 5))  {
		if ((argc == 2) && (argv[1][0] == '-') && (argv[1][1] == 'L'))  {
			license();
			exit(0);
		}
		fprintf(stderr, "Convert UTM coordinates to latitude/longitude coordinates.\n");
		fprintf(stderr, "Usage:  %s utm_x utm_y zone [nad27 | nad83 | wgs84]\n", argv[0]);
		fprintf(stderr, "The default is nad27\n.");
		exit(0);
	}
	utm_x = atof(argv[1]);
	utm_y = atof(argv[2]);
	zone = atoi(argv[3]);
	if (argc == 5)  {
		if (strcmp(argv[4], "nad27") == 0)  {
			dtype = 0;
		}
		else if (strcmp(argv[4], "nad83") == 0)  {
			dtype = 1;
		}
		else if (strcmp(argv[4], "wgs84") == 0)  {
			dtype = 2;
		}
		else  {
			fprintf(stderr, "Unknown datum specified.\n");
			fprintf(stderr, "Usage:  %s utm_x utm_y zone [nad27 | nad83 | wgs84]\n", argv[0]);
			fprintf(stderr, "Default is nad27.\n");
			exit(0);
		}
	}
	else  {
		dtype = 0;
	}


	if (dtype == 0)  {
		/* Fill in the datum parameters for NAD-27. */
		datum.a = NAD27_SEMIMAJOR;
		datum.b = NAD27_SEMIMINOR;
		datum.e_2 = NAD27_E_SQUARED;
		datum.f_inv = NAD27_F_INV;
		datum.k0 = UTM_K0;
		datum.a0 = NAD27_A0;
		datum.a2 = NAD27_A2;
		datum.a4 = NAD27_A4;
		datum.a6 = NAD27_A6;
	}
	else if (dtype == 1)  {
		/* Fill in the datum parameters for NAD-83. */
		datum.a = NAD83_SEMIMAJOR;
		datum.b = NAD83_SEMIMINOR;
		datum.e_2 = NAD83_E_SQUARED;
		datum.f_inv = NAD83_F_INV;
		datum.k0 = UTM_K0;
		datum.a0 = NAD83_A0;
		datum.a2 = NAD83_A2;
		datum.a4 = NAD83_A4;
		datum.a6 = NAD83_A6;
	}
	else  {
		/* Fill in the datum parameters for WGS-84. */
		datum.a = WGS84_SEMIMAJOR;
		datum.b = WGS84_SEMIMINOR;
		datum.e_2 = WGS84_E_SQUARED;
		datum.f_inv = WGS84_F_INV;
		datum.k0 = UTM_K0;
		datum.a0 = WGS84_A0;
		datum.a2 = WGS84_A2;
		datum.a4 = WGS84_A4;
		datum.a6 = WGS84_A6;
	}


	if (redfearn_inverse(&datum, utm_x, utm_y, zone, &latitude, &longitude) != 0)  {
		fprintf(stderr, "error in input parameters.\n");
		exit(0);
	}

//	fprintf(stdout, "(%.10g %.10g %d) ===> (%.10g %.10g)\n", utm_x, utm_y, zone, latitude, longitude);
	fprintf(stdout, "%.10g %.10g\n", latitude, longitude);
	exit(0);
}