1 | /* img.h |
---|
2 | * Header file for routines to read and write processed survey data files |
---|
3 | * |
---|
4 | * These routines support reading processed survey data in a variety of formats |
---|
5 | * - currently: |
---|
6 | * |
---|
7 | * - Survex ".3d" image files |
---|
8 | * - Survex ".pos" files |
---|
9 | * - Compass Plot files (".plt" and ".plf") |
---|
10 | * - CMAP XYZ files (".sht", ".adj", ".una", ".xyz") |
---|
11 | * |
---|
12 | * Writing Survex ".3d" image files is supported. |
---|
13 | * |
---|
14 | * Copyright (C) Olly Betts 1993,1994,1997,2001,2002,2003,2004,2005,2006,2010,2011,2012,2013,2014,2016,2018 |
---|
15 | * |
---|
16 | * This program is free software; you can redistribute it and/or modify |
---|
17 | * it under the terms of the GNU General Public License as published by |
---|
18 | * the Free Software Foundation; either version 2 of the License, or |
---|
19 | * (at your option) any later version. |
---|
20 | * |
---|
21 | * This program is distributed in the hope that it will be useful, |
---|
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
24 | * GNU General Public License for more details. |
---|
25 | * |
---|
26 | * You should have received a copy of the GNU General Public License |
---|
27 | * along with this program; if not, write to the Free Software |
---|
28 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
29 | */ |
---|
30 | |
---|
31 | #ifndef IMG_H |
---|
32 | # define IMG_H |
---|
33 | |
---|
34 | /* Define IMG_API_VERSION if you want more recent versions of the img API. |
---|
35 | * |
---|
36 | * 0 (default) The old API. date1 and date2 give the survey date as time_t. |
---|
37 | * Set to 0 for "unknown". |
---|
38 | * 1 days1 and days2 give survey dates as days since 1st Jan 1900. |
---|
39 | * Set to -1 for "unknown". |
---|
40 | */ |
---|
41 | #ifndef IMG_API_VERSION |
---|
42 | # define IMG_API_VERSION 0 |
---|
43 | #elif IMG_API_VERSION > 1 |
---|
44 | # error IMG_API_VERSION > 1 too new |
---|
45 | #endif |
---|
46 | |
---|
47 | #ifdef __cplusplus |
---|
48 | extern "C" { |
---|
49 | #endif |
---|
50 | |
---|
51 | #include <stdio.h> |
---|
52 | #include <time.h> /* for time_t */ |
---|
53 | |
---|
54 | # define img_BAD -2 |
---|
55 | # define img_STOP -1 |
---|
56 | # define img_MOVE 0 |
---|
57 | # define img_LINE 1 |
---|
58 | /* NB: img_CROSS is never output and ignored on input. |
---|
59 | * Put crosses where labels are. */ |
---|
60 | /* # define img_CROSS 2 */ |
---|
61 | # define img_LABEL 3 |
---|
62 | # define img_XSECT 4 |
---|
63 | # define img_XSECT_END 5 |
---|
64 | /* Loop closure information for the *preceding* traverse (img_MOVE + one or |
---|
65 | * more img_LINEs). */ |
---|
66 | # define img_ERROR_INFO 6 |
---|
67 | |
---|
68 | /* Leg flags */ |
---|
69 | # define img_FLAG_SURFACE 0x01 |
---|
70 | # define img_FLAG_DUPLICATE 0x02 |
---|
71 | # define img_FLAG_SPLAY 0x04 |
---|
72 | |
---|
73 | /* Station flags */ |
---|
74 | # define img_SFLAG_SURFACE 0x01 |
---|
75 | # define img_SFLAG_UNDERGROUND 0x02 |
---|
76 | # define img_SFLAG_ENTRANCE 0x04 |
---|
77 | # define img_SFLAG_EXPORTED 0x08 |
---|
78 | # define img_SFLAG_FIXED 0x10 |
---|
79 | # define img_SFLAG_ANON 0x20 |
---|
80 | # define img_SFLAG_WALL 0x40 |
---|
81 | |
---|
82 | /* File-wide flags */ |
---|
83 | # define img_FFLAG_EXTENDED 0x80 |
---|
84 | |
---|
85 | /* When writing img_XSECT, img_XFLAG_END in pimg->flags means this is the last |
---|
86 | * img_XSECT in this tube: |
---|
87 | */ |
---|
88 | # define img_XFLAG_END 0x01 |
---|
89 | |
---|
90 | # define img_STYLE_UNKNOWN -1 |
---|
91 | # define img_STYLE_NORMAL 0 |
---|
92 | # define img_STYLE_DIVING 1 |
---|
93 | # define img_STYLE_CARTESIAN 2 |
---|
94 | # define img_STYLE_CYLPOLAR 3 |
---|
95 | # define img_STYLE_NOSURVEY 4 |
---|
96 | |
---|
97 | /* 3D coordinates (in metres) */ |
---|
98 | typedef struct { |
---|
99 | double x, y, z; |
---|
100 | } img_point; |
---|
101 | |
---|
102 | typedef struct { |
---|
103 | /* Members you can access when reading (don't touch when writing): */ |
---|
104 | char *label; |
---|
105 | int flags; |
---|
106 | char *title; |
---|
107 | /* If the coordinate system was specified, this contains a PROJ4 string |
---|
108 | * describing it. If not, this member will be NULL. |
---|
109 | */ |
---|
110 | char *cs; |
---|
111 | /* Older .3d format versions stored a human readable datestamp string. |
---|
112 | * Format versions >= 8 versions store a string consisting of "@" followed |
---|
113 | * by the number of seconds since midnight UTC on 1/1/1970. Some foreign |
---|
114 | * formats contain a human readable string, others no date information |
---|
115 | * (which results in "?" being returned). |
---|
116 | */ |
---|
117 | char *datestamp; |
---|
118 | /* The datestamp as a time_t (or (time_t)-1 if not available). |
---|
119 | * |
---|
120 | * For 3d format versions >= 8, this is a reliable value and in UTC. Older |
---|
121 | * 3d format versions store a human readable time, which img will attempt |
---|
122 | * to decode, but it may fail, particularly with handling timezones. Even |
---|
123 | * if it does work, beware that times in local time where DST applies are |
---|
124 | * inherently ambiguous around when the clocks go back. |
---|
125 | * |
---|
126 | * CMAP XYZ files contain a timestamp. It's probably in localtime (but |
---|
127 | * without any timezone information) and the example files are all pre-2000 |
---|
128 | * and have two digit years. We do our best to turn these into a useful |
---|
129 | * time_t value. |
---|
130 | */ |
---|
131 | time_t datestamp_numeric; |
---|
132 | char separator; /* character used to separate survey levels ('.' usually) */ |
---|
133 | |
---|
134 | /* Members that can be set when writing: */ |
---|
135 | #if IMG_API_VERSION == 0 |
---|
136 | time_t date1, date2; |
---|
137 | #else /* IMG_API_VERSION == 1 */ |
---|
138 | int days1, days2; |
---|
139 | #endif |
---|
140 | double l, r, u, d; |
---|
141 | |
---|
142 | /* Error information - valid when img_ERROR_INFO is returned: */ |
---|
143 | int n_legs; |
---|
144 | double length; |
---|
145 | double E, H, V; |
---|
146 | |
---|
147 | /* The filename actually opened (e.g. may have ".3d" added). |
---|
148 | * |
---|
149 | * This is only set if img opened the filename - if an existing stream |
---|
150 | * is used (via img_read_stream() or similar) then this member will be |
---|
151 | * NULL. |
---|
152 | */ |
---|
153 | char * filename_opened; |
---|
154 | |
---|
155 | /* Non-zero if reading an extended elevation: */ |
---|
156 | int is_extended_elevation; |
---|
157 | |
---|
158 | /* Members that can be set when writing: */ |
---|
159 | /* The style of the data - one of the img_STYLE_* constants above */ |
---|
160 | int style; |
---|
161 | |
---|
162 | /* All other members are for internal use only: */ |
---|
163 | FILE *fh; /* file handle of image file */ |
---|
164 | int (*close_func)(FILE*); |
---|
165 | char *label_buf; |
---|
166 | size_t buf_len; |
---|
167 | size_t label_len; |
---|
168 | int fRead; /* 1 for reading, 0 for writing */ |
---|
169 | long start; |
---|
170 | /* version of file format: |
---|
171 | * -4 => CMAP .xyz file, shot format |
---|
172 | * -3 => CMAP .xyz file, station format |
---|
173 | * -2 => Compass .plt file |
---|
174 | * -1 => .pos file |
---|
175 | * 0 => 0.01 ascii |
---|
176 | * 1 => 0.01 binary, |
---|
177 | * 2 => byte actions and flags |
---|
178 | * 3 => prefixes for legs; compressed prefixes |
---|
179 | * 4 => survey date |
---|
180 | * 5 => LRUD info |
---|
181 | * 6 => error info |
---|
182 | * 7 => more compact dates with wider range |
---|
183 | * 8 => lots of changes |
---|
184 | */ |
---|
185 | int version; |
---|
186 | char *survey; |
---|
187 | size_t survey_len; |
---|
188 | int pending; /* for old style text format files and survey filtering */ |
---|
189 | img_point mv; |
---|
190 | #if IMG_API_VERSION == 0 |
---|
191 | time_t olddate1, olddate2; |
---|
192 | #else /* IMG_API_VERSION == 1 */ |
---|
193 | int olddays1, olddays2; |
---|
194 | #endif |
---|
195 | int oldstyle; |
---|
196 | } img; |
---|
197 | |
---|
198 | /* Which version of the file format to output (defaults to newest) */ |
---|
199 | extern unsigned int img_output_version; |
---|
200 | |
---|
201 | /* Minimum supported value for img_output_version: */ |
---|
202 | #define IMG_VERSION_MIN 1 |
---|
203 | |
---|
204 | /* Maximum supported value for img_output_version: */ |
---|
205 | #define IMG_VERSION_MAX 8 |
---|
206 | |
---|
207 | /* Open a processed survey data file for reading |
---|
208 | * |
---|
209 | * fnm is the filename |
---|
210 | * |
---|
211 | * Returns pointer to an img struct or NULL |
---|
212 | */ |
---|
213 | #define img_open(F) img_open_survey((F), NULL) |
---|
214 | |
---|
215 | /* Open a processed survey data file for reading |
---|
216 | * |
---|
217 | * fnm is the filename |
---|
218 | * |
---|
219 | * survey points to a survey name to restrict reading to (or NULL for all |
---|
220 | * survey data in the file) |
---|
221 | * |
---|
222 | * Returns pointer to an img struct or NULL |
---|
223 | */ |
---|
224 | img *img_open_survey(const char *fnm, const char *survey); |
---|
225 | |
---|
226 | /* Read processed survey data from an existing stream. |
---|
227 | * |
---|
228 | * stream is a FILE* open on the stream (can be NULL which will give error |
---|
229 | * IMG_FILENOTFOUND so you don't need to handle that case specially). The |
---|
230 | * stream should be opened for reading in binary mode and positioned at the |
---|
231 | * start of the survey data file. |
---|
232 | * |
---|
233 | * close_func is a function to call to close the stream (most commonly |
---|
234 | * fclose, or pclose if the stream was opened using popen()) or NULL if |
---|
235 | * the caller wants to take care of closing the stream. |
---|
236 | * |
---|
237 | * filename is used to determine the format based on the file extension, |
---|
238 | * and also the leafname with the extension removed is used for the survey |
---|
239 | * title for formats which don't support a title or when no title is |
---|
240 | * specified. If you're not interested in default titles, you can just |
---|
241 | * pass the extension including a leading "." - e.g. ".3d". May not be |
---|
242 | * NULL. |
---|
243 | * |
---|
244 | * Returns pointer to an img struct or NULL on error. Any close function |
---|
245 | * specified is called on error (unless stream is NULL). |
---|
246 | */ |
---|
247 | #define img_read_stream(S, C, F) img_read_stream_survey((S), (C), (F), NULL) |
---|
248 | |
---|
249 | /* Read processed survey data from an existing stream. |
---|
250 | * |
---|
251 | * stream is a FILE* open on the stream (can be NULL which will give error |
---|
252 | * IMG_FILENOTFOUND so you don't need to handle that case specially). The |
---|
253 | * stream should be opened for reading in binary mode and positioned at the |
---|
254 | * start of the survey data file. |
---|
255 | * |
---|
256 | * close_func is a function to call to close the stream (most commonly |
---|
257 | * fclose, or pclose if the stream was opened using popen()) or NULL if |
---|
258 | * the caller wants to take care of closing the stream. |
---|
259 | * |
---|
260 | * filename is used to determine the format based on the file extension, |
---|
261 | * and also the leafname with the extension removed is used for the survey |
---|
262 | * title for formats which don't support a title or when no title is |
---|
263 | * specified. If you're not interested in default titles, you can just |
---|
264 | * pass the extension including a leading "." - e.g. ".3d". filename must |
---|
265 | * not be NULL. |
---|
266 | * |
---|
267 | * survey points to a survey name to restrict reading to (or NULL for all |
---|
268 | * survey data in the file) |
---|
269 | * |
---|
270 | * Returns pointer to an img struct or NULL on error. Any close function |
---|
271 | * specified is called on error. |
---|
272 | */ |
---|
273 | img *img_read_stream_survey(FILE *stream, int (*close_func)(FILE*), |
---|
274 | const char *filename, |
---|
275 | const char *survey); |
---|
276 | |
---|
277 | /* Open a .3d file for output with no specified coordinate system |
---|
278 | * |
---|
279 | * This is a very thin wrapper around img_open_write_cs() which passes NULL for |
---|
280 | * cs, provided for compatibility with the API provided before support for |
---|
281 | * coordinate systems was added. |
---|
282 | * |
---|
283 | * See img_open_write_cs() for documentation. |
---|
284 | */ |
---|
285 | #define img_open_write(F, T, S) img_open_write_cs(F, T, NULL, S) |
---|
286 | |
---|
287 | /* Open a .3d file for output in a specified coordinate system |
---|
288 | * |
---|
289 | * fnm is the filename |
---|
290 | * |
---|
291 | * title is the title |
---|
292 | * |
---|
293 | * cs is a PROJ4 string describing the coordinate system (or NULL to not |
---|
294 | * specify a coordinate system). |
---|
295 | * |
---|
296 | * flags contains a bitwise-or of any file-wide flags - currently only one |
---|
297 | * is available: img_FFLAG_EXTENDED. |
---|
298 | * |
---|
299 | * Returns pointer to an img struct or NULL for error (check img_error() |
---|
300 | * for details) |
---|
301 | */ |
---|
302 | img *img_open_write_cs(const char *fnm, const char *title, const char * cs, |
---|
303 | int flags); |
---|
304 | |
---|
305 | /* Write a .3d file to a stream |
---|
306 | * |
---|
307 | * stream is a FILE* open on the stream (can be NULL which will give error |
---|
308 | * IMG_FILENOTFOUND so you don't need to handle that case specially). The |
---|
309 | * stream should be opened for writing in binary mode. |
---|
310 | * |
---|
311 | * close_func is a function to call to close the stream (most commonly |
---|
312 | * fclose, or pclose if the stream was opened using popen()) or NULL if |
---|
313 | * the caller wants to take care of closing the stream. |
---|
314 | * |
---|
315 | * title is the title |
---|
316 | * |
---|
317 | * cs is a PROJ4 string describing the coordinate system (or NULL to not |
---|
318 | * specify a coordinate system). |
---|
319 | * |
---|
320 | * flags contains a bitwise-or of any file-wide flags - currently only one |
---|
321 | * is available: img_FFLAG_EXTENDED. |
---|
322 | * |
---|
323 | * Returns pointer to an img struct or NULL for error (check img_error() |
---|
324 | * for details). Any close function specified is called on error (unless |
---|
325 | * stream is NULL). |
---|
326 | */ |
---|
327 | img *img_write_stream(FILE *stream, int (*close_func)(FILE*), |
---|
328 | const char *title, const char * cs, int flags); |
---|
329 | |
---|
330 | /* Read an item from a processed survey data file |
---|
331 | * |
---|
332 | * pimg is a pointer to an img struct returned by img_open() |
---|
333 | * |
---|
334 | * coordinates are returned in p |
---|
335 | * |
---|
336 | * flags and label name are returned in fields in pimg |
---|
337 | * |
---|
338 | * Returns img_XXXX as #define-d above |
---|
339 | */ |
---|
340 | int img_read_item(img *pimg, img_point *p); |
---|
341 | |
---|
342 | /* Write a item to a .3d file |
---|
343 | * |
---|
344 | * pimg is a pointer to an img struct returned by img_open_write() |
---|
345 | * |
---|
346 | * code is one of the img_XXXX #define-d above |
---|
347 | * |
---|
348 | * flags is the leg, station, or xsect flags |
---|
349 | * (meaningful for img_LINE, img_LABEL, and img_XSECT respectively) |
---|
350 | * |
---|
351 | * s is the label (only meaningful for img_LABEL) |
---|
352 | * |
---|
353 | * x, y, z are the coordinates |
---|
354 | */ |
---|
355 | void img_write_item(img *pimg, int code, int flags, const char *s, |
---|
356 | double x, double y, double z); |
---|
357 | |
---|
358 | /* Write error information for the current traverse |
---|
359 | * |
---|
360 | * n_legs is the number of legs in the traverse |
---|
361 | * |
---|
362 | * length is the traverse length (in m) |
---|
363 | * |
---|
364 | * E is the ratio of the observed misclosure to the theoretical one |
---|
365 | * |
---|
366 | * H is the ratio of the observed horizontal misclosure to the theoretical one |
---|
367 | * |
---|
368 | * V is the ratio of the observed vertical misclosure to the theoretical one |
---|
369 | */ |
---|
370 | void img_write_errors(img *pimg, int n_legs, double length, |
---|
371 | double E, double H, double V); |
---|
372 | |
---|
373 | /* rewind a processed survey data file opened for reading |
---|
374 | * |
---|
375 | * This is useful if you want to read the data in several passes. |
---|
376 | * |
---|
377 | * pimg is a pointer to an img struct returned by img_open() |
---|
378 | * |
---|
379 | * Returns: non-zero for success, zero for error (check img_error() for |
---|
380 | * details) |
---|
381 | */ |
---|
382 | int img_rewind(img *pimg); |
---|
383 | |
---|
384 | /* Close a processed survey data file |
---|
385 | * |
---|
386 | * pimg is a pointer to an img struct returned by img_open() or |
---|
387 | * img_open_write() |
---|
388 | * |
---|
389 | * Returns: non-zero for success, zero for error (check img_error() for |
---|
390 | * details) |
---|
391 | */ |
---|
392 | int img_close(img *pimg); |
---|
393 | |
---|
394 | /* Codes returned by img_error */ |
---|
395 | typedef enum { |
---|
396 | IMG_NONE = 0, IMG_FILENOTFOUND, IMG_OUTOFMEMORY, |
---|
397 | IMG_CANTOPENOUT, IMG_BADFORMAT, IMG_DIRECTORY, |
---|
398 | IMG_READERROR, IMG_WRITEERROR, IMG_TOONEW |
---|
399 | } img_errcode; |
---|
400 | |
---|
401 | /* Read the error code |
---|
402 | * |
---|
403 | * If img_open(), img_open_survey() or img_open_write() returns NULL, or |
---|
404 | * img_rewind() or img_close() returns 0, or img_read_item() returns img_BAD |
---|
405 | * then you can call this function to discover why. |
---|
406 | */ |
---|
407 | img_errcode img_error(void); |
---|
408 | |
---|
409 | #ifdef __cplusplus |
---|
410 | } |
---|
411 | #endif |
---|
412 | |
---|
413 | #endif |
---|