int DBNetworkIF::Trim () { DBInt i, row, col; DBPosition pos, min, max; DBRegion extent = DataPTR->Extent (); DBObjRecord *cellRec; DBCoordinate coord; min.Row = RowNum (); min.Col = ColNum (); max.Row = max.Col = 0; for (i = 0;i < CellNum ();++i) { DBPause (33 * i / CellNum ()); cellRec = CellTable->Item (i); pos = CellPosition (cellRec); min.Row = min.Row < pos.Row ? min.Row : pos.Row; min.Col = min.Col < pos.Col ? min.Col : pos.Col; max.Row = max.Row > pos.Row ? max.Row : pos.Row; max.Col = max.Col > pos.Col ? max.Col : pos.Col; } for (i = 0;i < CellNum ();++i) { DBPause (33 + 33 * i / CellNum ()); cellRec = CellTable->Item (i); pos = CellPosition (cellRec); pos.Row = pos.Row - min.Row; pos.Col = pos.Col - min.Col; PositionFLD->Position (cellRec,pos); } coord.X = min.Col * CellWidth (); coord.Y = min.Row * CellHeight (); extent.LowerLeft = extent.LowerLeft + coord; coord.X = (ColNum () - max.Col - 1) * CellWidth (); coord.Y = (RowNum () - max.Row - 1) * CellHeight (); extent.UpperRight = extent.UpperRight - coord; DataPTR->Extent (extent); row = max.Row - min.Row + 1; col = max.Col - min.Col + 1; RowNumFLD->Int (LayerRecord,row); ColNumFLD->Int (LayerRecord,col); DataRec->Realloc (row * col * sizeof (int)); for (row = 0;row < RowNum ();row++) for (col = 0;col < ColNum ();col++) ((DBInt *) DataRec->Data ()) [row * ColNum () + col] = DBFault; for (i = 0;i < CellNum ();++i) { DBPause (67 + 33 * i / CellNum ()); pos = CellPosition (CellTable->Item (i)); ((DBInt *) DataRec->Data ()) [pos.Row * ColNum () + pos.Col] = cellRec->RowID (); } return (DBSuccess); }
DBObjRecord *DBNetworkIF::Cell (DBPosition pos) const { DBInt cellID; if (pos.Col < 0) return ((DBObjRecord *) NULL); if (pos.Row < 0) return ((DBObjRecord *) NULL); if (pos.Col >= ColNum ()) return ((DBObjRecord *) NULL); if (pos.Row >= RowNum ()) return ((DBObjRecord *) NULL); if ((cellID = ((DBInt *) DataRec->Data ()) [pos.Row * ColNum () + pos.Col ]) == DBFault) return ((DBObjRecord *) NULL); return (CellTable->Item (cellID)); }
void DBGridIF::DiscreteStats() { DBInt recordID, layerID; DBFloat area, sumArea = 0.0; DBPosition pos; DBObjTableField *areaFLD = ItemTable->Field(DBrNGridArea); DBObjTableField *percentFLD = ItemTable->Field(DBrNGridPercent); DBObjRecord *layerRec, *record; for (recordID = 0; recordID < ItemTable->ItemNum(); ++recordID) { record = ItemTable->Item(recordID); areaFLD->Float(record, 0.0); percentFLD->Float(record, 0.0); } for (layerID = 0; layerID < LayerNum(); ++layerID) { layerRec = Layer(layerID); for (pos.Row = 0; pos.Row < RowNum(); ++pos.Row) for (pos.Col = 0; pos.Col < ColNum(); ++pos.Col) if ((record = GridItem(layerRec, pos)) != (DBObjRecord *) NULL) { area = CellArea(pos); sumArea += area; areaFLD->Float(record, areaFLD->Float(record) + area); } } for (recordID = 0; recordID < ItemTable->ItemNum(); ++recordID) { record = ItemTable->Item(recordID); percentFLD->Float(record, areaFLD->Float(record) * 100.0 / sumArea); } }
void DBGridIF::RecalcStats(DBObjRecord *layerRec) { DBInt obsNum = 0; DBPosition pos; DBFloat value, cellArea; DBFloat sumWeight = 0.0, minimum = DBHugeVal, maximum = -DBHugeVal, average = 0.0, stdDev = 0.0; for (pos.Row = 0; pos.Row < RowNum(); ++pos.Row) for (pos.Col = 0; pos.Col < ColNum(); ++pos.Col) if (Value(layerRec, pos, &value)) { cellArea = CellArea(pos); sumWeight += cellArea; average = average + value * cellArea; minimum = minimum < value ? minimum : value; maximum = maximum > value ? maximum : value; stdDev = stdDev + value * value * cellArea; obsNum++; } if (obsNum > 0) { average = average / sumWeight; stdDev = stdDev / sumWeight; stdDev = stdDev - average * average; stdDev = sqrt(stdDev); } else average = stdDev = minimum = maximum = MissingValue(); layerRec = ItemTable->Item(layerRec->RowID()); AverageFLD->Float(layerRec, average); MinimumFLD->Float(layerRec, minimum); MaximumFLD->Float(layerRec, maximum); StdDevFLD->Float(layerRec, stdDev); }
DBInt DBGridIF::Coord2Pos(DBCoordinate coord, DBPosition &pos) const { pos.Col = (DBInt) floor((coord.X - DataPTR->Extent().LowerLeft.X) / CellWidth()); pos.Row = (DBInt) floor((coord.Y - DataPTR->Extent().LowerLeft.Y) / CellHeight()); if ((pos.Col < 0) || (pos.Col >= ColNum())) return (DBFault); if ((pos.Row < 0) || (pos.Row >= RowNum())) return (DBFault); return (DBSuccess); }
DBInt DBNetworkIF::CellDelete (DBObjRecord *cellRec) { DBPosition pos; if (cellRec == (DBObjRecord *) NULL) return (DBFault); pos = PositionFLD->Position (cellRec); ((DBInt *) DataRec->Data ()) [pos.Row * ColNum () + pos.Col ] = DBFault; return (DBSuccess); }
DBInt DBGridIF::Pos2Coord(DBPosition pos, DBCoordinate &coord) const { DBInt ret = DBSuccess; if (pos.Col < 0) ret = DBFault; if (pos.Row < 0) ret = DBFault; if (pos.Col >= ColNum()) ret = DBFault; if (pos.Row >= RowNum()) ret = DBFault; coord.X = DataPTR->Extent().LowerLeft.X + pos.Col * CellWidth() + CellWidth() / 2.0; coord.Y = DataPTR->Extent().LowerLeft.Y + pos.Row * CellHeight() + CellHeight() / 2.0; return (ret); }
DBObjRecord *DBNetworkIF::CellAdd (DBPosition pos) { char nameSTR [DBStringLength]; DBInt dir, fromDir; DBPosition fromPos; DBObjRecord *cellRec; if (pos.Col < 0) return ((DBObjRecord *) NULL); if (pos.Row < 0) return ((DBObjRecord *) NULL); if (pos.Col >= ColNum ()) return ((DBObjRecord *) NULL); if (pos.Row >= RowNum ()) return ((DBObjRecord *) NULL); if (((DBInt *) DataRec->Data ()) [pos.Row * ColNum () + pos.Col] != DBFault) return ((DBObjRecord *) NULL); sprintf (nameSTR,"Cell:%6d",CellNum ()); cellRec = CellTable->Add (nameSTR); ((DBInt *) DataRec->Data ()) [pos.Row * ColNum () + pos.Col] = cellRec->RowID (); PositionFLD->Position (cellRec,pos); ToCellFLD->Int (cellRec,DBNetDirN); fromDir = DBNull; for (dir = 0;dir < 8;++dir) { fromPos = pos; if ((dir == DBNetDirNW) || (dir == DBNetDirN) || (dir == DBNetDirNE)) fromPos.Row++; if ((dir == DBNetDirSE) || (dir == DBNetDirS) || (dir == DBNetDirSW)) fromPos.Row--; if ((dir == DBNetDirNE) || (dir == DBNetDirE) || (dir == DBNetDirSE)) fromPos.Col++; if ((dir == DBNetDirNW) || (dir == DBNetDirW) || (dir == DBNetDirSW)) fromPos.Col--; if (((DBInt *) DataRec->Data ()) [fromPos.Row * ColNum () + fromPos.Col ] != DBFault) fromDir |= dir; } FromCellFLD->Int (cellRec,fromDir); OrderFLD->Int (cellRec,1); BasinFLD->Int (cellRec,1); BasinCellsFLD->Int (cellRec,1); TravelFLD->Int (cellRec,1); UpCellPosFLD->Position (cellRec,pos); CellAreaFLD->Float (cellRec,0.0); SubbasinLengthFLD->Float (cellRec,0.0); SubbasinAreaFLD->Float (cellRec,0.0); return (cellRec); }
DBObjRecord *DBNetworkIF::Cell (DBPosition pos,DBFloat area) const { DBInt dir, i; DBInt cellID; DBFloat bestDelta, delta; DBObjRecord *cellRec, *bestCellRec, *retCellRec; DBPosition cellPos; if (pos.Col < 0) return ((DBObjRecord *) NULL); if (pos.Row < 0) return ((DBObjRecord *) NULL); if (pos.Col >= ColNum ()) return ((DBObjRecord *) NULL); if (pos.Row >= RowNum ()) return ((DBObjRecord *) NULL); if ((cellID = ((DBInt *) DataRec->Data ()) [pos.Row * ColNum () + pos.Col ]) == DBFault) return ((DBObjRecord *) NULL); cellRec = CellTable->Item (cellID); delta = bestDelta = fabs (area - CellBasinArea (cellRec)) / (fabs (area) + fabs (CellBasinArea (cellRec))); retCellRec = bestCellRec = cellRec; for (i = 0;i < 8;++i) { dir = (0x01 << i) & 0xff; cellPos = pos; if ((dir == DBNetDirNW) || (dir == DBNetDirN) || (dir == DBNetDirNE)) cellPos.Row++; if ((dir == DBNetDirSE) || (dir == DBNetDirS) || (dir == DBNetDirSW)) cellPos.Row--; if ((dir == DBNetDirNE) || (dir == DBNetDirE) || (dir == DBNetDirSE)) cellPos.Col++; if ((dir == DBNetDirNW) || (dir == DBNetDirW) || (dir == DBNetDirSW)) cellPos.Col--; if ((cellID = ((DBInt *) DataRec->Data ()) [cellPos.Row * ColNum () + cellPos.Col]) == DBFault) continue; cellRec = CellTable->Item (cellID); delta = fabs (area - CellBasinArea (cellRec)) / (fabs (area) + fabs (CellBasinArea (cellRec))); if (delta < bestDelta) { bestDelta = delta; bestCellRec = cellRec; } } if ((bestDelta < 0.9) || (retCellRec == (DBObjRecord *) NULL)) retCellRec = bestCellRec; return (retCellRec); }
DBInt DBNetworkIF::Build () { DBInt i, j, row, col, basin, dir, projection = DataPTR->Projection (); DBCoordinate coord0, coord1; char nameStr [DBStringLength]; DBPosition pos; DBObjRecord *cellRec, *toCell, *fromCell, *basinRec, *symbolRec; _DBnetIF = this; for (j = 0;j < BasinTable->ItemNum ();++j) { basinRec = BasinTable->Item (j); if (((cellRec = Cell (MouthPosFLD->Position (basinRec))) == (DBObjRecord *) NULL) || (ToCell (cellRec) != (DBObjRecord *) NULL)) { BasinTable->Delete (basinRec); j--; } } for (i = 0;i < CellNum ();++i) { cellRec = CellTable->Item (i); DBPause (i * 10 / CellNum ()); if (Cell (CellPosition (cellRec)) == (DBObjRecord *) NULL) continue; else if (ToCell (ToCell (cellRec)) == cellRec) ToCellFLD->Int (cellRec,DBNull); } DBPause (10); for (i = 0;i < CellNum ();++i) { cellRec = CellTable->Item (i); DBPause (10 + i * 10 / CellNum ()); pos = CellPosition (cellRec); if (((DBInt *) DataRec->Data ()) [pos.Row * ColNum () + pos.Col] == DBFault) { CellTable->Delete (cellRec); --i; } else { cellRec->Flags (DBObjectFlagLocked,DBClear); FromCellFLD->Int (cellRec,DBNull); OrderFLD->Int (cellRec,1); BasinFLD->Int (cellRec,DBFault); BasinCellsFLD->Int (cellRec,1); TravelFLD->Int (cellRec,0); UpCellPosFLD->Position (cellRec,pos); CellAreaFLD->Float (cellRec,DBMathRectangleArea (projection,Center (cellRec) - CellSize () / 2,Center (cellRec) + CellSize () / 2)); coord0 = Center (cellRec); coord1 = Center (cellRec) + Delta (cellRec); CellLengthFLD->Float (cellRec,DBMathCoordinateDistance (DataPTR->Projection (),coord0,coord1)); SubbasinLengthFLD->Float (cellRec,CellLength (cellRec)); SubbasinAreaFLD->Float (cellRec,CellAreaFLD->Float (cellRec)); } } for (j = 0;j < BasinTable->ItemNum ();++j) { basinRec = BasinTable->Item (j); cellRec = Cell (MouthPosFLD->Position (basinRec)); if ((cellRec == (DBObjRecord *) NULL) || ((cellRec->Flags () & DBObjectFlagLocked) == DBObjectFlagLocked)) { BasinTable->Delete (basinRec); j--; } else cellRec->Flags (DBObjectFlagLocked,DBSet); } DBPause (20); for (row = 0;row < RowNum ();row++) for (col = 0;col < ColNum ();col++) ((DBInt *) DataRec->Data ()) [row * ColNum () + col] = DBFault; for (i = 0;i < CellNum ();++i) { cellRec = CellTable->Item (i); cellRec->Flags (DBObjectFlagLocked,DBClear); DBPause (20 + i * 10 / CellNum ()); pos = CellPosition (cellRec); if (((DBInt *) DataRec->Data ()) [pos.Row * ColNum () + pos.Col] != DBFault) { CellTable->Delete (cellRec); --i; } else ((DBInt *) DataRec->Data ()) [pos.Row * ColNum () + pos.Col] = cellRec->RowID (); } DBPause (30); for (i = 0;i < CellNum ();++i) { cellRec = CellTable->Item (i); if ((toCell = ToCell (cellRec)) != (DBObjRecord *) NULL) FromCellFLD->Int (toCell,FromCellFLD->Int (toCell) | _DBNetworkOppositeDirection (ToCellFLD->Int (cellRec))); } for (i = 0;i < CellNum ();++i) { DBPause (30 + i * 10 / CellNum ()); cellRec = CellTable->Item (i); if (ToCell (cellRec) == (DBObjRecord *) NULL) Climb (cellRec,0); } DBPause (40); CellTable->ItemSort (_DBGNetworkCellCompare); for (i = 0;i < CellNum ();++i) { cellRec = CellTable->Item (i); DBPause (35 + i * 10/ CellNum ()); pos = CellPosition (cellRec); ((DBInt *) DataRec->Data ()) [pos.Row * ColNum () + pos.Col] = cellRec->RowID (); } DBPause (50); basin = 0; for (i = 0;i < CellNum ();++i) { cellRec = CellTable->Item (i); DBPause (50 + i * 20 / CellNum ()); if (ToCell (cellRec) == (DBObjRecord *) NULL) { SetBasin (cellRec,basin + 1); basinRec = (DBObjRecord *) NULL; for (j = 0;j < BasinTable->ItemNum ();++j) { if (((basin - j) >= 0) && ((basin - j) < BasinTable->ItemNum ()) && (cellRec == MouthCell (basinRec = BasinTable->Item (basin - j)))) break; if (((basin + j) < BasinTable->ItemNum ()) && (cellRec == MouthCell (basinRec = BasinTable->Item (basin + j)))) break; basinRec = (DBObjRecord *) NULL; } if (basinRec == (DBObjRecord *) NULL) { basinRec = BasinTable->Add ("GHAASBasin"); MouthPosFLD->Position (basinRec,CellPosition (cellRec)); } basinRec->ListPos (basin++); } } DBPause (70); CellTable->ItemSort (_DBGNetworkCellCompare); BasinTable->ItemSort (); for (i = 0;i < CellNum ();++i) { cellRec = CellTable->Item (i); DBPause (70 + i * 10 / CellNum ()); pos = CellPosition (cellRec); ((DBInt *) DataRec->Data ()) [pos.Row * ColNum () + pos.Col] = cellRec->RowID (); sprintf (nameStr,"GHAASCell:%d",cellRec->RowID () + 1); cellRec->Name (nameStr); } DBPause (80); SymbolTable->DeleteAll (); symbolRec = SymbolTable->Add ("Network Symbol"); SymbolIDFLD->Int (symbolRec,1); ForegroundFLD->Int (symbolRec,1); BackgroundFLD->Int (symbolRec,0); for (j = 0;j < BasinTable->ItemNum ();++j) { basinRec = BasinTable->Item (j); if ((strncmp (basinRec->Name (),"GHAASBasin",strlen ("GHAASBasin")) == 0) || (strlen (basinRec->Name ()) < 1)) { sprintf (nameStr,"GHAASBasin%d",basinRec->RowID () + 1); basinRec->Name (nameStr); } ColorFLD->Int (basinRec,DBFault); BasinOrderFLD->Int (basinRec,CellOrder (MouthCell (basinRec))); BasinAreaFLD->Float (basinRec,CellBasinArea (MouthCell (basinRec))); BasinLengthFLD->Float (basinRec,CellBasinLength (MouthCell (basinRec))); SymbolFLD->Record (basinRec,symbolRec); } basin = DBFault; for (i = 0;i < CellNum ();++i) { cellRec = CellTable->Item (i); if ((basinRec = Basin (cellRec)) == (DBObjRecord *) NULL) { CMmsgPrint (CMmsgAppError,"BasinID: %d CellID:%d",BasinFLD->Int (cellRec),cellRec->RowID ()); continue; } if (basin != basinRec->RowID ()) { basin = basinRec->RowID (); DBPause (80 + basin * 20 / BasinNum ()); ColorFLD->Int (basinRec,7); toCell = cellRec; } for (dir = 0;dir < 8;dir += 2) { if ((fromCell = FromCell (cellRec,0x01 << dir,false)) == (DBObjRecord *) NULL) continue; if (basinRec == Basin (fromCell)) continue; if (ColorFLD->Int (basinRec) == ColorFLD->Int (Basin (fromCell))) { ColorFLD->Int (basinRec,ColorFLD->Int (basinRec) + 1); cellRec = MouthCell (basinRec); i = cellRec->RowID () - 1; } } } DBPause (100); if (DistToMouth ()) SetDistToMouth (); if (DistToOcean ()) SetDistToOcean (); if (Magnitude ()) SetMagnitude (); return (DBSuccess); }
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); }
DBInt Read (FILE *file,DBObjData *data) { DBInt i, layer, swap, valueType, valueSize, docLength; DMLayerHeader dmLayerHeader; DBObjRecord *layerRec, *dataRec; DBObjTable *layerTable = data->Table (DBrNLayers); DBObjTable *itemTable = data->Table (DBrNItems); DBObjTableField *rowNumFLD = layerTable->Field (DBrNRowNum); DBObjTableField *colNumFLD = layerTable->Field (DBrNColNum); DBObjTableField *cellWidthFLD = layerTable->Field (DBrNCellWidth); DBObjTableField *cellHeightFLD = layerTable->Field (DBrNCellHeight); DBObjTableField *valueTypeFLD = layerTable->Field (DBrNValueType); DBObjTableField *valueSizeFLD = layerTable->Field (DBrNValueSize); DBObjTableField *layerFLD = layerTable->Field (DBrNLayer); DBGridIF *gridIF; if ((swap = DMFileHeader::Read (file)) == DBFault) return (DBFault); data->Extent (Extent ()); data->Projection (DBMathGuessProjection (data->Extent ())); data->Precision (DBMathGuessPrecision (data->Extent ())); if (FileType () != DMMatrix) { CMmsgPrint (CMmsgAppError, "Wrong File Type in: %s %d",__FILE__,__LINE__); return (DBFault); } switch (DataType ()) { case DMFloat: valueType = DBTableFieldFloat; valueSize = sizeof (DBFloat4);break; case DMInt: valueType = DBTableFieldInt; valueSize = sizeof (DBInt); break; case DMByte: valueType = DBTableFieldInt; valueSize = sizeof (DBByte); break; default: CMmsgPrint (CMmsgAppError, "Wrong Data Value Type in: %s %d",__FILE__,__LINE__); return (DBFault); } for (layer = 0;layer < LayerNum ();++layer) { if (dmLayerHeader.Read (file,swap) == DBFault) return (DBFault); if (strlen (dmLayerHeader.Description ()) > 0) layerName = dmLayerHeader.Description (); else { sprintf (layerNameSTR,"GHAASLayer%4d",layer + 1); layerName = layerNameSTR; } layerTable->Add (layerName); if ((layerRec = layerTable->Item ()) == (DBObjRecord *) NULL) return (DBFault); rowNumFLD->Int (layerRec,RowNum ()); colNumFLD->Int (layerRec,ColNum ()); cellWidthFLD->Float (layerRec,CellWidth ()); cellHeightFLD->Float (layerRec,CellHeight ()); valueTypeFLD->Int (layerRec,((DBInt) DBTypeGridDiscrete) == data->Type () ? DBTableFieldInt : valueType); valueSizeFLD->Int (layerRec,valueSize); if ((dataRec = new DBObjRecord (layerName,((size_t) ColNum ()) * RowNum () * valueSize,valueSize)) == (DBObjRecord *) NULL) return (DBFault); (data->Arrays ())->Add (dataRec); layerFLD->Record (layerRec,dataRec); } if (fread (&docLength,sizeof (int),1,file) != 1) { CMmsgPrint (CMmsgSysError, "File Reading Error in: %s %d",__FILE__,__LINE__); return (DBFault); } if (swap) DBByteOrderSwapWord (&docLength); if (docLength > 0) { char *docString; if ((docString = (char *) calloc (docLength,sizeof (char))) == (char *) NULL) { CMmsgPrint (CMmsgSysError, "Memory Allocation Error in: %s %d",__FILE__,__LINE__); return (DBFault); } if (fread (docString,docLength,1,file) != 1) { CMmsgPrint (CMmsgSysError, "File Reading Error in: %s %d",__FILE__,__LINE__); return (DBFault); } data->Document (DBDocComment,docString); free (docString); } for (dataRec = (data->Arrays ())->First ();dataRec != (DBObjRecord *) NULL;dataRec = (data->Arrays ())->Next ()) if (fread (dataRec->Data (),ColNum () * valueSize * RowNum (),1,file) != 1) { CMmsgPrint (CMmsgSysError, "File Reading Error in: %s %d",__FILE__,__LINE__); return (DBFault); } if (swap && valueSize > 1) { int i; void (*swapFunc) (void *); switch (valueSize) { case 2: swapFunc = DBByteOrderSwapHalfWord; break; case 4: swapFunc = DBByteOrderSwapWord; break; case 8: swapFunc = DBByteOrderSwapLongWord; break; default: CMmsgPrint (CMmsgAppError, "Wrong Data Value Size in: %s %d",__FILE__,__LINE__); return (DBFault); } for (dataRec = (data->Arrays ())->First ();dataRec != (DBObjRecord *) NULL;dataRec = (data->Arrays ())->Next ()) for (i = 0;i < ColNum () * RowNum ();++i) (*swapFunc) ((char *) dataRec->Data () + i * valueSize); } switch (data->Type ()) { case DBTypeGridDiscrete: { DBInt value; char nameStr [DBStringLength]; DBObjRecord *symRec = (data->Table (DBrNSymbols))->Add ("Default Symbol"); DBObjRecord *itemRec; DBObjTableField *gridValueFLD = itemTable->Field (DBrNGridValue); DBObjTableField *gridSymbolFLD = itemTable->Field (DBrNSymbol); DBObjTableField *symbolIDFLD = (data->Table (DBrNSymbols))->Field (DBrNSymbolID); DBObjTableField *foregroundFLD = (data->Table (DBrNSymbols))->Field (DBrNForeground); DBObjTableField *backgroundFLD = (data->Table (DBrNSymbols))->Field (DBrNBackground); DBObjTableField *styleFLD = (data->Table (DBrNSymbols))->Field (DBrNStyle); symbolIDFLD->Int (symRec,0); foregroundFLD->Int (symRec,1); backgroundFLD->Int (symRec,0); styleFLD->Int (symRec,0); for (dataRec = (data->Arrays ())->First ();dataRec != (DBObjRecord *) NULL;dataRec = (data->Arrays ())->Next ()) { for (i = 0;i < ColNum () * RowNum ();++i) { switch (valueType) { case DBTableFieldFloat: value = (DBInt) rint (*((float *) ((char *) dataRec->Data () + i * valueSize))); break; case DBTableFieldInt: switch (valueSize) { case sizeof (DBByte): value = (DBInt) (*((DBByte *) ((char *) dataRec->Data () + i * valueSize))); break; case sizeof (DBShort): value = (DBInt) (*((DBShort *) ((char *) dataRec->Data () + i * valueSize))); break; case sizeof (DBInt): value = (DBInt) (*((DBInt *) ((char *) dataRec->Data () + i * valueSize))); break; default: CMmsgPrint (CMmsgAppError, "Wrong Data Size in: %s %d",__FILE__,__LINE__); return (DBFault); } break; default: CMmsgPrint (CMmsgAppError, "Wrong Data Type in: %s %d",__FILE__,__LINE__); return (DBFault); } sprintf (nameStr,"Category%04d",value); if ((itemRec = itemTable->Item (nameStr)) == (DBObjRecord *) NULL) { if ((itemRec = itemTable->Add (nameStr)) == (DBObjRecord *) NULL) { CMmsgPrint (CMmsgAppError, "Item Object Creation Error in: %s %d",__FILE__,__LINE__); return (DBFault); } gridValueFLD->Int (itemRec,value); gridSymbolFLD->Record (itemRec,symRec); } value = itemRec->RowID (); switch (valueSize) { case sizeof (DBByte): *((DBByte *) ((char *) dataRec->Data () + i * valueSize)) = value; break; case sizeof (DBShort): *((DBShort *) ((char *) dataRec->Data () + i * valueSize)) = value; break; case sizeof (DBInt): *((DBInt *) ((char *) dataRec->Data () + i * valueSize)) = value; break; default: CMmsgPrint (CMmsgAppError, "Wrong Data Size in: %s %d",__FILE__,__LINE__); return (DBFault); } } } itemTable->ListSort (gridValueFLD); for (dataRec = (data->Arrays ())->First ();dataRec != (DBObjRecord *) NULL;dataRec = (data->Arrays ())->Next ()) { for (i = 0;i < ColNum () * RowNum ();++i) { switch (valueSize) { case sizeof (DBByte): value = (DBInt) (*((DBByte *) ((char *) dataRec->Data () + i * valueSize))); break; case sizeof (DBShort): value = (DBInt) (*((DBShort *) ((char *) dataRec->Data () + i * valueSize))); break; case sizeof (DBInt): value = (DBInt) (*((DBInt *) ((char *) dataRec->Data () + i * valueSize))); break; default: CMmsgPrint (CMmsgAppError, "Wrong Data Size in: %s %d",__FILE__,__LINE__); return (DBFault); } itemRec = itemTable->Item (value); value = itemRec->ListPos (); switch (valueSize) { case sizeof (DBByte): *((DBByte *) ((char *) dataRec->Data () + i * valueSize)) = value; break; case sizeof (DBShort): *((DBShort *) ((char *) dataRec->Data () + i * valueSize)) = value; break; case sizeof (DBInt): *((DBInt *) ((char *) dataRec->Data () + i * valueSize)) = value; break; default: CMmsgPrint (CMmsgAppError, "Wrong Data Size in: %s %d",__FILE__,__LINE__); return (DBFault); } } } itemTable->ItemSort (); gridIF = new DBGridIF (data); gridIF->DiscreteStats (); delete gridIF; } break; case DBTypeGridContinuous: { DBObjTableField *missingValueFLD = itemTable->Field (DBrNMissingValue); for (dataRec = (data->Arrays ())->First ();dataRec != (DBObjRecord *) NULL;dataRec = (data->Arrays ())->Next ()) { itemTable->Add (dataRec->Name ()); missingValueFLD->Float (itemTable->Item (),MissingValue ()); } gridIF = new DBGridIF (data); gridIF->RecalcStats (); delete gridIF; data->Flags (DBDataFlagDispModeContBlueRed,DBSet); break; } default: break; } return (DBSuccess); }