| 163 | | * Transform syntax coordinates from GML 2.1.2 (x1,y1 x2,y2) into Postgis (x1 y1,x2 y2) |
| 164 | | */ |
| 165 | | buffer *fe_transform_coord_gml_to_psql(buffer * coord) |
| 166 | | { |
| 167 | | size_t i; |
| 168 | | |
| 169 | | assert(coord != NULL); |
| 170 | | |
| 171 | | /*check if the first separator is a comma else do nothing */ |
| 172 | | if (check_regexp(coord->buf, "^[0-9.-]+,")) { |
| 173 | | for (i = 0; i < coord->use; i++) { |
| 174 | | if (coord->buf[i] == ' ') |
| 175 | | coord->buf[i] = ','; |
| 176 | | else if (coord->buf[i] == ',') |
| 177 | | coord->buf[i] = ' '; |
| 178 | | } |
| 179 | | } |
| 180 | | |
| 181 | | return coord; |
| 182 | | } |
| 183 | | |
| 184 | | |
| 185 | | /* |
| 186 | | * Transform syntax coordinates from GML 3.1.1 (x1 y1 x2 y2) into Postgis' (x1 y1,x2 y2) |
| 187 | | */ |
| 188 | | buffer *fe_transform_coord_gml3_to_psql(buffer * coord) |
| 189 | | { |
| 190 | | size_t i; |
| 191 | | int nb_spaces; |
| 192 | | |
| 193 | | assert(coord != NULL); |
| 194 | | |
| 195 | | nb_spaces = 0; |
| 196 | | |
| 197 | | for (i = 0; i < coord->use; i++) { |
| 198 | | if (coord->buf[i] == ' ') { |
| 199 | | nb_spaces++; |
| 200 | | |
| 201 | | /* transform the second space separator into comma */ |
| 202 | | if (nb_spaces == 2) { |
| 203 | | coord->buf[i] = ','; |
| 204 | | nb_spaces = 0; |
| 205 | | } |
| 206 | | } |
| 207 | | } |
| 208 | | |
| 209 | | return coord; |
| 210 | | } |
| 211 | | |
| 212 | | |
| 213 | | /* |
| 214 | | * Transform a geometry expressed in GML into Postgis geometry syntax |
| 215 | | */ |
| 216 | | buffer *fe_transform_geometry_gml_to_psql(ows * o, buffer * typename, |
| 217 | | filter_encoding * fe, xmlNodePtr n) |
| 218 | | { |
| 219 | | xmlChar *content; |
| 220 | | xmlNodePtr node, node_coord; |
| 221 | | buffer *tmp, *geom; |
| 222 | | bool first_ring = true; |
| 223 | | int bracket; |
| | 183 | * Return the SQL request matching the spatial operator |
| | 184 | */ |
| | 185 | static buffer *fe_spatial_functions(ows * o, buffer * typename, |
| | 186 | filter_encoding * fe, xmlNodePtr n) |
| | 187 | { |
| | 188 | bool transform = false; |
| | 189 | buffer *sql; |
| 230 | | content = NULL; |
| 231 | | |
| 232 | | /* jump to the next element if there are spaces */ |
| 233 | | while (n->type != XML_ELEMENT_NODE) n = n->next; |
| 234 | | |
| 235 | | buffer_add_str(fe->sql, "setsrid('"); |
| 236 | | |
| 237 | | geom = buffer_init(); |
| 238 | | if (strcmp((char *) n->name, "MultiSurface") == 0) |
| 239 | | buffer_add_str(geom, "MultiPolygon"); |
| 240 | | else |
| 241 | | buffer_add_str(geom, (char *) n->name); |
| 242 | | |
| 243 | | /* print the geometry type */ |
| 244 | | if (buffer_cmp(geom, "MultiPolygon") || buffer_cmp(geom, "MultiSurface")) { |
| 245 | | buffer_add_str(geom, "((("); |
| 246 | | bracket = 3; |
| 247 | | } else if (buffer_cmp(geom, "MultiLineString") |
| 248 | | || buffer_cmp(geom, "Polygon")) { |
| 249 | | buffer_add_str(geom, "(("); |
| 250 | | bracket =2; |
| 251 | | } else if (buffer_cmp(geom, "MultiPoint") |
| 252 | | || buffer_cmp(geom, "Point") |
| 253 | | || buffer_cmp(geom, "LineString")) { |
| 254 | | buffer_add_str(geom, "("); |
| 255 | | bracket = 1; |
| 256 | | } else { |
| 257 | | fe->error_code = FE_ERROR_GEOMETRY; |
| 258 | | buffer_free(geom); |
| 259 | | return fe->sql; |
| 260 | | } |
| 261 | | n = n->children; |
| 262 | | |
| 263 | | /* jump to the next element if there are spaces */ |
| 264 | | while (n->type != XML_ELEMENT_NODE) n = n->next; |
| 265 | | |
| 266 | | /* print the coordinates */ |
| 267 | | for (node = n; node != NULL; node = node->next) { |
| 268 | | |
| 269 | | /* |
| 270 | | * Ignore GML properties if any |
| 271 | | */ |
| 272 | | if ((strcmp((char *) node->name, "description") == 0 |
| 273 | | || strcmp((char *) node->name, "name") == 0 |
| 274 | | || strcmp((char *) node->name, "metaDataProperty") == 0) |
| 275 | | && strcmp((char *) node->ns->href, "http://www.opengis.net/gml") == 0) |
| 276 | | node = node->next; |
| 277 | | |
| 278 | | if (node->type == XML_ELEMENT_NODE) { |
| 279 | | node_coord = node; |
| 280 | | |
| 281 | | |
| 282 | | /* go to node <coordinates> */ |
| 283 | | while (strcmp((char *) node_coord->name, "coordinates") != 0 |
| 284 | | && strcmp((char *) node_coord->name, "posList") != 0 |
| 285 | | && strcmp((char *) node_coord->name, "pos") != 0) { |
| 286 | | |
| 287 | | while (node_coord != NULL && node_coord->type != XML_ELEMENT_NODE) { |
| 288 | | node_coord = node_coord->next; |
| 289 | | } |
| 290 | | |
| 291 | | if (node_coord == NULL) { |
| 292 | | fe->error_code = FE_ERROR_GEOMETRY; |
| 293 | | buffer_free(geom); |
| 294 | | return fe->sql; |
| 295 | | } |
| 296 | | |
| 297 | | if (node_coord != NULL && node_coord->type == XML_ELEMENT_NODE |
| 298 | | && strcmp((char *) node_coord->name, "LinearRing") == 0) { |
| 299 | | buffer_pop(geom, 1); /* remove last coord comma or ( */ |
| 300 | | if (!first_ring) buffer_add_str(geom, "),"); |
| 301 | | buffer_add_str(geom, "("); |
| 302 | | first_ring = false; |
| 303 | | } |
| 304 | | |
| 305 | | /* jump to the next element if there are spaces */ |
| 306 | | if (strcmp((char *) node_coord->name, "coordinates") != 0 |
| 307 | | && strcmp((char *) node_coord->name, "posList") != 0 |
| 308 | | && strcmp((char *) node_coord->name, "pos") != 0) |
| 309 | | node_coord = node_coord->children; |
| 310 | | } |
| 311 | | |
| 312 | | tmp = buffer_init(); |
| 313 | | |
| 314 | | content = xmlNodeGetContent(node_coord); |
| 315 | | buffer_add_str(tmp, (char *) content); |
| 316 | | |
| 317 | | if (strcmp((char *) node_coord->name, "coordinates") == 0) |
| 318 | | tmp = fe_transform_coord_gml_to_psql(tmp); |
| 319 | | else |
| 320 | | tmp = fe_transform_coord_gml3_to_psql(tmp); |
| 321 | | |
| 322 | | buffer_copy(geom, tmp); |
| 323 | | buffer_free(tmp); |
| 324 | | xmlFree(content); |
| 325 | | } |
| 326 | | |
| 327 | | if (node->next != NULL) { |
| 328 | | if (node->next->type == XML_ELEMENT_NODE) { |
| 329 | | if (strcmp((char *) node->next->name, |
| 330 | | "lineStringMember") == 0) |
| 331 | | buffer_add_str(geom, "),("); |
| 332 | | else if (strcmp((char *) node->next->name, "polygonMember") == 0 |
| 333 | | || strcmp((char *) node->next->name, "surfaceMember") == 0) |
| 334 | | buffer_add_str(geom, ")),(("); |
| 335 | | else |
| 336 | | buffer_add_str(geom, ","); |
| 337 | | } |
| 338 | | } |
| 339 | | } |
| 340 | | |
| 341 | | /* print the geometry type */ |
| 342 | | if (bracket == 3) buffer_add_str(geom, ")))"); |
| 343 | | else if (bracket == 2) buffer_add_str(geom, "))"); |
| 344 | | else buffer_add_str(geom, ")"); |
| 345 | | |
| 346 | | if (!ows_psql_is_geometry_valid(o, geom)) { |
| 347 | | fe->error_code = FE_ERROR_GEOMETRY; |
| 348 | | buffer_free(geom); |
| 349 | | return fe->sql; |
| 350 | | } |
| 351 | | |
| 352 | | buffer_add_str(geom, "'::geometry,"); |
| 353 | | buffer_copy(fe->sql, geom); |
| 354 | | |
| 355 | | /* print the srid */ |
| 356 | | buffer_add_int(fe->sql, ows_srs_get_srid_from_layer(o, typename)); |
| 357 | | buffer_add_str(fe->sql, ")"); |
| 358 | | |
| 359 | | buffer_free(geom); |
| 360 | | |
| 361 | | return fe->sql; |
| 362 | | } |
| 363 | | |
| 364 | | |
| 365 | | /* |
| 366 | | * Return the SQL request matching the spatial operator |
| 367 | | */ |
| 368 | | static buffer *fe_spatial_functions(ows * o, buffer * typename, |
| 369 | | filter_encoding * fe, xmlNodePtr n) |
| 370 | | { |
| 371 | | bool transform = false; |
| 372 | | |
| 373 | | assert(o != NULL); |
| 374 | | assert(typename != NULL); |
| 375 | | assert(fe != NULL); |
| 376 | | assert(n != NULL); |
| 377 | | |