lwgeom.c 44.1 KB
Newer Older
Sandro Santilli's avatar
Sandro Santilli committed
1 2 3
/**********************************************************************
 *
 * PostGIS - Spatial Types for PostgreSQL
4
 * http://postgis.net
5 6
 *
 * Copyright (C) 2001-2006 Refractions Research Inc.
Sandro Santilli's avatar
Sandro Santilli committed
7 8 9
 *
 * This is free software; you can redistribute and/or modify it under
 * the terms of the GNU General Public Licence. See the COPYING file.
10
 *
Sandro Santilli's avatar
Sandro Santilli committed
11 12
 **********************************************************************/

Sandro Santilli's avatar
Sandro Santilli committed
13 14 15 16
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

17
#include "liblwgeom_internal.h"
18
#include "lwgeom_log.h"
Sandro Santilli's avatar
Sandro Santilli committed
19

20

21
/** Force Right-hand-rule on LWGEOM polygons **/
22
void
23
lwgeom_force_clockwise(LWGEOM *lwgeom)
24 25 26 27
{
	LWCOLLECTION *coll;
	int i;

28
	switch (lwgeom->type)
29
	{
30
	case POLYGONTYPE:
31
		lwpoly_force_clockwise((LWPOLY *)lwgeom);
32 33
		return;

34
	case TRIANGLETYPE:
35
		lwtriangle_force_clockwise((LWTRIANGLE *)lwgeom);
36 37
		return;

38
		/* Not handle POLYHEDRALSURFACE and TIN
39 40
		   as they are supposed to be well oriented */
	case MULTIPOLYGONTYPE:
41 42 43
	case COLLECTIONTYPE:
		coll = (LWCOLLECTION *)lwgeom;
		for (i=0; i<coll->ngeoms; i++)
44
			lwgeom_force_clockwise(coll->geoms[i]);
45
		return;
46 47 48
	}
}

49
/** Reverse vertex order of LWGEOM **/
50 51 52 53 54 55
void
lwgeom_reverse(LWGEOM *lwgeom)
{
	int i;
	LWCOLLECTION *col;

56
	switch (lwgeom->type)
57
	{
58 59 60 61 62 63
	case LINETYPE:
		lwline_reverse((LWLINE *)lwgeom);
		return;
	case POLYGONTYPE:
		lwpoly_reverse((LWPOLY *)lwgeom);
		return;
64 65 66
	case TRIANGLETYPE:
		lwtriangle_reverse((LWTRIANGLE *)lwgeom);
		return;
67 68
	case MULTILINETYPE:
	case MULTIPOLYGONTYPE:
69
	case POLYHEDRALSURFACETYPE:
70
	case TINTYPE:
71 72 73 74 75
	case COLLECTIONTYPE:
		col = (LWCOLLECTION *)lwgeom;
		for (i=0; i<col->ngeoms; i++)
			lwgeom_reverse(col->geoms[i]);
		return;
76 77 78
	}
}

79
LWPOINT *
80
lwgeom_as_lwpoint(const LWGEOM *lwgeom)
81
{
82
	if ( lwgeom == NULL ) return NULL;
83
	if ( lwgeom->type == POINTTYPE )
84
		return (LWPOINT *)lwgeom;
85 86 87 88
	else return NULL;
}

LWLINE *
89
lwgeom_as_lwline(const LWGEOM *lwgeom)
90
{
91
	if ( lwgeom == NULL ) return NULL;
92
	if ( lwgeom->type == LINETYPE )
93
		return (LWLINE *)lwgeom;
94 95 96
	else return NULL;
}

97
LWCIRCSTRING *
98
lwgeom_as_lwcircstring(const LWGEOM *lwgeom)
99
{
100
	if ( lwgeom == NULL ) return NULL;
101
	if ( lwgeom->type == CIRCSTRINGTYPE )
102 103
		return (LWCIRCSTRING *)lwgeom;
	else return NULL;
104 105
}

106 107 108 109
LWCOMPOUND *
lwgeom_as_lwcompound(const LWGEOM *lwgeom)
{
	if ( lwgeom == NULL ) return NULL;
110
	if ( lwgeom->type == COMPOUNDTYPE )
111 112 113 114 115 116 117 118
		return (LWCOMPOUND *)lwgeom;
	else return NULL;
}

LWCURVEPOLY *
lwgeom_as_lwcurvepoly(const LWGEOM *lwgeom)
{
	if ( lwgeom == NULL ) return NULL;
119
	if ( lwgeom->type == CURVEPOLYTYPE )
120 121 122 123
		return (LWCURVEPOLY *)lwgeom;
	else return NULL;
}

124
LWPOLY *
125
lwgeom_as_lwpoly(const LWGEOM *lwgeom)
126
{
127
	if ( lwgeom == NULL ) return NULL;
128
	if ( lwgeom->type == POLYGONTYPE )
129
		return (LWPOLY *)lwgeom;
130 131 132
	else return NULL;
}

133 134 135
LWTRIANGLE *
lwgeom_as_lwtriangle(const LWGEOM *lwgeom)
{
136
	if ( lwgeom == NULL ) return NULL;
137
	if ( lwgeom->type == TRIANGLETYPE )
138 139 140 141
		return (LWTRIANGLE *)lwgeom;
	else return NULL;
}

142
LWCOLLECTION *
143
lwgeom_as_lwcollection(const LWGEOM *lwgeom)
144
{
145
	if ( lwgeom == NULL ) return NULL;
146
	if ( lwgeom_is_collection(lwgeom) )
147
		return (LWCOLLECTION*)lwgeom;
148 149 150 151
	else return NULL;
}

LWMPOINT *
152
lwgeom_as_lwmpoint(const LWGEOM *lwgeom)
153
{
154
	if ( lwgeom == NULL ) return NULL;
155
	if ( lwgeom->type == MULTIPOINTTYPE )
156
		return (LWMPOINT *)lwgeom;
157 158 159 160
	else return NULL;
}

LWMLINE *
161
lwgeom_as_lwmline(const LWGEOM *lwgeom)
162
{
163
	if ( lwgeom == NULL ) return NULL;
164
	if ( lwgeom->type == MULTILINETYPE )
165
		return (LWMLINE *)lwgeom;
166 167 168 169
	else return NULL;
}

LWMPOLY *
170
lwgeom_as_lwmpoly(const LWGEOM *lwgeom)
171
{
172
	if ( lwgeom == NULL ) return NULL;
173
	if ( lwgeom->type == MULTIPOLYGONTYPE )
174
		return (LWMPOLY *)lwgeom;
175 176 177
	else return NULL;
}

178
LWPSURFACE *
179
lwgeom_as_lwpsurface(const LWGEOM *lwgeom)
180
{
181
	if ( lwgeom->type == POLYHEDRALSURFACETYPE )
182 183 184 185
		return (LWPSURFACE *)lwgeom;
	else return NULL;
}

186 187 188
LWTIN *
lwgeom_as_lwtin(const LWGEOM *lwgeom)
{
189
	if ( lwgeom->type == TINTYPE )
190 191 192 193 194 195 196 197 198 199
		return (LWTIN *)lwgeom;
	else return NULL;
}

LWGEOM *lwtin_as_lwgeom(const LWTIN *obj)
{
	return (LWGEOM *)obj;
}

LWGEOM *lwpsurface_as_lwgeom(const LWPSURFACE *obj)
200 201 202 203
{
	return (LWGEOM *)obj;
}

204
LWGEOM *lwmpoly_as_lwgeom(const LWMPOLY *obj)
205
{
206
	if ( obj == NULL ) return NULL;
207 208
	return (LWGEOM *)obj;
}
209
LWGEOM *lwmline_as_lwgeom(const LWMLINE *obj)
210
{
211
	if ( obj == NULL ) return NULL;
212 213
	return (LWGEOM *)obj;
}
214
LWGEOM *lwmpoint_as_lwgeom(const LWMPOINT *obj)
215
{
216
	if ( obj == NULL ) return NULL;
217 218
	return (LWGEOM *)obj;
}
219
LWGEOM *lwcollection_as_lwgeom(const LWCOLLECTION *obj)
220
{
221
	if ( obj == NULL ) return NULL;
222 223
	return (LWGEOM *)obj;
}
224
LWGEOM *lwcircstring_as_lwgeom(const LWCIRCSTRING *obj)
225
{
226
	if ( obj == NULL ) return NULL;
227 228
	return (LWGEOM *)obj;
}
229 230 231 232 233 234 235 236 237 238
LWGEOM *lwcurvepoly_as_lwgeom(const LWCURVEPOLY *obj)
{
	if ( obj == NULL ) return NULL;
	return (LWGEOM *)obj;
}
LWGEOM *lwcompound_as_lwgeom(const LWCOMPOUND *obj)
{
	if ( obj == NULL ) return NULL;
	return (LWGEOM *)obj;
}
239
LWGEOM *lwpoly_as_lwgeom(const LWPOLY *obj)
240
{
241
	if ( obj == NULL ) return NULL;
242 243
	return (LWGEOM *)obj;
}
244 245
LWGEOM *lwtriangle_as_lwgeom(const LWTRIANGLE *obj)
{
246
	if ( obj == NULL ) return NULL;
247 248
	return (LWGEOM *)obj;
}
249
LWGEOM *lwline_as_lwgeom(const LWLINE *obj)
250
{
251
	if ( obj == NULL ) return NULL;
252 253
	return (LWGEOM *)obj;
}
254
LWGEOM *lwpoint_as_lwgeom(const LWPOINT *obj)
255
{
256
	if ( obj == NULL ) return NULL;
257 258
	return (LWGEOM *)obj;
}
259

Paul Ramsey's avatar
Paul Ramsey committed
260

261 262
/**
** Look-up for the correct MULTI* type promotion for singleton types.
Paul Ramsey's avatar
Paul Ramsey committed
263
*/
264
uint8_t MULTITYPE[NUMTYPES] =
Paul Ramsey's avatar
Paul Ramsey committed
265 266
{
	0,
267 268 269
	MULTIPOINTTYPE,        /*  1 */
	MULTILINETYPE,         /*  2 */
	MULTIPOLYGONTYPE,      /*  3 */
Paul Ramsey's avatar
Paul Ramsey committed
270
	0,0,0,0,
271 272 273 274 275 276
	MULTICURVETYPE,        /*  8 */
	MULTICURVETYPE,        /*  9 */
	MULTISURFACETYPE,      /* 10 */
	POLYHEDRALSURFACETYPE, /* 11 */
	0, 0,
	TINTYPE,               /* 14 */
277
	0
Paul Ramsey's avatar
Paul Ramsey committed
278
};
Paul Ramsey's avatar
Paul Ramsey committed
279

280 281
/**
* Create a new LWGEOM of the appropriate MULTI* type.
Paul Ramsey's avatar
Paul Ramsey committed
282 283
*/
LWGEOM *
284
lwgeom_as_multi(const LWGEOM *lwgeom)
Paul Ramsey's avatar
Paul Ramsey committed
285 286 287
{
	LWGEOM **ogeoms;
	LWGEOM *ogeom = NULL;
288
	GBOX *box = NULL;
Paul Ramsey's avatar
Paul Ramsey committed
289 290
	int type;

291
	type = lwgeom->type;
Paul Ramsey's avatar
Paul Ramsey committed
292

293 294 295 296 297 298 299 300 301 302 303 304
	if ( ! MULTITYPE[type] ) return lwgeom_clone(lwgeom);

	if( lwgeom_is_empty(lwgeom) )
	{
		ogeom = (LWGEOM *)lwcollection_construct_empty(
			MULTITYPE[type],
			lwgeom->srid,
			FLAGS_GET_Z(lwgeom->flags),
			FLAGS_GET_M(lwgeom->flags)
		);
	}
	else
Paul Ramsey's avatar
Paul Ramsey committed
305
	{
306
		ogeoms = lwalloc(sizeof(LWGEOM*));
Paul Ramsey's avatar
Paul Ramsey committed
307 308 309 310 311
		ogeoms[0] = lwgeom_clone(lwgeom);

		/* Sub-geometries are not allowed to have bboxes or SRIDs, move the bbox to the collection */
		box = ogeoms[0]->bbox;
		ogeoms[0]->bbox = NULL;
312
		ogeoms[0]->srid = SRID_UNKNOWN;
313

314
		ogeom = (LWGEOM *)lwcollection_construct(MULTITYPE[type], lwgeom->srid, box, 1, ogeoms);
Paul Ramsey's avatar
Paul Ramsey committed
315 316 317 318 319
	}

	return ogeom;
}

320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
/**
* Create a new LWGEOM of the appropriate CURVE* type.
*/
LWGEOM *
lwgeom_as_curve(const LWGEOM *lwgeom)
{
	LWGEOM *ogeom;
	int type = lwgeom->type;
/*
  int hasz = FLAGS_GET_Z(lwgeom->flags);
  int hasm = FLAGS_GET_M(lwgeom->flags);
  int srid = lwgeom->srid;
*/

	switch(type)
	{
		case LINETYPE:
      /* turn to COMPOUNDCURVE */
      ogeom = (LWGEOM*)lwcompound_construct_from_lwline((LWLINE*)lwgeom);
      break;
		case POLYGONTYPE:
      ogeom = (LWGEOM*)lwcurvepoly_construct_from_lwpoly(lwgeom_as_lwpoly(lwgeom));
      break;
		case MULTILINETYPE:
      /* turn to MULTICURVE */
      ogeom = lwgeom_clone(lwgeom);
      ogeom->type = MULTICURVETYPE;
      break;
		case MULTIPOLYGONTYPE:
      /* turn to MULTISURFACE */
      ogeom = lwgeom_clone(lwgeom);
      ogeom->type = MULTISURFACETYPE;
      break;
		case COLLECTIONTYPE:
		default:
      ogeom = lwgeom_clone(lwgeom);
      break;
  }

  /* TODO: copy bbox from input geom ? */

  return ogeom;
}

364 365 366 367 368 369 370

/**
* Free the containing LWGEOM and the associated BOX. Leave the underlying 
* geoms/points/point objects intact. Useful for functions that are stripping
* out subcomponents of complex objects, or building up new temporary objects
* on top of subcomponents.
*/
371 372 373
void
lwgeom_release(LWGEOM *lwgeom)
{
Sandro Santilli's avatar
 
Sandro Santilli committed
374
	if ( ! lwgeom )
375
		lwerror("lwgeom_release: someone called on 0x0");
Sandro Santilli's avatar
 
Sandro Santilli committed
376

377
	LWDEBUGF(3, "releasing type %s", lwtype_name(lwgeom->type));
378

379
	/* Drop bounding box (always a copy) */
380 381
	if ( lwgeom->bbox )
	{
382
		LWDEBUGF(3, "lwgeom_release: releasing bbox. %p", lwgeom->bbox);
383 384
		lwfree(lwgeom->bbox);
	}
385
	lwfree(lwgeom);
386 387 388

}

Paul Ramsey's avatar
Paul Ramsey committed
389

390 391 392 393
/* @brief Clone LWGEOM object. Serialized point lists are not copied.
 *
 * @see ptarray_clone 
 */
394 395 396
LWGEOM *
lwgeom_clone(const LWGEOM *lwgeom)
{
397
	LWDEBUGF(2, "lwgeom_clone called with %p, %s",
398
	         lwgeom, lwtype_name(lwgeom->type));
399

400
	switch (lwgeom->type)
401
	{
402 403 404 405 406 407 408 409
	case POINTTYPE:
		return (LWGEOM *)lwpoint_clone((LWPOINT *)lwgeom);
	case LINETYPE:
		return (LWGEOM *)lwline_clone((LWLINE *)lwgeom);
	case CIRCSTRINGTYPE:
		return (LWGEOM *)lwcircstring_clone((LWCIRCSTRING *)lwgeom);
	case POLYGONTYPE:
		return (LWGEOM *)lwpoly_clone((LWPOLY *)lwgeom);
410 411
	case TRIANGLETYPE:
		return (LWGEOM *)lwtriangle_clone((LWTRIANGLE *)lwgeom);
412 413
	case COMPOUNDTYPE:
	case CURVEPOLYTYPE:
414 415
	case MULTICURVETYPE:
	case MULTISURFACETYPE:
416 417 418
	case MULTIPOINTTYPE:
	case MULTILINETYPE:
	case MULTIPOLYGONTYPE:
419
	case POLYHEDRALSURFACETYPE:
420
	case TINTYPE:
421 422 423
	case COLLECTIONTYPE:
		return (LWGEOM *)lwcollection_clone((LWCOLLECTION *)lwgeom);
	default:
424
		lwerror("lwgeom_clone: Unknown geometry type: %s", lwtype_name(lwgeom->type));
Paul Ramsey's avatar
Paul Ramsey committed
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
		return NULL;
	}
}

/** 
* Deep-clone an #LWGEOM object. #POINTARRAY <em>are</em> copied. 
*/
LWGEOM *
lwgeom_clone_deep(const LWGEOM *lwgeom)
{
	LWDEBUGF(2, "lwgeom_clone called with %p, %s",
	         lwgeom, lwtype_name(lwgeom->type));

	switch (lwgeom->type)
	{
	case POINTTYPE:
	case LINETYPE:
	case CIRCSTRINGTYPE:
	case TRIANGLETYPE:
		return (LWGEOM *)lwline_clone_deep((LWLINE *)lwgeom);
	case POLYGONTYPE:
		return (LWGEOM *)lwpoly_clone_deep((LWPOLY *)lwgeom);
	case COMPOUNDTYPE:
	case CURVEPOLYTYPE:
	case MULTICURVETYPE:
	case MULTISURFACETYPE:
	case MULTIPOINTTYPE:
	case MULTILINETYPE:
	case MULTIPOLYGONTYPE:
	case POLYHEDRALSURFACETYPE:
	case TINTYPE:
	case COLLECTIONTYPE:
		return (LWGEOM *)lwcollection_clone_deep((LWCOLLECTION *)lwgeom);
	default:
459
		lwerror("lwgeom_clone_deep: Unknown geometry type: %s", lwtype_name(lwgeom->type));
460
		return NULL;
461 462 463
	}
}

464

465
/**
466 467
 * Return an alloced string
 */
468
char*
469
lwgeom_to_ewkt(const LWGEOM *lwgeom)
470
{
471 472 473 474
	char* wkt = NULL;
	size_t wkt_size = 0;
	
	wkt = lwgeom_to_wkt(lwgeom, WKT_EXTENDED, 12, &wkt_size);
475

476
	if ( ! wkt )
477
	{
478
		lwerror("Error writing geom %p to WKT", lwgeom);
479
	}
480

481
	return wkt;
482 483
}

484 485 486 487 488 489 490 491 492
/**
 * @brief geom1 same as geom2
 *  	iff
 *      	+ have same type
 *			+ have same # objects
 *      	+ have same bvol
 *      	+ each object in geom1 has a corresponding object in geom2 (see above)
 *	@param lwgeom1
 *	@param lwgeom2
493
 */
Sandro Santilli's avatar
 
Sandro Santilli committed
494
char
495
lwgeom_same(const LWGEOM *lwgeom1, const LWGEOM *lwgeom2)
Sandro Santilli's avatar
 
Sandro Santilli committed
496
{
497
	LWDEBUGF(2, "lwgeom_same(%s, %s) called",
498 499
	         lwtype_name(lwgeom1->type),
	         lwtype_name(lwgeom2->type));
500

501
	if ( lwgeom1->type != lwgeom2->type )
502
	{
503 504
		LWDEBUG(3, " type differ");

505
		return LW_FALSE;
506
	}
Sandro Santilli's avatar
 
Sandro Santilli committed
507

508
	if ( FLAGS_GET_ZM(lwgeom1->flags) != FLAGS_GET_ZM(lwgeom2->flags) )
509
	{
510 511
		LWDEBUG(3, " ZM flags differ");

512
		return LW_FALSE;
513
	}
Sandro Santilli's avatar
 
Sandro Santilli committed
514

515
	/* Check boxes if both already computed  */
Sandro Santilli's avatar
 
Sandro Santilli committed
516 517
	if ( lwgeom1->bbox && lwgeom2->bbox )
	{
518
		/*lwnotice("bbox1:%p, bbox2:%p", lwgeom1->bbox, lwgeom2->bbox);*/
519
		if ( ! gbox_same(lwgeom1->bbox, lwgeom2->bbox) )
520
		{
521 522
			LWDEBUG(3, " bounding boxes differ");

523
			return LW_FALSE;
524
		}
Sandro Santilli's avatar
 
Sandro Santilli committed
525 526
	}

527
	/* geoms have same type, invoke type-specific function */
528
	switch (lwgeom1->type)
529
	{
530 531 532 533 534 535 536 537 538
	case POINTTYPE:
		return lwpoint_same((LWPOINT *)lwgeom1,
		                    (LWPOINT *)lwgeom2);
	case LINETYPE:
		return lwline_same((LWLINE *)lwgeom1,
		                   (LWLINE *)lwgeom2);
	case POLYGONTYPE:
		return lwpoly_same((LWPOLY *)lwgeom1,
		                   (LWPOLY *)lwgeom2);
539 540 541
	case TRIANGLETYPE:
		return lwtriangle_same((LWTRIANGLE *)lwgeom1,
		                       (LWTRIANGLE *)lwgeom2);
542 543 544
	case CIRCSTRINGTYPE:
		return lwcircstring_same((LWCIRCSTRING *)lwgeom1,
					 (LWCIRCSTRING *)lwgeom2);
545 546 547
	case MULTIPOINTTYPE:
	case MULTILINETYPE:
	case MULTIPOLYGONTYPE:
548 549
	case MULTICURVETYPE:
	case MULTISURFACETYPE:
550
	case COMPOUNDTYPE:
551
	case CURVEPOLYTYPE:
552
	case POLYHEDRALSURFACETYPE:
553
	case TINTYPE:
554 555 556 557
	case COLLECTIONTYPE:
		return lwcollection_same((LWCOLLECTION *)lwgeom1,
		                         (LWCOLLECTION *)lwgeom2);
	default:
558
		lwerror("lwgeom_same: unsupported geometry type: %s",
559
		        lwtype_name(lwgeom1->type));
560
		return LW_FALSE;
561 562
	}

Sandro Santilli's avatar
 
Sandro Santilli committed
563 564
}

565 566 567
int
lwpoint_inside_circle(const LWPOINT *p, double cx, double cy, double rad)
{
568
	const POINT2D *pt;
569 570 571 572 573
	POINT2D center;

	if ( ! p || ! p->point )
		return LW_FALSE;
		
574
	pt = getPoint2d_cp(p->point, 0);
575 576 577 578

	center.x = cx;
	center.y = cy;

579
	if ( distance2d_pt_pt(pt, &center) < rad ) 
580 581 582 583 584
		return LW_TRUE;

	return LW_FALSE;
}

Sandro Santilli's avatar
 
Sandro Santilli committed
585
void
586
lwgeom_drop_bbox(LWGEOM *lwgeom)
Sandro Santilli's avatar
 
Sandro Santilli committed
587
{
588
	if ( lwgeom->bbox ) lwfree(lwgeom->bbox);
Sandro Santilli's avatar
 
Sandro Santilli committed
589
	lwgeom->bbox = NULL;
590
	FLAGS_SET_BBOX(lwgeom->flags, 0);
Sandro Santilli's avatar
 
Sandro Santilli committed
591 592
}

593
/**
594 595 596 597 598
 * Ensure there's a box in the LWGEOM.
 * If the box is already there just return,
 * else compute it.
 */
void
599
lwgeom_add_bbox(LWGEOM *lwgeom)
600
{
601
	/* an empty LWGEOM has no bbox */
602
	if ( lwgeom_is_empty(lwgeom) ) return;
603

604
	if ( lwgeom->bbox ) return;
605
	FLAGS_SET_BBOX(lwgeom->flags, 1);
606 607
	lwgeom->bbox = gbox_new(lwgeom->flags);
	lwgeom_calculate_gbox(lwgeom, lwgeom->bbox);
608 609
}

610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
void 
lwgeom_add_bbox_deep(LWGEOM *lwgeom, GBOX *gbox)
{
	if ( lwgeom_is_empty(lwgeom) ) return;

	FLAGS_SET_BBOX(lwgeom->flags, 1);
	
	if ( ! ( gbox || lwgeom->bbox ) )
	{
		lwgeom->bbox = gbox_new(lwgeom->flags);
		lwgeom_calculate_gbox(lwgeom, lwgeom->bbox);		
	}
	else if ( gbox && ! lwgeom->bbox )
	{
		lwgeom->bbox = gbox_clone(gbox);
	}
	
	if ( lwgeom_is_collection(lwgeom) )
	{
		int i;
		LWCOLLECTION *lwcol = (LWCOLLECTION*)lwgeom;

		for ( i = 0; i < lwcol->ngeoms; i++ )
		{
			lwgeom_add_bbox_deep(lwcol->geoms[i], lwgeom->bbox);
		}
	}
}

639 640 641 642 643 644 645 646 647
const GBOX *
lwgeom_get_bbox(const LWGEOM *lwg)
{
	/* add it if not already there */
	lwgeom_add_bbox((LWGEOM *)lwg);
	return lwg->bbox;
}


648 649 650 651 652 653 654 655 656 657 658 659 660
/**
* Calculate the gbox for this goemetry, a cartesian box or
* geodetic box, depending on how it is flagged.
*/
int lwgeom_calculate_gbox(const LWGEOM *lwgeom, GBOX *gbox)
{
	gbox->flags = lwgeom->flags;
	if( FLAGS_GET_GEODETIC(lwgeom->flags) )
		return lwgeom_calculate_gbox_geodetic(lwgeom, gbox);
	else
		return lwgeom_calculate_gbox_cartesian(lwgeom, gbox);	
}

Sandro Santilli's avatar
 
Sandro Santilli committed
661
void
662
lwgeom_drop_srid(LWGEOM *lwgeom)
Sandro Santilli's avatar
 
Sandro Santilli committed
663
{
664
	lwgeom->srid = SRID_UNKNOWN;	/* TODO: To be changed to SRID_UNKNOWN */
Sandro Santilli's avatar
 
Sandro Santilli committed
665
}
666 667 668 669

LWGEOM *
lwgeom_segmentize2d(LWGEOM *lwgeom, double dist)
{
670
	switch (lwgeom->type)
671
	{
672 673 674 675 676 677 678 679 680 681 682 683 684 685
	case LINETYPE:
		return (LWGEOM *)lwline_segmentize2d((LWLINE *)lwgeom,
		                                     dist);
	case POLYGONTYPE:
		return (LWGEOM *)lwpoly_segmentize2d((LWPOLY *)lwgeom,
		                                     dist);
	case MULTILINETYPE:
	case MULTIPOLYGONTYPE:
	case COLLECTIONTYPE:
		return (LWGEOM *)lwcollection_segmentize2d(
		           (LWCOLLECTION *)lwgeom, dist);

	default:
		return lwgeom_clone(lwgeom);
686 687
	}
}
688

689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
LWGEOM*
lwgeom_force_2d(const LWGEOM *geom)
{	
	return lwgeom_force_dims(geom, 0, 0);
}

LWGEOM*
lwgeom_force_3dz(const LWGEOM *geom)
{	
	return lwgeom_force_dims(geom, 1, 0);
}

LWGEOM*
lwgeom_force_3dm(const LWGEOM *geom)
{	
	return lwgeom_force_dims(geom, 0, 1);
}

LWGEOM*
lwgeom_force_4d(const LWGEOM *geom)
{	
	return lwgeom_force_dims(geom, 1, 1);
}

LWGEOM*
lwgeom_force_dims(const LWGEOM *geom, int hasz, int hasm)
{	
716
	switch(geom->type)
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
	{
		case POINTTYPE:
			return lwpoint_as_lwgeom(lwpoint_force_dims((LWPOINT*)geom, hasz, hasm));
		case CIRCSTRINGTYPE:
		case LINETYPE:
		case TRIANGLETYPE:
			return lwline_as_lwgeom(lwline_force_dims((LWLINE*)geom, hasz, hasm));
		case POLYGONTYPE:
			return lwpoly_as_lwgeom(lwpoly_force_dims((LWPOLY*)geom, hasz, hasm));
		case COMPOUNDTYPE:
		case CURVEPOLYTYPE:
		case MULTICURVETYPE:
		case MULTISURFACETYPE:
		case MULTIPOINTTYPE:
		case MULTILINETYPE:
		case MULTIPOLYGONTYPE:
		case POLYHEDRALSURFACETYPE:
		case TINTYPE:
		case COLLECTIONTYPE:
			return lwcollection_as_lwgeom(lwcollection_force_dims((LWCOLLECTION*)geom, hasz, hasm));
		default:
738
			lwerror("lwgeom_force_2d: unsupported geom type: %s", lwtype_name(geom->type));
739 740 741 742
			return NULL;
	}
}

743
LWGEOM*
744
lwgeom_force_sfs(LWGEOM *geom, int version)
745 746 747 748 749
{	
	LWCOLLECTION *col;
	int i;
	LWGEOM *g;

750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
	/* SFS 1.2 version */
	if (version == 120)
	{
		switch(geom->type)
		{
			/* SQL/MM types */
			case CIRCSTRINGTYPE:
			case COMPOUNDTYPE:
			case CURVEPOLYTYPE:
			case MULTICURVETYPE:
			case MULTISURFACETYPE:
				return lwgeom_segmentize(geom, 32);

			case COLLECTIONTYPE:
				col = (LWCOLLECTION*)geom;
				for ( i = 0; i < col->ngeoms; i++ ) 
					col->geoms[i] = lwgeom_force_sfs((LWGEOM*)col->geoms[i], version);

				return lwcollection_as_lwgeom((LWCOLLECTION*)geom);

			default:
				return (LWGEOM *)geom;
		}
	}
	
775

776
	/* SFS 1.1 version */
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
	switch(geom->type)
	{
		/* SQL/MM types */
		case CIRCSTRINGTYPE:
		case COMPOUNDTYPE:
		case CURVEPOLYTYPE:
		case MULTICURVETYPE:
		case MULTISURFACETYPE:
			return lwgeom_segmentize(geom, 32);

		/* SFS 1.2 types */
		case TRIANGLETYPE:
			g = lwpoly_as_lwgeom(lwpoly_from_lwlines((LWLINE*)geom, 0, NULL));
			lwgeom_free(geom);
			return g;

		case TINTYPE:
			col = (LWCOLLECTION*) geom;
			for ( i = 0; i < col->ngeoms; i++ )
			{
				g = lwpoly_as_lwgeom(lwpoly_from_lwlines((LWLINE*)col->geoms[i], 0, NULL));
				lwgeom_free(col->geoms[i]);
				col->geoms[i] = g;
			}
801
			col->type = COLLECTIONTYPE;
802 803 804
			return lwmpoly_as_lwgeom((LWMPOLY*)geom);
		
		case POLYHEDRALSURFACETYPE:
805
			geom->type = COLLECTIONTYPE;
806 807 808 809 810 811
			return (LWGEOM *)geom;

		/* Collection */
		case COLLECTIONTYPE:
			col = (LWCOLLECTION*)geom;
			for ( i = 0; i < col->ngeoms; i++ ) 
812
				col->geoms[i] = lwgeom_force_sfs((LWGEOM*)col->geoms[i], version);
813 814 815 816 817 818 819 820

			return lwcollection_as_lwgeom((LWCOLLECTION*)geom);
		
		default:
			return (LWGEOM *)geom;
	}
}

821 822 823 824 825 826 827
int32_t 
lwgeom_get_srid(const LWGEOM *geom)
{
	if ( ! geom ) return SRID_UNKNOWN;
	return geom->srid;
}

828 829 830 831 832 833 834
uint32_t 
lwgeom_get_type(const LWGEOM *geom)
{
	if ( ! geom ) return 0;
	return geom->type;
}

835 836 837 838 839 840 841 842 843 844 845 846 847 848
int 
lwgeom_has_z(const LWGEOM *geom)
{
	if ( ! geom ) return LW_FALSE;
	return FLAGS_GET_Z(geom->flags);
}

int 
lwgeom_has_m(const LWGEOM *geom)
{
	if ( ! geom ) return LW_FALSE;
	return FLAGS_GET_M(geom->flags);
}

Paul Ramsey's avatar
Paul Ramsey committed
849 850 851 852 853 854 855 856
int 
lwgeom_ndims(const LWGEOM *geom)
{
	if ( ! geom ) return 0;
	return FLAGS_NDIMS(geom->flags);
}


857 858 859 860 861 862 863 864 865 866
void
lwgeom_set_geodetic(LWGEOM *geom, int value)
{
	LWPOINT *pt;
	LWLINE *ln;
	LWPOLY *ply;
	LWCOLLECTION *col;
	int i;
	
	FLAGS_SET_GEODETIC(geom->flags, value);
867 868 869
	if ( geom->bbox )
		FLAGS_SET_GEODETIC(geom->bbox->flags, value);
	
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
	switch(geom->type)
	{
		case POINTTYPE:
			pt = (LWPOINT*)geom;
			if ( pt->point )
				FLAGS_SET_GEODETIC(pt->point->flags, value);
			break;
		case LINETYPE:
			ln = (LWLINE*)geom;
			if ( ln->points )
				FLAGS_SET_GEODETIC(ln->points->flags, value);
			break;
		case POLYGONTYPE:
			ply = (LWPOLY*)geom;
			for ( i = 0; i < ply->nrings; i++ )
				FLAGS_SET_GEODETIC(ply->rings[i]->flags, value);
			break;
		case MULTIPOINTTYPE:
		case MULTILINETYPE:
		case MULTIPOLYGONTYPE:
		case COLLECTIONTYPE:
			col = (LWCOLLECTION*)geom;
			for ( i = 0; i < col->ngeoms; i++ )
				lwgeom_set_geodetic(col->geoms[i], value);
			break;
		default:
896
			lwerror("lwgeom_set_geodetic: unsupported geom type: %s", lwtype_name(geom->type));
897 898 899 900
			return;
	}
}

901 902 903 904
void
lwgeom_longitude_shift(LWGEOM *lwgeom)
{
	int i;
905
	switch (lwgeom->type)
906 907 908 909
	{
		LWPOINT *point;
		LWLINE *line;
		LWPOLY *poly;
910
		LWTRIANGLE *triangle;
911 912
		LWCOLLECTION *coll;

913 914 915 916 917 918 919 920 921 922 923 924 925
	case POINTTYPE:
		point = (LWPOINT *)lwgeom;
		ptarray_longitude_shift(point->point);
		return;
	case LINETYPE:
		line = (LWLINE *)lwgeom;
		ptarray_longitude_shift(line->points);
		return;
	case POLYGONTYPE:
		poly = (LWPOLY *)lwgeom;
		for (i=0; i<poly->nrings; i++)
			ptarray_longitude_shift(poly->rings[i]);
		return;
926 927 928 929
	case TRIANGLETYPE:
		triangle = (LWTRIANGLE *)lwgeom;
		ptarray_longitude_shift(triangle->points);
		return;
930 931 932
	case MULTIPOINTTYPE:
	case MULTILINETYPE:
	case MULTIPOLYGONTYPE:
933
	case POLYHEDRALSURFACETYPE:
934
	case TINTYPE:
935 936 937 938 939 940
	case COLLECTIONTYPE:
		coll = (LWCOLLECTION *)lwgeom;
		for (i=0; i<coll->ngeoms; i++)
			lwgeom_longitude_shift(coll->geoms[i]);
		return;
	default:
941 942
		lwerror("lwgeom_longitude_shift: unsupported geom type: %s",
		        lwtype_name(lwgeom->type));
943 944
	}
}
945

946 947 948 949 950
int 
lwgeom_is_closed(const LWGEOM *geom)
{
	int type = geom->type;
	
951 952 953
	if( lwgeom_is_empty(geom) )
		return LW_FALSE;
	
954 955 956 957 958
	/* Test linear types for closure */
	switch (type)
	{
	case LINETYPE:
		return lwline_is_closed((LWLINE*)geom);
959 960
	case POLYGONTYPE:
		return lwpoly_is_closed((LWPOLY*)geom);
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
	case CIRCSTRINGTYPE:
		return lwcircstring_is_closed((LWCIRCSTRING*)geom);
	case COMPOUNDTYPE:
		return lwcompound_is_closed((LWCOMPOUND*)geom);
	case TINTYPE:
		return lwtin_is_closed((LWTIN*)geom);
	case POLYHEDRALSURFACETYPE:
		return lwpsurface_is_closed((LWPSURFACE*)geom);
	}
	
	/* Recurse into collections and see if anything is not closed */
	if ( lwgeom_is_collection(geom) )
	{
		LWCOLLECTION *col = lwgeom_as_lwcollection(geom);
		int i;
		int closed;
		for ( i = 0; i < col->ngeoms; i++ ) 
		{
			closed = lwgeom_is_closed(col->geoms[i]);
			if ( ! closed ) 
				return LW_FALSE;
		}
		return LW_TRUE;
	}
	
	/* All non-linear non-collection types we will call closed */
	return LW_TRUE;
}

990 991 992 993
int 
lwgeom_is_collection(const LWGEOM *geom)
{
	if( ! geom ) return LW_FALSE;
994
	return lwtype_is_collection(geom->type);
995 996
}

997
/** Return TRUE if the geometry may contain sub-geometries, i.e. it is a MULTI* or COMPOUNDCURVE */
998
int
999
lwtype_is_collection(uint8_t type)
1000
{
1001

1002 1003 1004 1005 1006 1007
	switch (type)
	{
	case MULTIPOINTTYPE:
	case MULTILINETYPE:
	case MULTIPOLYGONTYPE:
	case COLLECTIONTYPE:
1008
	case CURVEPOLYTYPE:
1009 1010 1011
	case COMPOUNDTYPE:
	case MULTICURVETYPE:
	case MULTISURFACETYPE:
1012
	case POLYHEDRALSURFACETYPE:
1013
	case TINTYPE:
1014
		return LW_TRUE;
1015 1016 1017
		break;

	default:
1018
		return LW_FALSE;
1019 1020 1021
	}
}

1022 1023 1024 1025
/**
* Given an lwtype number, what homogeneous collection can hold it?
*/
int 
1026
lwtype_get_collectiontype(uint8_t type)
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
{
	switch (type)
	{
		case POINTTYPE:
			return MULTIPOINTTYPE;
		case LINETYPE:
			return MULTILINETYPE;
		case POLYGONTYPE:
			return MULTIPOLYGONTYPE;
		case CIRCSTRINGTYPE:
			return MULTICURVETYPE;
		case COMPOUNDTYPE:
			return MULTICURVETYPE;
		case CURVEPOLYTYPE:
			return MULTISURFACETYPE;
		case TRIANGLETYPE:
			return TINTYPE;
		default:
			return COLLECTIONTYPE;
	}
}


1050 1051
void lwgeom_free(LWGEOM *lwgeom)
{
1052

1053 1054
	/* There's nothing here to free... */
	if( ! lwgeom ) return;
1055 1056

	LWDEBUGF(5,"freeing a %s",lwtype_name(lwgeom->type));
1057
	
1058
	switch (lwgeom->type)
1059
	{
1060 1061 1062 1063 1064 1065 1066 1067 1068
	case POINTTYPE:
		lwpoint_free((LWPOINT *)lwgeom);
		break;
	case LINETYPE:
		lwline_free((LWLINE *)lwgeom);
		break;
	case POLYGONTYPE:
		lwpoly_free((LWPOLY *)lwgeom);
		break;
1069 1070 1071
	case CIRCSTRINGTYPE:
		lwcircstring_free((LWCIRCSTRING *)lwgeom);
		break;
1072 1073 1074
	case TRIANGLETYPE:
		lwtriangle_free((LWTRIANGLE *)lwgeom);
		break;
1075 1076 1077 1078 1079 1080 1081 1082 1083
	case MULTIPOINTTYPE:
		lwmpoint_free((LWMPOINT *)lwgeom);
		break;
	case MULTILINETYPE:
		lwmline_free((LWMLINE *)lwgeom);
		break;
	case MULTIPOLYGONTYPE:
		lwmpoly_free((LWMPOLY *)lwgeom);
		break;
1084 1085 1086
	case POLYHEDRALSURFACETYPE:
		lwpsurface_free((LWPSURFACE *)lwgeom);
		break;
1087 1088 1089
	case TINTYPE:
		lwtin_free((LWTIN *)lwgeom);
		break;
1090 1091 1092 1093
	case CURVEPOLYTYPE:
	case COMPOUNDTYPE:
	case MULTICURVETYPE:
	case MULTISURFACETYPE:
1094 1095 1096
	case COLLECTIONTYPE:
		lwcollection_free((LWCOLLECTION *)lwgeom);
		break;
1097
	default:
1098
		lwerror("lwgeom_free called with unknown type (%d) %s", lwgeom->type, lwtype_name(lwgeom->type));
1099
	}
1100
	return;
1101
}
1102

1103
int lwgeom_needs_bbox(const LWGEOM *geom)
1104 1105
{
	assert(geom);
1106
	if ( geom->type == POINTTYPE )
1107 1108 1109
	{
		return LW_FALSE;
	}
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
	else if ( geom->type == LINETYPE )
	{
		if ( lwgeom_count_vertices(geom) <= 2 )
			return LW_FALSE;
		else
			return LW_TRUE;
	}
	else if ( geom->type == MULTIPOINTTYPE )
	{
		if ( ((LWCOLLECTION*)geom)->ngeoms == 1 )
			return LW_FALSE;
		else
			return LW_TRUE;
	}
	else if ( geom->type == MULTILINETYPE )
	{
		if ( ((LWCOLLECTION*)geom)->ngeoms == 1 && lwgeom_count_vertices(geom) <= 2 )
			return LW_FALSE;
		else
			return LW_TRUE;
	}
	else
	{
		return LW_TRUE;
	}
1135 1136
}

1137 1138
/**
* Count points in an #LWGEOM.
1139
*/
1140
int lwgeom_count_vertices(const LWGEOM *geom)
1141 1142
{
	int result = 0;
1143 1144 1145 1146
	
	/* Null? Zero. */
	if( ! geom ) return 0;
	
1147 1148
	LWDEBUGF(4, "lwgeom_count_vertices got type %s",
	         lwtype_name(geom->type));
1149

1150 1151 1152
	/* Empty? Zero. */
	if( lwgeom_is_empty(geom) ) return 0;

1153
	switch (geom->type)
1154
	{
Paul Ramsey's avatar
Paul Ramsey committed
1155
	case POINTTYPE:
1156
		result = 1;
Paul Ramsey's avatar