1 | /* findentrances.cc |
---|
2 | * Simple converter from survex .3d files to a list of entrances in GPX format |
---|
3 | * |
---|
4 | * Copyright (C) 2012 Olaf Kähler |
---|
5 | * Copyright (C) 2012,2013 Olly Betts |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or modify |
---|
8 | * it under the terms of the GNU General Public License as published by |
---|
9 | * the Free Software Foundation; either version 2 of the License, or |
---|
10 | * (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
20 | */ |
---|
21 | |
---|
22 | /* |
---|
23 | * This program parses the survex file, creates a list of entrances and does |
---|
24 | * the coordinate transformations from almost arbitrary formats into WGS84 |
---|
25 | * using the PROJ.4 library. The output is then written as a GPX file ready |
---|
26 | * for use in your favourite GPS software. Everything else is kept as simple |
---|
27 | * and minimalistic as possible. |
---|
28 | * |
---|
29 | * Usage: |
---|
30 | * findentrances -d <+proj +datum +string> <input.3d> |
---|
31 | * |
---|
32 | * Example for data given in BMN M31 (Totes Gebirge, Austria): |
---|
33 | * findentrances -d '+proj=tmerc +lat_0=0 +lon_0=13d20 +k=1 +x_0=0 +y_0=-5200000 +ellps=bessel +towgs84=577.326,90.129,463.919,5.137,1.474,5.297,2.4232' cucc_austria.3d > ent.gpx |
---|
34 | * |
---|
35 | * Example for data given in british grid SD (Yorkshire): |
---|
36 | * findentrances -d '+proj=tmerc +lat_0=49d +lon_0=-2d +k=0.999601 +x_0=100000 +y_0=-500000 +ellps=airy +towgs84=375,-111,431,0,0,0,0' yorkshire/all.3d > ent.gpx |
---|
37 | * |
---|
38 | * Example for data given as proper british grid reference: |
---|
39 | * findentrances -d '+proj=tmerc +lat_0=49d +lon_0=-2d +k=0.999601 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=375,-111,431,0,0,0,0' all.3d > ent.gpx |
---|
40 | * |
---|
41 | */ |
---|
42 | |
---|
43 | #include <stdio.h> |
---|
44 | #include <string> |
---|
45 | #include <vector> |
---|
46 | #include <algorithm> |
---|
47 | #include <math.h> |
---|
48 | |
---|
49 | #include <proj_api.h> |
---|
50 | |
---|
51 | #include "message.h" |
---|
52 | #include "cmdline.h" |
---|
53 | #include "img_hosted.h" |
---|
54 | |
---|
55 | using namespace std; |
---|
56 | |
---|
57 | #define WGS84_DATUM_STRING "+proj=longlat +ellps=WGS84 +datum=WGS84" |
---|
58 | |
---|
59 | struct Point { |
---|
60 | string label; |
---|
61 | double x, y, z; |
---|
62 | }; |
---|
63 | |
---|
64 | static void |
---|
65 | read_survey(const char *filename, vector<Point> & points) |
---|
66 | { |
---|
67 | img *survey = img_open_survey(filename, NULL); |
---|
68 | if (!survey) { |
---|
69 | fatalerror(img_error2msg(img_error()), filename); |
---|
70 | } |
---|
71 | |
---|
72 | int result; |
---|
73 | do { |
---|
74 | img_point pt; |
---|
75 | result = img_read_item(survey, &pt); |
---|
76 | |
---|
77 | if (result == img_LABEL) { |
---|
78 | if (survey->flags & img_SFLAG_ENTRANCE) { |
---|
79 | Point newPt; |
---|
80 | newPt.label.assign(survey->label); |
---|
81 | newPt.x = pt.x; |
---|
82 | newPt.y = pt.y; |
---|
83 | newPt.z = pt.z; |
---|
84 | points.push_back(newPt); |
---|
85 | } |
---|
86 | } else if (result == img_BAD) { |
---|
87 | img_close(survey); |
---|
88 | fatalerror(img_error2msg(img_error()), filename); |
---|
89 | } |
---|
90 | } while (result != img_STOP); |
---|
91 | |
---|
92 | img_close(survey); |
---|
93 | } |
---|
94 | |
---|
95 | static void |
---|
96 | convert_coordinates(vector<Point> & points, const char *inputDatum, const char *outputDatum) |
---|
97 | { |
---|
98 | projPJ pj_input, pj_output; |
---|
99 | if (!(pj_input = pj_init_plus(inputDatum))) { |
---|
100 | fatalerror(/*Failed to initialise input coordinate system “%s”*/287, inputDatum); |
---|
101 | } |
---|
102 | if (!(pj_output = pj_init_plus(outputDatum))) { |
---|
103 | fatalerror(/*Failed to initialise output coordinate system “%s”*/288, outputDatum); |
---|
104 | } |
---|
105 | |
---|
106 | for (size_t i=0; i<points.size(); ++i) { |
---|
107 | pj_transform(pj_input, pj_output, 1, 1, &(points[i].x), &(points[i].y), &(points[i].z)); |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | struct SortPointsByLabel { |
---|
112 | bool operator()(const Point & a, const Point & b) |
---|
113 | { return a.label < b.label; } |
---|
114 | } SortPointsByLabel; |
---|
115 | |
---|
116 | static void |
---|
117 | sort_points(vector<Point> & points) |
---|
118 | { |
---|
119 | sort(points.begin(), points.end(), SortPointsByLabel); |
---|
120 | } |
---|
121 | |
---|
122 | static void |
---|
123 | write_gpx(const vector<Point> & points, FILE *file) |
---|
124 | { |
---|
125 | fprintf(file, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<gpx version=\"1.0\" creator=\"survex - findentrances\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.topografix.com/GPX/1/0\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">\n"); |
---|
126 | for (size_t i=0; i<points.size(); ++i) { |
---|
127 | const Point & pt = points[i]; |
---|
128 | // %.8f is at worst just over 1mm. |
---|
129 | fprintf(file, "<wpt lon=\"%.8f\" lat=\"%.8f\"><ele>%.2f</ele><name>%s</name></wpt>\n", pt.x*180.0/M_PI, pt.y*180.0/M_PI, pt.z, pt.label.c_str()); |
---|
130 | } |
---|
131 | |
---|
132 | fprintf(file, "</gpx>\n"); |
---|
133 | } |
---|
134 | |
---|
135 | static const struct option long_opts[] = { |
---|
136 | {"datum", required_argument, 0, 'd'}, |
---|
137 | {"help", no_argument, 0, HLP_HELP}, |
---|
138 | {"version", no_argument, 0, HLP_VERSION}, |
---|
139 | {0, 0, 0, 0} |
---|
140 | }; |
---|
141 | |
---|
142 | static const char *short_opts = "d:"; |
---|
143 | |
---|
144 | static struct help_msg help[] = { |
---|
145 | /* <-- */ |
---|
146 | /* TRANSLATORS: The PROJ library is used to do coordinate transformations |
---|
147 | * (https://trac.osgeo.org/proj/) - the user passes a string to tell PROJ |
---|
148 | * what the input datum is. */ |
---|
149 | {HLP_ENCODELONG(0), /*input datum as string to pass to PROJ*/389, 0}, |
---|
150 | {0, 0, 0} |
---|
151 | }; |
---|
152 | |
---|
153 | int main(int argc, char **argv) |
---|
154 | { |
---|
155 | msg_init(argv); |
---|
156 | |
---|
157 | const char *datum_string = NULL; |
---|
158 | cmdline_set_syntax_message(/*-d PROJ_DATUM 3D_FILE*/388, 0, NULL); |
---|
159 | cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, 1); |
---|
160 | while (1) { |
---|
161 | int opt = cmdline_getopt(); |
---|
162 | if (opt == EOF) break; |
---|
163 | else if (opt == 'd') datum_string = optarg; |
---|
164 | } |
---|
165 | |
---|
166 | if (!datum_string) { |
---|
167 | cmdline_syntax(); |
---|
168 | exit(1); |
---|
169 | } |
---|
170 | |
---|
171 | const char *survey_filename = argv[optind]; |
---|
172 | |
---|
173 | vector<Point> points; |
---|
174 | read_survey(survey_filename, points); |
---|
175 | convert_coordinates(points, datum_string, WGS84_DATUM_STRING); |
---|
176 | sort_points(points); |
---|
177 | write_gpx(points, stdout); |
---|
178 | } |
---|