📄 ogrutils.cpp
字号:
/****************************************************************************** * $Id: ogrutils.cpp,v 1.28 2006/11/18 20:43:28 mloskot Exp $ * * Project: OpenGIS Simple Features Reference Implementation * Purpose: Utility functions for OGR classes, including some related to * parsing well known text format vectors. * 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: ogrutils.cpp,v $ * Revision 1.28 2006/11/18 20:43:28 mloskot * Added visible casts from long to GByte in OGRParseDate function. * * Revision 1.27 2006/08/25 18:27:29 fwarmerdam * Use /vsimem/ prefix for memory filesystem. * * Revision 1.26 2006/08/23 19:18:58 fwarmerdam * added -mempreload option * * Revision 1.25 2006/07/14 16:18:02 fwarmerdam * Include readonly/readwrite info in --formats list. * * Revision 1.24 2006/05/15 16:55:43 fwarmerdam * improve debugging output * * Revision 1.23 2006/04/07 06:29:07 fwarmerdam * Added support for parsing coordinates starting with a bare decimal from WKT. * http://bugzilla.remotesensing.org/show_bug.cgi?id=1150 * * Revision 1.22 2006/03/31 17:44:20 fwarmerdam * header updates * * Revision 1.21 2006/02/15 04:25:37 fwarmerdam * added date support * * Revision 1.20 2006/01/07 17:15:38 dron * #include "ogrsf_frmts.h" only when compiling with the OGR formats. * * Revision 1.19 2006/01/06 17:23:26 dron * Use OGR_ENABLED macro to separate OGR formats related parts of the code. * * Revision 1.18 2005/10/16 01:59:06 cfis * Added declaration for OGRGeneralCmdLineProcessor to ogr_p.h, and included it * into ogr2ogr. Also changed call to CPL_DLL from CPL_STDCALL * * Revision 1.17 2005/10/16 01:51:43 cfis * Added #include for ogrsf_frmts.f to support the --formats command line parameter * in OGRGeneralCmdLineProcessor. * * Revision 1.16 2005/10/16 01:49:02 cfis * Added support for general command line processing similar to what GDAL utilities have. * * Revision 1.15 2005/09/15 00:52:28 fwarmerdam * fixed debug message * * Revision 1.14 2005/07/29 04:13:17 fwarmerdam * preserve 'full' precision in OGRMAkeWktCoordinate * * Revision 1.13 2005/07/22 19:32:43 fwarmerdam * Preserve more precision in WKT encoding of coordinates. * * Revision 1.12 2005/07/20 01:43:51 fwarmerdam * upgraded OGR geometry dimension handling * * Revision 1.11 2003/01/06 17:57:18 warmerda * Added some extra validation in OGRMakeWktCoordinate() * * Revision 1.10 2002/12/09 18:55:07 warmerda * moved DMS stuff to gdal/port * * Revision 1.9 2002/12/09 16:10:39 warmerda * added DMS translation * * Revision 1.8 2002/08/07 02:46:10 warmerda * improved comments * * Revision 1.7 2001/11/01 17:01:28 warmerda * pass output buffer into OGRMakeWktCoordinate * * Revision 1.6 2001/07/18 05:03:05 warmerda * added CPL_CVSID * * Revision 1.5 2001/05/29 02:24:00 warmerda * fixed negative support for Z coordinate * * Revision 1.4 2001/05/24 18:05:36 warmerda * fixed support for negative coordinte parsing * * Revision 1.3 1999/11/18 19:02:19 warmerda * expanded tabs * * Revision 1.2 1999/09/13 02:27:33 warmerda * incorporated limited 2.5d support * * Revision 1.1 1999/05/20 14:35:00 warmerda * New * */#include <ctype.h>#include "ogr_geometry.h"#include "ogr_p.h"#ifdef OGR_ENABLED# include "ogrsf_frmts.h"#endif /* OGR_ENABLED */CPL_CVSID("$Id: ogrutils.cpp,v 1.28 2006/11/18 20:43:28 mloskot Exp $");/************************************************************************//* OGRTrimExtraZeros() *//************************************************************************/static void OGRTrimExtraZeros( char *pszTarget ){ int i = 0; while( pszTarget[i] != '\0' ) i++;/* -------------------------------------------------------------------- *//* Trim trailing 000001's as they are likely roundoff error. *//* -------------------------------------------------------------------- */ if( i > 10 && pszTarget[i-1] == '1' && pszTarget[i-2] == '0' && pszTarget[i-3] == '0' && pszTarget[i-4] == '0' && pszTarget[i-5] == '0' && pszTarget[i-6] == '0' ) { pszTarget[--i] = '\0'; }/* -------------------------------------------------------------------- *//* Trim trailing zeros. *//* -------------------------------------------------------------------- */ while( i > 2 && pszTarget[i-1] == '0' && pszTarget[i-2] != '.' ) { pszTarget[--i] = '\0'; }}/************************************************************************//* OGRMakeWktCoordinate() *//* *//* Format a well known text coordinate, trying to keep the *//* ASCII representation compact, but accurate. These rules *//* will have to tighten up in the future. *//* *//* Currently a new point should require no more than 64 *//* characters barring the X or Y value being extremely large. *//************************************************************************/void OGRMakeWktCoordinate( char *pszTarget, double x, double y, double z, int nDimension ){ char szX[40], szY[40], szZ[40]; szZ[0] = '\0'; if( x == (int) x && y == (int) y && z == (int) z ) { sprintf( szX, "%d", (int) x ); sprintf( szY, " %d", (int) y ); } else { sprintf( szX, "%.15f", x ); OGRTrimExtraZeros( szX ); sprintf( szY, " %.15f", y ); OGRTrimExtraZeros( szY ); } if( nDimension == 3 ) { if( z == (int) z ) sprintf( szZ, " %d", (int) z ); else { sprintf( szZ, " %.15f", z ); OGRTrimExtraZeros( szZ ); } } if( strlen(szX) + strlen(szY) + strlen(szZ) > 75 ) { strcpy( szX, "0" ); strcpy( szY, "0" ); if( nDimension == 3 ) strcpy( szZ, "0" );#ifdef DEBUG CPLDebug( "OGR", "Yow! Got this big result in OGRMakeWktCoordinate()\n" "%s %s %s", szX, szY, szZ );#endif } strcpy( pszTarget, szX ); strcat( pszTarget, szY ); strcat( pszTarget, szZ );}/************************************************************************//* OGRWktReadToken() *//* *//* Read one token or delimeter and put into token buffer. Pre *//* and post white space is swallowed. *//************************************************************************/const char *OGRWktReadToken( const char * pszInput, char * pszToken ){ if( pszInput == NULL ) return NULL; /* -------------------------------------------------------------------- *//* Swallow pre-white space. *//* -------------------------------------------------------------------- */ while( *pszInput == ' ' || *pszInput == '\t' ) pszInput++;/* -------------------------------------------------------------------- *//* If this is a delimeter, read just one character. *//* -------------------------------------------------------------------- */ if( *pszInput == '(' || *pszInput == ')' || *pszInput == ',' ) { pszToken[0] = *pszInput; pszToken[1] = '\0'; pszInput++; }/* -------------------------------------------------------------------- *//* Or if it alpha numeric read till we reach non-alpha numeric *//* text. *//* -------------------------------------------------------------------- */ else { int iChar = 0; while( iChar < OGR_WKT_TOKEN_MAX-1 && ((*pszInput >= 'a' && *pszInput <= 'z') || (*pszInput >= 'A' && *pszInput <= 'Z') || (*pszInput >= '0' && *pszInput <= '9') || *pszInput == '.' || *pszInput == '+' || *pszInput == '-') ) { pszToken[iChar++] = *(pszInput++); } pszToken[iChar++] = '\0'; }/* -------------------------------------------------------------------- *//* Eat any trailing white space. *//* -------------------------------------------------------------------- */ while( *pszInput == ' ' || *pszInput == '\t' ) pszInput++; return( pszInput );}/************************************************************************//* OGRWktReadPoints() *//* *//* Read a point string. The point list must be contained in *//* brackets and each point pair separated by a comma. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -