source: spike/wktraster/rt_core/rt_api.h

Last change on this file was 5823, checked in by pracine, 14 years ago

-Ticket 569. Added a serie of ST_AddBand function

  • Property svn:keywords set to Id
File size: 18.6 KB
Line 
1/*
2 * $Id: rt_api.h 5823 2010-08-13 19:09:11Z pracine $
3 *
4 * WKTRaster - Raster Types for PostGIS
5 * http://www.postgis.org/support/wiki/index.php?WKTRasterHomePage
6 *
7 * Copyright (C) 2009 Sandro Santilli <strk@keybit.net>, Pierre Racine <pierre.racine@sbf.ulaval.ca>,
8 * Jorge Arevalo <jorge.arevalo@deimos-space.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 *
24 */
25
26#ifndef RT_API_H_INCLUDED
27#define RT_API_H_INCLUDED
28
29/* define the systems */
30#if defined(__linux__) /* (predefined) */
31#if !defined(LINUX)
32#define LINUX
33#endif
34#if !defined(UNIX)
35#define UNIX /* make sure this is defined */
36#endif
37#endif
38
39
40#if defined(__FreeBSD__) || defined(__OpenBSD__) /* seems to work like Linux... */
41#if !defined(LINUX)
42#define LINUX
43#endif
44#if !defined(UNIX)
45#define UNIX /* make sure this is defined */
46#endif
47#endif
48
49#if defined(__MSDOS__)
50#if !defined(MSDOS)
51#define MSDOS /* make sure this is defined */
52#endif
53#endif
54
55#if defined(__WIN32__) || defined(__NT__) || defined(_WIN32)
56#if !defined(WIN32)
57#define WIN32
58#endif
59#if defined(__BORLANDC__) && defined(MSDOS) /* Borland always defines MSDOS */
60#undef MSDOS
61#endif
62#endif
63
64#if defined(__APPLE__)
65#if !defined(UNIX)
66#define UNIX
67#endif
68#endif
69
70/* if we are in Unix define stricmp to be strcasecmp and strnicmp to */
71/* be strncasecmp. I'm not sure if all Unices have these, but Linux */
72/* does. */
73#if defined(UNIX)
74#if !defined(HAVE_STRICMP)
75#define stricmp strcasecmp
76#endif
77#if !defined(HAVE_STRNICMP)
78#define strnicmp strncasecmp
79#endif
80#endif
81
82
83
84#include <stdlib.h> /* For size_t */
85#include <stdint.h> /* For C99 int types */
86
87#include "liblwgeom.h"
88
89#include "gdal_alg.h"
90#include "gdal_frmts.h"
91#include "gdal.h"
92#include "ogr_api.h"
93
94typedef struct rt_context_t* rt_context;
95typedef struct rt_raster_t* rt_raster;
96typedef struct rt_band_t* rt_band;
97typedef struct rt_wktgeomval_t* rt_wktgeomval;
98
99
100/*- rt_context -------------------------------------------------------*/
101
102typedef void* (*rt_allocator)(size_t size);
103typedef void* (*rt_reallocator)(void *mem, size_t size);
104typedef void (*rt_deallocator)(void *mem);
105typedef void (*rt_message_handler)(const char* string, ...);
106
107/* Initialize a context object
108 * @param allocator memory allocator to use, 0 to use malloc
109 * @param reallocator memory reallocator to use, 0 to use realloc
110 * @param deallocator memory deallocator to use, 0 to use free
111 * @return an opaque rt_context, or 0 on failure (out of memory)
112 */
113rt_context rt_context_new(rt_allocator allocator,
114 rt_reallocator reallocator,
115 rt_deallocator deallocator);
116
117/* Destroy context */
118void rt_context_destroy(rt_context ctx);
119
120/* Set message handlers */
121void rt_context_set_message_handlers(rt_context ctx,
122 rt_message_handler error_handler,
123 rt_message_handler warning_handler,
124 rt_message_handler info_handler);
125
126/*- rt_pixtype --------------------------------------------------------*/
127
128/* Pixel types */
129typedef enum {
130 PT_1BB=0, /* 1-bit boolean */
131 PT_2BUI=1, /* 2-bit unsigned integer */
132 PT_4BUI=2, /* 4-bit unsigned integer */
133 PT_8BSI=3, /* 8-bit signed integer */
134 PT_8BUI=4, /* 8-bit unsigned integer */
135 PT_16BSI=5, /* 16-bit signed integer */
136 PT_16BUI=6, /* 16-bit unsigned integer */
137 PT_32BSI=7, /* 32-bit signed integer */
138 PT_32BUI=8, /* 32-bit unsigned integer */
139 PT_32BF=10, /* 32-bit float */
140 PT_64BF=11, /* 64-bit float */
141 PT_END=13
142} rt_pixtype;
143
144/**
145 * Return size in bytes of a value in the given pixtype
146 */
147int rt_pixtype_size(rt_context ctx, rt_pixtype pixtype);
148
149/**
150 * Return alignment requirements for data in the given pixel type.
151 * Fast access to pixel values of this type must be aligned to as
152 * many bytes as returned by this function.
153 */
154int rt_pixtype_alignment(rt_context ctx, rt_pixtype pixtype);
155
156/* Return human-readable name of pixel type */
157const char* rt_pixtype_name(rt_context ctx, rt_pixtype pixtype);
158
159/* Return pixel type index from human-readable name */
160rt_pixtype rt_pixtype_index_from_name(rt_context ctx, const char* pixname);
161
162/*- rt_band ----------------------------------------------------------*/
163
164/**
165 * Create an in-buffer rt_band with no data
166 *
167 * @param ctx : context, for thread safety
168 * @param width : number of pixel columns
169 * @param height : number of pixel rows
170 * @param pixtype : pixel type for the band
171 * @param hasnodata : indicates if the band has nodata value
172 * @param nodataval : the nodata value, will be appropriately
173 * truncated to fit the pixtype size.
174 * @param data : pointer to actual band data, required to
175 * be aligned accordingly to
176 * rt_pixtype_aligment(pixtype) and big enough
177 * to hold raster width*height values.
178 * Data will NOT be copied, ownership is left
179 * to caller which is responsible to keep it
180 * allocated for the whole lifetime of the returned
181 * rt_band.
182 *
183 * @return an rt_band, or 0 on failure
184 */
185rt_band rt_band_new_inline(rt_context ctx,
186 uint16_t width, uint16_t height,
187 rt_pixtype pixtype, uint32_t hasnodata,
188 double nodataval, uint8_t* data);
189
190/**
191 * Create an on-disk rt_band
192 *
193 * @param ctx : context, for thread safety
194 * @param width : number of pixel columns
195 * @param height : number of pixel rows
196 * @param pixtype : pixel type for the band
197 * @param nodataval : the nodata value, will be appropriately
198 * truncated to fit the pixtype size.
199 * @param bandNum : 0-based band number in the external file
200 * to associate this band with.
201 * @param path : NULL-terminated path string pointing to the file
202 * containing band data. The string will NOT be
203 * copied, ownership is left to caller which is
204 * responsible to keep it allocated for the whole
205 * lifetime of the returned rt_band.
206 *
207 * @return an rt_band, or 0 on failure
208 */
209rt_band rt_band_new_offline(rt_context ctx,
210 uint16_t width, uint16_t height,
211 rt_pixtype pixtype, uint32_t hasnodata,
212 double nodataval, uint8_t bandNum, const char* path);
213
214/**
215 * Return non-zero if the given band data is on
216 * the filesystem.
217 *
218 * @param ctx : context, for thread safety
219 * @param band : the band
220 *
221 * @return non-zero if the given band data is on
222 * the filesystem.
223 */
224int rt_band_is_offline(rt_context ctx, rt_band band);
225
226/**
227 * Return bands' external path (only valid when rt_band_is_offline
228 * returns non-zero).
229 */
230const char* rt_band_get_ext_path(rt_context ctx, rt_band band);
231
232/**
233 * Return bands' external band number (only valid when
234 * rt_band_is_offline returns non-zero).
235 */
236uint8_t rt_band_get_ext_band_num(rt_context ctx, rt_band band);
237
238
239/* Get pixeltype of this band */
240rt_pixtype rt_band_get_pixtype(rt_context ctx, rt_band band);
241
242/* Get width of this band */
243uint16_t rt_band_get_width(rt_context ctx, rt_band band);
244
245/* Get height of this band */
246uint16_t rt_band_get_height(rt_context ctx, rt_band band);
247
248/* Get pointer to inline raster band data
249 * @@deprecate ?
250 */
251void* rt_band_get_data(rt_context ctx, rt_band band);
252
253/* Destroy a raster band */
254void rt_band_destroy(rt_context ctx, rt_band band);
255
256/**
257 * Get hasnodata flag value
258 * @param ctx : context, for thread safety
259 * @param band : the band on which to check the hasnodata flag
260 * @return the hasnodata flag.
261 */
262int rt_band_get_hasnodata_flag(rt_context ctx, rt_band band);
263
264/**
265 * Set hasnodata flag value
266 * @param ctx : context, for thread safety
267 * @param band : the band on which to set the hasnodata flag
268 * @param flag : the new hasnodata flag value. Must be 1 or 0.
269 */
270void rt_band_set_hasnodata_flag(rt_context ctx, rt_band band, int flag);
271
272/**
273 * Set nodata value
274 * @param ctx : context, for thread safety
275 * @param band : the band to set nodata value to
276 * @param val : the nodata value, must be in the range
277 * of values supported by this band's pixeltype
278 * or a warning will be printed and non-zero
279 * returned.
280 *
281 * @return 0 on success, -1 on error (value out of valid range).
282 *
283 */
284int rt_band_set_nodata(rt_context ctx, rt_band band, double val);
285
286/**
287 * Get nodata value
288 * @param ctx : context, for thread safety
289 * @param band : the band to set nodata value to
290 * @return nodata value
291 */
292double rt_band_get_nodata(rt_context ctx, rt_band band);
293
294/**
295 * Set pixel value
296 * @param ctx : context, for thread safety
297 * @param band : the band to set nodata value to
298 * @param x : x ordinate
299 * @param y : x ordinate
300 * @param val : the pixel value, must be in the range
301 * of values supported by this band's pixeltype
302 * or a warning will be printed and non-zero
303 * returned.
304 *
305 * @return 0 on success, -1 on error (value out of valid range).
306 */
307int rt_band_set_pixel(rt_context ctx, rt_band band,
308 uint16_t x, uint16_t y, double val);
309
310/**
311 * Get pixel value
312 *
313 * @param ctx : context, for thread safety
314 * @param band : the band to set nodata value to
315 * @param x : x ordinate
316 * @param y : x ordinate
317 * @param *result: result if there is a value
318 * @return the pixel value, as a double.
319 */
320int rt_band_get_pixel(rt_context ctx, rt_band band,
321 uint16_t x, uint16_t y, double *result );
322
323/*- rt_raster --------------------------------------------------------*/
324
325/**
326 * Construct a raster with given dimensions.
327 *
328 * Transform will be set to identity.
329 * Will contain no bands.
330 *
331 * @param ctx : context, for thread safety
332 * @param width : number of pixel columns
333 * @param height : number of pixel rows
334 *
335 * @return an rt_raster or 0 if out of memory
336 */
337rt_raster rt_raster_new(rt_context ctx, uint16_t width, uint16_t height);
338
339/**
340 * Construct an rt_raster from a binary WKB representation
341 *
342 * @param ctx : context, for thread safety
343 * @param wkb : an octet stream
344 * @param wkbsize : size (in bytes) of the wkb octet stream
345 *
346 * @return an rt_raster or 0 on error (out of memory or
347 * malformed WKB).
348 *
349 */
350rt_raster rt_raster_from_wkb(rt_context ctx, const uint8_t* wkb,
351 uint32_t wkbsize);
352
353/**
354 * Construct an rt_raster from a text HEXWKB representation
355 *
356 * @param ctx : context, for thread safety
357 * @param hexwkb : an hex-encoded stream
358 * @param hexwkbsize : size (in bytes) of the hexwkb stream
359 *
360 * @return an rt_raster or 0 on error (out of memory or
361 * malformed WKB).
362 *
363 */
364rt_raster rt_raster_from_hexwkb(rt_context ctx, const char* hexwkb,
365 uint32_t hexwkbsize);
366
367/**
368 * Return this raster in WKB form
369 *
370 * @param ctx : context, for thread safety
371 * @param raster : the raster
372 * @param wkbsize : will be set to the size of returned wkb form
373 */
374uint8_t *rt_raster_to_wkb(rt_context ctx, rt_raster raster,
375 uint32_t *wkbsize);
376
377/**
378 * Return this raster in HEXWKB form (null-terminated hex)
379 *
380 * @param ctx : context, for thread safety
381 * @param raster : the raster
382 * @param hexwkbsize : will be set to the size of returned wkb form,
383 * not including the null termination
384 */
385char *rt_raster_to_hexwkb(rt_context ctx, rt_raster raster,
386 uint32_t *hexwkbsize);
387
388/**
389 * Release memory associated to a raster
390 *
391 * Note that this will not release data
392 * associated to the band themselves (but only
393 * the one associated with the pointers pointing
394 * at them).
395 *
396 * @param ctx : context, for thread safety
397 * @param raster : the raster to destroy
398 */
399void rt_raster_destroy(rt_context ctx, rt_raster raster);
400
401/* Get number of bands */
402int rt_raster_get_num_bands(rt_context ctx, rt_raster raster);
403
404/* Return Nth band, or 0 if unavailable */
405rt_band rt_raster_get_band(rt_context ctx, rt_raster raster, int bandNum);
406
407/* Get number of rows */
408uint16_t rt_raster_get_width(rt_context ctx, rt_raster raster);
409
410/* Get number of columns */
411uint16_t rt_raster_get_height(rt_context ctx, rt_raster raster);
412
413/**
414 * Add band data to a raster.
415 *
416 * @param ctx : context, for thread safety
417 * @param raster : the raster to add a band to
418 * @param band : the band to add, ownership left to caller.
419 * Band dimensions are required to match with raster ones.
420 * @param index : the position where to insert the new band (0 based)
421 *
422 * @return identifier (position) for the just-added raster, or -1 on error
423 */
424int32_t rt_raster_add_band(rt_context ctx, rt_raster raster, rt_band band, int index);
425
426/**
427 * Set pixel size in projection units
428 *
429 * @param ctx : context, for thread safety
430 * @param raster : the raster to set georeference of
431 * @param scaleX : pixel width in projection units
432 * @param scaleY : pixel height in projection units
433 *
434 * NOTE: doesn't recompute offsets
435 */
436void rt_raster_set_pixel_sizes(rt_context ctx, rt_raster raster,
437 double scaleX, double scaleY);
438
439/**
440 * Get pixel width in projection units
441 *
442 * @param ctx : context, for thread safety
443 * @param raster : the raster to get georeference of
444 *
445 * @return pixel width in projection units
446 */
447double rt_raster_get_pixel_width(rt_context ctx, rt_raster raster);
448
449/**
450 * Get pixel height in projection units
451 *
452 * @param ctx : context, for thread safety
453 * @param raster : the raster to get georeference of
454 *
455 * @return pixel height in projection units
456 */
457double rt_raster_get_pixel_height(rt_context ctx, rt_raster raster);
458
459/**
460 * Set insertion points in projection units
461 *
462 * @param ctx : context, for thread safety
463 * @param raster : the raster to set georeference of
464 * @param x : x ordinate of the upper-left corner of upper-left pixel,
465 * in projection units
466 * @param y : y ordinate of the upper-left corner of upper-left pixel,
467 * in projection units
468 */
469void rt_raster_set_offsets(rt_context ctx, rt_raster raster,
470 double x, double y);
471
472/**
473 * Get raster x offset, in projection units
474 *
475 * @param ctx : context, for thread safety
476 * @param raster : the raster to get georeference of
477 *
478 * @return x ordinate of the upper-left corner of upper-left pixel,
479 * in projection units
480 */
481double rt_raster_get_x_offset(rt_context ctx, rt_raster raster);
482
483/**
484 * Get raster y offset, in projection units
485 *
486 * @param ctx : context, for thread safety
487 * @param raster : the raster to get georeference of
488 *
489 * @return y ordinate of the upper-left corner of upper-left pixel,
490 * in projection units
491 */
492double rt_raster_get_y_offset(rt_context ctx, rt_raster raster);
493
494/**
495 * Set skews about the X and Y axis
496 *
497 * @param ctx : context, for thread safety
498 * @param raster : the raster to set georeference of
499 * @param skewX : skew about the x axis
500 * @param skewY : skew about the y axis
501 */
502void rt_raster_set_skews(rt_context ctx, rt_raster raster,
503 double skewX, double skewY);
504
505/**
506 * Get skew about the X axis
507 *
508 * @param ctx : context, for thread safety
509 * @param raster : the raster to set georeference of
510 * @return skew about the Y axis
511 */
512double rt_raster_get_x_skew(rt_context ctx, rt_raster raster);
513
514/**
515 * Get skew about the Y axis
516 *
517 * @param ctx : context, for thread safety
518 * @param raster : the raster to set georeference of
519 * @return skew about the Y axis
520 */
521double rt_raster_get_y_skew(rt_context ctx, rt_raster raster);
522
523/**
524 * Set raster's SRID
525 *
526 * @param ctx : context, for thread safety
527 * @param raster : the raster to set SRID of
528 * @param srid : the SRID to set for the raster
529 */
530void rt_raster_set_srid(rt_context ctx, rt_raster raster, int32_t srid);
531
532/**
533 * Get raster's SRID
534 * @param ctx : context, for thread safety
535 * @param raster : the raster to set SRID of
536 *
537 * @return the raster's SRID
538 */
539int32_t rt_raster_get_srid(rt_context ctx, rt_raster raster);
540
541/**
542 * Convert an x,y raster point to an x1,y1 point on map
543 *
544 * @param ctx : context for thread safety
545 * @param raster : the raster to get info from
546 * @param x : the pixel's column
547 * @param y : the pixel's row
548 * @param x1 : output parameter, X ordinate of the geographical point
549 * @param y1 : output parameter, Y ordinate of the geographical point
550 */
551void rt_raster_cell_to_geopoint(rt_context ctx, rt_raster raster,
552 double x, double y,
553 double* x1, double* y1);
554
555/**
556 * Get raster's polygon convex hull.
557 *
558 * The convex hull is a 4 vertices (5 to be closed) single
559 * ring polygon bearing the raster's rotation
560 * and using projection coordinates
561 *
562 * @param ctx : context for thread safety
563 * @param raster : the raster to get info from
564 *
565 * @return the convex hull, or NULL on error.
566 *
567 */
568LWPOLY* rt_raster_get_convex_hull(rt_context ctx, rt_raster raster);
569
570
571/**
572 * Returns a set of "wktgeomval" value, one for each group of pixel
573 * sharing the same value for the provided band.
574 *
575 * A "wktgeomval " value is a complex type composed of a the wkt
576 * representation of a geometry (one for each group of pixel sharing
577 * the same value) and the value associated with this geometry.
578 *
579 * @param ctx: context for thread safety.
580 * @param raster: the raster to get info from.
581 * @param nband: the band to polygonize. From 1 to rt_raster_get_num_bands
582 *
583 * @return A set of "wktgeomval" values, one for each group of pixels
584 * sharing the same value for the provided band.
585 */
586rt_wktgeomval
587rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
588 int * pnElements);
589
590
591/**
592 * Return this raster in serialized form.
593 *
594 * Serialized form is documented in doc/RFC1-SerializedFormat.
595 *
596 */
597void* rt_raster_serialize(rt_context ctx, rt_raster raster);
598
599/**
600 * Return a raster from a serialized form.
601 *
602 * Serialized form is documented in doc/RFC1-SerializedFormat.
603 *
604 * NOTE: the raster will contain pointer to the serialized
605 * form, which must be kept alive.
606 */
607rt_raster rt_raster_deserialize(rt_context ctx, void* serialized);
608
609#endif /* RT_API_H_INCLUDED */
Note: See TracBrowser for help on using the repository browser.