1 /*
2  * Copyright 2013 Luciad (http://www.luciad.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef GPKG_WKB_H
17 #define GPKG_WKB_H
18 
19 #include "binstream.h"
20 #include "geomio.h"
21 #include "error.h"
22 
23 /**
24  * \addtogroup wkb Well-known binary I/O
25  * @{
26  */
27 
28 typedef enum {
29   WKB_ISO,
30   WKB_SPATIALITE
31 } wkb_dialect;
32 
33 /**
34  * A Well-Known Binary writer. wkb_writer_t instances can be used to generate a WKB blob based on
35  * any geometry source. Use wkb_writer_geom_consumer() to obtain a geom_consumer_t pointer that can be passed to
36  * geomtery sources.
37  */
38 typedef struct {
39   /** @private */
40   geom_consumer_t geom_consumer;
41   /** @private */
42   binstream_t stream;
43   /** @private */
44   size_t start[GEOM_MAX_DEPTH];
45   /** @private */
46   size_t children[GEOM_MAX_DEPTH];
47   /** @private */
48   int offset;
49   wkb_dialect dialect;
50 } wkb_writer_t;
51 
52 /**
53  * Initializes a Well-Known Binary writer.
54  * @param writer the writer to initialize
55  * @return SQLITE_OK on success, an error code otherwise
56  */
57 int wkb_writer_init(wkb_writer_t *writer, wkb_dialect dialect);
58 
59 /**
60  * Destroys a Well-Known Binary writer.
61  * @param writer the writer to destroy
62  */
63 void wkb_writer_destroy(wkb_writer_t *writer, int free_data);
64 
65 /**
66  * Returns a Well-Known Binary writer as a geometry consumer. This function should be used
67  * to pass the writer to another function that takes a geom_consumer_t as input.
68  * @param writer the writer
69  */
70 geom_consumer_t *wkb_writer_geom_consumer(wkb_writer_t *writer);
71 
72 /**
73  * Returns a pointer to the Well-Known Binary data that was written by the given writer. The length of the returned
74  * buffer can be obtained using the wkb_writer_length() function.
75  * @param writer the writer
76  * @return a pointer to the Well-Known Binary data
77  */
78 uint8_t *wkb_writer_getwkb(wkb_writer_t *writer);
79 
80 /**
81  * Returns the length of the buffer obtained using the wkb_writer_getwkb() function.
82  * @param writer the writer
83  * @return the length of the Well-Known Binary data buffer
84  */
85 size_t wkb_writer_length(wkb_writer_t *writer);
86 
87 /**
88  * Parses a Well-Known Binary geometry from the given stream. The stream should be positioned at the start
89  * of the WKB geometry.
90  *
91  * @param stream the stream containing the WKB geometry
92  * @param consumer the geometry consumer that will receive the parsed geometry
93  * @param[out] error the error buffer to write to in case of I/O errors
94  * @return SQLITE_OK on success, an error code otherwise
95  */
96 int wkb_read_geometry(binstream_t *stream, wkb_dialect dialect, geom_consumer_t const *consumer, error_t *error);
97 
98 /**
99  * Parses the header of a Well-Known Binary geometry from the given stream. The stream should be positioned at the start
100  * of the WKB geometry. The data contained in the header is written to the header parameter.
101  *
102  * @param stream the stream containing the WKB geometry
103  * @param[out] header the header to populate
104  * @param[out] error the error buffer to write to in case of I/O errors
105  * @return SQLITE_OK on success, an error code otherwise
106  */
107 int wkb_read_header(binstream_t *stream, wkb_dialect dialect, geom_header_t *header, error_t *error);
108 
109 /**
110  * Populates a geometry envelope based on the coordinates of a Well-Known Binary geometry from the given stream. The
111  * stream should be positioned at the start of the WKB geometry.
112  *
113  * @param stream the stream containing the WKB geometry
114  * @param[out] envelope the envelope to populate
115  * @param[out] error the error buffer to write to in case of I/O errors
116  * @return SQLITE_OK on success, an error code otherwise
117  */
118 int wkb_fill_envelope(binstream_t *stream, wkb_dialect dialect, geom_envelope_t *envelope, error_t *error);
119 
120 int wkb_fill_geom_header(uint32_t wkb_type, geom_header_t *header, error_t *error);
121 
122 /** @} */
123 
124 #endif
125