PostGIS  2.4.9dev-r@@SVN_REVISION@@
cu_raster_basics.c
Go to the documentation of this file.
1 /*
2  * PostGIS Raster - Raster Types for PostGIS
3  * http://trac.osgeo.org/postgis/wiki/WKTRaster
4  *
5  * Copyright (C) 2012 Regents of the University of California
6  * <bkpark@ucdavis.edu>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  */
23 
24 #include "CUnit/Basic.h"
25 #include "cu_tester.h"
26 
27 static void test_raster_new() {
28  rt_raster raster = NULL;
29 
30  raster = rt_raster_new(0, 0);
31  CU_ASSERT(raster != NULL);
32  cu_free_raster(raster);
33 
34  raster = rt_raster_new(1, 1);
35  CU_ASSERT(raster != NULL);
36  cu_free_raster(raster);
37 
38  raster = rt_raster_new(10, 10);
39  CU_ASSERT(raster != NULL);
40  cu_free_raster(raster);
41 }
42 
43 static void test_raster_empty() {
44  rt_raster raster = NULL;
45 
46  /* check that raster is empty */
47  raster = rt_raster_new(0, 0);
48  CU_ASSERT(raster != NULL);
49  CU_ASSERT(rt_raster_is_empty(raster));
50  cu_free_raster(raster);
51 
52  /* create raster */
53  raster = rt_raster_new(1, 1);
54  CU_ASSERT(raster != NULL);
55 
56  /* check that raster is not empty */
57  CU_ASSERT(!rt_raster_is_empty(raster));
58 
59  cu_free_raster(raster);
60 }
61 
62 static void test_raster_metadata() {
63  rt_raster raster = NULL;
64 
65  /* create raster */
66  raster = rt_raster_new(5, 5);
67  CU_ASSERT(raster != NULL);
68 
69  /* # of bands */
70  CU_ASSERT_EQUAL(rt_raster_get_num_bands(raster), 0);
71 
72  /* has bands */
73  CU_ASSERT(!rt_raster_has_band(raster, 1));
74 
75  /* upper-left corner */
76  rt_raster_set_offsets(raster, 30, -70);
77  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_offset(raster), 30, DBL_EPSILON);
78  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_offset(raster), -70, DBL_EPSILON);
79 
80  /* scale */
81  rt_raster_set_scale(raster, 10, -10);
82  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_scale(raster), 10, DBL_EPSILON);
83  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_scale(raster), -10, DBL_EPSILON);
84 
85  /* skew */
86  rt_raster_set_skews(raster, 0.0001, -0.05);
87  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_x_skew(raster), 0.0001, DBL_EPSILON);
88  CU_ASSERT_DOUBLE_EQUAL(rt_raster_get_y_skew(raster), -0.05, DBL_EPSILON);
89 
90  /* srid */
91  rt_raster_set_srid(raster, 4326);
92  CU_ASSERT_EQUAL(rt_raster_get_srid(raster), 4326);
93  rt_raster_set_srid(raster, 4269);
94  CU_ASSERT_EQUAL(rt_raster_get_srid(raster), 4269);
95 
96  cu_free_raster(raster);
97 }
98 
99 static void test_raster_clone() {
100  rt_raster rast1;
101  rt_raster rast2;
102  rt_band band;
103 
104  int maxX = 5;
105  int maxY = 5;
106  double gt[6];
107 
108  rast1 = rt_raster_new(maxX, maxY);
109  CU_ASSERT(rast1 != NULL);
110 
111  rt_raster_set_offsets(rast1, 0, 0);
112  rt_raster_set_scale(rast1, 1, -1);
113  rt_raster_set_srid(rast1, 4326);
114 
115  band = cu_add_band(rast1, PT_32BUI, 1, 6);
116  CU_ASSERT(band != NULL);
117 
118  /* clone without bands */
119  rast2 = rt_raster_clone(rast1, 0);
120  CU_ASSERT(rast2 != NULL);
121  CU_ASSERT_EQUAL(rt_raster_get_num_bands(rast2), 0);
122 
124  CU_ASSERT_EQUAL(rt_raster_get_srid(rast2), 4326);
125  CU_ASSERT_DOUBLE_EQUAL(gt[0], 0, DBL_EPSILON);
126  CU_ASSERT_DOUBLE_EQUAL(gt[1], 1, DBL_EPSILON);
127  CU_ASSERT_DOUBLE_EQUAL(gt[2], 0, DBL_EPSILON);
128  CU_ASSERT_DOUBLE_EQUAL(gt[3], 0, DBL_EPSILON);
129  CU_ASSERT_DOUBLE_EQUAL(gt[4], 0, DBL_EPSILON);
130  CU_ASSERT_DOUBLE_EQUAL(gt[5], -1, DBL_EPSILON);
131 
132  cu_free_raster(rast2);
133 
134  /* clone with bands */
135  rast2 = rt_raster_clone(rast1, 1);
136  CU_ASSERT(rast2 != NULL);
137  CU_ASSERT_EQUAL(rt_raster_get_num_bands(rast2), 1);
138 
139  cu_free_raster(rast2);
140  cu_free_raster(rast1);
141 }
142 
143 static void test_raster_from_band() {
144  uint32_t bandNums[] = {1,3};
145  int lenBandNums = 2;
147  rt_raster rast;
148  rt_band band;
149  uint32_t xmax = 100;
150  uint32_t ymax = 100;
151  uint32_t x;
152 
153  raster = rt_raster_new(xmax, ymax);
154  CU_ASSERT(raster != NULL);
155 
156  for (x = 0; x < 5; x++) {
157  band = cu_add_band(raster, PT_32BUI, 0, 0);
158  CU_ASSERT(band != NULL);
159  rt_band_set_nodata(band, 0, NULL);
160  }
161 
162  rast = rt_raster_from_band(raster, bandNums, lenBandNums);
163  CU_ASSERT(rast != NULL);
164 
165  CU_ASSERT(!rt_raster_is_empty(rast));
166  CU_ASSERT(rt_raster_has_band(rast, 1));
167 
168  cu_free_raster(rast);
169  cu_free_raster(raster);
170 }
171 
174  rt_band band;
175  rt_band rband;
176  void* mem;
177  size_t datasize;
178  uint16_t width;
179  uint16_t height;
180  double nodata;
181 
182  raster = rt_raster_new(10, 10);
183  CU_ASSERT(raster != NULL); /* or we're out of virtual memory */
184  band = cu_add_band(raster, PT_8BUI, 0, 0);
185  CU_ASSERT(band != NULL);
186  band = cu_add_band(raster, PT_8BUI, 1, 255);
187  CU_ASSERT(band != NULL);
188 
189  width = rt_raster_get_width(raster);
190  height = rt_raster_get_height(raster);
191 
192  datasize = rt_pixtype_size(PT_8BUI) * width * height;
193  mem = rtalloc(datasize);
194  band = rt_band_new_inline(width, height, PT_8BUI, 1, 1, mem);
195  CU_ASSERT(band != NULL);
196  rt_band_set_ownsdata_flag(band, 1);
197 
198  rband = rt_raster_replace_band(raster, band, 0);
199  CU_ASSERT(rband != NULL);
200  rt_band_get_nodata(rt_raster_get_band(raster, 0), &nodata);
201  CU_ASSERT_DOUBLE_EQUAL(nodata, 1, DBL_EPSILON);
202 
203  rt_band_destroy(rband);
204  cu_free_raster(raster);
205 }
206 
207 /* register tests */
208 void raster_basics_suite_setup(void);
210 {
211  CU_pSuite suite = CU_add_suite("raster_basics", NULL, NULL);
218 }
219 
double rt_raster_get_x_offset(rt_raster raster)
Get raster x offset, in projection units.
Definition: rt_raster.c:213
int rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:372
double rt_raster_get_y_skew(rt_raster raster)
Get skew about the Y axis.
Definition: rt_raster.c:190
raster
Be careful!! Zeros function&#39;s input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
rt_raster rt_raster_from_band(rt_raster raster, uint32_t *bandNums, int count)
Construct a new rt_raster from an existing rt_raster and an array of band numbers.
Definition: rt_raster.c:1439
void rt_raster_get_geotransform_matrix(rt_raster raster, double *gt)
Get 6-element array of raster geotransform matrix.
Definition: rt_raster.c:706
static void test_raster_clone()
static void test_raster_metadata()
void rt_raster_set_skews(rt_raster raster, double skewX, double skewY)
Set skews about the X and Y axis.
Definition: rt_raster.c:168
band
Definition: ovdump.py:57
void * rtalloc(size_t size)
Wrappers used for managing memory.
Definition: rt_context.c:171
static void test_raster_empty()
rt_errorstate rt_band_set_nodata(rt_band band, double val, int *converted)
Set nodata value.
Definition: rt_band.c:600
gt
Definition: window.py:77
void rt_band_destroy(rt_band band)
Destroy a raster band.
Definition: rt_band.c:242
static void test_raster_replace_band()
int rt_raster_is_empty(rt_raster raster)
Return TRUE if the raster is empty.
Definition: rt_raster.c:1338
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
Definition: rt_band.c:1597
unsigned int uint32_t
Definition: uthash.h:78
void cu_free_raster(rt_raster raster)
rt_band cu_add_band(rt_raster raster, rt_pixtype pixtype, int hasnodata, double nodataval)
void rt_band_set_ownsdata_flag(rt_band band, int flag)
Definition: rt_band.c:534
static void test_raster_new()
void rt_raster_set_scale(rt_raster raster, double scaleX, double scaleY)
Set scale in projection units.
Definition: rt_raster.c:137
rt_raster rt_raster_clone(rt_raster raster, uint8_t deep)
Clone an existing raster.
Definition: rt_raster.c:1544
void rt_raster_set_offsets(rt_raster raster, double x, double y)
Set insertion points in projection units.
Definition: rt_raster.c:199
#define PG_ADD_TEST(suite, testfunc)
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
Definition: rt_raster.c:381
double rt_raster_get_x_scale(rt_raster raster)
Get scale X in projection units.
Definition: rt_raster.c:150
int rt_raster_has_band(rt_raster raster, int nband)
Return TRUE if the raster has a band of this number.
Definition: rt_raster.c:1351
static void test_raster_from_band()
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster&#39;s SRID.
Definition: rt_raster.c:363
int32_t rt_raster_get_srid(rt_raster raster)
Get raster&#39;s SRID.
Definition: rt_raster.c:356
rt_band rt_raster_replace_band(rt_raster raster, rt_band band, int index)
Replace band at provided index with new band.
Definition: rt_raster.c:1502
double rt_raster_get_y_scale(rt_raster raster)
Get scale Y in projection units.
Definition: rt_raster.c:159
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
Definition: rt_raster.c:48
double rt_raster_get_x_skew(rt_raster raster)
Get skew about the X axis.
Definition: rt_raster.c:181
rt_band rt_band_new_inline(uint16_t width, uint16_t height, rt_pixtype pixtype, uint32_t hasnodata, double nodataval, uint8_t *data)
Create an in-db rt_band with no data.
Definition: rt_band.c:58
int rt_pixtype_size(rt_pixtype pixtype)
Return size in bytes of a value in the given pixtype.
Definition: rt_pixel.c:39
uint16_t rt_raster_get_width(rt_raster raster)
Definition: rt_raster.c:121
void raster_basics_suite_setup(void)
uint16_t rt_raster_get_height(rt_raster raster)
Definition: rt_raster.c:129
double rt_raster_get_y_offset(rt_raster raster)
Get raster y offset, in projection units.
Definition: rt_raster.c:222