Line data Source code
1 : /***********************************************************************
2 : * File : postgisrastertools.cpp
3 : * Project: PostGIS Raster driver
4 : * Purpose: Tools for PostGIS Raster driver
5 : * Author: Jorge Arevalo, jorge.arevalo@deimos-space.com
6 : * jorgearevalo@libregis.org
7 : *
8 : * Author: David Zwarg, dzwarg@azavea.com
9 : *
10 : * Last changes: $Id$
11 : *
12 : ***********************************************************************
13 : * Copyright (c) 2009 - 2013, Jorge Arevalo, David Zwarg
14 : *
15 : * Permission is hereby granted, free of charge, to any person obtaining
16 : * a copy of this software and associated documentation files (the
17 : * "Software"), to deal in the Software without restriction, including
18 : * without limitation the rights to use, copy, modify, merge, publish,
19 : * distribute, sublicense, and/or sell copies of the Software, and to
20 : * permit persons to whom the Software is furnished to do so, subject to
21 : * the following conditions:
22 : *
23 : * The above copyright notice and this permission notice shall be
24 : * included in all copies or substantial portions of the Software.
25 : *
26 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 : * NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 : * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 : * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 : * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 : * SOFTWARE.
34 : **********************************************************************/
35 : #include "postgisraster.h"
36 :
37 : /**********************************************************************
38 : * \brief Replace the quotes by single quotes in the input string
39 : *
40 : * Needed in the 'where' part of the input string
41 : **********************************************************************/
42 0 : char *ReplaceQuotes(const char *pszInput, int nLength)
43 : {
44 : int i;
45 0 : char *pszOutput = nullptr;
46 :
47 0 : if (nLength == -1)
48 0 : nLength = static_cast<int>(strlen(pszInput));
49 :
50 0 : pszOutput = static_cast<char *>(CPLCalloc(nLength + 1, sizeof(char)));
51 :
52 0 : for (i = 0; i < nLength; i++)
53 : {
54 0 : if (pszInput[i] == '"')
55 0 : pszOutput[i] = '\'';
56 : else
57 0 : pszOutput[i] = pszInput[i];
58 : }
59 :
60 0 : return pszOutput;
61 : }
62 :
63 : /***********************************************************************
64 : * \brief Translate a PostGIS Raster datatype string in a valid
65 : * GDALDataType object.
66 : **********************************************************************/
67 0 : GBool TranslateDataType(const char *pszDataType,
68 : GDALDataType *poDataType = nullptr,
69 : int *pnBitsDepth = nullptr)
70 : {
71 0 : if (!pszDataType)
72 0 : return false;
73 :
74 0 : if (EQUAL(pszDataType, "1BB"))
75 : {
76 0 : if (pnBitsDepth)
77 0 : *pnBitsDepth = 1;
78 0 : if (poDataType)
79 0 : *poDataType = GDT_Byte;
80 : }
81 :
82 0 : else if (EQUAL(pszDataType, "2BUI"))
83 : {
84 0 : if (pnBitsDepth)
85 0 : *pnBitsDepth = 2;
86 0 : if (poDataType)
87 0 : *poDataType = GDT_Byte;
88 : }
89 :
90 0 : else if (EQUAL(pszDataType, "4BUI"))
91 : {
92 0 : if (pnBitsDepth)
93 0 : *pnBitsDepth = 4;
94 0 : if (poDataType)
95 0 : *poDataType = GDT_Byte;
96 : }
97 :
98 0 : else if (EQUAL(pszDataType, "8BUI"))
99 : {
100 0 : if (pnBitsDepth)
101 0 : *pnBitsDepth = 8;
102 0 : if (poDataType)
103 0 : *poDataType = GDT_Byte;
104 : }
105 :
106 0 : else if (EQUAL(pszDataType, "8BSI"))
107 : {
108 0 : if (pnBitsDepth)
109 0 : *pnBitsDepth = 8;
110 0 : if (poDataType)
111 0 : *poDataType = GDT_Int8;
112 : }
113 0 : else if (EQUAL(pszDataType, "16BSI"))
114 : {
115 0 : if (pnBitsDepth)
116 0 : *pnBitsDepth = 16;
117 0 : if (poDataType)
118 0 : *poDataType = GDT_Int16;
119 : }
120 :
121 0 : else if (EQUAL(pszDataType, "16BUI"))
122 : {
123 0 : if (pnBitsDepth)
124 0 : *pnBitsDepth = 16;
125 0 : if (poDataType)
126 0 : *poDataType = GDT_UInt16;
127 : }
128 :
129 0 : else if (EQUAL(pszDataType, "32BSI"))
130 : {
131 0 : if (pnBitsDepth)
132 0 : *pnBitsDepth = 32;
133 0 : if (poDataType)
134 0 : *poDataType = GDT_Int32;
135 : }
136 :
137 0 : else if (EQUAL(pszDataType, "32BUI"))
138 : {
139 0 : if (pnBitsDepth)
140 0 : *pnBitsDepth = 32;
141 0 : if (poDataType)
142 0 : *poDataType = GDT_UInt32;
143 : }
144 :
145 0 : else if (EQUAL(pszDataType, "32BF"))
146 : {
147 0 : if (pnBitsDepth)
148 0 : *pnBitsDepth = 32;
149 0 : if (poDataType)
150 0 : *poDataType = GDT_Float32;
151 : }
152 :
153 0 : else if (EQUAL(pszDataType, "64BF"))
154 : {
155 0 : if (pnBitsDepth)
156 0 : *pnBitsDepth = 64;
157 0 : if (poDataType)
158 0 : *poDataType = GDT_Float64;
159 : }
160 :
161 : else
162 : {
163 0 : if (pnBitsDepth)
164 0 : *pnBitsDepth = -1;
165 0 : if (poDataType)
166 0 : *poDataType = GDT_Unknown;
167 :
168 0 : return false;
169 : }
170 :
171 0 : return true;
172 : }
|