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 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 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 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 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 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 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 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
|
/******************************************************************************
* $Id: ogr_api.cpp 22416 2011-05-22 20:19:45Z rouault $
*
* Project: OpenGIS Simple Features Reference Implementation
* Purpose: C API Functions that don't correspond one-to-one with C++
* methods, such as the "simplified" geometry access functions.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
******************************************************************************
* Copyright (c) 2002, Frank Warmerdam
*
* 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 "ogr_geometry.h"
#include "ogr_api.h"
#include "cpl_error.h"
/************************************************************************/
/* OGR_G_GetPointCount() */
/************************************************************************/
/**
* \brief Fetch number of points from a geometry.
*
* Only wkbPoint[25D] or wkbLineString[25D] may return a valid value.
* Other geometry types will silently return 0.
*
* @param hGeom handle to the geometry from which to get the number of points.
* @return the number of points.
*/
int OGR_G_GetPointCount( OGRGeometryH hGeom )
{
VALIDATE_POINTER1( hGeom, "OGR_G_GetPointCount", 0 );
switch( wkbFlatten(((OGRGeometry *) hGeom)->getGeometryType()) )
{
case wkbPoint:
return 1;
case wkbLineString:
{
OGRLineString *poLine = (OGRLineString *) hGeom;
return poLine->getNumPoints();
}
default:
// autotest/pymod/ogrtest.py calls this method on any geometry. So keep silent
//CPLError(CE_Failure, CPLE_NotSupported, "Incompatible geometry for operation");
return 0;
}
}
/************************************************************************/
/* OGR_G_GetX() */
/************************************************************************/
/**
* \brief Fetch the x coordinate of a point from a geometry.
*
* @param hGeom handle to the geometry from which to get the x coordinate.
* @param i point to get the x coordinate.
* @return the X coordinate of this point.
*/
double OGR_G_GetX( OGRGeometryH hGeom, int i )
{
VALIDATE_POINTER1( hGeom, "OGR_G_GetX", 0 );
switch( wkbFlatten(((OGRGeometry *) hGeom)->getGeometryType()) )
{
case wkbPoint:
{
if( i == 0 )
return ((OGRPoint *) hGeom)->getX();
else
{
CPLError(CE_Failure, CPLE_NotSupported, "Only i == 0 is supported");
return 0.0;
}
}
case wkbLineString:
{
OGRLineString* poLS = (OGRLineString *) hGeom;
if (i < 0 || i >= poLS->getNumPoints())
{
CPLError(CE_Failure, CPLE_NotSupported, "Index out of bounds");
return 0.0;
}
return poLS->getX( i );
}
default:
CPLError(CE_Failure, CPLE_NotSupported, "Incompatible geometry for operation");
return 0.0;
}
}
/************************************************************************/
/* OGR_G_GetY() */
/************************************************************************/
/**
* \brief Fetch the x coordinate of a point from a geometry.
*
* @param hGeom handle to the geometry from which to get the y coordinate.
* @param i point to get the Y coordinate.
* @return the Y coordinate of this point.
*/
double OGR_G_GetY( OGRGeometryH hGeom, int i )
{
VALIDATE_POINTER1( hGeom, "OGR_G_GetY", 0 );
switch( wkbFlatten(((OGRGeometry *) hGeom)->getGeometryType()) )
{
case wkbPoint:
{
if( i == 0 )
return ((OGRPoint *) hGeom)->getY();
else
{
CPLError(CE_Failure, CPLE_NotSupported, "Only i == 0 is supported");
return 0.0;
}
}
case wkbLineString:
{
OGRLineString* poLS = (OGRLineString *) hGeom;
if (i < 0 || i >= poLS->getNumPoints())
{
CPLError(CE_Failure, CPLE_NotSupported, "Index out of bounds");
return 0.0;
}
return poLS->getY( i );
}
default:
CPLError(CE_Failure, CPLE_NotSupported, "Incompatible geometry for operation");
return 0.0;
}
}
/************************************************************************/
/* OGR_G_GetZ() */
/************************************************************************/
/**
* \brief Fetch the z coordinate of a point from a geometry.
*
* @param hGeom handle to the geometry from which to get the Z coordinate.
* @param i point to get the Z coordinate.
* @return the Z coordinate of this point.
*/
double OGR_G_GetZ( OGRGeometryH hGeom, int i )
{
VALIDATE_POINTER1( hGeom, "OGR_G_GetZ", 0 );
switch( wkbFlatten(((OGRGeometry *) hGeom)->getGeometryType()) )
{
case wkbPoint:
{
if( i == 0 )
return ((OGRPoint *) hGeom)->getZ();
else
{
CPLError(CE_Failure, CPLE_NotSupported, "Only i == 0 is supported");
return 0.0;
}
}
case wkbLineString:
{
OGRLineString* poLS = (OGRLineString *) hGeom;
if (i < 0 || i >= poLS->getNumPoints())
{
CPLError(CE_Failure, CPLE_NotSupported, "Index out of bounds");
return 0.0;
}
return poLS->getZ( i );
}
default:
CPLError(CE_Failure, CPLE_NotSupported, "Incompatible geometry for operation");
return 0.0;
}
}
/************************************************************************/
/* OGR_G_GetPoints() */
/************************************************************************/
/**
* \brief Returns all points of line string.
*
* This method copies all points into user arrays. The user provides the
* stride between 2 consecutives elements of the array.
*
* On some CPU architectures, care must be taken so that the arrays are properly aligned.
*
* @param hGeom handle to the geometry from which to get the coordinates.
* @param pabyX a buffer of at least (sizeof(double) * nXStride * nPointCount) bytes, may be NULL.
* @param nXStride the number of bytes between 2 elements of pabyX.
* @param pabyY a buffer of at least (sizeof(double) * nYStride * nPointCount) bytes, may be NULL.
* @param nYStride the number of bytes between 2 elements of pabyY.
* @param pabyZ a buffer of at last size (sizeof(double) * nZStride * nPointCount) bytes, may be NULL.
* @param nZStride the number of bytes between 2 elements of pabyZ.
*
* @return the number of points
*
* @since OGR 1.9.0
*/
int OGR_G_GetPoints( OGRGeometryH hGeom,
void* pabyX, int nXStride,
void* pabyY, int nYStride,
void* pabyZ, int nZStride)
{
VALIDATE_POINTER1( hGeom, "OGR_G_GetPoints", 0 );
switch( wkbFlatten(((OGRGeometry *) hGeom)->getGeometryType()) )
{
case wkbPoint:
{
if (pabyX) *((double*)pabyX) = ((OGRPoint *)hGeom)->getX();
if (pabyY) *((double*)pabyY) = ((OGRPoint *)hGeom)->getY();
if (pabyZ) *((double*)pabyZ) = ((OGRPoint *)hGeom)->getZ();
return 1;
}
break;
case wkbLineString:
{
OGRLineString* poLS = (OGRLineString *) hGeom;
poLS->getPoints(pabyX, nXStride, pabyY, nYStride, pabyZ, nZStride);
return poLS->getNumPoints();
}
break;
default:
CPLError(CE_Failure, CPLE_NotSupported, "Incompatible geometry for operation");
return 0;
break;
}
}
/************************************************************************/
/* OGR_G_GetPoint() */
/************************************************************************/
/**
* \brief Fetch a point in line string or a point geometry.
*
* @param hGeom handle to the geometry from which to get the coordinates.
* @param i the vertex to fetch, from 0 to getNumPoints()-1, zero for a point.
* @param pdfX value of x coordinate.
* @param pdfY value of y coordinate.
* @param pdfZ value of z coordinate.
*/
void OGR_G_GetPoint( OGRGeometryH hGeom, int i,
double *pdfX, double *pdfY, double *pdfZ )
{
VALIDATE_POINTER0( hGeom, "OGR_G_GetPoint" );
switch( wkbFlatten(((OGRGeometry *) hGeom)->getGeometryType()) )
{
case wkbPoint:
{
if( i == 0 )
{
*pdfX = ((OGRPoint *)hGeom)->getX();
*pdfY = ((OGRPoint *)hGeom)->getY();
if( pdfZ != NULL )
*pdfZ = ((OGRPoint *)hGeom)->getZ();
}
else
{
CPLError(CE_Failure, CPLE_NotSupported, "Only i == 0 is supported");
}
}
break;
case wkbLineString:
{
OGRLineString* poLS = (OGRLineString *) hGeom;
if (i < 0 || i >= poLS->getNumPoints())
{
CPLError(CE_Failure, CPLE_NotSupported, "Index out of bounds");
*pdfX = *pdfY = 0;
if( pdfZ != NULL )
*pdfZ = 0;
}
else
{
*pdfX = poLS->getX( i );
*pdfY = poLS->getY( i );
if( pdfZ != NULL )
*pdfZ = poLS->getZ( i );
}
}
break;
default:
CPLError(CE_Failure, CPLE_NotSupported, "Incompatible geometry for operation");
break;
}
}
/************************************************************************/
/* OGR_G_SetPoint() */
/************************************************************************/
/**
* \brief Set the location of a vertex in a point or linestring geometry.
*
* If iPoint is larger than the number of existing
* points in the linestring, the point count will be increased to
* accommodate the request.
*
* @param hGeom handle to the geometry to add a vertex to.
* @param i the index of the vertex to assign (zero based) or
* zero for a point.
* @param dfX input X coordinate to assign.
* @param dfY input Y coordinate to assign.
* @param dfZ input Z coordinate to assign (defaults to zero).
*/
void OGR_G_SetPoint( OGRGeometryH hGeom, int i,
double dfX, double dfY, double dfZ )
{
VALIDATE_POINTER0( hGeom, "OGR_G_SetPoint" );
switch( wkbFlatten(((OGRGeometry *) hGeom)->getGeometryType()) )
{
case wkbPoint:
{
if( i == 0 )
{
((OGRPoint *) hGeom)->setX( dfX );
((OGRPoint *) hGeom)->setY( dfY );
((OGRPoint *) hGeom)->setZ( dfZ );
}
else
{
CPLError(CE_Failure, CPLE_NotSupported, "Only i == 0 is supported");
}
}
break;
case wkbLineString:
{
if (i < 0)
{
CPLError(CE_Failure, CPLE_NotSupported, "Index out of bounds");
return;
}
((OGRLineString *) hGeom)->setPoint( i, dfX, dfY, dfZ );
break;
}
default:
CPLError(CE_Failure, CPLE_NotSupported, "Incompatible geometry for operation");
break;
}
}
/************************************************************************/
/* OGR_G_SetPoint_2D() */
/************************************************************************/
/**
* \brief Set the location of a vertex in a point or linestring geometry.
*
* If iPoint is larger than the number of existing
* points in the linestring, the point count will be increased to
* accommodate the request.
*
* @param hGeom handle to the geometry to add a vertex to.
* @param i the index of the vertex to assign (zero based) or
* zero for a point.
* @param dfX input X coordinate to assign.
* @param dfY input Y coordinate to assign.
*/
void OGR_G_SetPoint_2D( OGRGeometryH hGeom, int i,
double dfX, double dfY )
{
VALIDATE_POINTER0( hGeom, "OGR_G_SetPoint_2D" );
switch( wkbFlatten(((OGRGeometry *) hGeom)->getGeometryType()) )
{
case wkbPoint:
{
if( i == 0 )
{
((OGRPoint *) hGeom)->setX( dfX );
((OGRPoint *) hGeom)->setY( dfY );
}
else
{
CPLError(CE_Failure, CPLE_NotSupported, "Only i == 0 is supported");
}
}
break;
case wkbLineString:
{
if (i < 0)
{
CPLError(CE_Failure, CPLE_NotSupported, "Index out of bounds");
return;
}
((OGRLineString *) hGeom)->setPoint( i, dfX, dfY );
break;
}
default:
CPLError(CE_Failure, CPLE_NotSupported, "Incompatible geometry for operation");
break;
}
}
/************************************************************************/
/* OGR_G_AddPoint() */
/************************************************************************/
/**
* \brief Add a point to a geometry (line string or point).
*
* The vertex count of the line string is increased by one, and assigned from
* the passed location value.
*
* @param hGeom handle to the geometry to add a point to.
* @param dfX x coordinate of point to add.
* @param dfY y coordinate of point to add.
* @param dfZ z coordinate of point to add.
*/
void OGR_G_AddPoint( OGRGeometryH hGeom,
double dfX, double dfY, double dfZ )
{
VALIDATE_POINTER0( hGeom, "OGR_G_AddPoint" );
switch( wkbFlatten(((OGRGeometry *) hGeom)->getGeometryType()) )
{
case wkbPoint:
{
((OGRPoint *) hGeom)->setX( dfX );
((OGRPoint *) hGeom)->setY( dfY );
((OGRPoint *) hGeom)->setZ( dfZ );
}
break;
case wkbLineString:
((OGRLineString *) hGeom)->addPoint( dfX, dfY, dfZ );
break;
default:
CPLError(CE_Failure, CPLE_NotSupported, "Incompatible geometry for operation");
break;
}
}
/************************************************************************/
/* OGR_G_AddPoint() */
/************************************************************************/
/**
* \brief Add a point to a geometry (line string or point).
*
* The vertex count of the line string is increased by one, and assigned from
* the passed location value.
*
* @param hGeom handle to the geometry to add a point to.
* @param dfX x coordinate of point to add.
* @param dfY y coordinate of point to add.
*/
void OGR_G_AddPoint_2D( OGRGeometryH hGeom,
double dfX, double dfY )
{
VALIDATE_POINTER0( hGeom, "OGR_G_AddPoint_2D" );
switch( wkbFlatten(((OGRGeometry *) hGeom)->getGeometryType()) )
{
case wkbPoint:
{
((OGRPoint *) hGeom)->setX( dfX );
((OGRPoint *) hGeom)->setY( dfY );
}
break;
case wkbLineString:
((OGRLineString *) hGeom)->addPoint( dfX, dfY );
break;
default:
CPLError(CE_Failure, CPLE_NotSupported, "Incompatible geometry for operation");
break;
}
}
/************************************************************************/
/* OGR_G_GetGeometryCount() */
/************************************************************************/
/**
* \brief Fetch the number of elements in a geometry or number of geometries in container.
*
* Only geometries of type wkbPolygon[25D], wkbMultiPoint[25D], wkbMultiLineString[25D],
* wkbMultiPolygon[25D] or wkbGeometryCollection[25D] may return a valid value.
* Other geometry types will silently return 0.
*
* For a polygon, the returned number is the number of rings (exterior ring + interior rings).
*
* @param hGeom single geometry or geometry container from which to get
* the number of elements.
* @return the number of elements.
*/
int OGR_G_GetGeometryCount( OGRGeometryH hGeom )
{
VALIDATE_POINTER1( hGeom, "OGR_G_GetGeometryCount", 0 );
switch( wkbFlatten(((OGRGeometry *) hGeom)->getGeometryType()) )
{
case wkbPolygon:
if( ((OGRPolygon *)hGeom)->getExteriorRing() == NULL )
return 0;
else
return ((OGRPolygon *)hGeom)->getNumInteriorRings() + 1;
case wkbMultiPoint:
case wkbMultiLineString:
case wkbMultiPolygon:
case wkbGeometryCollection:
return ((OGRGeometryCollection *)hGeom)->getNumGeometries();
default:
// autotest/pymod/ogrtest.py calls this method on any geometry. So keep silent
//CPLError(CE_Failure, CPLE_NotSupported, "Incompatible geometry for operation");
return 0;
}
}
/************************************************************************/
/* OGR_G_GetGeometryRef() */
/************************************************************************/
/**
* \brief Fetch geometry from a geometry container.
*
* This function returns an handle to a geometry within the container.
* The returned geometry remains owned by the container, and should not be
* modified. The handle is only valid untill the next change to the
* geometry container. Use OGR_G_Clone() to make a copy.
*
* This function relates to the SFCOM
* IGeometryCollection::get_Geometry() method.
*
* This function is the same as the CPP method
* OGRGeometryCollection::getGeometryRef().
*
* For a polygon, OGR_G_GetGeometryRef(iSubGeom) returns the exterior ring
* if iSubGeom == 0, and the interior rings for iSubGeom > 0.
*
* @param hGeom handle to the geometry container from which to get a
* geometry from.
* @param iSubGeom the index of the geometry to fetch, between 0 and
* getNumGeometries() - 1.
* @return handle to the requested geometry.
*/
OGRGeometryH OGR_G_GetGeometryRef( OGRGeometryH hGeom, int iSubGeom )
{
VALIDATE_POINTER1( hGeom, "OGR_G_GetGeometryRef", NULL );
switch( wkbFlatten(((OGRGeometry *) hGeom)->getGeometryType()) )
{
case wkbPolygon:
if( iSubGeom == 0 )
return (OGRGeometryH)
((OGRPolygon *)hGeom)->getExteriorRing();
else
return (OGRGeometryH)
((OGRPolygon *)hGeom)->getInteriorRing(iSubGeom-1);
case wkbMultiPoint:
case wkbMultiLineString:
case wkbMultiPolygon:
case wkbGeometryCollection:
return (OGRGeometryH)
((OGRGeometryCollection *)hGeom)->getGeometryRef( iSubGeom );
default:
CPLError(CE_Failure, CPLE_NotSupported, "Incompatible geometry for operation");
return 0;
}
}
/************************************************************************/
/* OGR_G_AddGeometry() */
/************************************************************************/
/**
* \brief Add a geometry to a geometry container.
*
* Some subclasses of OGRGeometryCollection restrict the types of geometry
* that can be added, and may return an error. The passed geometry is cloned
* to make an internal copy.
*
* There is no SFCOM analog to this method.
*
* This function is the same as the CPP method
* OGRGeometryCollection::addGeometry.
*
* For a polygon, hNewSubGeom must be a linearring. If the polygon is empty,
* the first added subgeometry will be the exterior ring. The next ones will be
* the interior rings.
*
* @param hGeom existing geometry container.
* @param hNewSubGeom geometry to add to the container.
*
* @return OGRERR_NONE if successful, or OGRERR_UNSUPPORTED_GEOMETRY_TYPE if
* the geometry type is illegal for the type of existing geometry.
*/
OGRErr OGR_G_AddGeometry( OGRGeometryH hGeom, OGRGeometryH hNewSubGeom )
{
VALIDATE_POINTER1( hGeom, "OGR_G_AddGeometry", OGRERR_UNSUPPORTED_OPERATION );
VALIDATE_POINTER1( hNewSubGeom, "OGR_G_AddGeometry", OGRERR_UNSUPPORTED_OPERATION );
switch( wkbFlatten(((OGRGeometry *) hGeom)->getGeometryType()) )
{
case wkbPolygon:
{
if( !EQUAL( ((OGRGeometry*) hNewSubGeom)->getGeometryName(), "LINEARRING" ) )
{
return OGRERR_UNSUPPORTED_GEOMETRY_TYPE;
}
else
{
((OGRPolygon *)hGeom)->addRing( (OGRLinearRing *) hNewSubGeom );
return OGRERR_NONE;
}
}
case wkbMultiPoint:
case wkbMultiLineString:
case wkbMultiPolygon:
case wkbGeometryCollection:
return ((OGRGeometryCollection *)hGeom)->addGeometry(
(OGRGeometry *) hNewSubGeom );
default:
return OGRERR_UNSUPPORTED_OPERATION;
}
}
/************************************************************************/
/* OGR_G_AddGeometryDirectly() */
/************************************************************************/
/**
* \brief Add a geometry directly to an existing geometry container.
*
* Some subclasses of OGRGeometryCollection restrict the types of geometry
* that can be added, and may return an error. Ownership of the passed
* geometry is taken by the container rather than cloning as addGeometry()
* does.
*
* This function is the same as the CPP method
* OGRGeometryCollection::addGeometryDirectly.
*
* There is no SFCOM analog to this method.
*
* For a polygon, hNewSubGeom must be a linearring. If the polygon is empty,
* the first added subgeometry will be the exterior ring. The next ones will be
* the interior rings.
*
* @param hGeom existing geometry.
* @param hNewSubGeom geometry to add to the existing geometry.
*
* @return OGRERR_NONE if successful, or OGRERR_UNSUPPORTED_GEOMETRY_TYPE if
* the geometry type is illegal for the type of geometry container.
*/
OGRErr OGR_G_AddGeometryDirectly( OGRGeometryH hGeom,
OGRGeometryH hNewSubGeom )
{
VALIDATE_POINTER1( hGeom, "OGR_G_AddGeometryDirectly", OGRERR_UNSUPPORTED_OPERATION );
VALIDATE_POINTER1( hNewSubGeom, "OGR_G_AddGeometryDirectly", OGRERR_UNSUPPORTED_OPERATION );
switch( wkbFlatten(((OGRGeometry *) hGeom)->getGeometryType()) )
{
case wkbPolygon:
{
if( !EQUAL( ((OGRGeometry*) hNewSubGeom)->getGeometryName(), "LINEARRING" ) )
{
return OGRERR_UNSUPPORTED_GEOMETRY_TYPE;
}
else
{
((OGRPolygon *)hGeom)->addRingDirectly( (OGRLinearRing *) hNewSubGeom );
return OGRERR_NONE;
}
}
case wkbMultiPoint:
case wkbMultiLineString:
case wkbMultiPolygon:
case wkbGeometryCollection:
return ((OGRGeometryCollection *)hGeom)->addGeometryDirectly(
(OGRGeometry *) hNewSubGeom );
default:
return OGRERR_UNSUPPORTED_OPERATION;
}
}
/************************************************************************/
/* OGR_G_RemoveGeometry() */
/************************************************************************/
/**
* \brief Remove a geometry from an exiting geometry container.
*
* Removing a geometry will cause the geometry count to drop by one, and all
* "higher" geometries will shuffle down one in index.
*
* There is no SFCOM analog to this method.
*
* This function is the same as the CPP method
* OGRGeometryCollection::removeGeometry().
*
* @param hGeom the existing geometry to delete from.
* @param iGeom the index of the geometry to delete. A value of -1 is a
* special flag meaning that all geometries should be removed.
*
* @param bDelete if TRUE the geometry will be destroyed, otherwise it will
* not. The default is TRUE as the existing geometry is considered to own the
* geometries in it.
*
* @return OGRERR_NONE if successful, or OGRERR_FAILURE if the index is
* out of range.
*/
OGRErr OGR_G_RemoveGeometry( OGRGeometryH hGeom, int iGeom, int bDelete )
{
VALIDATE_POINTER1( hGeom, "OGR_G_RemoveGeometry", 0 );
switch( wkbFlatten(((OGRGeometry *) hGeom)->getGeometryType()) )
{
case wkbPolygon:
{
CPLError( CE_Failure, CPLE_AppDefined,
"OGR_G_RemoveGeometry() not supported on polygons yet." );
return OGRERR_UNSUPPORTED_OPERATION;
}
case wkbMultiPoint:
case wkbMultiLineString:
case wkbMultiPolygon:
case wkbGeometryCollection:
return ((OGRGeometryCollection *)hGeom)->removeGeometry( iGeom,bDelete);
default:
return OGRERR_UNSUPPORTED_OPERATION;
}
}
/************************************************************************/
/* OGR_G_Length() */
/************************************************************************/
/**
* \brief Compute length of a geometry.
*
* Computes the area for OGRCurve or MultiCurve objects.
* Undefined for all other geometry types (returns zero).
*
* This function utilizes the C++ get_Length() method.
*
* @param hGeom the geometry to operate on.
* @return the lenght or 0.0 for unsupported geometry types.
*
* @since OGR 1.8.0
*/
double OGR_G_Length( OGRGeometryH hGeom )
{
VALIDATE_POINTER1( hGeom, "OGR_G_GetLength", 0 );
double fLength = 0.0;
switch( wkbFlatten(((OGRGeometry *) hGeom)->getGeometryType()) )
{
case wkbLinearRing:
case wkbLineString:
fLength = ((OGRCurve *) hGeom)->get_Length();
break;
case wkbMultiLineString:
case wkbGeometryCollection:
fLength = ((OGRGeometryCollection *) hGeom)->get_Length();
break;
default:
CPLError( CE_Warning, CPLE_AppDefined,
"OGR_G_Length() called against a non-curve geometry type." );
fLength = 0.0;
}
return fLength;
}
/************************************************************************/
/* OGR_G_Area() */
/************************************************************************/
/**
* \brief Compute geometry area.
*
* Computes the area for an OGRLinearRing, OGRPolygon or OGRMultiPolygon.
* Undefined for all other geometry types (returns zero).
*
* This function utilizes the C++ get_Area() methods such as
* OGRPolygon::get_Area().
*
* @param hGeom the geometry to operate on.
* @return the area or 0.0 for unsupported geometry types.
*
* @since OGR 1.8.0
*/
double OGR_G_Area( OGRGeometryH hGeom )
{
VALIDATE_POINTER1( hGeom, "OGR_G_Area", 0 );
double fArea = 0.0;
switch( wkbFlatten(((OGRGeometry *) hGeom)->getGeometryType()) )
{
case wkbPolygon:
fArea = ((OGRPolygon *) hGeom)->get_Area();
break;
case wkbMultiPolygon:
fArea = ((OGRMultiPolygon *) hGeom)->get_Area();
break;
case wkbLinearRing:
case wkbLineString:
/* This test below is required to filter out wkbLineString geometries
* not being of type of wkbLinearRing.
*/
if( EQUAL( ((OGRGeometry*) hGeom)->getGeometryName(), "LINEARRING" ) )
{
fArea = ((OGRLinearRing *) hGeom)->get_Area();
}
break;
case wkbGeometryCollection:
fArea = ((OGRGeometryCollection *) hGeom)->get_Area();
break;
default:
CPLError( CE_Warning, CPLE_AppDefined,
"OGR_G_Area() called against non-surface geometry type." );
fArea = 0.0;
}
return fArea;
}
/**
* \brief Compute geometry area (deprecated)
*
* @deprecated
* @see OGR_G_Area()
*/
double OGR_G_GetArea( OGRGeometryH hGeom )
{
return OGR_G_Area( hGeom );
}
|