| | 345 | |
| | 346 | /* |
| | 347 | ** Look-up for the correct MULTI* type promotion for |
| | 348 | ** singleton types. |
| | 349 | */ |
| | 350 | static unsigned char MULTITYPE[16] = { |
| | 351 | 0, |
| | 352 | MULTIPOINTTYPE, |
| | 353 | MULTILINETYPE, |
| | 354 | MULTIPOLYGONTYPE, |
| | 355 | 0,0,0,0, |
| | 356 | MULTICURVETYPE, |
| | 357 | MULTICURVETYPE, |
| | 358 | 0,0,0, |
| | 359 | MULTISURFACETYPE, |
| | 360 | 0,0 |
| | 361 | }; |
| | 362 | |
| | 363 | /* |
| | 364 | ** Create a new LWGEOM of the appropriate MULTI* type. |
| | 365 | */ |
| | 366 | LWGEOM * |
| | 367 | lwgeom_as_multi(LWGEOM *lwgeom) |
| | 368 | { |
| | 369 | LWGEOM **ogeoms; |
| | 370 | LWGEOM *ogeom = NULL; |
| | 371 | BOX2DFLOAT4 *box = NULL; |
| | 372 | int type; |
| | 373 | |
| | 374 | ogeoms = lwalloc(sizeof(LWGEOM*)); |
| | 375 | |
| | 376 | /* |
| | 377 | ** This funx is a no-op only if a bbox cache is already present |
| | 378 | ** in input. |
| | 379 | */ |
| | 380 | if ( lwgeom_contains_subgeoms(TYPE_GETTYPE(lwgeom->type)) ) |
| | 381 | { |
| | 382 | return lwgeom_clone(lwgeom); |
| | 383 | } |
| | 384 | |
| | 385 | type = TYPE_GETTYPE(lwgeom->type); |
| | 386 | |
| | 387 | if ( MULTITYPE[type] ) |
| | 388 | { |
| | 389 | ogeoms[0] = lwgeom_clone(lwgeom); |
| | 390 | |
| | 391 | /* Sub-geometries are not allowed to have bboxes or SRIDs, move the bbox to the collection */ |
| | 392 | box = ogeoms[0]->bbox; |
| | 393 | ogeoms[0]->bbox = NULL; |
| | 394 | ogeoms[0]->SRID = -1; |
| | 395 | |
| | 396 | ogeom = (LWGEOM *)lwcollection_construct(MULTITYPE[type], lwgeom->SRID, box, 1, ogeoms); |
| | 397 | } |
| | 398 | else |
| | 399 | { |
| | 400 | return lwgeom_clone(lwgeom); |
| | 401 | } |
| | 402 | |
| | 403 | return ogeom; |
| | 404 | } |
| | 405 | |