int NASReader::PrescanForSchema( int bGetExtents ) { GMLFeature *poFeature; if( m_pszFilename == NULL ) return FALSE; SetClassListLocked( FALSE ); ClearClasses(); if( !SetupParser() ) return FALSE; while( (poFeature = NextFeature()) != NULL ) { GMLFeatureClass *poClass = poFeature->GetClass(); if( poClass->GetFeatureCount() == -1 ) poClass->SetFeatureCount( 1 ); else poClass->SetFeatureCount( poClass->GetFeatureCount() + 1 ); #ifdef SUPPORT_GEOMETRY if( bGetExtents ) { OGRGeometry *poGeometry = NULL; if( poFeature->GetGeometry() != NULL && strlen(poFeature->GetGeometry()) != 0 ) { poGeometry = (OGRGeometry *) OGR_G_CreateFromGML( poFeature->GetGeometry() ); } if( poGeometry != NULL ) { double dfXMin, dfXMax, dfYMin, dfYMax; OGREnvelope sEnvelope; OGRwkbGeometryType eGType = (OGRwkbGeometryType) poClass->GetGeometryType(); // Merge geometry type into layer. if( poClass->GetFeatureCount() == 1 && eGType == wkbUnknown ) eGType = wkbNone; poClass->SetGeometryType( (int) OGRMergeGeometryTypes( eGType, poGeometry->getGeometryType() ) ); // merge extents. poGeometry->getEnvelope( &sEnvelope ); delete poGeometry; if( poClass->GetExtents(&dfXMin, &dfXMax, &dfYMin, &dfYMax) ) { dfXMin = MIN(dfXMin,sEnvelope.MinX); dfXMax = MAX(dfXMax,sEnvelope.MaxX); dfYMin = MIN(dfYMin,sEnvelope.MinY); dfYMax = MAX(dfYMax,sEnvelope.MaxY); } else { dfXMin = sEnvelope.MinX; dfXMax = sEnvelope.MaxX; dfYMin = sEnvelope.MinY; dfYMax = sEnvelope.MaxY; } poClass->SetExtents( dfXMin, dfXMax, dfYMin, dfYMax ); } else { if( poClass->GetGeometryType() == (int) wkbUnknown && poClass->GetFeatureCount() == 1 ) poClass->SetGeometryType( wkbNone ); } #endif /* def SUPPORT_GEOMETRY */ } delete poFeature; } CleanupParser(); return GetClassCount() > 0; }
int GMLReader::PrescanForSchema( int bGetExtents ) { GMLFeature *poFeature; if( m_pszFilename == NULL ) return FALSE; SetClassListLocked( FALSE ); ClearClasses(); if( !SetupParser() ) return FALSE; while( (poFeature = NextFeature()) != NULL ) { GMLFeatureClass *poClass = poFeature->GetClass(); if( poClass->GetFeatureCount() == -1 ) poClass->SetFeatureCount( 1 ); else poClass->SetFeatureCount( poClass->GetFeatureCount() + 1 ); #ifdef SUPPORT_GEOMETRY if( bGetExtents ) { OGRGeometry *poGeometry = NULL; if( poFeature->GetGeometry() != NULL && strlen(poFeature->GetGeometry()) != 0 ) { poGeometry = OGRGeometryFactory::createFromGML( poFeature->GetGeometry() ); } if( poGeometry != NULL ) { double dfXMin, dfXMax, dfYMin, dfYMax; OGREnvelope sEnvelope; poGeometry->getEnvelope( &sEnvelope ); delete poGeometry; if( poClass->GetExtents(&dfXMin, &dfXMax, &dfYMin, &dfYMax) ) { dfXMin = MIN(dfXMin,sEnvelope.MinX); dfXMax = MAX(dfXMax,sEnvelope.MaxX); dfYMin = MIN(dfYMin,sEnvelope.MinY); dfYMax = MAX(dfYMax,sEnvelope.MaxY); } else { dfXMin = sEnvelope.MinX; dfXMax = sEnvelope.MaxX; dfYMin = sEnvelope.MinY; dfYMax = sEnvelope.MaxY; } poClass->SetExtents( dfXMin, dfXMax, dfYMin, dfYMax ); } #endif /* def SUPPORT_GEOMETRY */ } delete poFeature; } CleanupParser(); return GetClassCount() > 0; }
OGRFeature *OGRNASLayer::GetNextFeature() { GMLFeature *poNASFeature = NULL; OGRGeometry *poGeom = NULL; if( iNextNASId == 0 ) ResetReading(); /* ==================================================================== */ /* Loop till we find and translate a feature meeting all our */ /* requirements. */ /* ==================================================================== */ while( TRUE ) { /* -------------------------------------------------------------------- */ /* Cleanup last feature, and get a new raw nas feature. */ /* -------------------------------------------------------------------- */ delete poNASFeature; delete poGeom; poNASFeature = NULL; poGeom = NULL; poNASFeature = poDS->GetReader()->NextFeature(); if( poNASFeature == NULL ) return NULL; /* -------------------------------------------------------------------- */ /* Is it of the proper feature class? */ /* -------------------------------------------------------------------- */ // We count reading low level NAS features as a feature read for // work checking purposes, though at least we didn't necessary // have to turn it into an OGRFeature. m_nFeaturesRead++; if( poNASFeature->GetClass() != poFClass ) continue; iNextNASId++; /* -------------------------------------------------------------------- */ /* Does it satisfy the spatial query, if there is one? */ /* -------------------------------------------------------------------- */ if( poNASFeature->GetGeometry() != NULL ) { poGeom = (OGRGeometry*) OGR_G_CreateFromGML( poNASFeature->GetGeometry() ); // We assume the createFromNAS() function would have already // reported the error. if( poGeom == NULL ) { delete poNASFeature; return NULL; } if( m_poFilterGeom != NULL && !FilterGeometry( poGeom ) ) continue; } /* -------------------------------------------------------------------- */ /* Convert the whole feature into an OGRFeature. */ /* -------------------------------------------------------------------- */ int iField; OGRFeature *poOGRFeature = new OGRFeature( GetLayerDefn() ); poOGRFeature->SetFID( iNextNASId ); for( iField = 0; iField < poFClass->GetPropertyCount(); iField++ ) { const GMLProperty *psGMLProperty = poNASFeature->GetProperty( iField ); if( psGMLProperty == NULL || psGMLProperty->nSubProperties == 0 ) continue; switch( poFClass->GetProperty(iField)->GetType() ) { case GMLPT_Real: { poOGRFeature->SetField( iField, CPLAtof(psGMLProperty->papszSubProperties[0]) ); } break; case GMLPT_IntegerList: { int nCount = psGMLProperty->nSubProperties; int *panIntList = (int *) CPLMalloc(sizeof(int) * nCount ); int i; for( i = 0; i < nCount; i++ ) panIntList[i] = atoi(psGMLProperty->papszSubProperties[i]); poOGRFeature->SetField( iField, nCount, panIntList ); CPLFree( panIntList ); } break; case GMLPT_RealList: { int nCount = psGMLProperty->nSubProperties; double *padfList = (double *)CPLMalloc(sizeof(double)*nCount); int i; for( i = 0; i < nCount; i++ ) padfList[i] = CPLAtof(psGMLProperty->papszSubProperties[i]); poOGRFeature->SetField( iField, nCount, padfList ); CPLFree( padfList ); } break; case GMLPT_StringList: { poOGRFeature->SetField( iField, psGMLProperty->papszSubProperties ); } break; default: poOGRFeature->SetField( iField, psGMLProperty->papszSubProperties[0] ); break; } } /* -------------------------------------------------------------------- */ /* Test against the attribute query. */ /* -------------------------------------------------------------------- */ if( m_poAttrQuery != NULL && !m_poAttrQuery->Evaluate( poOGRFeature ) ) { delete poOGRFeature; continue; } /* -------------------------------------------------------------------- */ /* Wow, we got our desired feature. Return it. */ /* -------------------------------------------------------------------- */ delete poNASFeature; poOGRFeature->SetGeometryDirectly( poGeom ); return poOGRFeature; } return NULL; }