OGRErr OGRPolygon::importFromWkt( char ** ppszInput ) { int bHasZ = FALSE; int bHasM = FALSE; bool bIsEmpty = false; OGRErr eErr = importPreambuleFromWkt(ppszInput, &bHasZ, &bHasM, &bIsEmpty); flags = 0; if( eErr != OGRERR_NONE ) return eErr; if( bHasZ ) flags |= OGR_G_3D; if( bHasM ) flags |= OGR_G_MEASURED; if( bIsEmpty ) return OGRERR_NONE; OGRRawPoint *paoPoints = NULL; int nMaxPoints = 0; double *padfZ = NULL; eErr = importFromWKTListOnly( ppszInput, bHasZ, bHasM, paoPoints, nMaxPoints, padfZ ); CPLFree( paoPoints ); CPLFree( padfZ ); return eErr; }
OGRErr OGRMultiSurface::importFromWkt( char ** ppszInput ) { int bHasZ = FALSE; int bHasM = FALSE; bool bIsEmpty = false; OGRErr eErr = importPreambuleFromWkt(ppszInput, &bHasZ, &bHasM, &bIsEmpty); flags = 0; if( eErr != OGRERR_NONE ) return eErr; if( bHasZ ) flags |= OGR_G_3D; if( bHasM ) flags |= OGR_G_MEASURED; if( bIsEmpty ) return OGRERR_NONE; char szToken[OGR_WKT_TOKEN_MAX] = {}; const char *pszInput = *ppszInput; eErr = OGRERR_NONE; // Skip first '('. pszInput = OGRWktReadToken( pszInput, szToken ); /* ==================================================================== */ /* Read each surface in turn. Note that we try to reuse the same */ /* point list buffer from ring to ring to cut down on */ /* allocate/deallocate overhead. */ /* ==================================================================== */ OGRRawPoint *paoPoints = NULL; int nMaxPoints = 0; double *padfZ = NULL; do { /* -------------------------------------------------------------------- */ /* Get the first token, which should be the geometry type. */ /* -------------------------------------------------------------------- */ const char* pszInputBefore = pszInput; pszInput = OGRWktReadToken( pszInput, szToken ); OGRSurface* poSurface = NULL; /* -------------------------------------------------------------------- */ /* Do the import. */ /* -------------------------------------------------------------------- */ if( EQUAL(szToken, "(") ) { OGRPolygon *poPolygon = new OGRPolygon(); poSurface = poPolygon; pszInput = pszInputBefore; eErr = poPolygon->importFromWKTListOnly( const_cast<char **>(&pszInput), bHasZ, bHasM, paoPoints, nMaxPoints, padfZ ); } else if( EQUAL(szToken, "EMPTY") ) { poSurface = new OGRPolygon(); } // We accept POLYGON() but this is an extension to the BNF, also // accepted by PostGIS. else if( EQUAL(szToken, "POLYGON") || EQUAL(szToken, "CURVEPOLYGON") ) { OGRGeometry* poGeom = NULL; pszInput = pszInputBefore; eErr = OGRGeometryFactory::createFromWkt( const_cast<char **>(&pszInput), NULL, &poGeom ); poSurface = dynamic_cast<OGRSurface*>(poGeom); if( poSurface == NULL ) { delete poGeom; eErr = OGRERR_CORRUPT_DATA; break; } } else { CPLError(CE_Failure, CPLE_AppDefined, "Unexpected token : %s", szToken); eErr = OGRERR_CORRUPT_DATA; break; } if( eErr == OGRERR_NONE ) eErr = addGeometryDirectly( poSurface ); if( eErr != OGRERR_NONE ) { delete poSurface; break; } /* -------------------------------------------------------------------- */ /* Read the delimiter following the surface. */ /* -------------------------------------------------------------------- */ pszInput = OGRWktReadToken( pszInput, szToken ); } while( szToken[0] == ',' && eErr == OGRERR_NONE ); CPLFree( paoPoints ); CPLFree( padfZ ); /* -------------------------------------------------------------------- */ /* freak if we don't get a closing bracket. */ /* -------------------------------------------------------------------- */ if( eErr != OGRERR_NONE ) return eErr; if( szToken[0] != ')' ) return OGRERR_CORRUPT_DATA; *ppszInput = const_cast<char *>(pszInput); return OGRERR_NONE; }
OGRErr OGRPoint::importFromWkt( char ** ppszInput ) { int bHasZ = FALSE, bHasM = FALSE; OGRErr eErr = importPreambuleFromWkt(ppszInput, &bHasZ, &bHasM); if( eErr >= 0 ) { if( eErr == OGRERR_NONE ) /* only the case for an EMPTY case */ nCoordDimension = (bHasZ) ? -3 : -2; return eErr; } const char *pszInput = *ppszInput; /* -------------------------------------------------------------------- */ /* Read the point list which should consist of exactly one point. */ /* -------------------------------------------------------------------- */ OGRRawPoint *poPoints = NULL; double *padfZ = NULL; int nMaxPoint = 0, nPoints = 0; pszInput = OGRWktReadPoints( pszInput, &poPoints, &padfZ, &nMaxPoint, &nPoints ); if( pszInput == NULL || nPoints != 1 ) { CPLFree( poPoints ); CPLFree( padfZ ); return OGRERR_CORRUPT_DATA; } x = poPoints[0].x; y = poPoints[0].y; CPLFree( poPoints ); if( padfZ != NULL ) { /* If there's a 3rd value, and it is not a POINT M, */ /* then assume it is the Z */ if ((!(bHasM && !bHasZ))) { z = padfZ[0]; nCoordDimension = 3; } else nCoordDimension = 2; CPLFree( padfZ ); } else if ( bHasZ ) { /* In theory we should have a z coordinate for POINT Z */ /* oh well, let be tolerant */ nCoordDimension = 3; } else nCoordDimension = 2; *ppszInput = (char *) pszInput; return OGRERR_NONE; }
OGRErr OGRPoint::importFromWkt( char ** ppszInput ) { int bHasZ = FALSE; int bHasM = FALSE; bool bIsEmpty = false; OGRErr eErr = importPreambuleFromWkt(ppszInput, &bHasZ, &bHasM, &bIsEmpty); flags = 0; if( eErr != OGRERR_NONE ) return eErr; if( bHasZ ) flags |= OGR_G_3D; if( bHasM ) flags |= OGR_G_MEASURED; if( bIsEmpty ) { // Should be at the end. if( !((*ppszInput[0] == '\000') || (*ppszInput[0] == ',')) ) return OGRERR_CORRUPT_DATA; return OGRERR_NONE; } else { flags |= OGR_G_NOT_EMPTY_POINT; } const char *pszInput = *ppszInput; /* -------------------------------------------------------------------- */ /* Read the point list which should consist of exactly one point. */ /* -------------------------------------------------------------------- */ OGRRawPoint *poPoints = NULL; double *padfZ = NULL; double *padfM = NULL; int nMaxPoint = 0; int nPoints = 0; int flagsFromInput = flags; pszInput = OGRWktReadPointsM( pszInput, &poPoints, &padfZ, &padfM, &flagsFromInput, &nMaxPoint, &nPoints ); if( pszInput == NULL || nPoints != 1 ) { CPLFree( poPoints ); CPLFree( padfZ ); CPLFree( padfM ); return OGRERR_CORRUPT_DATA; } if( (flagsFromInput & OGR_G_3D) && !(flags & OGR_G_3D) ) { flags |= OGR_G_3D; bHasZ = TRUE; } if( (flagsFromInput & OGR_G_MEASURED) && !(flags & OGR_G_MEASURED) ) { flags |= OGR_G_MEASURED; bHasM = TRUE; } x = poPoints[0].x; y = poPoints[0].y; CPLFree( poPoints ); if( bHasZ ) { if( padfZ != NULL ) z = padfZ[0]; } if( bHasM ) { if( padfM != NULL ) m = padfM[0]; } CPLFree( padfZ ); CPLFree( padfM ); *ppszInput = const_cast<char *>(pszInput); return OGRERR_NONE; }
OGRErr OGRMultiPoint::importFromWkt( char ** ppszInput ) { const char *pszInputBefore = *ppszInput; int bHasZ = FALSE; int bHasM = FALSE; bool bIsEmpty = false; OGRErr eErr = importPreambuleFromWkt(ppszInput, &bHasZ, &bHasM, &bIsEmpty); flags = 0; if( eErr != OGRERR_NONE ) return eErr; if( bHasZ ) flags |= OGR_G_3D; if( bHasM ) flags |= OGR_G_MEASURED; if( bIsEmpty ) return OGRERR_NONE; char szToken[OGR_WKT_TOKEN_MAX] = {}; const char *pszInput = *ppszInput; eErr = OGRERR_NONE; const char* pszPreScan = OGRWktReadToken( pszInput, szToken ); OGRWktReadToken( pszPreScan, szToken ); // Do we have an inner bracket? if( EQUAL(szToken,"(") || EQUAL(szToken, "EMPTY") ) { *ppszInput = const_cast<char *>(pszInputBefore); return importFromWkt_Bracketed( ppszInput, bHasM, bHasZ ); } if( bHasZ || bHasM ) { return OGRERR_CORRUPT_DATA; } /* -------------------------------------------------------------------- */ /* Read the point list which should consist of exactly one point. */ /* -------------------------------------------------------------------- */ OGRRawPoint *paoPoints = NULL; double *padfZ = NULL; double *padfM = NULL; int flagsFromInput = flags; int nMaxPoint = 0; int nPointCount = 0; pszInput = OGRWktReadPointsM( pszInput, &paoPoints, &padfZ, &padfM, &flagsFromInput, &nMaxPoint, &nPointCount ); if( pszInput == NULL ) { CPLFree( paoPoints ); CPLFree( padfZ ); CPLFree( padfM ); return OGRERR_CORRUPT_DATA; } if( (flagsFromInput & OGR_G_3D) && !(flags & OGR_G_3D) ) { flags |= OGR_G_3D; bHasZ = TRUE; } if( (flagsFromInput & OGR_G_MEASURED) && !(flags & OGR_G_MEASURED) ) { flags |= OGR_G_MEASURED; bHasM = TRUE; } /* -------------------------------------------------------------------- */ /* Transform raw points into point objects. */ /* -------------------------------------------------------------------- */ for( int iGeom = 0; iGeom < nPointCount && eErr == OGRERR_NONE; iGeom++ ) { OGRPoint *poPoint = new OGRPoint(paoPoints[iGeom].x, paoPoints[iGeom].y); if( bHasM ) { if( padfM != NULL ) poPoint->setM(padfM[iGeom]); else poPoint->setM(0.0); } if( bHasZ ) { if( padfZ != NULL ) poPoint->setZ(padfZ[iGeom]); else poPoint->setZ(0.0); } eErr = addGeometryDirectly( poPoint ); if( eErr != OGRERR_NONE ) { CPLFree( paoPoints ); CPLFree( padfZ ); CPLFree( padfM ); delete poPoint; return eErr; } } CPLFree( paoPoints ); CPLFree( padfZ ); CPLFree( padfM ); if( eErr != OGRERR_NONE ) return eErr; *ppszInput = const_cast<char *>(pszInput); return OGRERR_NONE; }
OGRErr OGRMultiPoint::importFromWkt( char ** ppszInput ) { const char *pszInputBefore = *ppszInput; int bHasZ = FALSE, bHasM = FALSE; OGRErr eErr = importPreambuleFromWkt(ppszInput, &bHasZ, &bHasM); if( eErr >= 0 ) return eErr; char szToken[OGR_WKT_TOKEN_MAX]; const char *pszInput = *ppszInput; int iGeom; eErr = OGRERR_NONE; const char* pszPreScan = OGRWktReadToken( pszInput, szToken ); OGRWktReadToken( pszPreScan, szToken ); // Do we have an inner bracket? if (EQUAL(szToken,"(") || EQUAL(szToken, "EMPTY") ) { *ppszInput = (char*) pszInputBefore; return importFromWkt_Bracketed( ppszInput, bHasM, bHasZ ); } if (bHasZ || bHasM) { return OGRERR_CORRUPT_DATA; } /* -------------------------------------------------------------------- */ /* Read the point list which should consist of exactly one point. */ /* -------------------------------------------------------------------- */ int nMaxPoint = 0; int nPointCount = 0; OGRRawPoint *paoPoints = NULL; double *padfZ = NULL; pszInput = OGRWktReadPoints( pszInput, &paoPoints, &padfZ, &nMaxPoint, &nPointCount ); if( pszInput == NULL ) { OGRFree( paoPoints ); OGRFree( padfZ ); return OGRERR_CORRUPT_DATA; } /* -------------------------------------------------------------------- */ /* Transform raw points into point objects. */ /* -------------------------------------------------------------------- */ for( iGeom = 0; iGeom < nPointCount && eErr == OGRERR_NONE; iGeom++ ) { OGRGeometry *poGeom; if( padfZ ) poGeom = new OGRPoint( paoPoints[iGeom].x, paoPoints[iGeom].y, padfZ[iGeom] ); else poGeom = new OGRPoint( paoPoints[iGeom].x, paoPoints[iGeom].y ); eErr = addGeometryDirectly( poGeom ); } OGRFree( paoPoints ); if( padfZ ) OGRFree( padfZ ); if( eErr != OGRERR_NONE ) return eErr; *ppszInput = (char *) pszInput; return OGRERR_NONE; }
OGRErr OGRGeometryCollection::importFromWktInternal( char ** ppszInput, int nRecLevel ) { /* Arbitrary value, but certainly large enough for reasonable usages ! */ if( nRecLevel == 32 ) { CPLError( CE_Failure, CPLE_AppDefined, "Too many recursion levels (%d) while parsing WKT geometry.", nRecLevel ); return OGRERR_CORRUPT_DATA; } int bHasZ = FALSE, bHasM = FALSE; bool bIsEmpty = false; OGRErr eErr = importPreambuleFromWkt(ppszInput, &bHasZ, &bHasM, &bIsEmpty); if( eErr != OGRERR_NONE ) return eErr; if( bHasZ ) flags |= OGR_G_3D; if( bHasM ) flags |= OGR_G_MEASURED; if( bIsEmpty ) return OGRERR_NONE; char szToken[OGR_WKT_TOKEN_MAX]; const char *pszInput = *ppszInput; /* Skip first '(' */ pszInput = OGRWktReadToken( pszInput, szToken ); /* ==================================================================== */ /* Read each subgeometry in turn. */ /* ==================================================================== */ do { OGRGeometry *poGeom = NULL; /* -------------------------------------------------------------------- */ /* Get the first token, which should be the geometry type. */ /* -------------------------------------------------------------------- */ OGRWktReadToken( pszInput, szToken ); /* -------------------------------------------------------------------- */ /* Do the import. */ /* -------------------------------------------------------------------- */ if (EQUAL(szToken,"GEOMETRYCOLLECTION")) { poGeom = new OGRGeometryCollection(); eErr = ((OGRGeometryCollection*)poGeom)-> importFromWktInternal( (char **) &pszInput, nRecLevel + 1 ); } else eErr = OGRGeometryFactory::createFromWkt( (char **) &pszInput, NULL, &poGeom ); if( eErr == OGRERR_NONE ) { /* if this has M but not Z it is an error if poGeom does not have M */ if( !Is3D() && IsMeasured() && !poGeom->IsMeasured() ) eErr = OGRERR_CORRUPT_DATA; else eErr = addGeometryDirectly( poGeom ); } if( eErr != OGRERR_NONE ) { delete poGeom; return eErr; } /* -------------------------------------------------------------------- */ /* Read the delimiter following the ring. */ /* -------------------------------------------------------------------- */ pszInput = OGRWktReadToken( pszInput, szToken ); } while( szToken[0] == ',' ); /* -------------------------------------------------------------------- */ /* freak if we don't get a closing bracket. */ /* -------------------------------------------------------------------- */ if( szToken[0] != ')' ) return OGRERR_CORRUPT_DATA; *ppszInput = (char *) pszInput; return OGRERR_NONE; }