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); }
static void _RGISToolsExportNetworkCBK (Widget widget,RGISWorkspace *workspace,XmAnyCallbackStruct *callData) { char *selection; FILE *outFILE; DBInt cellID, fieldID, fieldNum = 0; DBCoordinate coord; DBObjRecord *cellRec,*toCellRec, *basinRec; DBDataset *dataset = UIDataset (); DBObjData *netData = dataset->Data (); DBNetworkIF *netIF = new DBNetworkIF (netData); DBObjTable *cellTable = netData->Table (DBrNCells); DBObjTableField *field, **fields = (DBObjTableField **) NULL; static Widget dirSelect = NULL; widget = widget; workspace = workspace; callData = callData; if (dirSelect == NULL) { if ((dirSelect = UIFileSelectionCreate ((char *) "Network File",NULL,(char *) "*.txt",XmFILE_REGULAR)) == NULL) return; } if ((selection = UIFileSelection (dirSelect,False)) == NULL) return; if ((outFILE = fopen (selection,"w")) == (FILE *) NULL) { CMmsgPrint (CMmsgSysError, "File Opening Error in: %s %d",__FILE__,__LINE__); return; } fprintf (outFILE,"\"CellID\"\t\"XCoord\"\t\"YCoord\"\t\"BasinID\"\t\"ToCell\"\t\"CellArea\"\t\"CellLength\""); for (fieldID = 0;fieldID < cellTable->FieldNum (); fieldID++) { if ((field = cellTable->Field (fieldID)) == (DBObjTableField *) NULL) { if (fields != (DBObjTableField **) NULL) free (fields); CMmsgPrint (CMmsgSysError, "Invalid field in: %s %d",__FILE__,__LINE__); return; } if (field->Required ()) continue; if ((fields = (DBObjTableField **) realloc (fields,(fieldNum + 1) * sizeof (DBObjTableField *))) == (DBObjTableField **) NULL) { CMmsgPrint (CMmsgSysError, "Memory allocation error in: %s %d",__FILE__,__LINE__); return; } fields [fieldNum] = field; fprintf (outFILE,"\t\"%s\"",field->Name ()); fieldNum++; } fprintf (outFILE,"\n"); for (cellID = 0;cellID < netIF->CellNum (); cellID++) { cellRec = netIF->Cell (cellID); coord = netIF->Center (cellRec); basinRec = netIF->Basin (cellRec); toCellRec = netIF->ToCell (cellRec); fprintf (outFILE,"%d\t%f\t%f\t%d\t%d\t%f\t%f",cellRec->RowID () + 1, coord.X, coord.Y, basinRec->RowID () + 1, toCellRec == (DBObjRecord *) NULL ? DBFault : toCellRec->RowID () + 1, netIF->CellArea (cellRec), netIF->CellLength (cellRec)); for (fieldID = 0;fieldID < fieldNum; fieldID++) switch (fields [fieldID]->Type ()) { default: case DBTableFieldString: fprintf (outFILE,"%c\"%s\"",DBASCIISeparator,fields [fieldID]->String (cellRec)); break; case DBTableFieldInt: if (fields [fieldID]->Int (cellRec) == fields [fieldID]->IntNoData ()) fprintf (outFILE,"%c",DBASCIISeparator); else fprintf (outFILE,"%c%d",DBASCIISeparator,fields [fieldID]->Int (cellRec)); break; case DBTableFieldFloat: if (CMmathEqualValues (fields [fieldID]->Float (cellRec),fields [fieldID]->FloatNoData ())) fprintf (outFILE,"%c",DBASCIISeparator); else fprintf (outFILE,"%c%f",DBASCIISeparator,fields [fieldID]->Float (cellRec)); break; case DBTableFieldDate: fprintf (outFILE,"%c\"%s\"",DBASCIISeparator,fields [fieldID]->String(cellRec)); break; } fprintf (outFILE,"\n"); } fclose (outFILE); }
void RGISEditGridNetFilterCBK (Widget widget, RGISWorkspace *workspace,XmAnyCallbackStruct *callData) { DBInt layerID, cellID, count, ret, kernel, kernelSize, maxProgress, dir; DBFloat elev, cellElev, prevElev, upElev [5], meanElev, minElev, dElev; DBDataset *dataset = UIDataset (); DBObjData *grdData = dataset->Data (); DBObjData *netData = grdData->LinkedData (); DBGridIF *gridIF = new DBGridIF (grdData); DBNetworkIF *netIF = new DBNetworkIF (netData); DBObjRecord *cellRec, *fromCell, *nextCell, *layerRec; UIPauseDialogOpen ((char *) "Network Filtering"); maxProgress = netIF->CellNum () * gridIF->LayerNum (); for (layerID = 0;layerID < gridIF->LayerNum (); ++layerID) { layerRec = gridIF->Layer (layerID); for (cellID = 0;cellID < netIF->CellNum (); ++cellID) { if (UIPause (((layerID + 1) * netIF->CellNum () - cellID) * 100 / maxProgress)) goto Stop; fromCell = netIF->Cell (cellID); if (netIF->FromCell (fromCell) != (DBObjRecord *) NULL) continue; while (gridIF->Value (layerRec,netIF->Center (fromCell),&prevElev) == (DBInt) false) if ((fromCell = netIF->ToCell (fromCell)) == (DBObjRecord *) NULL) break; if (fromCell == (DBObjRecord *) NULL) continue; kernelSize = 0; for (cellRec = netIF->ToCell (fromCell); (cellRec != (DBObjRecord *) NULL) && (netIF->FromCell (cellRec) == fromCell); cellRec = netIF->ToCell (cellRec)) { dElev = netIF->CellLength (fromCell) * RGlibMinSLOPE; if ((ret = gridIF->Value (layerRec,netIF->Center (cellRec),&cellElev)) == false) { count = 0; meanElev = 0.0; } else { count = 1, meanElev = cellElev; } if (kernelSize + 1 < (int) (sizeof (upElev) / sizeof (upElev [0]))) kernelSize++; for (kernel = kernelSize - 1;kernel > 0;--kernel) upElev [kernel] = upElev [kernel - 1]; upElev [0] = prevElev; for (kernel = 0;kernel < kernelSize;++kernel) { meanElev += upElev [kernel]; count++; } minElev = prevElev; for (dir = 0; dir < 8;++dir) if (((fromCell = netIF->FromCell (cellRec,0x01 << dir,true)) != (DBObjRecord *) NULL) && (gridIF->Value (layerRec,netIF->Center (fromCell),&elev) == true) && (minElev > elev)) { minElev = elev; dElev = netIF->CellLength (fromCell) * RGlibMinSLOPE; } nextCell = netIF->ToCell (cellRec); for (kernel = 0;(kernel < kernelSize) && (nextCell != (DBObjRecord *) NULL);++kernel) { if(gridIF->Value (layerRec,netIF->Center (nextCell),&elev) != (DBInt) false) { meanElev += elev; count++; } nextCell = netIF->ToCell (nextCell); } if (count > 0) { meanElev = meanElev / count; if (meanElev > minElev - dElev) meanElev = minElev - dElev; gridIF->Value (layerRec,netIF->Center (cellRec),meanElev); prevElev = meanElev; } else gridIF->Value (layerRec,netIF->Center (cellRec),gridIF->MissingValue ()); fromCell = cellRec; } } gridIF->RecalcStats (layerRec); } Stop: UIPauseDialogClose (); delete gridIF; delete netIF; }