OGRNASLayer *OGRNASDataSource::TranslateNASSchema( GMLFeatureClass *poClass ) { OGRNASLayer *poLayer; OGRwkbGeometryType eGType = (OGRwkbGeometryType) poClass->GetGeometryType(); if( poClass->GetFeatureCount() == 0 ) eGType = wkbUnknown; /* -------------------------------------------------------------------- */ /* Create an empty layer. */ /* -------------------------------------------------------------------- */ poLayer = new OGRNASLayer( poClass->GetName(), NULL, eGType, this ); /* -------------------------------------------------------------------- */ /* Added attributes (properties). */ /* -------------------------------------------------------------------- */ for( int iField = 0; iField < poClass->GetPropertyCount(); iField++ ) { GMLPropertyDefn *poProperty = poClass->GetProperty( iField ); OGRFieldType eFType; if( poProperty->GetType() == GMLPT_Untyped ) eFType = OFTString; else if( poProperty->GetType() == GMLPT_String ) eFType = OFTString; else if( poProperty->GetType() == GMLPT_Integer ) eFType = OFTInteger; else if( poProperty->GetType() == GMLPT_Real ) eFType = OFTReal; else if( poProperty->GetType() == GMLPT_StringList ) eFType = OFTStringList; else if( poProperty->GetType() == GMLPT_IntegerList ) eFType = OFTIntegerList; else if( poProperty->GetType() == GMLPT_RealList ) eFType = OFTRealList; else eFType = OFTString; OGRFieldDefn oField( poProperty->GetName(), eFType ); if ( EQUALN(oField.GetNameRef(), "ogr:", 4) ) oField.SetName(poProperty->GetName()+4); if( poProperty->GetWidth() > 0 ) oField.SetWidth( poProperty->GetWidth() ); poLayer->GetLayerDefn()->AddFieldDefn( &oField ); } return poLayer; }
OGRNASLayer *OGRNASDataSource::TranslateNASSchema( GMLFeatureClass *poClass ) { OGRwkbGeometryType eGType = wkbNone; if( poClass->GetGeometryPropertyCount() != 0 ) { eGType = static_cast<OGRwkbGeometryType>( poClass->GetGeometryProperty(0)->GetType() ); if( poClass->GetFeatureCount() == 0 ) eGType = wkbUnknown; } /* -------------------------------------------------------------------- */ /* Translate SRS. */ /* -------------------------------------------------------------------- */ const char* pszSRSName = poClass->GetSRSName(); OGRSpatialReference* poSRS = NULL; if (pszSRSName) { const char *pszHandle = strrchr( pszSRSName, ':' ); if( pszHandle != NULL ) { pszHandle += 1; poSRS = new OGRSpatialReference(); for( int i = 0; apszURNNames[i*2+0] != NULL; i++ ) { const char *pszTarget = apszURNNames[i*2+0]; const int nTLen = static_cast<int>(strlen(pszTarget)); // Are we just looking for a prefix match? if( pszTarget[nTLen-1] == '*' ) { if( EQUALN(pszTarget,pszHandle,nTLen-1) ) pszSRSName = apszURNNames[i*2+1]; } else { if( EQUAL(pszTarget,pszHandle) ) pszSRSName = apszURNNames[i*2+1]; } } if (poSRS->SetFromUserInput(pszSRSName) != OGRERR_NONE) { CPLDebug( "NAS", "Failed to translate srsName='%s'", pszSRSName ); delete poSRS; poSRS = NULL; } } } /* -------------------------------------------------------------------- */ /* Create an empty layer. */ /* -------------------------------------------------------------------- */ OGRNASLayer *poLayer = new OGRNASLayer( poClass->GetName(), poSRS, eGType, this ); delete poSRS; /* -------------------------------------------------------------------- */ /* Added attributes (properties). */ /* -------------------------------------------------------------------- */ for( int iField = 0; iField < poClass->GetPropertyCount(); iField++ ) { GMLPropertyDefn *poProperty = poClass->GetProperty( iField ); OGRFieldType eFType; if( poProperty->GetType() == GMLPT_Untyped ) eFType = OFTString; else if( poProperty->GetType() == GMLPT_String ) eFType = OFTString; else if( poProperty->GetType() == GMLPT_Integer ) eFType = OFTInteger; else if( poProperty->GetType() == GMLPT_Real ) eFType = OFTReal; else if( poProperty->GetType() == GMLPT_StringList ) eFType = OFTStringList; else if( poProperty->GetType() == GMLPT_IntegerList ) eFType = OFTIntegerList; else if( poProperty->GetType() == GMLPT_RealList ) eFType = OFTRealList; else eFType = OFTString; OGRFieldDefn oField( poProperty->GetName(), eFType ); if ( STARTS_WITH_CI(oField.GetNameRef(), "ogr:") ) oField.SetName(poProperty->GetName()+4); if( poProperty->GetWidth() > 0 ) oField.SetWidth( poProperty->GetWidth() ); poLayer->GetLayerDefn()->AddFieldDefn( &oField ); } return poLayer; }
CPLXMLNode *GMLFeatureClass::SerializeToXML() { CPLXMLNode *psRoot; int iProperty; /* -------------------------------------------------------------------- */ /* Set feature class and core information. */ /* -------------------------------------------------------------------- */ psRoot = CPLCreateXMLNode( NULL, CXT_Element, "GMLFeatureClass" ); CPLCreateXMLElementAndValue( psRoot, "Name", GetName() ); CPLCreateXMLElementAndValue( psRoot, "ElementPath", GetElementName() ); if( GetGeometryElement() != NULL && strlen(GetGeometryElement()) > 0 ) CPLCreateXMLElementAndValue( psRoot, "GeometryElementPath", GetGeometryElement() ); if( GetGeometryType() != 0 /* wkbUnknown */ ) { char szValue[128]; sprintf( szValue, "%d", GetGeometryType() ); CPLCreateXMLElementAndValue( psRoot, "GeometryType", szValue ); } /* -------------------------------------------------------------------- */ /* Write out dataset specific information. */ /* -------------------------------------------------------------------- */ CPLXMLNode *psDSI; if( m_bHaveExtents || m_nFeatureCount != -1 || m_pszExtraInfo != NULL ) { psDSI = CPLCreateXMLNode( psRoot, CXT_Element, "DatasetSpecificInfo" ); if( m_nFeatureCount != -1 ) { char szValue[128]; sprintf( szValue, "%d", m_nFeatureCount ); CPLCreateXMLElementAndValue( psDSI, "FeatureCount", szValue ); } if( m_bHaveExtents ) { char szValue[128]; sprintf( szValue, "%.5f", m_dfXMin ); CPLCreateXMLElementAndValue( psDSI, "ExtentXMin", szValue ); sprintf( szValue, "%.5f", m_dfXMax ); CPLCreateXMLElementAndValue( psDSI, "ExtentXMax", szValue ); sprintf( szValue, "%.5f", m_dfYMin ); CPLCreateXMLElementAndValue( psDSI, "ExtentYMin", szValue ); sprintf( szValue, "%.5f", m_dfYMax ); CPLCreateXMLElementAndValue( psDSI, "ExtentYMax", szValue ); } if( m_pszExtraInfo ) CPLCreateXMLElementAndValue( psDSI, "ExtraInfo", m_pszExtraInfo ); } /* -------------------------------------------------------------------- */ /* emit property information. */ /* -------------------------------------------------------------------- */ for( iProperty = 0; iProperty < GetPropertyCount(); iProperty++ ) { GMLPropertyDefn *poPDefn = GetProperty( iProperty ); CPLXMLNode *psPDefnNode; const char *pszTypeName = "Unknown"; psPDefnNode = CPLCreateXMLNode( psRoot, CXT_Element, "PropertyDefn" ); CPLCreateXMLElementAndValue( psPDefnNode, "Name", poPDefn->GetName() ); CPLCreateXMLElementAndValue( psPDefnNode, "ElementPath", poPDefn->GetSrcElement() ); switch( poPDefn->GetType() ) { case GMLPT_Untyped: pszTypeName = "Untyped"; break; case GMLPT_String: pszTypeName = "String"; break; case GMLPT_Integer: pszTypeName = "Integer"; break; case GMLPT_Real: pszTypeName = "Real"; break; case GMLPT_Complex: pszTypeName = "Complex"; break; case GMLPT_IntegerList: pszTypeName = "IntegerList"; break; case GMLPT_RealList: pszTypeName = "RealList"; break; case GMLPT_StringList: pszTypeName = "StringList"; break; } CPLCreateXMLElementAndValue( psPDefnNode, "Type", pszTypeName ); if( EQUAL(pszTypeName,"String") ) { char szMaxLength[48]; sprintf(szMaxLength, "%d", poPDefn->GetWidth()); CPLCreateXMLElementAndValue ( psPDefnNode, "Width", szMaxLength ); } if( poPDefn->GetWidth() > 0 && EQUAL(pszTypeName,"Integer") ) { char szLength[48]; sprintf(szLength, "%d", poPDefn->GetWidth()); CPLCreateXMLElementAndValue ( psPDefnNode, "Width", szLength ); } if( poPDefn->GetWidth() > 0 && EQUAL(pszTypeName,"Real") ) { char szLength[48]; sprintf(szLength, "%d", poPDefn->GetWidth()); CPLCreateXMLElementAndValue ( psPDefnNode, "Width", szLength ); char szPrecision[48]; sprintf(szPrecision, "%d", poPDefn->GetPrecision()); CPLCreateXMLElementAndValue ( psPDefnNode, "Precision", szPrecision ); } } return psRoot; }
CPLXMLNode *GMLFeatureClass::SerializeToXML() { CPLXMLNode *psRoot; int iProperty; /* -------------------------------------------------------------------- */ /* Set feature class and core information. */ /* -------------------------------------------------------------------- */ psRoot = CPLCreateXMLNode( NULL, CXT_Element, "GMLFeatureClass" ); CPLCreateXMLElementAndValue( psRoot, "Name", GetName() ); CPLCreateXMLElementAndValue( psRoot, "ElementPath", GetElementName() ); if( m_nGeometryPropertyCount > 1 ) { for(int i=0; i < m_nGeometryPropertyCount; i++) { GMLGeometryPropertyDefn* poGeomFDefn = m_papoGeometryProperty[i]; CPLXMLNode *psPDefnNode; psPDefnNode = CPLCreateXMLNode( psRoot, CXT_Element, "GeomPropertyDefn" ); if( strlen(poGeomFDefn->GetName()) > 0 ) CPLCreateXMLElementAndValue( psPDefnNode, "Name", poGeomFDefn->GetName() ); if( poGeomFDefn->GetSrcElement() != NULL && strlen(poGeomFDefn->GetSrcElement()) > 0 ) CPLCreateXMLElementAndValue( psPDefnNode, "ElementPath", poGeomFDefn->GetSrcElement() ); if( poGeomFDefn->GetType() != 0 /* wkbUnknown */ ) { char szValue[128]; OGRwkbGeometryType eType = (OGRwkbGeometryType)poGeomFDefn->GetType(); CPLString osStr(OGRToOGCGeomType(eType)); if( wkbHasZ(eType) ) osStr += "Z"; CPLCreateXMLNode( psPDefnNode, CXT_Comment, osStr.c_str() ); sprintf( szValue, "%d", eType ); CPLCreateXMLElementAndValue( psPDefnNode, "Type", szValue ); } } } else if( m_nGeometryPropertyCount == 1 ) { GMLGeometryPropertyDefn* poGeomFDefn = m_papoGeometryProperty[0]; if( strlen(poGeomFDefn->GetName()) > 0 ) CPLCreateXMLElementAndValue( psRoot, "GeometryName", poGeomFDefn->GetName() ); if( poGeomFDefn->GetSrcElement() != NULL && strlen(poGeomFDefn->GetSrcElement()) > 0 ) CPLCreateXMLElementAndValue( psRoot, "GeometryElementPath", poGeomFDefn->GetSrcElement() ); if( poGeomFDefn->GetType() != 0 /* wkbUnknown */ ) { char szValue[128]; OGRwkbGeometryType eType = (OGRwkbGeometryType)poGeomFDefn->GetType(); CPLString osStr(OGRToOGCGeomType(eType)); if( wkbHasZ(eType) ) osStr += "Z"; CPLCreateXMLNode( psRoot, CXT_Comment, osStr.c_str() ); sprintf( szValue, "%d", eType ); CPLCreateXMLElementAndValue( psRoot, "GeometryType", szValue ); } } else { CPLCreateXMLElementAndValue( psRoot, "GeometryType", "100" ); } const char* pszSRSName = GetSRSName(); if( pszSRSName ) { CPLCreateXMLElementAndValue( psRoot, "SRSName", pszSRSName ); } /* -------------------------------------------------------------------- */ /* Write out dataset specific information. */ /* -------------------------------------------------------------------- */ CPLXMLNode *psDSI; if( m_bHaveExtents || m_nFeatureCount != -1 || m_pszExtraInfo != NULL ) { psDSI = CPLCreateXMLNode( psRoot, CXT_Element, "DatasetSpecificInfo" ); if( m_nFeatureCount != -1 ) { char szValue[128]; sprintf( szValue, CPL_FRMT_GIB, m_nFeatureCount ); CPLCreateXMLElementAndValue( psDSI, "FeatureCount", szValue ); } if( m_bHaveExtents && fabs(m_dfXMin) < 1e100 && fabs(m_dfXMax) < 1e100 && fabs(m_dfYMin) < 1e100 && fabs(m_dfYMax) < 1e100 ) { char szValue[128]; CPLsnprintf( szValue, sizeof(szValue), "%.5f", m_dfXMin ); CPLCreateXMLElementAndValue( psDSI, "ExtentXMin", szValue ); CPLsnprintf( szValue, sizeof(szValue), "%.5f", m_dfXMax ); CPLCreateXMLElementAndValue( psDSI, "ExtentXMax", szValue ); CPLsnprintf( szValue, sizeof(szValue), "%.5f", m_dfYMin ); CPLCreateXMLElementAndValue( psDSI, "ExtentYMin", szValue ); CPLsnprintf( szValue, sizeof(szValue), "%.5f", m_dfYMax ); CPLCreateXMLElementAndValue( psDSI, "ExtentYMax", szValue ); } if( m_pszExtraInfo ) CPLCreateXMLElementAndValue( psDSI, "ExtraInfo", m_pszExtraInfo ); } /* -------------------------------------------------------------------- */ /* emit property information. */ /* -------------------------------------------------------------------- */ for( iProperty = 0; iProperty < GetPropertyCount(); iProperty++ ) { GMLPropertyDefn *poPDefn = GetProperty( iProperty ); CPLXMLNode *psPDefnNode; const char *pszTypeName = "Unknown"; psPDefnNode = CPLCreateXMLNode( psRoot, CXT_Element, "PropertyDefn" ); CPLCreateXMLElementAndValue( psPDefnNode, "Name", poPDefn->GetName() ); CPLCreateXMLElementAndValue( psPDefnNode, "ElementPath", poPDefn->GetSrcElement() ); switch( poPDefn->GetType() ) { case GMLPT_Untyped: pszTypeName = "Untyped"; break; case GMLPT_String: case GMLPT_Boolean: pszTypeName = "String"; break; case GMLPT_Integer: case GMLPT_Short: case GMLPT_Integer64: pszTypeName = "Integer"; break; case GMLPT_Real: case GMLPT_Float: pszTypeName = "Real"; break; case GMLPT_Complex: pszTypeName = "Complex"; break; case GMLPT_IntegerList: case GMLPT_Integer64List: pszTypeName = "IntegerList"; break; case GMLPT_RealList: pszTypeName = "RealList"; break; case GMLPT_StringList: case GMLPT_BooleanList: pszTypeName = "StringList"; break; /* should not happen in practise for now because this is not */ /* autodetected */ case GMLPT_FeatureProperty: pszTypeName = "FeatureProperty"; break; /* should not happen in practise for now because this is not */ /* autodetected */ case GMLPT_FeaturePropertyList: pszTypeName = "FeaturePropertyList"; break; } CPLCreateXMLElementAndValue( psPDefnNode, "Type", pszTypeName ); if( poPDefn->GetType() == GMLPT_Boolean || poPDefn->GetType() == GMLPT_BooleanList ) CPLCreateXMLElementAndValue( psPDefnNode, "Subtype", "Boolean" ); else if( poPDefn->GetType() == GMLPT_Short ) CPLCreateXMLElementAndValue( psPDefnNode, "Subtype", "Short" ); else if( poPDefn->GetType() == GMLPT_Float ) CPLCreateXMLElementAndValue( psPDefnNode, "Subtype", "Float" ); else if( poPDefn->GetType() == GMLPT_Integer64 || poPDefn->GetType() == GMLPT_Integer64List ) CPLCreateXMLElementAndValue( psPDefnNode, "Subtype", "Integer64" ); if( EQUAL(pszTypeName,"String") ) { char szMaxLength[48]; sprintf(szMaxLength, "%d", poPDefn->GetWidth()); CPLCreateXMLElementAndValue ( psPDefnNode, "Width", szMaxLength ); } if( poPDefn->GetWidth() > 0 && EQUAL(pszTypeName,"Integer") ) { char szLength[48]; sprintf(szLength, "%d", poPDefn->GetWidth()); CPLCreateXMLElementAndValue ( psPDefnNode, "Width", szLength ); } if( poPDefn->GetWidth() > 0 && EQUAL(pszTypeName,"Real") ) { char szLength[48]; sprintf(szLength, "%d", poPDefn->GetWidth()); CPLCreateXMLElementAndValue ( psPDefnNode, "Width", szLength ); char szPrecision[48]; sprintf(szPrecision, "%d", poPDefn->GetPrecision()); CPLCreateXMLElementAndValue ( psPDefnNode, "Precision", szPrecision ); } } return psRoot; }