DBInt RGlibRGIS2DataStream(DBObjData *grdData, DBObjData *tmplData, char *fieldName, FILE *outFile) { DBInt layerID, ret = DBSuccess, itemSize, itemID; DBInt intValue; DBFloat floatValue; void *data; MFVarHeader_t varHeader; DBObjRecord *layerRec, *gridRec; DBObjTableField *fieldPTR = (DBObjTableField *) NULL; DBGridIF *gridIF; DBVPointIF *tmplPntIF = (DBVPointIF *) NULL; DBGridIF *tmplGrdIF = (DBGridIF *) NULL; DBNetworkIF *tmplNetIF = (DBNetworkIF *) NULL; gridIF = new DBGridIF(grdData); varHeader.Swap = 1; if (grdData->Type() == DBTypeGridDiscrete) { DBObjTable *itemTable = grdData->Table(DBrNItems); if (fieldName == (char *) NULL) fieldName = DBrNGridValue; if ((fieldPTR = itemTable->Field(fieldName)) == (DBObjTableField *) NULL) { CMmsgPrint(CMmsgAppError, "Error: Invalid field [%s] in: %s %d", fieldName, __FILE__, __LINE__); return (DBFault); } itemSize = fieldPTR->Length(); switch (fieldPTR->Type()) { case DBTableFieldInt: switch (itemSize) { default: case sizeof(DBByte): varHeader.DataType = MFByte; break; case sizeof(DBShort): varHeader.DataType = MFShort; break; case sizeof(DBInt): varHeader.DataType = MFInt; break; } varHeader.Missing.Int = fieldPTR->IntNoData(); break; case DBTableFieldFloat: switch (itemSize) { default: case sizeof(DBFloat4): varHeader.DataType = MFFloat; break; case sizeof(DBFloat): varHeader.DataType = MFDouble; break; } varHeader.Missing.Float = fieldPTR->FloatNoData(); break; } } else { if (fieldName != (char *) NULL) CMmsgPrint(CMmsgUsrError, "Warning: Fieldname ignored for continuous grid!"); itemSize = gridIF->ValueSize(); switch (gridIF->ValueType()) { case DBVariableInt: switch (itemSize) { case 1: varHeader.DataType = MFByte; break; case 2: varHeader.DataType = MFShort; break; case 4: varHeader.DataType = MFInt; break; } varHeader.Missing.Int = (int) gridIF->MissingValue(); break; case DBVariableFloat: switch (itemSize) { case 4: varHeader.DataType = MFFloat; break; case 8: varHeader.DataType = MFDouble; break; } varHeader.Missing.Float = gridIF->MissingValue(); break; } } if (tmplData == (DBObjData *) NULL) { tmplGrdIF = gridIF; varHeader.ItemNum = gridIF->RowNum() * gridIF->ColNum(); } else { switch (tmplData->Type()) { case DBTypeVectorPoint: tmplPntIF = new DBVPointIF(tmplData); varHeader.ItemNum = tmplPntIF->ItemNum(); break; case DBTypeGridContinuous: case DBTypeGridDiscrete: tmplGrdIF = new DBGridIF(tmplData); varHeader.ItemNum = gridIF->RowNum() * gridIF->ColNum(); break; case DBTypeNetwork: tmplNetIF = new DBNetworkIF(tmplData); varHeader.ItemNum = tmplNetIF->CellNum(); break; default: delete gridIF; return (DBFault); } } if ((data = (void *) calloc(varHeader.ItemNum, itemSize)) == (void *) NULL) { CMmsgPrint(CMmsgSysError, "Error! Allocating %d items of %d size in: %s %d", varHeader.ItemNum, itemSize, __FILE__, __LINE__); return (DBFault); } /************************************************************** * * * Point template * * * **************************************************************/ if (tmplPntIF != (DBVPointIF *) NULL) { DBObjRecord *pntRec; if (fieldPTR == (DBObjTableField *) NULL) { for (layerID = 0; layerID < gridIF->LayerNum(); ++layerID) { layerRec = gridIF->Layer(layerID); strncpy(varHeader.Date, layerRec->Name(), MFDateStringLength - 1); for (itemID = 0; itemID < varHeader.ItemNum; ++itemID) { pntRec = tmplPntIF->Item(itemID); if ((varHeader.DataType == MFByte) || (varHeader.DataType == MFShort) || (varHeader.DataType == MFInt)) { if (gridIF->Value(layerRec, tmplPntIF->Coordinate(pntRec), &intValue) == false) intValue = varHeader.Missing.Int; switch (varHeader.DataType) { case MFByte: ((char *) data)[itemID] = (char) intValue; break; case MFShort: ((short *) data)[itemID] = (short) intValue; break; case MFInt: ((int *) data)[itemID] = (short) intValue; break; } } else { if (gridIF->Value(layerRec, tmplPntIF->Coordinate(pntRec), &floatValue) == false) floatValue = varHeader.Missing.Float; switch (varHeader.DataType) { case MFFloat: ((float *) data)[itemID] = (float) floatValue; break; case MFDouble: ((double *) data)[itemID] = (double) floatValue; break; } } } if ((DBInt) fwrite(&varHeader, sizeof(MFVarHeader_t), 1, outFile) != 1) { CMmsgPrint(CMmsgSysError, "Error: Writing record header in: %s %d", __FILE__, __LINE__); ret = DBFault; break; } if ((DBInt) fwrite(data, itemSize, varHeader.ItemNum, outFile) != varHeader.ItemNum) { CMmsgPrint(CMmsgSysError, "Error: Writing data in: %s %d", __FILE__, __LINE__); ret = DBFault; break; } } } else { for (layerID = 0; layerID < gridIF->LayerNum(); ++layerID) { layerRec = gridIF->Layer(layerID); strncpy(varHeader.Date, layerRec->Name(), MFDateStringLength - 1); for (itemID = 0; itemID < varHeader.ItemNum; ++itemID) { pntRec = tmplPntIF->Item(itemID); gridRec = gridIF->GridItem(layerRec, tmplPntIF->Coordinate(pntRec)); switch (varHeader.DataType) { case MFByte: ((char *) data)[itemID] = gridRec != (DBObjRecord *) NULL ? fieldPTR->Int(gridRec) : fieldPTR->IntNoData(); break; case MFShort: ((short *) data)[itemID] = gridRec != (DBObjRecord *) NULL ? fieldPTR->Int(gridRec) : fieldPTR->IntNoData(); break; case MFInt: ((int *) data)[itemID] = gridRec != (DBObjRecord *) NULL ? fieldPTR->Int(gridRec) : fieldPTR->IntNoData(); break; case MFFloat: ((float *) data)[itemID] = gridRec != (DBObjRecord *) NULL ? fieldPTR->Int(gridRec) : fieldPTR->FloatNoData(); break; case MFDouble: ((double *) data)[itemID] = gridRec != (DBObjRecord *) NULL ? fieldPTR->Int(gridRec) : fieldPTR->FloatNoData(); break; } } if ((DBInt) fwrite(&varHeader, sizeof(MFVarHeader_t), 1, outFile) != 1) { CMmsgPrint(CMmsgSysError, "Error: Writing record header in: %s %d", __FILE__, __LINE__); ret = DBFault; break; } if ((DBInt) fwrite(data, itemSize, varHeader.ItemNum, outFile) != varHeader.ItemNum) { CMmsgPrint(CMmsgSysError, "Error: Writing data in: %s %d", __FILE__, __LINE__); ret = DBFault; break; } } } delete tmplPntIF; } /************************************************************** * * * Grid Template (default when no template coverage is given. * * * **************************************************************/ else if (tmplGrdIF != (DBGridIF *) NULL) { DBPosition pos; DBCoordinate coord; if (fieldPTR == (DBObjTableField *) NULL) { for (layerID = 0; layerID < gridIF->LayerNum(); ++layerID) { layerRec = gridIF->Layer(layerID); strncpy(varHeader.Date, layerRec->Name(), MFDateStringLength - 1); for (pos.Row = 0; pos.Row < tmplGrdIF->RowNum(); ++pos.Row) for (pos.Col = 0; pos.Col < tmplGrdIF->ColNum(); ++pos.Col) { itemID = pos.Row * tmplGrdIF->ColNum() + pos.Col; if ((varHeader.DataType == MFByte) || (varHeader.DataType == MFShort) || (varHeader.DataType == MFInt)) { if (tmplGrdIF != gridIF) { tmplGrdIF->Pos2Coord(pos, coord); if (gridIF->Value(layerRec, coord, &intValue) == false) intValue = varHeader.Missing.Int; } else { if (gridIF->Value(layerRec, pos, &intValue) == false) intValue = varHeader.Missing.Int; } switch (varHeader.DataType) { case MFByte: ((char *) data)[itemID] = (char) intValue; break; case MFShort: ((short *) data)[itemID] = (short) intValue; break; case MFInt: ((int *) data)[itemID] = (short) intValue; break; } } else { if (tmplGrdIF != gridIF) { tmplGrdIF->Pos2Coord(pos, coord); if (gridIF->Value(layerRec, coord, &floatValue) == false) floatValue = varHeader.Missing.Float; } else { if (gridIF->Value(layerRec, pos, &floatValue) == false) floatValue = varHeader.Missing.Float; } switch (varHeader.DataType) { case MFFloat: ((float *) data)[itemID] = (float) floatValue; break; case MFDouble: ((double *) data)[itemID] = (double) floatValue; break; } } } if ((DBInt) fwrite(&varHeader, sizeof(MFVarHeader_t), 1, outFile) != 1) { CMmsgPrint(CMmsgSysError, "Error: Writing record header in: %s %d", __FILE__, __LINE__); ret = DBFault; break; } if ((DBInt) fwrite(data, itemSize, varHeader.ItemNum, outFile) != varHeader.ItemNum) { CMmsgPrint(CMmsgSysError, "Error: Writing data in: %s %d", __FILE__, __LINE__); ret = DBFault; break; } } } else { for (layerID = 0; layerID < gridIF->LayerNum(); ++layerID) { layerRec = gridIF->Layer(layerID); strncpy(varHeader.Date, layerRec->Name(), MFDateStringLength - 1); for (pos.Row = 0; pos.Row < tmplGrdIF->RowNum(); ++pos.Row) for (pos.Col = 0; pos.Col < tmplGrdIF->ColNum(); ++pos.Col) { itemID = pos.Row * tmplGrdIF->ColNum() + pos.Col; if (tmplGrdIF != gridIF) { tmplGrdIF->Pos2Coord(pos, coord); gridRec = gridIF->GridItem(layerRec, coord); } else gridRec = gridIF->GridItem(layerRec, pos); switch (varHeader.DataType) { case MFByte: ((char *) data)[itemID] = gridRec != (DBObjRecord *) NULL ? fieldPTR->Int(gridRec) : fieldPTR->IntNoData(); break; case MFShort: ((short *) data)[itemID] = gridRec != (DBObjRecord *) NULL ? fieldPTR->Int(gridRec) : fieldPTR->IntNoData(); break; case MFInt: ((int *) data)[itemID] = gridRec != (DBObjRecord *) NULL ? fieldPTR->Int(gridRec) : fieldPTR->IntNoData(); break; case MFFloat: ((float *) data)[itemID] = gridRec != (DBObjRecord *) NULL ? fieldPTR->Int(gridRec) : fieldPTR->FloatNoData(); break; case MFDouble: ((double *) data)[itemID] = gridRec != (DBObjRecord *) NULL ? fieldPTR->Int(gridRec) : fieldPTR->FloatNoData(); break; } } if ((DBInt) fwrite(&varHeader, sizeof(MFVarHeader_t), 1, outFile) != 1) { CMmsgPrint(CMmsgSysError, "Error: Writing record header in: %s %d", __FILE__, __LINE__); ret = DBFault; break; } if ((DBInt) fwrite(data, itemSize, varHeader.ItemNum, outFile) != varHeader.ItemNum) { CMmsgPrint(CMmsgSysError, "Error: Writing data in: %s %d", __FILE__, __LINE__); ret = DBFault; break; } } } if (tmplGrdIF != gridIF) delete tmplGrdIF; } /************************************************************** * * * Network Template * * * **************************************************************/ else if (tmplNetIF != (DBNetworkIF *) NULL) { DBObjRecord *cellRec; if (fieldPTR == (DBObjTableField *) NULL) { for (layerID = 0; layerID < gridIF->LayerNum(); ++layerID) { layerRec = gridIF->Layer(layerID); strncpy(varHeader.Date, layerRec->Name(), MFDateStringLength - 1); for (itemID = 0; itemID < varHeader.ItemNum; ++itemID) { cellRec = tmplNetIF->Cell(itemID); if ((varHeader.DataType == MFByte) || (varHeader.DataType == MFShort) || (varHeader.DataType == MFInt)) { if (gridIF->Value(layerRec, tmplNetIF->Center(cellRec), &intValue) == false) intValue = varHeader.Missing.Int; switch (varHeader.DataType) { case MFByte: ((char *) data)[itemID] = (char) intValue; break; case MFShort: ((short *) data)[itemID] = (short) intValue; break; case MFInt: ((int *) data)[itemID] = (short) intValue; break; } } else { if (gridIF->Value(layerRec, tmplNetIF->Center(cellRec), &floatValue) == false) floatValue = varHeader.Missing.Float; switch (varHeader.DataType) { case MFFloat: ((float *) data)[itemID] = (float) floatValue; break; case MFDouble: ((double *) data)[itemID] = (double) floatValue; break; } } } if ((DBInt) fwrite(&varHeader, sizeof(MFVarHeader_t), 1, outFile) != 1) { CMmsgPrint(CMmsgSysError, "Error: Writing record header in: %s %d", __FILE__, __LINE__); ret = DBFault; break; } if ((DBInt) fwrite(data, itemSize, varHeader.ItemNum, outFile) != varHeader.ItemNum) { CMmsgPrint(CMmsgSysError, "Error: Writing data in: %s %d", __FILE__, __LINE__); ret = DBFault; break; } } } else { for (layerID = 0; layerID < gridIF->LayerNum(); ++layerID) { layerRec = gridIF->Layer(layerID); strncpy(varHeader.Date, layerRec->Name(), MFDateStringLength - 1); for (itemID = 0; itemID < varHeader.ItemNum; ++itemID) { cellRec = tmplNetIF->Cell(itemID); gridRec = gridIF->GridItem(layerRec, tmplNetIF->Center(cellRec)); switch (varHeader.DataType) { case MFByte: ((char *) data)[itemID] = gridRec != (DBObjRecord *) NULL ? fieldPTR->Int(gridRec) : fieldPTR->IntNoData(); break; case MFShort: ((short *) data)[itemID] = gridRec != (DBObjRecord *) NULL ? fieldPTR->Int(gridRec) : fieldPTR->IntNoData(); break; case MFInt: ((int *) data)[itemID] = gridRec != (DBObjRecord *) NULL ? fieldPTR->Int(gridRec) : fieldPTR->IntNoData(); break; case MFFloat: ((float *) data)[itemID] = gridRec != (DBObjRecord *) NULL ? fieldPTR->Float(gridRec) : fieldPTR->FloatNoData(); break; case MFDouble: ((double *) data)[itemID] = gridRec != (DBObjRecord *) NULL ? fieldPTR->Float(gridRec) : fieldPTR->FloatNoData(); break; } } if ((DBInt) fwrite(&varHeader, sizeof(MFVarHeader_t), 1, outFile) != 1) { CMmsgPrint(CMmsgSysError, "Error: Writing record header in: %s %d", __FILE__, __LINE__); ret = DBFault; break; } if ((DBInt) fwrite(data, itemSize, varHeader.ItemNum, outFile) != varHeader.ItemNum) { CMmsgPrint(CMmsgSysError, "Error: Writing data in: %s %d", __FILE__, __LINE__); ret = DBFault; break; } } } delete tmplNetIF; } free(data); delete gridIF; return (ret); }
DBInt Write (FILE *file,DBObjData *data) { char *dmRecord; DBInt layerID, docLen, dbType, intVal; DBFloat floatVal; DBPosition pos; DBObjRecord *layerRec; DBGridIF *gridIF = new DBGridIF (data); DMLayerHeader dmLayerHeader; dbType = gridIF->ValueType (); switch (dbType) { case DBTableFieldFloat: DataType (DMFloat); break; case DBTableFieldInt: DataType (gridIF->ValueSize () > 1 ? DMInt : DMByte); break; } CellWidth (gridIF->CellWidth ()); CellHeight (gridIF->CellHeight ()); Extent (data->Extent ()); LayerNum (gridIF->LayerNum ()); RowNum (gridIF->RowNum ()); ColNum (gridIF->ColNum ()); dmRecord = (char *) calloc (RowNum () * ColNum (),DataType () != DMByte ? sizeof (int) : sizeof (char)); if (dmRecord == (char *) NULL) { CMmsgPrint (CMmsgSysError, "Memory Allocation Error in: %s %d",__FILE__,__LINE__); return (DBFault); } DMFileHeader::Write (file); for (layerID = 0;layerID < gridIF->LayerNum ();++layerID) { layerRec = gridIF->Layer (layerID); dmLayerHeader.Description (layerRec->Name ()); dmLayerHeader.Write (file); } docLen = strlen (data->Document (DBDocComment)); if (fwrite (&docLen,sizeof (int),1,file) != 1) { CMmsgPrint (CMmsgSysError, "File Writing Error in: %s %d",__FILE__,__LINE__); return (DBFault); } if (docLen > 0) { if (fwrite (data->Document (DBDocComment),docLen,1,file) != 1) { CMmsgPrint (CMmsgSysError, "File Writing Error in: %s %d",__FILE__,__LINE__); return (DBFault); } } for (layerID = 0;layerID < gridIF->LayerNum ();++layerID) { layerRec = gridIF->Layer (layerID); switch (data->Type ()) { case DBTypeGridContinuous: layerRec = gridIF->Layer (layerID); if (dbType == DBTableFieldFloat) { for (pos.Row = 0;pos.Row < RowNum ();pos.Row++) for (pos.Col = 0;pos.Col < ColNum ();pos.Col++) if (gridIF->Value (layerRec,pos,&floatVal)) ((float *) dmRecord) [(RowNum () - pos.Row - 1) * ColNum () + pos.Col] = (float) floatVal; else ((float *) dmRecord) [(RowNum () - pos.Row - 1) * ColNum () + pos.Col] = MissingValue (); } else { for (pos.Row = 0;pos.Row < RowNum ();pos.Row++) for (pos.Col = 0;pos.Col < ColNum ();pos.Col++) if (gridIF->Value (layerRec,pos,&intVal)) { if (DataType () == DMInt) ((int *) dmRecord) [(RowNum () - pos.Row - 1) * ColNum () + pos.Col] = (int) intVal; else dmRecord [(RowNum () - pos.Row - 1) * ColNum () + pos.Col] = intVal; } else { if (DataType () == DMInt) ((int *) dmRecord) [(RowNum () - pos.Row - 1) * ColNum () + pos.Col] = (int) MissingValue (); else dmRecord [(RowNum () - pos.Row - 1) * ColNum () + pos.Col] = -99; } } break; case DBTypeGridDiscrete: for (pos.Row = 0;pos.Row < RowNum ();pos.Row++) for (pos.Col = 0;pos.Col < ColNum ();pos.Col++) { intVal = gridIF->GridValue (layerRec,pos); if (DataType () == DMInt) ((int *) dmRecord) [(RowNum () - pos.Row - 1) * ColNum () + pos.Col] = intVal; else dmRecord [(RowNum () - pos.Row - 1) * ColNum () + pos.Col] = -99; } break; default: CMmsgPrint (CMmsgAppError, "Invalid Data Type in: %s %d",__FILE__,__LINE__); free (dmRecord); delete gridIF; return (DBFault); } if ((DBInt) fwrite (dmRecord,DataType () == DMByte ? sizeof (char) : sizeof (int),DataPointNum (),file) != DataPointNum ()) { CMmsgPrint (CMmsgSysError, "File Writing Error in: %s %d",__FILE__,__LINE__); return (DBFault); } } free (dmRecord); delete gridIF; return (DBSuccess); }