Esempio n. 1
0
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);
}
Esempio n. 2
0
DBInt RGlibPointSubbasinCenter(DBObjData *pntData, DBObjData *netData) {
    DBCoordinate massCoord;
    DBVPointIF *pntIF = new DBVPointIF(pntData);
    DBObjTable *pointTable = pntData->Table(DBrNItems);
    DBObjTableField *massCoordXFLD = pointTable->Field(RGlibMassCoordX);
    DBObjTableField *massCoordYFLD = pointTable->Field(RGlibMassCoordY);
    DBNetworkIF *netIF = new DBNetworkIF(netData);
    DBObjRecord *pointRec, *cellRec;

    if (massCoordXFLD == NULL) {
        massCoordXFLD = new DBObjTableField(RGlibMassCoordX, DBTableFieldFloat, "%10.3f", sizeof(DBFloat4));
        pointTable->AddField(massCoordXFLD);
    }
    if (massCoordYFLD == NULL) {
        massCoordYFLD = new DBObjTableField(RGlibMassCoordY, DBTableFieldFloat, "%10.3f", sizeof(DBFloat4));
        pointTable->AddField(massCoordYFLD);
    }

    for (pointRec = pntIF->FirstItem(); pointRec != (DBObjRecord *) NULL; pointRec = pntIF->NextItem()) {
        if ((pointRec->Flags() & DBObjectFlagIdle) == DBObjectFlagIdle) {
            massCoordXFLD->Float(pointRec, massCoordXFLD->FloatNoData());
            massCoordYFLD->Float(pointRec, massCoordYFLD->FloatNoData());
            continue;
        }
        if (DBPause(pointRec->RowID() * 100 / pntIF->ItemNum())) goto Stop;
        if ((cellRec = netIF->Cell(pntIF->Coordinate(pointRec))) == (DBObjRecord *) NULL)
            massCoord = pntIF->Coordinate(pointRec);
        else {
            if (netIF->CellBasinCells(cellRec) > 1) {
                massCoord.X = 0.0;
                massCoord.Y = 0.0;
                netIF->UpStreamSearch(cellRec, (DBNetworkACTION) _RGlibSubbasinCenterAction, &massCoord);
                massCoord.X = massCoord.X / (DBFloat) netIF->CellBasinCells(cellRec);
                massCoord.Y = massCoord.Y / (DBFloat) netIF->CellBasinCells(cellRec);
            }
            else massCoord = netIF->Center(cellRec);
        }
        massCoordXFLD->Float(pointRec, massCoord.X);
        massCoordYFLD->Float(pointRec, massCoord.Y);
    }
    Stop:
    if (pointRec != (DBObjRecord *) NULL) {
        pointTable->DeleteField(massCoordXFLD);
        pointTable->DeleteField(massCoordYFLD);
        return (DBFault);
    }
    return (DBSuccess);
}
Esempio n. 3
0
void RGISEditPointAddXYCBK (Widget widget, RGISWorkspace *workspace,XmAnyCallbackStruct *callData)

	{
	DBDataset *dataset = UIDataset ();
	DBObjData *dbData =dataset->Data ();
	DBVPointIF *pntIF = new DBVPointIF (dbData);
	DBObjTable *pointTable = dbData->Table (DBrNItems);
	DBObjTableField *xCoordFLD	= pointTable->Field (RGISEditPointXCoord);
	DBObjTableField *yCoordFLD = pointTable->Field (RGISEditPointYCoord);
	DBCoordinate coord;
	DBObjRecord *pointRec;
	UITable *tableCLS = (UITable *) dbData->Display (UITableName (dbData,pointTable));

	widget = widget; workspace = workspace; callData = callData;
	UIPauseDialogOpen ((char *) "Adding XY Coordinates");
	if (xCoordFLD == NULL)
		{
		xCoordFLD = new DBObjTableField (RGISEditPointXCoord,DBTableFieldFloat,"%10.3f",sizeof (DBFloat4));
		pointTable->AddField (xCoordFLD);
		if (tableCLS != (UITable *) NULL) tableCLS->AddField (xCoordFLD);
		UIPause (40);
		}
	if (yCoordFLD == NULL)
		{
		yCoordFLD = new DBObjTableField (RGISEditPointYCoord,DBTableFieldFloat,"%10.3f",sizeof (DBFloat4));
		pointTable->AddField (yCoordFLD);
		if (tableCLS != (UITable *) NULL) tableCLS->AddField (yCoordFLD);
		UIPause (80);
		}
	for (pointRec = pntIF->FirstItem (); pointRec != (DBObjRecord *) NULL; pointRec = pntIF->NextItem ())
		{
		if (UIPause (80 + pointRec->RowID () * 20 / pntIF->ItemNum ())) goto Stop;
		coord = pntIF->Coordinate  (pointRec);
		xCoordFLD->Float (pointRec,coord.X);
		yCoordFLD->Float (pointRec,coord.Y);
		}
Stop:
	UIPauseDialogClose ();
	if (tableCLS != (UITable *) NULL) tableCLS->Draw ();
	}
Esempio n. 4
0
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);
}
Esempio n. 5
0
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);
}
Esempio n. 6
0
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);
}
Esempio n. 7
0
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);
	}
Esempio n. 8
0
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);
}
Esempio n. 9
0
DBInt RGlibPointSubbasinHist(DBObjData *pntData, DBObjData *netData, DBObjData *grdData, DBObjData *tblData) {
    DBInt layerID, layerNum = 0, progress = 0, maxProgress;
    DBObjTable *itemTable = grdData->Table(DBrNItems);
    DBObjTable *table = tblData->Table(DBrNItems);
    DBObjTableField *pointIDFLD;
    DBObjTableField *layerIDFLD;
    DBObjTableField *layerNameFLD;
    DBObjTableField *categoryIDFLD;
    DBObjTableField *categoryFLD;
    DBObjTableField *percentFLD;
    DBObjTableField *areaFLD;
    DBObjTableField *cellNumFLD;
    DBVPointIF *pntIF;
    DBNetworkIF *netIF;
    DBObjRecord *pntRec, *itemRec, *tblRec;
    DBObjectLIST<DBObjTableField> *fields;

    _RGlibPointGrdIF = new DBGridIF(grdData);
    for (layerID = 0; layerID < _RGlibPointGrdIF->LayerNum(); ++layerID) {
        _RGlibPointGrdLayerRec = _RGlibPointGrdIF->Layer(layerID);
        if ((_RGlibPointGrdLayerRec->Flags() & DBObjectFlagIdle) != DBObjectFlagIdle) ++layerNum;
    }
    if (layerNum < 1) {
        CMmsgPrint(CMmsgUsrError, "No Layer to Process!");
        delete _RGlibPointGrdIF;
        return (DBFault);
    }
    pntIF = new DBVPointIF(pntData);
    netIF = new DBNetworkIF(netData);

    table->AddField(pointIDFLD = new DBObjTableField("GHAASPointID", DBTableFieldInt, "%8d", sizeof(DBInt)));
    table->AddField(layerIDFLD = new DBObjTableField("LayerID", DBTableFieldInt, "%4d", sizeof(DBShort)));
    table->AddField(layerNameFLD = new DBObjTableField("LayerName", DBTableFieldString, "%s", DBStringLength));
    table->AddField(categoryIDFLD = new DBObjTableField(DBrNCategoryID, DBTableFieldInt, "%2d", sizeof(DBShort)));
    table->AddField(categoryFLD = new DBObjTableField(DBrNCategory, DBTableFieldString, _RGlibPointGrdIF->ValueFormat(),
                                                      DBStringLength));
    table->AddField(cellNumFLD = new DBObjTableField("CellNum", DBTableFieldInt, "%8d", sizeof(DBInt)));
    table->AddField(areaFLD = new DBObjTableField(DBrNArea, DBTableFieldFloat, "%10.1f", sizeof(DBFloat4)));
    table->AddField(percentFLD = new DBObjTableField(DBrNPercent, DBTableFieldFloat, "%6.2f", sizeof(DBFloat4)));

    _RGlibHistogram = (Histogram *) malloc(itemTable->ItemNum() * sizeof(Histogram));
    if (_RGlibHistogram == (Histogram *) NULL) {
        CMmsgPrint(CMmsgAppError, "Memory Allocation Error in: %s %d", __FILE__, __LINE__);
        return (DBFault);
    }
    maxProgress = pntIF->ItemNum() * _RGlibPointGrdIF->LayerNum();
    for (layerID = 0; layerID < _RGlibPointGrdIF->LayerNum(); ++layerID) {
        _RGlibPointGrdLayerRec = _RGlibPointGrdIF->Layer(layerID);
        if ((_RGlibPointGrdLayerRec->Flags() & DBObjectFlagIdle) == DBObjectFlagIdle) continue;
        for (pntRec = pntIF->FirstItem(); pntRec != (DBObjRecord *) NULL; pntRec = pntIF->NextItem()) {
            if (DBPause(progress * 100 / maxProgress)) goto Stop;
            progress++;
            if ((pntRec->Flags() & DBObjectFlagIdle) == DBObjectFlagIdle) continue;
            for (itemRec = itemTable->First(); itemRec != (DBObjRecord *) NULL; itemRec = itemTable->Next())
                _RGlibHistogram[itemRec->RowID()].Initialize();
            netIF->UpStreamSearch(netIF->Cell(pntIF->Coordinate(pntRec)), (DBNetworkACTION) _RGlibSubbasinCategories);
            for (itemRec = itemTable->First(); itemRec != (DBObjRecord *) NULL; itemRec = itemTable->Next())
                if (_RGlibHistogram[itemRec->RowID()].cellNum > 0) {
                    tblRec = table->Add(pntRec->Name());
                    pointIDFLD->Int(tblRec, pntRec->RowID() + 1);
                    layerIDFLD->Int(tblRec, _RGlibPointGrdLayerRec->RowID());
                    layerNameFLD->String(tblRec, _RGlibPointGrdLayerRec->Name());
                    categoryIDFLD->Int(tblRec, itemRec->RowID() + 1);
                    categoryFLD->String(tblRec, itemRec->Name());
                    areaFLD->Float(tblRec, _RGlibHistogram[itemRec->RowID()].area);
                    percentFLD->Float(tblRec, _RGlibHistogram[itemRec->RowID()].area /
                                              netIF->CellBasinArea(netIF->Cell(pntIF->Coordinate(pntRec))) * 100.0);
                    cellNumFLD->Int(tblRec, _RGlibHistogram[itemRec->RowID()].cellNum);
                }
        }
    }
    Stop:
    delete _RGlibPointGrdIF;
    delete netIF;
    delete pntIF;
    free(_RGlibHistogram);

    if (progress == maxProgress) {
        fields = new DBObjectLIST<DBObjTableField>("Field List");
        fields->Add(new DBObjTableField(*pointIDFLD));
        fields->Add(new DBObjTableField(*layerIDFLD));
        fields->Add(areaFLD = new DBObjTableField(*areaFLD));
        areaFLD->Flags(DBObjectFlagSortReversed, DBSet);
        table->ListSort(fields);
        delete fields;
        return (DBSuccess);
    }
    return (DBFault);
}
Esempio n. 10
0
DBInt RGlibPointSubbasinStats(DBObjData *pntData, DBObjData *netData, DBObjData *grdData, DBObjData *tblData) {
    DBInt layerID, layerNum = 0, progress = 0, maxProgress;
    DBObjTable *table;
    DBObjTableField *pointIDFLD;
    DBObjTableField *layerIDFLD;
    DBObjTableField *layerNameFLD;
    DBObjTableField *minimumFLD;
    DBObjTableField *maximumFLD;
    DBObjTableField *averageFLD;
    DBObjTableField *stdDevFLD;
    DBObjTableField *areaFLD;
    DBVPointIF *pntIF;
    DBNetworkIF *netIF;
    DBObjRecord *pntRec, *tblRec;
    DBObjectLIST<DBObjTableField> *fields;

    _RGlibPointGrdIF = new DBGridIF(grdData);
    for (layerID = 0; layerID < _RGlibPointGrdIF->LayerNum(); ++layerID) {
        _RGlibPointGrdLayerRec = _RGlibPointGrdIF->Layer(layerID);
        if ((_RGlibPointGrdLayerRec->Flags() & DBObjectFlagIdle) != DBObjectFlagIdle) ++layerNum;
    }
    if (layerNum < 1) {
        CMmsgPrint(CMmsgUsrError, "No Layer to Process!");
        delete _RGlibPointGrdIF;
        return (DBFault);
    }

    table = tblData->Table(DBrNItems);
    pntIF = new DBVPointIF(pntData);
    netIF = new DBNetworkIF(netData);

    table->AddField(pointIDFLD = new DBObjTableField("GHAASPointID", DBTableFieldInt, "%8d", sizeof(DBInt)));
    table->AddField(layerIDFLD = new DBObjTableField("LayerID", DBTableFieldInt, "%4d", sizeof(DBShort)));
    table->AddField(layerNameFLD = new DBObjTableField("LayerName", DBTableFieldString, "%s", DBStringLength));
    table->AddField(averageFLD = new DBObjTableField(RGlibPointMean, DBTableFieldFloat, _RGlibPointGrdIF->ValueFormat(),
                                                     sizeof(DBFloat4)));
    table->AddField(minimumFLD = new DBObjTableField(RGlibPointMin, DBTableFieldFloat, _RGlibPointGrdIF->ValueFormat(),
                                                     sizeof(DBFloat4)));
    table->AddField(maximumFLD = new DBObjTableField(RGlibPointMax, DBTableFieldFloat, _RGlibPointGrdIF->ValueFormat(),
                                                     sizeof(DBFloat4)));
    table->AddField(
            stdDevFLD = new DBObjTableField(RGlibPointStdDev, DBTableFieldFloat, _RGlibPointGrdIF->ValueFormat(),
                                            sizeof(DBFloat4)));
    table->AddField(areaFLD = new DBObjTableField(RGlibPointArea, DBTableFieldFloat, _RGlibPointGrdIF->ValueFormat(),
                                                  sizeof(DBFloat4)));

    grdData->Flags(DBObjectFlagProcessed, DBSet);
    maxProgress = pntIF->ItemNum() * _RGlibPointGrdIF->LayerNum();
    for (layerID = 0; layerID < _RGlibPointGrdIF->LayerNum(); ++layerID) {
        _RGlibPointGrdLayerRec = _RGlibPointGrdIF->Layer(layerID);
        if ((_RGlibPointGrdLayerRec->Flags() & DBObjectFlagIdle) == DBObjectFlagIdle) continue;
        for (pntRec = pntIF->FirstItem(); pntRec != (DBObjRecord *) NULL; pntRec = pntIF->NextItem()) {
            if (DBPause(progress * 100 / maxProgress)) goto Stop;
            progress++;
            if ((pntRec->Flags() & DBObjectFlagIdle) == DBObjectFlagIdle) continue;
            tblRec = table->Add(pntRec->Name());
            pointIDFLD->Int(tblRec, pntRec->RowID() + 1);
            layerIDFLD->Int(tblRec, _RGlibPointGrdLayerRec->RowID());
            layerNameFLD->String(tblRec, _RGlibPointGrdLayerRec->Name());
            _RGlibSubbasinArea = 0.0;
            _RGlibSubbasinMin = DBHugeVal;
            _RGlibSubbasinMax = -DBHugeVal;
            _RGlibSubbasinMean = 0.0;
            _RGlibSubbasinStdDev = 0.0;
            netIF->UpStreamSearch(netIF->Cell(pntIF->Coordinate(pntRec)), (DBNetworkACTION) _RGlibSubbasinStatistics);
            _RGlibSubbasinMean = _RGlibSubbasinMean / _RGlibSubbasinArea;
            _RGlibSubbasinStdDev = _RGlibSubbasinStdDev / _RGlibSubbasinArea;
            _RGlibSubbasinStdDev = _RGlibSubbasinStdDev - _RGlibSubbasinMean * _RGlibSubbasinMean;
            _RGlibSubbasinStdDev = sqrt(_RGlibSubbasinStdDev);
            minimumFLD->Float(tblRec, _RGlibSubbasinMin);
            maximumFLD->Float(tblRec, _RGlibSubbasinMax);
            averageFLD->Float(tblRec, _RGlibSubbasinMean);
            stdDevFLD->Float(tblRec, _RGlibSubbasinStdDev);
            areaFLD->Float(tblRec, _RGlibSubbasinArea);
        }
    }
    Stop:
    delete _RGlibPointGrdIF;
    delete netIF;
    delete pntIF;

    if (progress == maxProgress) {
        fields = new DBObjectLIST<DBObjTableField>("Field List");
        fields->Add(new DBObjTableField(*pointIDFLD));
        fields->Add(new DBObjTableField(*layerIDFLD));
        table->ListSort(fields);
        delete fields;
        return (DBSuccess);
    }
    return (DBFault);
}