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; }
static GMLFeatureClass* GMLParseFeatureType(CPLXMLNode *psSchemaNode, const char* pszName, CPLXMLNode *psComplexType) { /* -------------------------------------------------------------------- */ /* Grab the sequence of extensions greatgrandchild. */ /* -------------------------------------------------------------------- */ CPLXMLNode *psAttrSeq = CPLGetXMLNode( psComplexType, "complexContent.extension.sequence" ); if( psAttrSeq == NULL ) { return NULL; } /* -------------------------------------------------------------------- */ /* We are pretty sure this going to be a valid Feature class */ /* now, so create it. */ /* -------------------------------------------------------------------- */ GMLFeatureClass *poClass = new GMLFeatureClass( pszName ); /* -------------------------------------------------------------------- */ /* Loop over each of the attribute elements being defined for */ /* this feature class. */ /* -------------------------------------------------------------------- */ CPLXMLNode *psAttrDef; int nAttributeIndex = 0; int bGotUnrecognizedType = FALSE; for( psAttrDef = psAttrSeq->psChild; psAttrDef != NULL; psAttrDef = psAttrDef->psNext ) { if( strcmp(psAttrDef->pszValue,"group") == 0 ) { /* Too complex schema for us. Aborts parsing */ delete poClass; return NULL; } if( !EQUAL(psAttrDef->pszValue,"element") ) continue; /* MapServer WFS writes element type as an attribute of element */ /* not as a simpleType definition */ const char* pszType = CPLGetXMLValue( psAttrDef, "type", NULL ); const char* pszElementName = CPLGetXMLValue( psAttrDef, "name", NULL ); if (pszType != NULL) { const char* pszStrippedNSType = StripNS(pszType); int nWidth = 0, nPrecision = 0; GMLPropertyType gmlType = GMLPT_Untyped; if (EQUAL(pszStrippedNSType, "string") || EQUAL(pszStrippedNSType, "Character")) gmlType = GMLPT_String; /* TODO: Would be nice to have a proper date type */ else if (EQUAL(pszStrippedNSType, "date") || EQUAL(pszStrippedNSType, "dateTime")) gmlType = GMLPT_String; else if (EQUAL(pszStrippedNSType, "real") || EQUAL(pszStrippedNSType, "double") || EQUAL(pszStrippedNSType, "float") || EQUAL(pszStrippedNSType, "decimal")) gmlType = GMLPT_Real; else if (EQUAL(pszStrippedNSType, "short") || EQUAL(pszStrippedNSType, "int") || EQUAL(pszStrippedNSType, "integer") || EQUAL(pszStrippedNSType, "long")) gmlType = GMLPT_Integer; else if (strncmp(pszType, "gml:", 4) == 0) { const AssocNameType* psIter = apsPropertyTypes; while(psIter->pszName) { if (strncmp(pszType + 4, psIter->pszName, strlen(psIter->pszName)) == 0) { if (poClass->GetGeometryAttributeIndex() != -1) { CPLDebug("GML", "Geometry field already found ! Ignoring the following ones"); } else { poClass->SetGeometryElement(pszElementName); poClass->SetGeometryType(psIter->eType); poClass->SetGeometryAttributeIndex( nAttributeIndex ); nAttributeIndex ++; } break; } psIter ++; } if (psIter->pszName == NULL) { /* Can be a non geometry gml type */ /* Too complex schema for us. Aborts parsing */ delete poClass; return NULL; } if (poClass->GetGeometryAttributeIndex() == -1) bGotUnrecognizedType = TRUE; continue; } /* Integraph stuff */ else if (strcmp(pszType, "G:Point_MultiPointPropertyType") == 0 || strcmp(pszType, "gmgml:Point_MultiPointPropertyType") == 0) { poClass->SetGeometryElement(pszElementName); poClass->SetGeometryType(wkbMultiPoint); poClass->SetGeometryAttributeIndex( nAttributeIndex ); nAttributeIndex ++; continue; } else if (strcmp(pszType, "G:LineString_MultiLineStringPropertyType") == 0 || strcmp(pszType, "gmgml:LineString_MultiLineStringPropertyType") == 0) { poClass->SetGeometryElement(pszElementName); poClass->SetGeometryType(wkbMultiLineString); poClass->SetGeometryAttributeIndex( nAttributeIndex ); nAttributeIndex ++; continue; } else if (strcmp(pszType, "G:Polygon_MultiPolygonPropertyType") == 0 || strcmp(pszType, "gmgml:Polygon_MultiPolygonPropertyType") == 0 || strcmp(pszType, "gmgml:Polygon_Surface_MultiSurface_CompositeSurfacePropertyType") == 0) { poClass->SetGeometryElement(pszElementName); poClass->SetGeometryType(wkbMultiPolygon); poClass->SetGeometryAttributeIndex( nAttributeIndex ); nAttributeIndex ++; continue; } /* ERDAS Apollo stuff (like in http://apollo.erdas.com/erdas-apollo/vector/WORLDWIDE?SERVICE=WFS&VERSION=1.0.0&REQUEST=DescribeFeatureType&TYPENAME=wfs:cntry98) */ else if (strcmp(pszType, "wfs:MixedPolygonPropertyType") == 0) { poClass->SetGeometryElement(pszElementName); poClass->SetGeometryType(wkbMultiPolygon); poClass->SetGeometryAttributeIndex( nAttributeIndex ); nAttributeIndex ++; continue; } else { gmlType = GMLPT_Untyped; if ( ! LookForSimpleType(psSchemaNode, pszStrippedNSType, &gmlType, &nWidth, &nPrecision) ) { /* Too complex schema for us. Aborts parsing */ delete poClass; return NULL; } } if (pszElementName == NULL) pszElementName = "unnamed"; GMLPropertyDefn *poProp = new GMLPropertyDefn( pszElementName, pszElementName ); poProp->SetType( gmlType ); poProp->SetAttributeIndex( nAttributeIndex ); poProp->SetWidth( nWidth ); poProp->SetPrecision( nPrecision ); if (poClass->AddProperty( poProp ) < 0) delete poProp; else nAttributeIndex ++; continue; } // For now we skip geometries .. fixup later. CPLXMLNode* psSimpleType = CPLGetXMLNode( psAttrDef, "simpleType" ); if( psSimpleType == NULL ) { const char* pszRef = CPLGetXMLValue( psAttrDef, "ref", NULL ); /* FME .xsd */ if (pszRef != NULL && strncmp(pszRef, "gml:", 4) == 0) { const AssocNameType* psIter = apsRefTypes; while(psIter->pszName) { if (strncmp(pszRef + 4, psIter->pszName, strlen(psIter->pszName)) == 0) { if (poClass->GetGeometryAttributeIndex() != -1) { OGRwkbGeometryType eNewType = psIter->eType; OGRwkbGeometryType eOldType = (OGRwkbGeometryType)poClass->GetGeometryType(); if ((eNewType == wkbMultiPoint && eOldType == wkbPoint) || (eNewType == wkbMultiLineString && eOldType == wkbLineString) || (eNewType == wkbMultiPolygon && eOldType == wkbPolygon)) { poClass->SetGeometryType(eNewType); } else { CPLDebug("GML", "Geometry field already found ! Ignoring the following ones"); } } else { poClass->SetGeometryElement(pszElementName); poClass->SetGeometryType(psIter->eType); poClass->SetGeometryAttributeIndex( nAttributeIndex ); nAttributeIndex ++; } break; } psIter ++; } if (psIter->pszName == NULL) { /* Can be a non geometry gml type */ /* Too complex schema for us. Aborts parsing */ delete poClass; return NULL; } if (poClass->GetGeometryAttributeIndex() == -1) bGotUnrecognizedType = TRUE; continue; } /* Parse stuff like the following found in http://199.29.1.81:8181/miwfs/GetFeature.ashx?REQUEST=GetFeature&MAXFEATURES=1&SERVICE=WFS&VERSION=1.0.0&TYPENAME=miwfs:World : <xs:element name="Obj" minOccurs="0" maxOccurs="1"> <xs:complexType> <xs:sequence> <xs:element ref="gml:_Geometry"/> </xs:sequence> </xs:complexType> </xs:element> */ CPLXMLNode* psComplexType = GetSingleChildElement( psAttrDef, "complexType" ); CPLXMLNode* psComplexTypeSequence = GetSingleChildElement( psComplexType, "sequence" ); CPLXMLNode* psComplexTypeSequenceElement = GetSingleChildElement( psComplexTypeSequence, "element" ); if( pszElementName != NULL && CheckMinMaxOccursCardinality(psAttrDef) && psComplexTypeSequenceElement != NULL && CheckMinMaxOccursCardinality(psComplexTypeSequence) && strcmp(CPLGetXMLValue( psComplexTypeSequenceElement, "ref", "" ), "gml:_Geometry") == 0 ) { poClass->SetGeometryElement(pszElementName); poClass->SetGeometryType(wkbUnknown); poClass->SetGeometryAttributeIndex( nAttributeIndex ); nAttributeIndex ++; continue; } else { /* Too complex schema for us. Aborts parsing */ delete poClass; return NULL; } } if (pszElementName == NULL) pszElementName = "unnamed"; GMLPropertyDefn *poProp = new GMLPropertyDefn( pszElementName, pszElementName ); GMLPropertyType eType = GMLPT_Untyped; int nWidth = 0, nPrecision = 0; GetSimpleTypeProperties(psSimpleType, &eType, &nWidth, &nPrecision); poProp->SetType( eType ); poProp->SetWidth( nWidth ); poProp->SetPrecision( nPrecision ); poProp->SetAttributeIndex( nAttributeIndex ); if (poClass->AddProperty( poProp ) < 0) delete poProp; else nAttributeIndex ++; } /* Only report wkbNone if we didn't find a known geometry type */ /* and there were not any unknown types (in case this unknown type */ /* would be a geometry type) */ if (poClass->GetGeometryAttributeIndex() == -1 && !bGotUnrecognizedType) { poClass->SetGeometryType(wkbNone); } /* -------------------------------------------------------------------- */ /* Class complete, add to reader class list. */ /* -------------------------------------------------------------------- */ poClass->SetSchemaLocked( TRUE ); return poClass; }
int NASReader::PrescanForSchema( int bGetExtents ) { GMLFeature *poFeature; if( m_pszFilename == NULL ) return FALSE; SetClassListLocked( FALSE ); ClearClasses(); if( !SetupParser() ) return FALSE; std::string osWork; 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; const CPLXMLNode* const * papsGeometry = poFeature->GetGeometryList(); if( papsGeometry[0] != NULL ) { poGeometry = (OGRGeometry*) OGR_G_CreateFromGMLTree(papsGeometry[0]); } if( poGeometry != NULL ) { double dfXMin, dfXMax, dfYMin, dfYMax; OGREnvelope sEnvelope; OGRwkbGeometryType eGType = (OGRwkbGeometryType) poClass->GetGeometryType(); // Merge SRSName into layer. const char* pszSRSName = GML_ExtractSrsNameFromGeometry(papsGeometry, osWork, FALSE); // if (pszSRSName != NULL) // m_bCanUseGlobalSRSName = FALSE; poClass->MergeSRSName(pszSRSName); // 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; }