DBInt RGlibPointSTNCoordinates(DBObjData *dbData, DBObjTableField *field) { DBInt pointID, ret = DBFault; DBCoordinate coord; DBPosition pos; DBObjData *linkedData = dbData->LinkedData(); DBVPointIF *pntIF; DBNetworkIF *netIF; DBObjRecord *pntRec, *cellRec; if (linkedData == (DBObjData *) NULL) return (DBFault); pntIF = new DBVPointIF(dbData); netIF = new DBNetworkIF(linkedData); for (pointID = 0; pointID < pntIF->ItemNum(); ++pointID) { pntRec = pntIF->Item(pointID); if (DBPause(pointID * 100 / pntIF->ItemNum())) goto Stop; if ((pntRec->Flags () & DBObjectFlagIdle) == DBObjectFlagIdle) continue; coord = pntIF->Coordinate(pntRec); if (netIF->Coord2Pos(coord, pos) == DBFault) continue; netIF->Pos2Coord(pos, coord); if ((field != (DBObjTableField *) NULL) && (!CMmathEqualValues(field->Float(pntRec), field->FloatNoData())) && ((cellRec = netIF->Cell(coord, field->Float(pntRec))) != (DBObjRecord *) NULL)) coord = netIF->Center(cellRec); pntIF->Coordinate(pntRec, coord); } ret = DBSuccess; Stop: delete netIF; delete pntIF; return (ret); }
DBInt DBPointToGrid(DBObjData *pntData, DBObjData *grdData, DBFloat factor) { DBInt startID, pnt0ID, pntID, id, itemNum; double dist, minDist, box, box0, bWidth, bHeight; DBPosition pos; DBCoordinate gCoord, *pCoord; DBObjRecord *grdRec, *pntRec, *symRec; DBObjTable *itemTable, *symTable; DBObjTableField *valField, *symField; DBVPointIF *pntIF; DBGridIF *gridIF; DBMathDistanceFunction distFunc = DBMathGetDistanceFunction(pntData); if (distFunc != DBMathGetDistanceFunction(grdData)) { CMmsgPrint(CMmsgAppError, "Incompatible projections in: %s %d", __FILE__, __LINE__); return (DBFault); } pntIF = new DBVPointIF(pntData); itemNum = pntIF->ItemNum(); if ((pCoord = (DBCoordinate *) calloc(itemNum, sizeof(DBCoordinate))) == (DBCoordinate *) NULL) { CMmsgPrint(CMmsgSysError, "Memory allocation Error in: %s %d", __FILE__, __LINE__); return (DBFault); } gridIF = new DBGridIF(grdData); for (pntID = 0; pntID < itemNum; ++pntID) pCoord[pntID] = pntIF->Coordinate(pntIF->Item(pntID)); if (grdData->Type() == DBTypeGridContinuous) gridIF->RenameLayer(gridIF->Layer((DBInt) 0), (char *) "Distance to Station"); else { gridIF->RenameLayer(gridIF->Layer((DBInt) 0), (char *) "Station grid"); itemTable = grdData->Table(DBrNItems); symTable = grdData->Table(DBrNSymbols); valField = itemTable->Field(DBrNGridValue); symField = itemTable->Field(DBrNSymbol); if ((symRec = symTable->Item(0)) == (DBObjRecord *) NULL) CMmsgPrint(CMmsgAppError, "Total Metal Gebasz in: %s %d", __FILE__, __LINE__); for (pntID = 0; pntID < itemNum; ++pntID) { pntRec = pntIF->Item(pntID); grdRec = itemTable->Add(pntRec->Name()); valField->Int(grdRec, pntID + 1); symField->Record(grdRec, symRec); } } startID = 0; for (pos.Row = 0; pos.Row < gridIF->RowNum(); ++pos.Row) { DBPause(pos.Row * 100 / gridIF->RowNum()); for (pos.Col = 0; pos.Col < gridIF->ColNum(); ++pos.Col) { gridIF->Pos2Coord(pos, gCoord); minDist = box0 = DBHugeVal; pnt0ID = pntID = startID; id = DBFault; do { bWidth = fabs(gCoord.X - pCoord[pntID].X); bHeight = fabs(gCoord.Y - pCoord[pntID].Y); box = bWidth > bHeight ? bWidth : bHeight; if ((box < box0) && ((dist = DBMathCoordinateDistance(distFunc, gCoord, pCoord[pntID])) < minDist)) { minDist = dist; id = startID = pntID; box *= factor; if (box0 > box) box0 = box; } pntID = pntID + 1 < itemNum ? pntID + 1 : 0; } while (pntID != pnt0ID); if (grdData->Type() == DBTypeGridContinuous) gridIF->Value(pos, minDist); else gridIF->Value(pos, id); } } if (grdData->Type() == DBTypeGridContinuous) gridIF->RecalcStats(); else gridIF->DiscreteStats(); delete gridIF; free(pCoord); return (DBSuccess); }
DBInt DBPointToGrid(DBObjData *pntData, DBObjData *netData, DBObjData *grdData) { DBInt i; DBPosition pos; DBObjTable *pntTable = pntData->Table(DBrNItems); DBObjTable *grdTable = grdData->Table(DBrNItems); DBObjTable *symTable = grdData->Table(DBrNSymbols); DBObjectLIST<DBObjTableField> *pntFields = pntTable->Fields(); DBObjTableField *pntFLD; DBObjTableField *grdAttribFLD; DBObjTableField *grdFLD = grdTable->Field(DBrNGridValue); DBObjTableField *symFLD = grdTable->Field(DBrNSymbol); DBObjRecord *cellRec, *toCell, *pntRec, *itemRec; DBObjRecord *symRec = symTable->First(); DBVPointIF *pntIF; DBNetworkIF *netIF; DBGridIF *grdIF; pntIF = new DBVPointIF(pntData); netIF = new DBNetworkIF(netData); grdIF = new DBGridIF(grdData); grdIF->RenameLayer(grdIF->Layer((DBInt) 0), (char *) "Subbasins"); for (pos.Row = 0; pos.Row < grdIF->RowNum(); ++pos.Row) for (pos.Col = 0; pos.Col < grdIF->ColNum(); ++pos.Col) grdIF->Value(pos, DBFault); for (pntFLD = pntFields->First(); pntFLD != (DBObjTableField *) NULL; pntFLD = pntFields->Next()) if (DBTableFieldIsVisible(pntFLD)) grdTable->AddField(new DBObjTableField(*pntFLD)); for (i = 0; i < pntIF->ItemNum(); ++i) { DBPause(i * 100 / pntIF->ItemNum()); pntRec = pntIF->Item(i); if ((pntRec->Flags() & DBObjectFlagIdle) == DBObjectFlagIdle) continue; if ((cellRec = netIF->Cell(pntIF->Coordinate(pntRec))) == (DBObjRecord *) NULL) continue; itemRec = grdTable->Add(pntRec->Name()); grdFLD->Int(itemRec, pntRec->RowID() + 1); symFLD->Record(itemRec, symRec); for (pntFLD = pntFields->First(); pntFLD != (DBObjTableField *) NULL; pntFLD = pntFields->Next()) if ((grdAttribFLD = grdTable->Field(pntFLD->Name())) != (DBObjTableField *) NULL) switch (pntFLD->Type()) { case DBTableFieldString: grdAttribFLD->String(itemRec, pntFLD->String(pntRec)); break; case DBTableFieldInt: grdAttribFLD->Int(itemRec, pntFLD->Int(pntRec)); break; case DBTableFieldFloat: grdAttribFLD->Float(itemRec, pntFLD->Float(pntRec)); break; case DBTableFieldDate: grdAttribFLD->Date(itemRec, pntFLD->Date(pntRec)); break; } grdIF->Value(netIF->CellPosition(cellRec), itemRec->RowID()); } for (i = 0; i < netIF->CellNum(); ++i) { if ((cellRec = netIF->Cell(i)) == (DBObjRecord *) NULL) continue; if ((itemRec = grdIF->GridItem(netIF->CellPosition(cellRec))) != (DBObjRecord *) NULL) continue; if ((toCell = netIF->ToCell(cellRec)) == (DBObjRecord *) NULL) continue; if ((itemRec = grdIF->GridItem(netIF->CellPosition(toCell))) != (DBObjRecord *) NULL) grdIF->Value(netIF->CellPosition(cellRec), itemRec->RowID()); } grdIF->DiscreteStats(); delete pntIF; delete netIF; delete grdIF; return (DBSuccess); }
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); }
int main (int argc,char *argv []) { FILE *outFile; DBInt argPos, argNum = argc, ret; int objID, size; DBFloat lCorrection = 1.0; MFDomain_t *domain = (MFDomain_t *) NULL; DBCoordinate coord; DBObjRecord *objRec; DBObjData *data; for (argPos = 1;argPos < argNum; ) { if (CMargTest (argv [argPos],"-l","--lengthcorrection")) { if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos) { CMmsgPrint (CMmsgUsrError,"Missing length correction!"); return (CMfailed); } if (sscanf (argv [argPos],"%lf", &lCorrection) != 1) { CMmsgPrint (CMmsgUsrError, "Invalid length correction!"); return (CMfailed); } if ((argNum = CMargShiftLeft (argPos,argv,argNum)) <= argPos) break; continue; } if (CMargTest (argv [argPos],"-h","--help")) { CMmsgPrint (CMmsgInfo,"%s [options] <input rgisdata> <output domain>",CMprgName(argv[0])); CMmsgPrint (CMmsgInfo," -l,--lengthcorrection"); CMmsgPrint (CMmsgInfo," -h,--help"); return (DBSuccess); } if ((argv [argPos][0] == '-') && (strlen (argv [argPos]) > 1)) { CMmsgPrint (CMmsgUsrError,"Unknown option: %s!",argv [argPos]); return (CMfailed); } argPos++; } if (argNum > 3) { CMmsgPrint (CMmsgUsrError,"Extra arguments!"); return (CMfailed); } outFile = (argNum > 2) && (strcmp (argv [2],"-") != 0) ? fopen (argv [2],"w") : stdout; if (outFile == (FILE *) NULL) { CMmsgPrint (CMmsgUsrError,"Output file Opening error in: %s",CMprgName(argv[0])); exit (DBFault); } data = new DBObjData (); ret = (argNum > 1) && (strcmp (argv [1],"-") != 0) ? data->Read (argv [1]) : data->Read (stdin); if ((domain = (MFDomain_t *) calloc (1,sizeof (MFDomain_t))) != (MFDomain_t *) NULL) { domain->Objects = (MFObject_t *) NULL; switch (data->Type ()) { case DBTypeVectorPoint: { DBVPointIF *pntIF = new DBVPointIF (data); domain->ObjNum = pntIF->ItemNum (); if ((domain->Objects = (MFObject_t *) calloc (domain->ObjNum,sizeof (MFObject_t))) == (MFObject_t *) NULL) { CMmsgPrint (CMmsgSysError, "Memory Allocation Error in: %s %d",__FILE__,__LINE__); MFDomainFree (domain); goto Stop; } for (objID = 0;objID < domain->ObjNum;++objID) { objRec = pntIF->Item (objID); coord = pntIF->Coordinate (objRec); domain->Objects [objID].ID = objRec->RowID (); domain->Objects [objID].DLinkNum = 0; domain->Objects [objID].ULinkNum = 0; domain->Objects [objID].DLinks = (size_t *) NULL; domain->Objects [objID].ULinks = (size_t *) NULL; domain->Objects [objID].XCoord = domain->Objects [objID].Lon = coord.X; domain->Objects [objID].YCoord = domain->Objects [objID].Lat = coord.Y; domain->Objects [objID].Area = 0.0; domain->Objects [objID].Length = 0.0; } } break; case DBTypeGridContinuous: case DBTypeGridDiscrete: { } break; case DBTypeNetwork: { DBInt dir; DBObjRecord *nextCell; DBNetworkIF *netIF = new DBNetworkIF (data); domain->ObjNum = netIF->CellNum (); if ((domain->Objects = (MFObject_t *) calloc (domain->ObjNum,sizeof (MFObject_t))) == (MFObject_t *) NULL) { CMmsgPrint (CMmsgSysError, "Memory Allocation Error in: %s %d",__FILE__,__LINE__); MFDomainFree (domain); goto Stop; } for (objID = 0;objID < domain->ObjNum;++objID) { domain->Objects [objID].DLinks = (size_t *) NULL; domain->Objects [objID].ULinks = (size_t *) NULL; } for (objID = 0;objID < domain->ObjNum;++objID) { objRec = netIF->Cell (objID); coord = netIF->Center (objRec); domain->Objects [objID].ID = objRec->RowID (); domain->Objects [objID].DLinkNum = 0; domain->Objects [objID].ULinkNum = 0; domain->Objects [objID].XCoord = domain->Objects [objID].Lon = coord.X; domain->Objects [objID].YCoord = domain->Objects [objID].Lat = coord.Y; domain->Objects [objID].Area = netIF->CellArea (objRec); domain->Objects [objID].Length = netIF->CellLength (objRec) *lCorrection; if ((nextCell = netIF->ToCell (objRec)) != (DBObjRecord *) NULL) { size = (domain->Objects [objID].DLinkNum + 1) * sizeof (size_t); if ((domain->Objects [objID].DLinks = (size_t *) realloc (domain->Objects [objID].DLinks,size)) == (size_t *) NULL) { CMmsgPrint (CMmsgSysError, "Memory Allocation Error in: %s %d",__FILE__,__LINE__); MFDomainFree (domain); goto Stop; } domain->Objects [objID].DLinks [domain->Objects [objID].DLinkNum] = nextCell->RowID (); domain->Objects [objID].DLinkNum++; } for (dir = 0;dir < 8;++dir) if ((nextCell = netIF->FromCell (objRec,0x01 << dir)) != (DBObjRecord *) NULL) { size = (domain->Objects [objID].ULinkNum + 1) * sizeof (size_t); if ((domain->Objects [objID].ULinks = (size_t *) realloc (domain->Objects [objID].ULinks,size)) == (size_t *) NULL) { CMmsgPrint (CMmsgSysError, "Memory Allocation Error in: %s %d",__FILE__,__LINE__); MFDomainFree (domain);goto Stop; } domain->Objects [objID].ULinks [domain->Objects [objID].ULinkNum] = nextCell->RowID (); domain->Objects [objID].ULinkNum++; } } } break; } ret = MFDomainWrite (domain,outFile); } Stop: if (outFile != stdout) fclose (outFile); return (ret); }