ogr2kmlgeometry.cpp

来自「支持各种栅格图像和矢量图像读取的库」· C++ 代码 · 共 430 行 · 第 1/2 页

CPP
430
字号
/****************************************************************************** * $Id: ogr2kmlgeometry.cpp 10646 2007-01-18 02:38:10Z warmerdam $ * * Project:  KML Driver * Purpose:  Implementation of OGR -> KML geometries writer. * Author:   Christopher Condit, condit@sdsc.edu * ****************************************************************************** * Copyright (c) 2006, Christopher Condit * * 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 "cpl_minixml.h"#include "ogr_geometry.h"#include "ogr_api.h"#include "ogr_p.h"#include "cpl_error.h"#include "cpl_conv.h"/************************************************************************//*                        MakeKMLCoordinate()                           *//************************************************************************/static void MakeKMLCoordinate( char *pszTarget,                                double x, double y, double z, int b3D ){    OGRMakeWktCoordinate( pszTarget, x, y, z, b3D ? 3 : 2 );    while( *pszTarget != '\0' )    {        if( *pszTarget == ' ' )            *pszTarget = ',';        pszTarget++;    }#ifdef notdef    if( !b3D )    {        if( x == (int) x && y == (int) y )            sprintf( pszTarget, "%d,%d", (int) x, (int) y );        else if( fabs(x) < 370 && fabs(y) < 370 )            sprintf( pszTarget, "%.16g,%.16g", x, y );        else if( fabs(x) > 100000000.0 || fabs(y) > 100000000.0 )            sprintf( pszTarget, "%.16g,%.16g", x, y );        else            sprintf( pszTarget, "%.3f,%.3f", x, y );    }    else    {        if( x == (int) x && y == (int) y && z == (int) z )            sprintf( pszTarget, "%d,%d,%d", (int) x, (int) y, (int) z );        else if( fabs(x) < 370 && fabs(y) < 370 )            sprintf( pszTarget, "%.16g,%.16g,%.16g", x, y, z );        else if( fabs(x) > 100000000.0 || fabs(y) > 100000000.0                  || fabs(z) > 100000000.0 )            sprintf( pszTarget, "%.16g,%.16g,%.16g", x, y, z );        else            sprintf( pszTarget, "%.3f,%.3f,%.3f", x, y, z );    }#endif}/************************************************************************//*                            _GrowBuffer()                             *//************************************************************************/static void _GrowBuffer( int nNeeded, char **ppszText, int *pnMaxLength ){    if( nNeeded+1 >= *pnMaxLength )    {        *pnMaxLength = MAX(*pnMaxLength * 2,nNeeded+1);        *ppszText = (char *) CPLRealloc(*ppszText, *pnMaxLength);    }}/************************************************************************//*                            AppendString()                            *//************************************************************************/static void AppendString( char **ppszText, int *pnLength, int *pnMaxLength,                          const char *pszTextToAppend ){    _GrowBuffer( *pnLength + strlen(pszTextToAppend) + 1,                  ppszText, pnMaxLength );    strcat( *ppszText + *pnLength, pszTextToAppend );    *pnLength += strlen( *ppszText + *pnLength );}/************************************************************************//*                        AppendCoordinateList()                        *//************************************************************************/static void AppendCoordinateList( OGRLineString *poLine,                                   char **ppszText, int *pnLength,                                   int *pnMaxLength ){    char        szCoordinate[256];    int         b3D = (poLine->getGeometryType() & wkb25DBit);    *pnLength += strlen(*ppszText + *pnLength);    _GrowBuffer( *pnLength + 20, ppszText, pnMaxLength );    strcat( *ppszText + *pnLength, "<coordinates>" );    *pnLength += strlen(*ppszText + *pnLength);        for( int iPoint = 0; iPoint < poLine->getNumPoints(); iPoint++ )    {        MakeKMLCoordinate( szCoordinate,                            poLine->getX(iPoint),                           poLine->getY(iPoint),                           poLine->getZ(iPoint),                           b3D );        _GrowBuffer( *pnLength + strlen(szCoordinate)+1,                      ppszText, pnMaxLength );        if( iPoint != 0 )            strcat( *ppszText + *pnLength, " " );                    strcat( *ppszText + *pnLength, szCoordinate );        *pnLength += strlen(*ppszText + *pnLength);    }        _GrowBuffer( *pnLength + 20, ppszText, pnMaxLength );    strcat( *ppszText + *pnLength, "</coordinates>" );    *pnLength += strlen(*ppszText + *pnLength);}/************************************************************************//*                       OGR2KMLGeometryAppend()                        *//************************************************************************/static int OGR2KMLGeometryAppend( OGRGeometry *poGeometry,                                   char **ppszText, int *pnLength,                                   int *pnMaxLength ){/* -------------------------------------------------------------------- *//*      2D Point                                                        *//* -------------------------------------------------------------------- */    if( poGeometry->getGeometryType() == wkbPoint )    {        char    szCoordinate[256];        OGRPoint *poPoint = (OGRPoint *) poGeometry;        MakeKMLCoordinate( szCoordinate,                            poPoint->getX(), poPoint->getY(), 0.0, FALSE );                                   _GrowBuffer( *pnLength + strlen(szCoordinate) + 60,                      ppszText, pnMaxLength );        sprintf( *ppszText + *pnLength,                 "<Point><coordinates>%s</coordinates></Point>",                 szCoordinate );        *pnLength += strlen( *ppszText + *pnLength );    }/* -------------------------------------------------------------------- *//*      3D Point                                                        *//* -------------------------------------------------------------------- */    else if( poGeometry->getGeometryType() == wkbPoint25D )    {        char    szCoordinate[256];        OGRPoint *poPoint = (OGRPoint *) poGeometry;        MakeKMLCoordinate( szCoordinate,                            poPoint->getX(), poPoint->getY(), poPoint->getZ(),                            TRUE );                                   _GrowBuffer( *pnLength + strlen(szCoordinate) + 70,                      ppszText, pnMaxLength );        sprintf( *ppszText + *pnLength,                 "<Point><coordinates>%s</coordinates></Point>",                 szCoordinate );        *pnLength += strlen( *ppszText + *pnLength );    }/* -------------------------------------------------------------------- *//*      LineString and LinearRing                                       *//* -------------------------------------------------------------------- */    else if( poGeometry->getGeometryType() == wkbLineString              || poGeometry->getGeometryType() == wkbLineString25D )    {        int bRing = EQUAL(poGeometry->getGeometryName(),"LINEARRING");        if( bRing )            AppendString( ppszText, pnLength, pnMaxLength,                          "<LinearRing>" );        else            AppendString( ppszText, pnLength, pnMaxLength,                          "<LineString>" );

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?