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 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
|
/*
* Linear reference system library
*/
/******************************************************************************
* Copyright (c) 2004, Radim Blazek (blazek@itc.it)
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <grass/gis.h>
#include <grass/dbmi.h>
#include "lrs.h"
/* threshold used if 2 offsets are checked if identical, necessary because of
* representation error */
#define LRS_THRESH 1e-10
typedef struct
{
int lcat, lid;
double start_map, end_map;
double start_mp, start_off; /* milepost, offset for the beginning of ref. segment */
double end_mp, end_off; /* milepost, offset for the end of ref. segment */
} RSEGMENT;
/* Compare 2 segments by start_mp and start_off, used by qsort */
int cmp_rsegment(const void *pa, const void *pb)
{
RSEGMENT *p1 = (RSEGMENT *) pa;
RSEGMENT *p2 = (RSEGMENT *) pb;
return LR_cmp_mileposts(p1->start_mp, p1->start_off, p2->start_mp,
p2->start_off);
}
/* For given line cat and offset along the line in the map
* find line id and mpost (milepost) + offset in real world in rstable in opened database.
* multip - multiplier specifies number of map units in mpost unit
* Returns: 0 - no record found
* 1 - one record
* 2 - too many records -> ambiguous
*/
int LR_get_milepost(dbDriver * driver, char *table_name,
char *lcat_col, char *lid_col,
char *start_map_col, char *end_map_col,
char *start_mp_col, char *start_off_col,
char *end_mp_col, char *end_off_col,
int line_cat, double map_offset,
double multip, int *lid, double *mpost, double *offset)
{
int i, more, mp;
char buf[2000];
double moff, off, soff, roff;
dbString stmt;
dbCursor cursor;
dbTable *table;
dbColumn *column;
dbValue *value;
RSEGMENT *rseg;
int nrseg;
double length, map_length, k;
G_debug(4, "LR_get_milepost() line_cat = %d, map_offset = %f", line_cat,
map_offset);
*lid = 0;
*mpost = *offset = 0.0;
/* Because some drivers (dbf) do not support complex queries mixing OR and AND
* more records with simple condition are selected and processed here */
sprintf(buf, "select %s, %s, %s, %s, %s, %s, %s from %s where " "%s = %d and " /* lcat_col = line_cat */
"%s <= %f and " /* start_map_col <= map_offset */
"%s >= %f", /* end_map_col >= map_offset */
lid_col, start_map_col, end_map_col,
start_mp_col, start_off_col, end_mp_col, end_off_col,
table_name,
lcat_col, line_cat,
start_map_col, map_offset, end_map_col, map_offset);
G_debug(3, " SQL: %s", buf);
db_init_string(&stmt);
db_set_string(&stmt, buf);
if (db_open_select_cursor(driver, &stmt, &cursor, DB_SEQUENTIAL) != DB_OK)
G_fatal_error("Cannot select records from LRS table:\n%s", buf);
table = db_get_cursor_table(&cursor);
nrseg = db_get_num_rows(&cursor);
G_debug(3, " nrseg = %d", nrseg);
if (nrseg <= 0)
return 0;
if (nrseg >= 3)
return 2;
rseg = (RSEGMENT *) G_malloc(nrseg * sizeof(RSEGMENT));
i = 0;
while (1) {
if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK)
G_fatal_error("Cannot fetch line id from line table");
if (!more)
break;
column = db_get_table_column(table, 0); /* first column */
value = db_get_column_value(column);
rseg[i].lid = db_get_value_int(value);
column = db_get_table_column(table, 1);
value = db_get_column_value(column);
rseg[i].start_map = db_get_value_double(value);
column = db_get_table_column(table, 2);
value = db_get_column_value(column);
rseg[i].end_map = db_get_value_double(value);
column = db_get_table_column(table, 3);
value = db_get_column_value(column);
rseg[i].start_mp = db_get_value_double(value);
column = db_get_table_column(table, 4);
value = db_get_column_value(column);
rseg[i].start_off = db_get_value_double(value);
column = db_get_table_column(table, 5);
value = db_get_column_value(column);
rseg[i].end_mp = db_get_value_double(value);
column = db_get_table_column(table, 6);
value = db_get_column_value(column);
rseg[i].end_off = db_get_value_double(value);
i++;
}
db_close_cursor(&cursor);
/* Two segments may be selected for one point if they share common MPs */
/* If 2 segments were found, check if it is correct */
if (nrseg == 2) {
G_debug(4, "rseg[0].lid = %d rseg[1].lid = %d", rseg[0].lid,
rseg[1].lid);
G_debug(4, "rseg[0].start_map = %f rseg[0].end_map = %f",
rseg[0].start_map, rseg[0].end_map);
G_debug(4, "rseg[1].start_map = %f rseg[1].end_map = %f",
rseg[1].start_map, rseg[1].end_map);
/* Warning: start_map/end_map stored in the table may lose precision and will not be equal to
* map_offset */
if (rseg[0].lid == rseg[1].lid &&
rseg[0].end_map == rseg[1].start_map) {
G_debug(4,
" point at the end of 1. end beginning of 2. segment -> OK");
*lid = rseg[0].lid;
*mpost = rseg[1].start_mp;
*offset = rseg[1].start_off;
return 1;
}
else if (rseg[0].lid == rseg[1].lid &&
rseg[1].end_map == rseg[0].start_map) {
G_debug(4,
" point at the end of 2. end beginning of 1. segment -> OK");
*lid = rseg[0].lid;
*mpost = rseg[0].start_mp;
*offset = rseg[0].start_off;
return 1;
}
else {
G_debug(4, " too many segments found in the table -> error ");
return 2;
}
}
/* One segment was found -> calculate map_offset */
*lid = rseg[0].lid;
/* Real world length of segment in offset units */
length =
(rseg[0].end_map + rseg[0].end_off) - (rseg[0].start_map +
rseg[0].start_off);
map_length = rseg[0].end_map - rseg[0].start_map;
k = map_length / length;
G_debug(4,
" rseg[0].end_mp=%f, rseg[0].end_off=%f, rseg[0].start_mp=%f, rseg[0].start_off=%f, multip=%f",
rseg[0].end_mp, rseg[0].end_off, rseg[0].start_mp,
rseg[0].start_off, multip);
G_debug(4, " seg length=%f, seg map_length=%f, k=%f", length, map_length,
k);
/* Milepost and offset */
/* Offset from start milepost measured in map in map units */
moff = map_offset - rseg[0].start_map;
/* Offset from start milepost in real world in offset units */
soff = moff / k;
/* Offset in real world (from the beginning of 'feature') in offset units */
roff = multip * rseg[0].start_mp + rseg[0].start_off + soff;
G_debug(4, " moff = %f soff = %f roff = %f", moff, soff, roff);
/* Translate offset to milepost + offset */
mp = (int)roff / multip;
off = roff - mp * multip;
G_debug(4, " mp = %d off = %f", mp, off);
/* It may happen, that 'end_off > multip', in that case mpost could be now > rseg[0].end_mp
* which is incorrect */
if (mp > rseg[0].end_mp) {
mp = rseg[0].end_mp;
off = roff - mp * multip;
}
G_debug(4, " mp = %d off = %f", mp, off);
*mpost = mp;
*offset = off;
return 1;
}
/* For given lid (line id), mpost (milepost) + offset in real world find
* line_cat and map_offset along the line in the map from rstable
* in opened database.
* multip - multiplier specifies number of map units in mpost unit
* Returns: 0 - no offset found
* 1 - one offset found
* 3 - 2 or more records exactly at requested position with different line_cat/map_offset,
* the line_cat/map_offset is set to first found in direction up
*/
int LR_get_offset(dbDriver * driver, char *table_name,
char *lcat_col, char *lid_col,
char *start_map_col, char *end_map_col,
char *start_mp_col, char *start_off_col,
char *end_mp_col, char *end_off_col,
int lid, double mpost, double offset,
double multip, int *line_cat, double *map_offset)
{
int ret;
G_debug(3, "LR_get_offset() lid = %d, mpost = %f, offset = %f", lid,
mpost, offset);
/* direction down -> first segment returned if multiple found */
ret = LR_get_nearest_offset(driver, table_name, lcat_col, lid_col,
start_map_col, end_map_col,
start_mp_col, start_off_col, end_mp_col,
end_off_col, lid, mpost, offset, multip, 1,
line_cat, map_offset);
if (ret == 2)
return 0;
return ret;
}
/* Find if requested lid/mpost/offset falls into given RSEGMENT
*
* rseg - pointer to RSEGMENT
* multip - multiplier specifies number of map units in mpost unit
* mpost - requested MP
* offset - requested offset
*
* map_offset - output offset if the mpost/offset falls in rseg
*
* Return: 0 - outside, map_offset not set
* 1 - OK, mpost/offset falls in rseg, map_offset set
*/
int offset_in_rsegment(RSEGMENT * rseg, double multip, double mpost,
double offset, double *map_offset)
{
int ret;
double length, map_length, k;
G_debug(3, "offset_in_rsegment: %f+%f rseg: %f+%f - %f+%f", mpost,
offset, rseg->start_mp, rseg->start_off, rseg->end_mp,
rseg->end_off);
*map_offset = 0.0;
/* Check if >= start */
ret = LR_cmp_mileposts(mpost, offset, rseg->start_mp, rseg->start_off);
if (ret == -1) {
G_debug(4, " < start");
return 0;
}
/* Check if <= end */
ret = LR_cmp_mileposts(mpost, offset, rseg->end_mp, rseg->end_off);
if (ret == 1) {
G_debug(4, " > end");
return 0;
}
/* Within segment -> calculate map_offset */
length = (multip * rseg->end_mp + rseg->end_off) -
(multip * rseg->start_mp + rseg->start_off);
map_length = rseg->end_map - rseg->start_map;
k = map_length / length;
G_debug(4, " seg length = %f seg map_length = %f k = %f", length,
map_length, k);
/* length from last MP in real world */
length = (multip * mpost + offset) -
(multip * rseg->start_mp + rseg->start_off);
G_debug(4, " length in real world from previous milepost = %f", length);
*map_offset = rseg->start_map + k * length;
G_debug(3, "map_offset = %f", *map_offset);
return 1;
}
/* For given lid (line id), mpost (milepost) + offset in real world find
* nearest available (along the feature in specified direction)
* line_cat and map_offset along the line in the map from rstable
* in opened database.
* For example, if RS table contains only segment 2+000 - 3+000 and requested
* position is 1+500/direction up, the point returned will be 2+000
*
* multip - multiplier specifies number of map units in mpost unit
* direction - direction in which search the nearest available position on linear feature
* 0 - up (in the direction to the end)
* 1 - down (in the direction to the beginning, to 0)
* Returns: 0 - offset not found
* 1 - one offset found exactly at requested position
* 2 - one nearest offset found in requested direction
* 3 - 2 or more records exactly at requested position with different line_cat/map_offset,
* the line_cat/map_offset is set to the last segment in the specified direction,
* i.e. if direction is down, the first segment in direction up is used
* (this is suitable for segment creation)
*
*/
int LR_get_nearest_offset(dbDriver * driver, char *table_name,
char *lcat_col, char *lid_col,
char *start_map_col, char *end_map_col,
char *start_mp_col, char *start_off_col,
char *end_mp_col, char *end_off_col,
int lid, double mpost, double offset,
double multip, int direction,
int *line_cat, double *map_offset)
{
int i, more, ret;
char buf[2000];
dbString stmt;
dbCursor cursor;
dbTable *table;
dbColumn *column;
dbValue *value;
RSEGMENT *rseg;
int nrseg;
int current_line_cat;
double current_map_offset;
int seg_found; /* number of segments found containing the offset */
int differ; /* more segments with different line_cat/offset */
int nearest; /* nearest found */
G_debug(2, "LR_get_offset() lid = %d, mpost = %f, offset = %f", lid,
mpost, offset);
*line_cat = 0;
*map_offset = 0;
/* 1. select all available records for requested lid
* 2. order by start_mp, start_off (dbf driver does not support order by more columns)
* 3. search in requested direction until either
* a. requested position is reached
* b. next nearest segment is reached
*/
sprintf(buf, "select %s, %s, %s, %s, %s, %s, %s from %s where %s = %d",
lcat_col, start_map_col, end_map_col,
start_mp_col, start_off_col, end_mp_col, end_off_col,
table_name, lid_col, lid);
G_debug(3, " SQL: %s", buf);
db_init_string(&stmt);
db_append_string(&stmt, buf);
if (db_open_select_cursor(driver, &stmt, &cursor, DB_SEQUENTIAL) != DB_OK)
G_fatal_error("Cannot select records from LRS table:\n%s", buf);
table = db_get_cursor_table(&cursor);
nrseg = db_get_num_rows(&cursor);
G_debug(3, "nrseg = %d", nrseg);
rseg = (RSEGMENT *) G_malloc(nrseg * sizeof(RSEGMENT));
i = 0;
while (1) {
if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK)
G_fatal_error("Cannot fetch line id from line table");
if (!more)
break;
column = db_get_table_column(table, 0); /* first column */
value = db_get_column_value(column);
rseg[i].lcat = db_get_value_int(value);
column = db_get_table_column(table, 1);
value = db_get_column_value(column);
rseg[i].start_map = db_get_value_double(value);
column = db_get_table_column(table, 2);
value = db_get_column_value(column);
rseg[i].end_map = db_get_value_double(value);
column = db_get_table_column(table, 3);
value = db_get_column_value(column);
rseg[i].start_mp = db_get_value_double(value);
column = db_get_table_column(table, 4);
value = db_get_column_value(column);
rseg[i].start_off = db_get_value_double(value);
column = db_get_table_column(table, 5);
value = db_get_column_value(column);
rseg[i].end_mp = db_get_value_double(value);
column = db_get_table_column(table, 6);
value = db_get_column_value(column);
rseg[i].end_off = db_get_value_double(value);
G_debug(3, " start_mp = %f start_off = %f", rseg[i].start_mp,
rseg[i].start_off);
i++;
}
db_close_cursor(&cursor);
qsort((void *)rseg, (unsigned int)nrseg, sizeof(RSEGMENT), cmp_rsegment);
/* Select the right segment */
/* Two segments may be selected for one point if they share common MPs */
seg_found = 0;
differ = 0;
nearest = 0;
if (direction == 0) { /* direction up */
for (i = 0; i < nrseg; i++) {
double off;
G_debug(3, "up: %d: %f - %f %f+%f %f+%f", rseg[i].lcat,
rseg[i].start_map, rseg[i].end_map,
rseg[i].start_mp, rseg[i].start_off,
rseg[i].end_mp, rseg[i].end_off);
ret = offset_in_rsegment(&(rseg[i]), multip, mpost, offset, &off);
if (ret == 0) { /* outside segment */
if (seg_found == 0) {
/* It can be that we passed already requested position, in that case
* return the first found */
if (-1 ==
LR_cmp_mileposts(mpost, offset, rseg[i].start_mp,
rseg[i].start_off)) {
current_line_cat = rseg[i].lcat;
current_map_offset = off;
G_debug(3,
"requested position passed -> use nearest: line_cat = %d, map_offset = %f ",
current_line_cat, current_map_offset);
seg_found = 1;
nearest = 1;
break;
}
else {
continue; /* not yet reached */
}
}
else {
break; /* segment found and passed */
}
}
if (seg_found) { /* next found */
/* Check if it is the same offset */
/* Because offsets calculated from different segments may differ because of representation
* error we have to use LRS_THRESH */
/* TODO: avoid LRS_THRESH ? */
if (rseg[i].lcat != current_line_cat ||
fabs(off - current_map_offset) > LRS_THRESH) {
G_debug(3,
"%d != %d || fab(soff-current_map_offset) = %e > LRS_THRESH",
rseg[i].lcat, current_line_cat,
fabs(off - current_map_offset));
differ = 1;
}
}
current_line_cat = rseg[i].lcat;
current_map_offset = off;
seg_found++;
}
}
else { /* direction down */
for (i = nrseg - 1; i >= 0; i--) {
double off;
G_debug(3, "down: %d: %f - %f %f+%f %f+%f", rseg[i].lcat,
rseg[i].start_map, rseg[i].end_map,
rseg[i].start_mp, rseg[i].start_off,
rseg[i].end_mp, rseg[i].end_off);
ret = offset_in_rsegment(&(rseg[i]), multip, mpost, offset, &off);
if (ret == 0) { /* outside segment */
if (seg_found == 0) {
/* It can be that we passed already requested position, in that case
* return the first found */
if (1 ==
LR_cmp_mileposts(mpost, offset, rseg[i].end_mp,
rseg[i].end_off)) {
current_line_cat = rseg[i].lcat;
current_map_offset = off;
G_debug(3,
"requested position passed -> use nearest: line_cat = %d, map_offset = %f ",
current_line_cat, current_map_offset);
seg_found = 1;
nearest = 1;
break;
}
else {
continue; /* not yet reached */
}
}
else {
break; /* segment found and passed */
}
}
if (seg_found) { /* next found */
/* Check if it is the same offset */
/* Because offsets calculated from different segments may differ because of representation
* error we have to use LRS_THRESH */
/* TODO: avoid LRS_THRESH ? */
if (rseg[i].lcat != current_line_cat ||
fabs(off - current_map_offset) > LRS_THRESH) {
G_debug(3,
"%d != %d || fab(soff-current_map_offset) = %e > LRS_THRESH",
rseg[i].lcat, current_line_cat,
fabs(off - current_map_offset));
differ = 1;
}
}
current_line_cat = rseg[i].lcat;
current_map_offset = off;
seg_found++;
}
}
free(rseg);
/* Was any segment found ? */
if (seg_found == 0) { /* no */
G_debug(2, " no segment found in the reference table");
return 0;
}
G_debug(2, " lcat = %d map_offset = %f", current_line_cat,
current_map_offset);
*line_cat = current_line_cat;
*map_offset = current_map_offset;
/* More segments */
if (seg_found > 1) {
if (differ) {
G_debug(2,
" point within more segments with different line_cat/map_offset");
return 3;
}
else {
G_debug(2,
" point within more segments with the same line_cat/map_offset (OK)");
}
}
if (nearest)
return 2;
return 1;
}
/* Compare 2 mileposts
* Returns: -1 if 1. < 2.
* 0 1. = 2.
* 1 1. > 2.
*/
int LR_cmp_mileposts(double mp1, double off1, double mp2, double off2)
{
G_debug(3, "LR_cmp_mileposts(): %f+%f x %f+%f", mp1, off1, mp2, off2);
/* Note: this should be safe, because mp may be only integers */
/* compare mp */
if (mp1 < mp2) {
G_debug(4, "1. < 2.");
return -1;
}
if (mp1 > mp2) {
G_debug(4, "1. > 2.");
return 1;
}
/* the same mp -> compare off */
if (off1 < off2) {
G_debug(4, "1. < 2.");
return -1;
}
if (off1 > off2) {
G_debug(4, "1. > 2.");
return 1;
}
G_debug(4, "1. = 2.");
return 0;
}
|