📄 s57writer.cpp
字号:
double *padfX, *padfY; CPLAssert( poFeature->GetFieldAsInteger( "RCNM") == RCNM_VE ); padfX = (double *) CPLMalloc(sizeof(double) * nVCount); padfY = (double *) CPLMalloc(sizeof(double) * nVCount); for( i = 0; i < nVCount; i++ ) { padfX[i] = poLS->getX(i); padfY[i] = poLS->getY(i); } WriteGeometry( poRec, nVCount, padfX, padfY, NULL ); CPLFree( padfX ); CPLFree( padfY ); }/* -------------------------------------------------------------------- *//* edge node linkages. *//* -------------------------------------------------------------------- */ if( poFeature->GetDefnRef()->GetFieldIndex( "NAME_RCNM_0" ) >= 0 ) { DDFField *poField; char szName[5]; int nRCID; CPLAssert( poFeature->GetFieldAsInteger( "NAME_RCNM_0") == RCNM_VC ); poField = poRec->AddField( poModule->FindFieldDefn( "VRPT" ) ); nRCID = poFeature->GetFieldAsInteger( "NAME_RCID_0"); szName[0] = RCNM_VC; szName[1] = nRCID & 0xff; szName[2] = (nRCID & 0xff00) >> 8; szName[3] = (nRCID & 0xff0000) >> 16; szName[4] = (nRCID & 0xff000000) >> 24; poRec->SetStringSubfield( "VRPT", 0, "NAME", 0, szName, 5 ); poRec->SetIntSubfield ( "VRPT", 0, "ORNT", 0, poFeature->GetFieldAsInteger( "ORNT_0") ); poRec->SetIntSubfield ( "VRPT", 0, "USAG", 0, poFeature->GetFieldAsInteger( "USAG_0") ); poRec->SetIntSubfield ( "VRPT", 0, "TOPI", 0, poFeature->GetFieldAsInteger( "TOPI_0") ); poRec->SetIntSubfield ( "VRPT", 0, "MASK", 0, poFeature->GetFieldAsInteger( "MASK_0") ); nRCID = poFeature->GetFieldAsInteger( "NAME_RCID_1"); szName[0] = RCNM_VC; szName[1] = nRCID & 0xff; szName[2] = (nRCID & 0xff00) >> 8; szName[3] = (nRCID & 0xff0000) >> 16; szName[4] = (nRCID & 0xff000000) >> 24; poRec->SetStringSubfield( "VRPT", 0, "NAME", 1, szName, 5 ); poRec->SetIntSubfield ( "VRPT", 0, "ORNT", 1, poFeature->GetFieldAsInteger( "ORNT_1") ); poRec->SetIntSubfield ( "VRPT", 0, "USAG", 1, poFeature->GetFieldAsInteger( "USAG_1") ); poRec->SetIntSubfield ( "VRPT", 0, "TOPI", 1, poFeature->GetFieldAsInteger( "TOPI_1") ); poRec->SetIntSubfield ( "VRPT", 0, "MASK", 1, poFeature->GetFieldAsInteger( "MASK_1") ); }/* -------------------------------------------------------------------- *//* Write out the record. *//* -------------------------------------------------------------------- */ poRec->Write(); delete poRec; return TRUE;}/************************************************************************//* GetHEXChar() *//************************************************************************/static int GetHEXChar( const char *pszSrcHEXString ){ int nResult = 0; if( pszSrcHEXString[0] == '\0' || pszSrcHEXString[1] == '\0' ) return 0; if( pszSrcHEXString[0] >= '0' && pszSrcHEXString[0] <= '9' ) nResult += (pszSrcHEXString[0] - '0') * 16; else if( pszSrcHEXString[0] >= 'a' && pszSrcHEXString[0] <= 'f' ) nResult += (pszSrcHEXString[0] - 'a' + 10) * 16; else if( pszSrcHEXString[0] >= 'A' && pszSrcHEXString[0] <= 'F' ) nResult += (pszSrcHEXString[0] - 'A' + 10) * 16; if( pszSrcHEXString[1] >= '0' && pszSrcHEXString[1] <= '9' ) nResult += pszSrcHEXString[1] - '0'; else if( pszSrcHEXString[1] >= 'a' && pszSrcHEXString[1] <= 'f' ) nResult += pszSrcHEXString[1] - 'a' + 10; else if( pszSrcHEXString[1] >= 'A' && pszSrcHEXString[1] <= 'F' ) nResult += pszSrcHEXString[1] - 'A' + 10; return nResult;}/************************************************************************//* WriteCompleteFeature() *//************************************************************************/int S57Writer::WriteCompleteFeature( OGRFeature *poFeature ){ OGRFeatureDefn *poFDefn = poFeature->GetDefnRef();/* -------------------------------------------------------------------- *//* We handle primitives in a seperate method. *//* -------------------------------------------------------------------- */ if( EQUAL(poFDefn->GetName(),OGRN_VI) || EQUAL(poFDefn->GetName(),OGRN_VC) || EQUAL(poFDefn->GetName(),OGRN_VE) ) return WritePrimitive( poFeature );/* -------------------------------------------------------------------- *//* Create the record. *//* -------------------------------------------------------------------- */ DDFRecord *poRec = MakeRecord();/* -------------------------------------------------------------------- *//* Add the FRID. *//* -------------------------------------------------------------------- */ DDFField *poField; poField = poRec->AddField( poModule->FindFieldDefn( "FRID" ) ); poRec->SetIntSubfield ( "FRID", 0, "RCNM", 0, 100 ); poRec->SetIntSubfield ( "FRID", 0, "RCID", 0, poFeature->GetFieldAsInteger( "RCID" ) ); poRec->SetIntSubfield ( "FRID", 0, "PRIM", 0, poFeature->GetFieldAsInteger( "PRIM" ) ); poRec->SetIntSubfield ( "FRID", 0, "GRUP", 0, poFeature->GetFieldAsInteger( "GRUP") ); poRec->SetIntSubfield ( "FRID", 0, "OBJL", 0, poFeature->GetFieldAsInteger( "OBJL") ); poRec->SetIntSubfield ( "FRID", 0, "RVER", 0, 1 ); /* always new insert*/ poRec->SetIntSubfield ( "FRID", 0, "RUIN", 0, 1 ); /* -------------------------------------------------------------------- *//* Add the FOID *//* -------------------------------------------------------------------- */ poField = poRec->AddField( poModule->FindFieldDefn( "FOID" ) ); poRec->SetIntSubfield ( "FOID", 0, "AGEN", 0, poFeature->GetFieldAsInteger( "AGEN") ); poRec->SetIntSubfield ( "FOID", 0, "FIDN", 0, poFeature->GetFieldAsInteger( "FIDN") ); poRec->SetIntSubfield ( "FOID", 0, "FIDS", 0, poFeature->GetFieldAsInteger( "FIDS") );/* -------------------------------------------------------------------- *//* ATTF support. *//* -------------------------------------------------------------------- */ if( poRegistrar != NULL && poRegistrar->SelectClass( poFeature->GetDefnRef()->GetName() ) && !WriteATTF( poRec, poFeature ) ) return FALSE;/* -------------------------------------------------------------------- *//* Add the FSPT if needed. *//* -------------------------------------------------------------------- */ if( poFeature->IsFieldSet( poFeature->GetFieldIndex("NAME_RCNM") ) ) { int nItemCount, i; const int *panRCNM, *panRCID, *panORNT, *panUSAG, *panMASK; unsigned char *pabyRawData; int nRawDataSize; panRCNM = poFeature->GetFieldAsIntegerList( "NAME_RCNM", &nItemCount ); panRCID = poFeature->GetFieldAsIntegerList( "NAME_RCID", &nItemCount ); panORNT = poFeature->GetFieldAsIntegerList( "ORNT", &nItemCount ); panUSAG = poFeature->GetFieldAsIntegerList( "USAG", &nItemCount ); panMASK = poFeature->GetFieldAsIntegerList( "MASK", &nItemCount ); CPLAssert( sizeof(int) == sizeof(GInt32) ); nRawDataSize = nItemCount * 8 + 1; pabyRawData = (unsigned char *) CPLMalloc(nRawDataSize); pabyRawData[nRawDataSize-1] = DDF_UNIT_TERMINATOR; for( i = 0; i < nItemCount; i++ ) { GInt32 nRCID = CPL_LSBWORD32(panRCID[i]); pabyRawData[i*8 + 0] = panRCNM[i]; memcpy( pabyRawData + i*8 + 1, &nRCID, 4 ); pabyRawData[i*8 + 5] = panORNT[i]; pabyRawData[i*8 + 6] = panUSAG[i]; pabyRawData[i*8 + 7] = panMASK[i]; } poField = poRec->AddField( poModule->FindFieldDefn( "FSPT" ) ); poRec->SetFieldRaw( poField, 0, (const char *) pabyRawData, nRawDataSize ); CPLFree( pabyRawData ); }/* -------------------------------------------------------------------- *//* Add the FFPT if needed. *//* -------------------------------------------------------------------- */ char **papszLNAM_REFS = poFeature->GetFieldAsStringList( "LNAM_REFS" ); if( CSLCount(papszLNAM_REFS) > 0 ) { int i, nRefCount = CSLCount(papszLNAM_REFS); const int *panRIND = poFeature->GetFieldAsIntegerList( "FFPT_RIND", NULL ); poRec->AddField( poModule->FindFieldDefn( "FFPT" ) ); for( i = 0; i < nRefCount; i++ ) { char szLNAM[9]; if( strlen(papszLNAM_REFS[i]) < 16 ) continue; // AGEN szLNAM[1] = GetHEXChar( papszLNAM_REFS[i] + 0 ); szLNAM[0] = GetHEXChar( papszLNAM_REFS[i] + 2 ); // FIDN szLNAM[5] = GetHEXChar( papszLNAM_REFS[i] + 4 ); szLNAM[4] = GetHEXChar( papszLNAM_REFS[i] + 6 ); szLNAM[3] = GetHEXChar( papszLNAM_REFS[i] + 8 ); szLNAM[2] = GetHEXChar( papszLNAM_REFS[i] + 10 ); // FIDS szLNAM[7] = GetHEXChar( papszLNAM_REFS[i] + 12 ); szLNAM[6] = GetHEXChar( papszLNAM_REFS[i] + 14 ); szLNAM[8] = '\0'; poRec->SetStringSubfield( "FFPT", 0, "LNAM", i, (char *) szLNAM, 8 ); poRec->SetIntSubfield( "FFPT", 0, "RIND", i, panRIND[i] ); } }/* -------------------------------------------------------------------- *//* Write out the record. *//* -------------------------------------------------------------------- */ poRec->Write(); delete poRec; return TRUE;}/************************************************************************//* SetClassBased() *//************************************************************************/void S57Writer::SetClassBased( S57ClassRegistrar * poReg ){ poRegistrar = poReg;}/************************************************************************//* WriteATTF() *//************************************************************************/int S57Writer::WriteATTF( DDFRecord *poRec, OGRFeature *poFeature ){ int nRawSize=0, nACount = 0; char achRawData[5000]; char **papszAttrList; CPLAssert( poRegistrar != NULL );/* -------------------------------------------------------------------- *//* Loop over all attributes. *//* -------------------------------------------------------------------- */ papszAttrList = poRegistrar->GetAttributeList(NULL); for( int iAttr = 0; papszAttrList[iAttr] != NULL; iAttr++ ) { int iField = poFeature->GetFieldIndex( papszAttrList[iAttr] ); OGRFieldType eFldType = poFeature->GetDefnRef()->GetFieldDefn(iField)->GetType(); GInt16 nATTL; const char *pszATVL; if( iField < 0 ) continue; if( !poFeature->IsFieldSet( iField ) ) continue; nATTL = poRegistrar->FindAttrByAcronym( papszAttrList[iAttr] ); if( nATTL == -1 ) continue; nATTL = CPL_LSBWORD16( nATTL ); memcpy( achRawData + nRawSize, &nATTL, 2 ); nRawSize += 2; pszATVL = poFeature->GetFieldAsString( iField ); // Special hack to handle special "empty" marker in integer fields. if( atoi(pszATVL) == EMPTY_NUMBER_MARKER && (eFldType == OFTInteger || eFldType == OFTReal) ) pszATVL = ""; // Watch for really long data. if( strlen(pszATVL) + nRawSize + 10 > sizeof(achRawData) ) { CPLError( CE_Failure, CPLE_AppDefined, "Too much ATTF data for fixed buffer size." ); return FALSE; } // copy data into record buffer. memcpy( achRawData + nRawSize, pszATVL, strlen(pszATVL) ); nRawSize += strlen(pszATVL); achRawData[nRawSize++] = DDF_UNIT_TERMINATOR; nACount++; }/* -------------------------------------------------------------------- *//* If we got no attributes, return without adding ATTF. *//* -------------------------------------------------------------------- */ if( nACount == 0 ) return TRUE;/* -------------------------------------------------------------------- *//* Write the new field value. *//* -------------------------------------------------------------------- */ DDFField *poField; poField = poRec->AddField( poModule->FindFieldDefn( "ATTF" ) ); return poRec->SetFieldRaw( poField, 0, achRawData, nRawSize );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -