2 * Routines for reading and writing processed survey data files
4 * These routines support reading processed survey data in a variety of formats
7 * - Survex ".3d" image files
8 * - Survex ".pos" files
9 * - Compass Plot files (".plt" and ".plf")
10 * - CMAP XYZ files (".sht", ".adj", ".una", ".xyz")
12 * Writing Survex ".3d" image files is supported.
14 * Copyright (C) Olly Betts 1993-2024
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.
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.
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
34 /* Define IMG_API_VERSION if you want more recent versions of the img API.
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".
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
52 #include <time.h> /* for time_t */
58 /* NB: img_CROSS is never output and ignored on input.
59 * Put crosses where labels are. */
60 /* # define img_CROSS 2 */
63 # define img_XSECT_END 5
64 /* Loop closure information for the *preceding* traverse (img_MOVE + one or
66 # define img_ERROR_INFO 6
69 # define img_FLAG_SURFACE 0x01
70 # define img_FLAG_DUPLICATE 0x02
71 # define img_FLAG_SPLAY 0x04
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
83 # define img_FFLAG_EXTENDED 0x80
84 # define img_FFLAG_SEPARATOR(CH) (((((int)CH) & 0xff) << 9) | 0x100)
86 /* When writing img_XSECT, img_XFLAG_END in pimg->flags means this is the last
87 * img_XSECT in this tube:
89 # define img_XFLAG_END 0x01
91 # define img_STYLE_UNKNOWN -1
92 # define img_STYLE_NORMAL 0
93 # define img_STYLE_DIVING 1
94 # define img_STYLE_CARTESIAN 2
95 # define img_STYLE_CYLPOLAR 3
96 # define img_STYLE_NOSURVEY 4
98 /* 3D coordinates (in metres) */
104 /* Members you can access when reading (don't touch when writing): */
108 /* This contains a string describing the coordinate system which the data is
109 * in, suitable for passing to PROJ. For a coordinate system with an EPSG
110 * code this will typically be "EPSG:" followed by the code number.
112 * If no coordinate system was specified then this member will be NULL.
115 /* Older .3d format versions stored a human readable datestamp string.
116 * Format versions >= 8 versions store a string consisting of "@" followed
117 * by the number of seconds since midnight UTC on 1/1/1970. Some foreign
118 * formats contain a human readable string, others no date information
119 * (which results in "?" being returned).
122 /* The datestamp as a time_t (or (time_t)-1 if not available).
124 * For 3d format versions >= 8, this is a reliable value and in UTC. Older
125 * 3d format versions store a human readable time, which img will attempt
126 * to decode, but it may fail, particularly with handling timezones. Even
127 * if it does work, beware that times in local time where DST applies are
128 * inherently ambiguous around when the clocks go back.
130 * CMAP XYZ files contain a timestamp. It's probably in localtime (but
131 * without any timezone information) and the example files are all pre-2000
132 * and have two digit years. We do our best to turn these into a useful
135 time_t datestamp_numeric
;
136 char separator
; /* character used to separate survey levels ('.' usually) */
138 /* Members that can be set when writing: */
139 #if IMG_API_VERSION == 0
141 #else /* IMG_API_VERSION == 1 */
146 /* Error information - valid when img_ERROR_INFO is returned: */
151 /* The filename actually opened (e.g. may have ".3d" added).
153 * This is only set if img opened the filename - if an existing stream
154 * is used (via img_read_stream() or similar) then this member will be
157 char * filename_opened
;
159 /* Non-zero if reading an extended elevation: */
160 int is_extended_elevation
;
162 /* Members that can be set when writing: */
163 /* The style of the data - one of the img_STYLE_* constants above */
166 /* All other members are for internal use only: */
167 FILE *fh
; /* file handle of image file */
168 int (*close_func
)(FILE*);
172 int fRead
; /* 1 for reading, 0 for writing */
174 /* version of file format:
175 * -4 => CMAP .xyz file, shot format
176 * -3 => CMAP .xyz file, station format
177 * -2 => Compass .plt file
181 * 2 => byte actions and flags
182 * 3 => prefixes for legs; compressed prefixes
186 * 7 => more compact dates with wider range
187 * 8 => lots of changes
192 /* Used to track state in various ways depending on the format, and also for
193 * filtering by survey. */
196 #if IMG_API_VERSION == 0
197 time_t olddate1
, olddate2
;
198 #else /* IMG_API_VERSION == 1 */
199 int olddays1
, olddays2
;
202 /* Pointer to extra data reading some formats requires. */
206 /* Which version of the file format to output (defaults to newest) */
207 extern unsigned int img_output_version
;
209 /* Minimum supported value for img_output_version: */
210 #define IMG_VERSION_MIN 1
212 /* Maximum supported value for img_output_version: */
213 #define IMG_VERSION_MAX 8
215 /* Open a processed survey data file for reading
217 * fnm is the filename
219 * Returns pointer to an img struct or NULL
221 #define img_open(F) img_open_survey((F), NULL)
223 /* Open a processed survey data file for reading
225 * fnm is the filename
227 * survey points to a survey name to restrict reading to (or NULL for all
228 * survey data in the file)
230 * Returns pointer to an img struct or NULL
232 img
*img_open_survey(const char *fnm
, const char *survey
);
234 /* Read processed survey data from an existing stream.
236 * stream is a FILE* open on the stream (can be NULL which will give error
237 * IMG_FILENOTFOUND so you don't need to handle that case specially). The
238 * stream should be opened for reading in binary mode and positioned at the
239 * start of the survey data file.
241 * close_func is a function to call to close the stream (most commonly
242 * fclose, or pclose if the stream was opened using popen()) or NULL if
243 * the caller wants to take care of closing the stream.
245 * filename is used to determine the format based on the file extension,
246 * and also the leafname with the extension removed is used for the survey
247 * title for formats which don't support a title or when no title is
248 * specified. If you're not interested in default titles, you can just
249 * pass the extension including a leading "." - e.g. ".3d". May not be
252 * Returns pointer to an img struct or NULL on error. Any close function
253 * specified is called on error (unless stream is NULL).
255 #define img_read_stream(S, C, F) img_read_stream_survey((S), (C), (F), NULL)
257 /* Read processed survey data from an existing stream.
259 * stream is a FILE* open on the stream (can be NULL which will give error
260 * IMG_FILENOTFOUND so you don't need to handle that case specially). The
261 * stream should be opened for reading in binary mode and positioned at the
262 * start of the survey data file.
264 * close_func is a function to call to close the stream (most commonly
265 * fclose, or pclose if the stream was opened using popen()) or NULL if
266 * the caller wants to take care of closing the stream.
268 * filename is used to determine the format based on the file extension,
269 * and also the leafname with the extension removed is used for the survey
270 * title for formats which don't support a title or when no title is
271 * specified. If you're not interested in default titles, you can just
272 * pass the extension including a leading "." - e.g. ".3d". filename must
275 * survey points to a survey name to restrict reading to (or NULL for all
276 * survey data in the file)
278 * Returns pointer to an img struct or NULL on error. Any close function
279 * specified is called on error.
281 img
*img_read_stream_survey(FILE *stream
, int (*close_func
)(FILE*),
282 const char *filename
,
285 /* Open a .3d file for output with no specified coordinate system
287 * This is a very thin wrapper around img_open_write_cs() which passes NULL for
288 * cs, provided for compatibility with the API provided before support for
289 * coordinate systems was added.
291 * See img_open_write_cs() for documentation.
293 #define img_open_write(F, T, S) img_open_write_cs(F, T, NULL, S)
295 /* Open a .3d file for output in a specified coordinate system
297 * fnm is the filename
301 * cs is a string describing the coordinate system, suitable for passing to
302 * PROJ (or NULL to not specify a coordinate system). For a coordinate system
303 * with an assigned EPSG code number, "EPSG:" followed by the code number is
304 * the recommended way to specify this.
306 * flags contains a bitwise-or of any file-wide flags - currently these are
309 * img_FFLAG_EXTENDED : this is an extended elevation
310 * img_FFLAG_SEPARATOR(CHARACTER) : specify the separator character
313 * Returns pointer to an img struct or NULL for error (check img_error()
316 img
*img_open_write_cs(const char *fnm
, const char *title
, const char * cs
,
319 /* Write a .3d file to a stream
321 * stream is a FILE* open on the stream (can be NULL which will give error
322 * IMG_FILENOTFOUND so you don't need to handle that case specially). The
323 * stream should be opened for writing in binary mode.
325 * close_func is a function to call to close the stream (most commonly
326 * fclose, or pclose if the stream was opened using popen()) or NULL if
327 * the caller wants to take care of closing the stream.
331 * cs is a string describing the coordinate system, suitable for passing to
332 * PROJ (or NULL to not specify a coordinate system). For a coordinate system
333 * with an EPSG, "EPSG:" followed by the code number is the recommended way
336 * flags contains a bitwise-or of any file-wide flags - currently these are
339 * img_FFLAG_EXTENDED : this is an extended elevation
340 * img_FFLAG_SEPARATOR(CHARACTER) : specify the separator character
343 * Returns pointer to an img struct or NULL for error (check img_error()
344 * for details). Any close function specified is called on error (unless
347 img
*img_write_stream(FILE *stream
, int (*close_func
)(FILE*),
348 const char *title
, const char * cs
, int flags
);
350 /* Read an item from a processed survey data file
352 * pimg is a pointer to an img struct returned by img_open()
354 * coordinates are returned in p
356 * flags and label name are returned in fields in pimg
358 * Returns img_XXXX as #define-d above
360 int img_read_item(img
*pimg
, img_point
*p
);
362 /* Write a item to a .3d file
364 * pimg is a pointer to an img struct returned by img_open_write()
366 * code is one of the img_XXXX #define-d above
368 * flags is the leg, station, or xsect flags
369 * (meaningful for img_LINE, img_LABEL, and img_XSECT respectively)
371 * s is the label (only meaningful for img_LABEL)
373 * x, y, z are the coordinates
375 void img_write_item(img
*pimg
, int code
, int flags
, const char *s
,
376 double x
, double y
, double z
);
378 /* Write error information for the current traverse
380 * n_legs is the number of legs in the traverse
382 * length is the traverse length (in m)
384 * E is the ratio of the observed misclosure to the theoretical one
386 * H is the ratio of the observed horizontal misclosure to the theoretical one
388 * V is the ratio of the observed vertical misclosure to the theoretical one
390 void img_write_errors(img
*pimg
, int n_legs
, double length
,
391 double E
, double H
, double V
);
393 /* rewind a processed survey data file opened for reading
395 * This is useful if you want to read the data in several passes.
397 * pimg is a pointer to an img struct returned by img_open()
399 * Returns: non-zero for success, zero for error (check img_error() for
402 int img_rewind(img
*pimg
);
404 /* Close a processed survey data file
406 * pimg is a pointer to an img struct returned by img_open() or
409 * Returns: non-zero for success, zero for error (check img_error() for
412 int img_close(img
*pimg
);
414 /* Codes returned by img_error */
416 IMG_NONE
= 0, IMG_FILENOTFOUND
, IMG_OUTOFMEMORY
,
417 IMG_CANTOPENOUT
, IMG_BADFORMAT
, IMG_DIRECTORY
,
418 IMG_READERROR
, IMG_WRITEERROR
, IMG_TOONEW
421 /* Read the error code
423 * If img_open(), img_open_survey() or img_open_write() returns NULL, or
424 * img_rewind() or img_close() returns 0, or img_read_item() returns img_BAD
425 * then you can call this function to discover why.
427 img_errcode
img_error(void);
429 /* Datum codes returned by img_parse_compass_datum_string().
431 * We currently don't handle the following, which appear in the datum list
432 * in Compass, but there don't seem to be any EPSG codes for UTM with any
437 * Camp Area Astro (Antarctica only)
441 * Ordnance Survey 1936
443 * South American 1956
444 * South American 1969
447 img_DATUM_UNKNOWN
= 0,
452 img_DATUM_EUROPEAN1950
,
454 img_DATUM_HUTZUSHAN1950
,
455 img_DATUM_INDIAN1960
,
463 /* Parse a Compass datum string and return an img_datum code. */
464 img_datum
img_parse_compass_datum_string(const char *s
, size_t len
);
466 /* Return a CRS string to pass to PROJ from an img_datum and UTM zone.
468 * utm_zone can be between -60 and -1 (Southern Hemisphere), or 1 and 60
469 * (Northern Hemisphere).
471 * Where possible a string of the form "EPSG:1234" is returned.
473 * Example Compass files we've seen use "North American 1927" outside of where
474 * it's defined for use, presumably because some users fail to change the datum
475 * from Compass' default. To enable reading such files we return a PROJ4
476 * string of the form "+proj=utm ..." for "North American 1927" and "North
477 * American 1983" for UTM zones which don't have an EPSG code.
479 * If no mapping is known NULL is returned.
481 * The returned value is allocated with malloc() and the caller is responsible
482 * for calling free().
484 char *img_compass_utm_proj_str(img_datum datum
, int utm_zone
);