コード例 #1
0
ファイル: RGlibPoint.C プロジェクト: bmfekete/RGIS
DBInt RGlibPointSTNCharacteristics(DBObjData *dbData) {
    DBInt i, pointID, dPointID, cellID, mouthID, basinID, color, ret = DBFault, dir;
    DBVPointIF *pntIF;
    DBObjTable *pointTable, *cellTable;
    DBObjTableField *cellIDFLD;
    DBObjTableField *basinFLD;
    DBObjTableField *basinNameFLD;
    DBObjTableField *orderFLD;
    DBObjTableField *colorFLD;
    DBObjTableField *basinCellsFLD;
    DBObjTableField *basinLengthFLD;
    DBObjTableField *basinAreaFLD;
    DBObjTableField *interAreaFLD;
    DBObjTableField *nextStationFLD;
    DBObjData *netData;
    DBNetworkIF *netIF;
    DBObjRecord *pointRec, *dPointRec, *cellRec, *fromCell, *basinRec;

    if ((netData = dbData->LinkedData()) == (DBObjData *) NULL) return (DBFault);
    pointTable = dbData->Table(DBrNItems);
    pntIF = new DBVPointIF(dbData);
    netIF = new DBNetworkIF(netData);
    cellTable = netData->Table(DBrNCells);
    if ((cellIDFLD = pointTable->Field(RGlibCellID)) == NULL) {
        cellIDFLD = new DBObjTableField(RGlibCellID, DBTableFieldInt, "%8d", sizeof(DBInt));
        pointTable->AddField(cellIDFLD);
        DBPause(1);
    }
    if ((basinFLD = pointTable->Field(DBrNBasin)) == NULL) {
        basinFLD = new DBObjTableField(DBrNBasin, DBTableFieldInt, "%8d", sizeof(DBInt));
        pointTable->AddField(basinFLD);
        DBPause(2);
    }
    if ((basinNameFLD = pointTable->Field(RGlibBasinName)) == NULL) {
        basinNameFLD = new DBObjTableField(RGlibBasinName, DBTableFieldString, "%32s", DBStringLength);
        pointTable->AddField(basinNameFLD);
        DBPause(3);
    }
    if ((orderFLD = pointTable->Field(DBrNOrder)) == NULL) {
        orderFLD = new DBObjTableField(DBrNOrder, DBTableFieldInt, "%3d", sizeof(DBByte));
        pointTable->AddField(orderFLD);
        DBPause(4);
    }
    if ((colorFLD = pointTable->Field(RGlibColor)) == NULL) {
        colorFLD = new DBObjTableField(RGlibColor, DBTableFieldInt, "%2d", sizeof(DBShort));
        pointTable->AddField(colorFLD);
        DBPause(5);
    }
    if ((basinCellsFLD = pointTable->Field(RGlibCellNum)) == NULL) {
        basinCellsFLD = new DBObjTableField(RGlibCellNum, DBTableFieldInt, "%8d", sizeof(DBInt));
        pointTable->AddField(basinCellsFLD);
        DBPause(6);
    }
    if ((basinLengthFLD = pointTable->Field(RGlibLength)) == NULL) {
        basinLengthFLD = new DBObjTableField(RGlibLength, DBTableFieldFloat, "%10.1f", sizeof(DBFloat4));
        pointTable->AddField(basinLengthFLD);
        DBPause(7);
    }
    if ((basinAreaFLD = pointTable->Field(RGlibArea)) == NULL) {
        basinAreaFLD = new DBObjTableField(RGlibArea, DBTableFieldFloat, "%10.1f", sizeof(DBFloat4));
        pointTable->AddField(basinAreaFLD);
        DBPause(8);
    }
    if ((interAreaFLD = pointTable->Field(RGlibInterStation)) == NULL) {
        interAreaFLD = new DBObjTableField(RGlibInterStation, DBTableFieldFloat, "%10.1f", sizeof(DBFloat4));
        pointTable->AddField(interAreaFLD);
        DBPause(9);
    }
    if ((nextStationFLD = pointTable->Field(RGlibNextStation)) == NULL) {
        nextStationFLD = new DBObjTableField(RGlibNextStation, DBTableFieldInt, "%8d", sizeof(DBInt));
        pointTable->AddField(nextStationFLD);
        DBPause(10);
    }
    if ((_RGlibTEMPPointIDFLD = cellTable->Field(RGlibTEMPPointID)) == NULL) {
        _RGlibTEMPPointIDFLD = new DBObjTableField(RGlibTEMPPointID, DBTableFieldInt, "%8d", sizeof(DBInt));
        cellTable->AddField(_RGlibTEMPPointIDFLD);
    }
    for (pointID = 0; pointID < pointTable->ItemNum(); pointID++) {
        pointRec = pointTable->Item(pointID);
        if (DBPause(10 + pointID * 10 / pointTable->ItemNum())) goto Stop;
        if ((pointRec->Flags() & DBObjectFlagIdle) == DBObjectFlagIdle) {
            cellIDFLD->Int(pointRec, cellIDFLD->IntNoData());
            basinFLD->Int(pointRec, basinFLD->IntNoData());
            basinNameFLD->String(pointRec, "");
            orderFLD->Int(pointRec, orderFLD->IntNoData());
            colorFLD->Int(pointRec, colorFLD->IntNoData());
            basinCellsFLD->Int(pointRec, basinCellsFLD->IntNoData());
            basinAreaFLD->Float(pointRec, basinAreaFLD->FloatNoData());
            interAreaFLD->Float(pointRec, interAreaFLD->FloatNoData());
            continue;
        }
        if ((cellRec = netIF->Cell(pntIF->Coordinate(pointRec))) == (DBObjRecord *) NULL) {
            cellIDFLD->Int(pointRec, 0);
            basinFLD->Int(pointRec, 0);
            basinNameFLD->String(pointRec, "Water");
            orderFLD->Int(pointRec, colorFLD->IntNoData());
            colorFLD->Int(pointRec, colorFLD->IntNoData());
            basinCellsFLD->Int(pointRec, 0);
            basinAreaFLD->Float(pointRec, 0.0);
            interAreaFLD->Float(pointRec, 0.0);
        }
        else {
            cellIDFLD->Int(pointRec, cellRec->RowID() + 1);
            basinRec = netIF->Basin(cellRec);
            basinFLD->Int(pointRec, basinRec->RowID() + 1);
            basinNameFLD->String(pointRec, basinRec->Name());
            orderFLD->Int(pointRec, netIF->CellOrder(cellRec));
            colorFLD->Int(pointRec, 0);
            basinCellsFLD->Int(pointRec, netIF->CellBasinCells(cellRec));
            basinLengthFLD->Float(pointRec, netIF->CellBasinLength(cellRec));
            basinAreaFLD->Float(pointRec, netIF->CellBasinArea(cellRec));
            interAreaFLD->Float(pointRec, netIF->CellBasinArea(cellRec));
        }
        nextStationFLD->Int(pointRec, 0);
    }
    for (cellID = 0; cellID < cellTable->ItemNum(); ++cellID) {
        if (DBPause(20 + cellID * 20 / cellTable->ItemNum())) goto Stop;
        cellRec = cellTable->Item(cellID);
        _RGlibTEMPPointIDFLD->Int(cellRec, DBFault);
    }
    pointTable->ListSort(basinAreaFLD);
    for (pointRec = pointTable->Last(); pointRec != (DBObjRecord *) NULL; pointRec = pointTable->Next(DBBackward)) {
        if ((pointRec->Flags() & DBObjectFlagIdle) == DBObjectFlagIdle) continue;
        if (DBPause(40 + pointID * 20 / pointTable->ItemNum())) goto Stop;
        cellRec = netIF->Cell(pntIF->Coordinate(pointRec));
        netIF->UpStreamSearch(cellRec, (DBNetworkACTION) _RGlibSetPointID,
                              (void *) ((char *) NULL + pointRec->RowID()));
    }
    for (pointID = 0; pointID < pointTable->ItemNum(); pointID++) {
        pointRec = pointTable->Item(pointID);
        if ((pointRec->Flags() & DBObjectFlagIdle) == DBObjectFlagIdle) continue;
        if (DBPause(60 + pointID * 20 / pointTable->ItemNum())) goto Stop;
        if ((cellRec = netIF->Cell(pntIF->Coordinate(pointRec))) != (DBObjRecord *) NULL) {
            if ((cellRec = netIF->ToCell(cellRec)) == (DBObjRecord *) NULL) continue;
            if ((dPointID = _RGlibTEMPPointIDFLD->Int(cellRec)) != DBFault) {
                dPointRec = pointTable->Item(dPointID);
                nextStationFLD->Int(pointRec, dPointRec->RowID() + 1);
                interAreaFLD->Float(dPointRec, interAreaFLD->Float(dPointRec) - basinAreaFLD->Float(pointRec));
            }
        }
    }
    pointTable->ListSort(interAreaFLD);

    i = 0;
    for (pointRec = pointTable->Last(); pointRec != (DBObjRecord *) NULL; pointRec = pointTable->Next(DBBackward)) {
        if (DBPause(80 + (i++) * 20 / pointTable->ItemNum())) goto Stop;
        pointID = pointRec->RowID();
        if ((pointRec->Flags() & DBObjectFlagIdle) == DBObjectFlagIdle) continue;

        if ((basinID = basinFLD->Int(pointRec)) == 0) continue;
        cellRec = netIF->Cell(pntIF->Coordinate(pointRec));
        mouthID = cellRec->RowID();
        color = 1;
        Start:
        for (cellID = mouthID; cellID < cellTable->ItemNum(); ++cellID) {
            cellRec = cellTable->Item(cellID);
            if (netIF->CellBasinID(cellRec) != basinID) break;
            if (_RGlibTEMPPointIDFLD->Int(cellRec) != pointID) continue;

            for (dir = 0; dir < 8; ++dir) {
                if ((fromCell = netIF->FromCell(cellRec, 0x01 << dir, false)) == (DBObjRecord *) NULL) continue;
                if ((dPointID = _RGlibTEMPPointIDFLD->Int(fromCell)) == pointID) continue;
                if (dPointID == DBFault) continue;

                dPointRec = pointTable->Item(dPointID);
                if (colorFLD->Int(dPointRec) == color) {
                    color++;
                    goto Start;
                }
            }
        }
        colorFLD->Int(pointRec, color);
    }
    ret = DBSuccess;
    Stop:
    pointTable->ListSort();
    cellTable->DeleteField(_RGlibTEMPPointIDFLD);
    delete pntIF;
    delete netIF;
    return (ret);
}
コード例 #2
0
ファイル: CMDrgis2domain.C プロジェクト: amiara/RGIS
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);
	}
コード例 #3
0
ファイル: RGISEditGrid.C プロジェクト: bandi13/RGIS
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;
	}