1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 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 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 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 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
|
/**********************************************************************
*
* PostGIS - Spatial Types for PostgreSQL
* http://postgis.net
*
* PostGIS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* PostGIS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
*
**********************************************************************
*
* Copyright (C) 2012 Sandro Santilli <strk@kbt.io>
* Copyright (C) 2001-2006 Refractions Research Inc.
*
**********************************************************************/
/* basic LWLINE functions */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "liblwgeom_internal.h"
#include "lwgeom_log.h"
/*
* Construct a new LWLINE. points will *NOT* be copied
* use SRID=SRID_UNKNOWN for unknown SRID (will have 8bit type's S = 0)
*/
LWLINE *
lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
{
LWLINE *result;
result = (LWLINE*) lwalloc(sizeof(LWLINE));
LWDEBUG(2, "lwline_construct called.");
result->type = LINETYPE;
result->flags = points->flags;
FLAGS_SET_BBOX(result->flags, bbox?1:0);
LWDEBUGF(3, "lwline_construct type=%d", result->type);
result->srid = srid;
result->points = points;
result->bbox = bbox;
return result;
}
LWLINE *
lwline_construct_empty(int srid, char hasz, char hasm)
{
LWLINE *result = lwalloc(sizeof(LWLINE));
result->type = LINETYPE;
result->flags = gflags(hasz,hasm,0);
result->srid = srid;
result->points = ptarray_construct_empty(hasz, hasm, 1);
result->bbox = NULL;
return result;
}
void lwline_free (LWLINE *line)
{
if ( ! line ) return;
if ( line->bbox )
lwfree(line->bbox);
if ( line->points )
ptarray_free(line->points);
lwfree(line);
}
void printLWLINE(LWLINE *line)
{
lwnotice("LWLINE {");
lwnotice(" ndims = %i", (int)FLAGS_NDIMS(line->flags));
lwnotice(" srid = %i", (int)line->srid);
printPA(line->points);
lwnotice("}");
}
/* @brief Clone LWLINE object. Serialized point lists are not copied.
*
* @see ptarray_clone
*/
LWLINE *
lwline_clone(const LWLINE *g)
{
LWLINE *ret = lwalloc(sizeof(LWLINE));
LWDEBUGF(2, "lwline_clone called with %p", g);
memcpy(ret, g, sizeof(LWLINE));
ret->points = ptarray_clone(g->points);
if ( g->bbox ) ret->bbox = gbox_copy(g->bbox);
return ret;
}
/* Deep clone LWLINE object. POINTARRAY *is* copied. */
LWLINE *
lwline_clone_deep(const LWLINE *g)
{
LWLINE *ret = lwalloc(sizeof(LWLINE));
LWDEBUGF(2, "lwline_clone_deep called with %p", g);
memcpy(ret, g, sizeof(LWLINE));
if ( g->bbox ) ret->bbox = gbox_copy(g->bbox);
if ( g->points ) ret->points = ptarray_clone_deep(g->points);
FLAGS_SET_READONLY(ret->flags,0);
return ret;
}
void
lwline_release(LWLINE *lwline)
{
lwgeom_release(lwline_as_lwgeom(lwline));
}
void
lwline_reverse(LWLINE *line)
{
if ( lwline_is_empty(line) ) return;
ptarray_reverse(line->points);
}
LWLINE *
lwline_segmentize2d(LWLINE *line, double dist)
{
POINTARRAY *segmentized = ptarray_segmentize2d(line->points, dist);
if ( ! segmentized ) return NULL;
return lwline_construct(line->srid, NULL, segmentized);
}
/* check coordinate equality */
char
lwline_same(const LWLINE *l1, const LWLINE *l2)
{
return ptarray_same(l1->points, l2->points);
}
/*
* Construct a LWLINE from an array of point and line geometries
* LWLINE dimensions are large enough to host all input dimensions.
*/
LWLINE *
lwline_from_lwgeom_array(int srid, uint32_t ngeoms, LWGEOM **geoms)
{
int i;
int hasz = LW_FALSE;
int hasm = LW_FALSE;
POINTARRAY *pa;
LWLINE *line;
POINT4D pt;
LWPOINTITERATOR* it;
/*
* Find output dimensions, check integrity
*/
for (i=0; i<ngeoms; i++)
{
if ( FLAGS_GET_Z(geoms[i]->flags) ) hasz = LW_TRUE;
if ( FLAGS_GET_M(geoms[i]->flags) ) hasm = LW_TRUE;
if ( hasz && hasm ) break; /* Nothing more to learn! */
}
/*
* ngeoms should be a guess about how many points we have in input.
* It's an underestimate for lines and multipoints */
pa = ptarray_construct_empty(hasz, hasm, ngeoms);
for ( i=0; i < ngeoms; i++ )
{
LWGEOM *g = geoms[i];
if ( lwgeom_is_empty(g) ) continue;
if ( g->type == POINTTYPE )
{
lwpoint_getPoint4d_p((LWPOINT*)g, &pt);
ptarray_append_point(pa, &pt, LW_TRUE);
}
else if ( g->type == LINETYPE )
{
/*
* Append the new line points, de-duplicating against the previous points.
* Duplicated points internal to the linestring are untouched.
*/
ptarray_append_ptarray(pa, ((LWLINE*)g)->points, -1);
}
else if ( g->type == MULTIPOINTTYPE )
{
it = lwpointiterator_create(g);
while(lwpointiterator_next(it, &pt))
{
ptarray_append_point(pa, &pt, LW_TRUE);
}
lwpointiterator_destroy(it);
}
else
{
ptarray_free(pa);
lwerror("lwline_from_ptarray: invalid input type: %s", lwtype_name(g->type));
return NULL;
}
}
if ( pa->npoints > 0 )
line = lwline_construct(srid, NULL, pa);
else {
/* Is this really any different from the above ? */
ptarray_free(pa);
line = lwline_construct_empty(srid, hasz, hasm);
}
return line;
}
/*
* Construct a LWLINE from an array of LWPOINTs
* LWLINE dimensions are large enough to host all input dimensions.
*/
LWLINE *
lwline_from_ptarray(int srid, uint32_t npoints, LWPOINT **points)
{
int i;
int hasz = LW_FALSE;
int hasm = LW_FALSE;
POINTARRAY *pa;
LWLINE *line;
POINT4D pt;
/*
* Find output dimensions, check integrity
*/
for (i=0; i<npoints; i++)
{
if ( points[i]->type != POINTTYPE )
{
lwerror("lwline_from_ptarray: invalid input type: %s", lwtype_name(points[i]->type));
return NULL;
}
if ( FLAGS_GET_Z(points[i]->flags) ) hasz = LW_TRUE;
if ( FLAGS_GET_M(points[i]->flags) ) hasm = LW_TRUE;
if ( hasz && hasm ) break; /* Nothing more to learn! */
}
pa = ptarray_construct_empty(hasz, hasm, npoints);
for ( i=0; i < npoints; i++ )
{
if ( ! lwpoint_is_empty(points[i]) )
{
lwpoint_getPoint4d_p(points[i], &pt);
ptarray_append_point(pa, &pt, LW_TRUE);
}
}
if ( pa->npoints > 0 )
line = lwline_construct(srid, NULL, pa);
else
line = lwline_construct_empty(srid, hasz, hasm);
return line;
}
/*
* Construct a LWLINE from a LWMPOINT
*/
LWLINE *
lwline_from_lwmpoint(int srid, const LWMPOINT *mpoint)
{
uint32_t i;
POINTARRAY *pa = NULL;
LWGEOM *lwgeom = (LWGEOM*)mpoint;
POINT4D pt;
char hasz = lwgeom_has_z(lwgeom);
char hasm = lwgeom_has_m(lwgeom);
uint32_t npoints = mpoint->ngeoms;
if ( lwgeom_is_empty(lwgeom) )
{
return lwline_construct_empty(srid, hasz, hasm);
}
pa = ptarray_construct(hasz, hasm, npoints);
for (i=0; i < npoints; i++)
{
getPoint4d_p(mpoint->geoms[i]->point, 0, &pt);
ptarray_set_point4d(pa, i, &pt);
}
LWDEBUGF(3, "lwline_from_lwmpoint: constructed pointarray for %d points", mpoint->ngeoms);
return lwline_construct(srid, NULL, pa);
}
/**
* Returns freshly allocated #LWPOINT that corresponds to the index where.
* Returns NULL if the geometry is empty or the index invalid.
*/
LWPOINT*
lwline_get_lwpoint(const LWLINE *line, int where)
{
POINT4D pt;
LWPOINT *lwpoint;
POINTARRAY *pa;
if ( lwline_is_empty(line) || where < 0 || where >= line->points->npoints )
return NULL;
pa = ptarray_construct_empty(FLAGS_GET_Z(line->flags), FLAGS_GET_M(line->flags), 1);
pt = getPoint4d(line->points, where);
ptarray_append_point(pa, &pt, LW_TRUE);
lwpoint = lwpoint_construct(line->srid, NULL, pa);
return lwpoint;
}
int
lwline_add_lwpoint(LWLINE *line, LWPOINT *point, int where)
{
POINT4D pt;
getPoint4d_p(point->point, 0, &pt);
if ( ptarray_insert_point(line->points, &pt, where) != LW_SUCCESS )
return LW_FAILURE;
/* Update the bounding box */
if ( line->bbox )
{
lwgeom_drop_bbox(lwline_as_lwgeom(line));
lwgeom_add_bbox(lwline_as_lwgeom(line));
}
return LW_SUCCESS;
}
LWLINE *
lwline_removepoint(LWLINE *line, uint32_t index)
{
POINTARRAY *newpa;
LWLINE *ret;
newpa = ptarray_removePoint(line->points, index);
ret = lwline_construct(line->srid, NULL, newpa);
lwgeom_add_bbox((LWGEOM *) ret);
return ret;
}
/*
* Note: input will be changed, make sure you have permissions for this.
*/
void
lwline_setPoint4d(LWLINE *line, uint32_t index, POINT4D *newpoint)
{
ptarray_set_point4d(line->points, index, newpoint);
/* Update the box, if there is one to update */
if ( line->bbox )
{
lwgeom_drop_bbox((LWGEOM*)line);
lwgeom_add_bbox((LWGEOM*)line);
}
}
/**
* Re-write the measure ordinate (or add one, if it isn't already there) interpolating
* the measure between the supplied start and end values.
*/
LWLINE*
lwline_measured_from_lwline(const LWLINE *lwline, double m_start, double m_end)
{
int i = 0;
int hasm = 0, hasz = 0;
int npoints = 0;
double length = 0.0;
double length_so_far = 0.0;
double m_range = m_end - m_start;
double m;
POINTARRAY *pa = NULL;
POINT3DZ p1, p2;
if ( lwline->type != LINETYPE )
{
lwerror("lwline_construct_from_lwline: only line types supported");
return NULL;
}
hasz = FLAGS_GET_Z(lwline->flags);
hasm = 1;
/* Null points or npoints == 0 will result in empty return geometry */
if ( lwline->points )
{
npoints = lwline->points->npoints;
length = ptarray_length_2d(lwline->points);
getPoint3dz_p(lwline->points, 0, &p1);
}
pa = ptarray_construct(hasz, hasm, npoints);
for ( i = 0; i < npoints; i++ )
{
POINT4D q;
POINT2D a, b;
getPoint3dz_p(lwline->points, i, &p2);
a.x = p1.x;
a.y = p1.y;
b.x = p2.x;
b.y = p2.y;
length_so_far += distance2d_pt_pt(&a, &b);
if ( length > 0.0 )
m = m_start + m_range * length_so_far / length;
/* #3172, support (valid) zero-length inputs */
else if ( length == 0.0 && npoints > 1 )
m = m_start + m_range * i / (npoints-1);
else
m = 0.0;
q.x = p2.x;
q.y = p2.y;
q.z = p2.z;
q.m = m;
ptarray_set_point4d(pa, i, &q);
p1 = p2;
}
return lwline_construct(lwline->srid, NULL, pa);
}
LWGEOM*
lwline_remove_repeated_points(const LWLINE *lwline, double tolerance)
{
POINTARRAY* npts = ptarray_remove_repeated_points_minpoints(lwline->points, tolerance, 2);
LWDEBUGF(3, "%s: npts %p", __func__, npts);
return (LWGEOM*)lwline_construct(lwline->srid,
lwline->bbox ? gbox_copy(lwline->bbox) : 0,
npts);
}
int
lwline_is_closed(const LWLINE *line)
{
if (FLAGS_GET_Z(line->flags))
return ptarray_is_closed_3d(line->points);
return ptarray_is_closed_2d(line->points);
}
int
lwline_is_trajectory(const LWLINE *line)
{
POINT3DM p;
int i, n;
double m = -1 * FLT_MAX;
if ( ! FLAGS_GET_M(line->flags) ) {
lwnotice("Line does not have M dimension");
return LW_FALSE;
}
n = line->points->npoints;
if ( n < 2 ) return LW_TRUE; /* empty or single-point are "good" */
for (i=0; i<n; ++i) {
getPoint3dm_p(line->points, i, &p);
if ( p.m <= m ) {
lwnotice("Measure of vertex %d (%g) not bigger than measure of vertex %d (%g)",
i, p.m, i-1, m);
return LW_FALSE;
}
m = p.m;
}
return LW_TRUE;
}
LWLINE*
lwline_force_dims(const LWLINE *line, int hasz, int hasm)
{
POINTARRAY *pdims = NULL;
LWLINE *lineout;
/* Return 2D empty */
if( lwline_is_empty(line) )
{
lineout = lwline_construct_empty(line->srid, hasz, hasm);
}
else
{
pdims = ptarray_force_dims(line->points, hasz, hasm);
lineout = lwline_construct(line->srid, NULL, pdims);
}
lineout->type = line->type;
return lineout;
}
int lwline_is_empty(const LWLINE *line)
{
if ( !line->points || line->points->npoints < 1 )
return LW_TRUE;
return LW_FALSE;
}
int lwline_count_vertices(LWLINE *line)
{
assert(line);
if ( ! line->points )
return 0;
return line->points->npoints;
}
LWLINE* lwline_simplify(const LWLINE *iline, double dist, int preserve_collapsed)
{
static const int minvertices = 2; /* TODO: allow setting this */
LWLINE *oline;
POINTARRAY *pa;
LWDEBUG(2, "function called");
/* Skip empty case */
if( lwline_is_empty(iline) )
return NULL;
pa = ptarray_simplify(iline->points, dist, minvertices);
if ( ! pa ) return NULL;
/* Make sure single-point collapses have two points */
if ( pa->npoints == 1 )
{
/* Make sure single-point collapses have two points */
if ( preserve_collapsed )
{
POINT4D pt;
getPoint4d_p(pa, 0, &pt);
ptarray_append_point(pa, &pt, LW_TRUE);
}
/* Return null for collapse */
else
{
ptarray_free(pa);
return NULL;
}
}
oline = lwline_construct(iline->srid, NULL, pa);
oline->type = iline->type;
return oline;
}
double lwline_length(const LWLINE *line)
{
if ( lwline_is_empty(line) )
return 0.0;
return ptarray_length(line->points);
}
double lwline_length_2d(const LWLINE *line)
{
if ( lwline_is_empty(line) )
return 0.0;
return ptarray_length_2d(line->points);
}
LWLINE* lwline_grid(const LWLINE *line, const gridspec *grid)
{
LWLINE *oline;
POINTARRAY *opa;
opa = ptarray_grid(line->points, grid);
/* Skip line3d with less then 2 points */
if ( opa->npoints < 2 ) return NULL;
/* TODO: grid bounding box... */
oline = lwline_construct(line->srid, NULL, opa);
return oline;
}
|