📄 ogrgeometry.cpp
字号:
/****************************************************************************** * $Id: ogrgeometry.cpp,v 1.42 2006/11/24 16:50:52 fwarmerdam Exp $ * * Project: OpenGIS Simple Features Reference Implementation * Purpose: Implements a few base methods on OGRGeometry. * Author: Frank Warmerdam, warmerdam@pobox.com * ****************************************************************************** * Copyright (c) 1999, 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. ****************************************************************************** * * $Log: ogrgeometry.cpp,v $ * Revision 1.42 2006/11/24 16:50:52 fwarmerdam * Don't define error handlers if we don't have geos - avoid warning. * * Revision 1.41 2006/11/22 11:15:02 dron * Properly initialize GEOS warning/error message handlers in initGEOS(). * * Revision 1.40 2006/07/07 05:05:57 fwarmerdam * use GEOSGeom_destroy() in Distance() * * Revision 1.39 2006/07/07 00:05:46 mloskot * Removed GEOS C++ API usage from OGR and autotools. * * Revision 1.37 2006/07/06 19:06:42 mloskot * Added implementation of OGRGeometry::Distance with GEOS C API. Fixed GEOS_INIT macro in m4/geos.m4 * * Revision 1.36 2006/06/28 10:46:17 dron * Make OGRGeometry::dumpReadable() the const method. * * Revision 1.35 2006/03/31 17:57:32 fwarmerdam * header updates * * Revision 1.34 2005/10/21 15:58:33 fwarmerdam * use c++ casting for GEOSGeom * * Revision 1.33 2005/10/20 19:55:29 fwarmerdam * added GEOS C API support * * Revision 1.32 2005/09/21 00:50:53 fwarmerdam * Release SRS on geometry destruction * * Revision 1.31 2005/07/20 01:43:51 fwarmerdam * upgraded OGR geometry dimension handling * * Revision 1.30 2005/04/18 15:42:17 fwarmerdam * fix geos exception catching * * Revision 1.29 2005/04/06 20:43:00 fwarmerdam * fixed a variety of method signatures for documentation * * Revision 1.28 2005/02/22 12:39:32 fwarmerdam * Rename Equal to Equals, and Intersect to Intersects. * Intersects() now always uses GEOS if available. Also, Intersects() * returns TRUE if the other geometry is NULL. * * Revision 1.27 2004/09/16 18:29:21 fwarmerdam * Disable GEOS Insersect(). Is is very expensive for the common case of a * bounding rectangle intersection. We need something better eventually. * * Revision 1.26 2004/08/20 21:21:28 warmerda * added support for managing a persistent geos::GeometryFactory * * Revision 1.25 2004/07/10 06:57:53 warmerda * Added C entry points and docs for GEOS geometry functions * * Revision 1.24 2004/07/10 04:52:24 warmerda * Added lots of GEOS methods. * Added closeRings(). * * Revision 1.23 2004/02/21 15:36:14 warmerda * const correctness updates for geometry: bug 289 * * Revision 1.22 2004/01/06 19:07:45 warmerda * Added braces within case with variable declarations. * * Revision 1.21 2003/09/04 14:01:44 warmerda * added OGRGetGenerate_DB2_V72_BYTE_ORDER * * Revision 1.20 2003/08/27 15:40:37 warmerda * added support for generating DB2 V7.2 compatible WKB * * Revision 1.19 2003/06/10 14:51:07 warmerda * Allow Intersects() test against NULL geometry * * Revision 1.18 2003/04/03 23:39:11 danmo * Small updates to C API docs (Normand S.) * * Revision 1.17 2003/03/31 15:55:42 danmo * Added C API function docs * * Revision 1.16 2003/03/06 20:59:41 warmerda * Added exportToGML() implementation. * * Revision 1.15 2003/02/08 00:37:15 warmerda * try to improve documentation * * Revision 1.14 2002/10/24 16:46:33 warmerda * added docs and C implementation for Equal and FlattenTo2D * * Revision 1.13 2002/09/26 18:12:38 warmerda * added C support * * Revision 1.12 2002/05/03 14:16:23 warmerda * improve 3D geometry names * * Revision 1.11 2001/11/01 17:20:33 warmerda * added DISABLE_OGRGEOM_TRANSFORM macro * * Revision 1.10 2001/09/21 16:24:20 warmerda * added transform() and transformTo() methods * * Revision 1.9 2001/07/18 05:03:05 warmerda * added CPL_CVSID * * Revision 1.8 2001/02/06 14:14:09 warmerda * fixed up documentation * * Revision 1.7 2000/03/14 21:38:17 warmerda * added method to translate geometrytype to name * * Revision 1.6 1999/11/18 19:02:19 warmerda * expanded tabs * * Revision 1.5 1999/07/06 21:36:47 warmerda * tenatively added getEnvelope() and Intersect() * * Revision 1.4 1999/06/25 20:44:43 warmerda * implemented assignSpatialReference, carry properly * * Revision 1.3 1999/05/31 20:42:28 warmerda * added empty method * * Revision 1.2 1999/05/31 11:05:08 warmerda * added some documentation * * Revision 1.1 1999/05/20 14:35:00 warmerda * New * */#include "ogr_geometry.h"#include "ogr_api.h"#include "ogr_p.h"#include "ogr_geos.h"#include <assert.h>CPL_CVSID("$Id: ogrgeometry.cpp,v 1.42 2006/11/24 16:50:52 fwarmerdam Exp $");int OGRGeometry::bGenerate_DB2_V72_BYTE_ORDER = FALSE;#ifdef HAVE_GEOSstatic void _GEOSErrorHandler(const char *fmt, ...){ va_list args; va_start(args, fmt); CPLErrorV( CE_Failure, CPLE_AppDefined, fmt, args ); va_end(args);}static void _GEOSWarningHandler(const char *fmt, ...){ va_list args; va_start(args, fmt); CPLErrorV( CE_Warning, CPLE_AppDefined, fmt, args ); va_end(args);}#endif/************************************************************************//* OGRGeometry() *//************************************************************************/OGRGeometry::OGRGeometry(){ poSRS = NULL; nCoordDimension = 2;}/************************************************************************//* ~OGRGeometry() *//************************************************************************/OGRGeometry::~OGRGeometry(){ if( poSRS != NULL ) poSRS->Release();}/************************************************************************//* dumpReadable() *//************************************************************************//** * Dump geometry in well known text format to indicated output file. * * This method is the same as the C function OGR_G_DumpReadable(). * * @param fp the text file to write the geometry to. * @param pszPrefix the prefix to put on each line of output. */void OGRGeometry::dumpReadable( FILE * fp, const char * pszPrefix ) const{ char *pszWkt = NULL; if( pszPrefix == NULL ) pszPrefix = ""; if( fp == NULL ) fp = stdout; if( exportToWkt( &pszWkt ) == OGRERR_NONE ) { fprintf( fp, "%s%s\n", pszPrefix, pszWkt ); CPLFree( pszWkt ); }}/************************************************************************//* OGR_G_DumpReadable() *//************************************************************************//** * Dump geometry in well known text format to indicated output file. * * This method is the same as the CPP method OGRGeometry::dumpReadable. * * @param hGeom handle on the geometry to dump. * @param fp the text file to write the geometry to. * @param pszPrefix the prefix to put on each line of output. */void OGR_G_DumpReadable( OGRGeometryH hGeom, FILE *fp, const char *pszPrefix ){ ((OGRGeometry *) hGeom)->dumpReadable( fp, pszPrefix );}/************************************************************************//* assignSpatialReference() *//************************************************************************//** * \fn void OGRGeometry::assignSpatialReference( OGRSpatialReference * poSR ); * * Assign spatial reference to this object. Any existing spatial reference * is replaced, but under no circumstances does this result in the object * being reprojected. It is just changing the interpretation of the existing * geometry. Note that assigning a spatial reference increments the * reference count on the OGRSpatialReference, but does not copy it. * * This is similar to the SFCOM IGeometry::put_SpatialReference() method. * * This method is the same as the C function OGR_G_AssignSpatialReference(). * * @param poSR new spatial reference system to apply. */void OGRGeometry::assignSpatialReference( OGRSpatialReference * poSR ){ if( poSRS != NULL ) poSRS->Release(); poSRS = poSR; if( poSRS != NULL ) poSRS->Reference();}/************************************************************************//* OGR_G_AssignSpatialReference() *//************************************************************************//** * Assign spatial reference to this object. Any existing spatial reference * is replaced, but under no circumstances does this result in the object * being reprojected. It is just changing the interpretation of the existing * geometry. Note that assigning a spatial reference increments the * reference count on the OGRSpatialReference, but does not copy it. * * This is similar to the SFCOM IGeometry::put_SpatialReference() method. * * This function is the same as the CPP method * OGRGeometry::assignSpatialReference. * * @param hGeom handle on the geometry to apply the new spatial reference * system. * @param hSRS handle on the new spatial reference system to apply. */void OGR_G_AssignSpatialReference( OGRGeometryH hGeom, OGRSpatialReferenceH hSRS ){ ((OGRGeometry *) hGeom)->assignSpatialReference( (OGRSpatialReference *) hSRS );}/************************************************************************//* Intersects() *//************************************************************************//** * Do these features intersect? * * Determines whether two geometries intersect. If GEOS is enabled, then * this is done in rigerous fashion otherwise TRUE is returned if the * envelopes (bounding boxes) of the two features overlap. * * The poOtherGeom argument may be safely NULL, but in this case the method * will always return TRUE. That is, a NULL geometry is treated as being * everywhere. * * This method is the same as the C function OGR_G_Intersects(). * * @param poOtherGeom the other geometry to test against. * * @return TRUE if the geometries intersect, otherwise FALSE. */OGRBoolean OGRGeometry::Intersects( OGRGeometry *poOtherGeom ) const{ OGREnvelope oEnv1, oEnv2; if( this == NULL || poOtherGeom == NULL ) return TRUE; this->getEnvelope( &oEnv1 ); poOtherGeom->getEnvelope( &oEnv2 ); if( oEnv1.MaxX < oEnv2.MinX || oEnv1.MaxY < oEnv2.MinY || oEnv2.MaxX < oEnv1.MinX || oEnv2.MaxY < oEnv1.MinY ) return FALSE;#ifndef HAVE_GEOS // Without GEOS we assume that envelope overlap is equivelent to // actual intersection. return TRUE;#else GEOSGeom hThisGeosGeom = NULL; GEOSGeom hOtherGeosGeom = NULL; hThisGeosGeom = exportToGEOS(); hOtherGeosGeom = poOtherGeom->exportToGEOS(); if( hThisGeosGeom != NULL && hOtherGeosGeom != NULL ) { OGRBoolean bResult; if( GEOSIntersects( hThisGeosGeom, hOtherGeosGeom ) != 0 ) bResult = TRUE; else bResult = FALSE; GEOSGeom_destroy( hThisGeosGeom ); GEOSGeom_destroy( hOtherGeosGeom ); return bResult; } else { return TRUE; }#endif /* HAVE_GEOS */}// Old API compatibility function. OGRBoolean OGRGeometry::Intersect( OGRGeometry *poOtherGeom ) const{ return Intersects( poOtherGeom );}/************************************************************************//* OGR_G_Intersect() *//************************************************************************//** * Do these features intersect?
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -