Пример #1
0
    void ComputeTask(DBObjRecord *record, size_t taskId) {
        size_t i;
        DBPosition pos;
        DBCoordinate coord;

        pos.Row = taskId / GridIF->ColNum();
        pos.Col = taskId % GridIF->ColNum();

        GridIF->Pos2Coord(pos, coord);
        for (i = 0; i < VarNum; ++i) GrdVar[i]->GetVariable(record, coord);
        for (i = 0; i < ExpNum; ++i) Expressions[i]->Evaluate(record);
        GridIF->Value(LayerRec, pos, Operand->Float(record));
    }
Пример #2
0
void _RGISUserFuncionQuery (DBObjData *dbData,UI2DView *view,XEvent *event)

	{
	DBInt sX, sY;
	DBCoordinate coord;
	DBObjData *data = dbData;
	UIAttribView *attribView;

	if (event->type != ButtonPress) return;
	sX = event->xbutton.x;
	sY = event->xbutton.y;
	view->Window2Map  (sX,sY, &(coord.X), &(coord.Y));

	for (data = dbData;
		  (data != (DBObjData *) NULL) && ((data->Flags () & DBObjectFlagProcessed) != DBObjectFlagProcessed);
		  data = data->LinkedData ())
		{
		data->Flags (DBObjectFlagProcessed,DBSet);
		if ((data->Flags () & DBDataFlagUserModeFlags) == DBDataFlagUserModeQuery)
			{
			if ((attribView = (UIAttribView *) data->Display (UIAttribViewSTR)) == (UIAttribView *) NULL)
				data->DispAdd (attribView = new UIAttribView (data));
			switch (data->Type ())
				{
				case DBTypeVectorPoint:
				case DBTypeVectorLine:
				case DBTypeVectorPolygon:
					{
					DBVectorIF *vectorIF = new DBVectorIF (data);
					attribView->Draw (vectorIF->Item (coord));
					} break;
				case DBTypeGridDiscrete:
					{
					DBGridIF *gridIF = new DBGridIF (data);
					attribView->Draw (gridIF->GridItem (coord));
					delete gridIF;
					} break;
				case DBTypeGridContinuous:
					{
					DBPosition pos;
					DBGridIF *gridIF = new DBGridIF (data);
					char nameString [DBStringLength * 2];
					DBObjRecord *record = new DBObjRecord ("Noname",sizeof (DBCoordinate));
					gridIF->Coord2Pos (coord,pos);
					gridIF->Pos2Coord (pos,coord);
					sprintf (nameString,"CellCoordinate %10.2f%10.2f",coord.X,coord.Y);
					record->Name (nameString);
					memcpy (record->Data (),(void *) &coord,sizeof (coord));
					attribView->Draw (record);
					delete record;
					delete gridIF;
					} break;
				case DBTypeNetwork:
					{
					DBNetworkIF *netIF = new DBNetworkIF (data);
					attribView->Draw (netIF->Cell (coord));
					delete netIF;
					} break;
				default:
					CMmsgPrint (CMmsgAppError, "Invalid Data Type in: %s %d",__FILE__,__LINE__); break;
				}
			}
		}
	for (data = dbData;
		  (data != (DBObjData *) NULL) && ((data->Flags () & DBObjectFlagProcessed) == DBObjectFlagProcessed);
		  data = data->LinkedData ())
		data->Flags (DBObjectFlagProcessed,DBClear);
	}
Пример #3
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);
}
Пример #4
0
void DBGridOperation(DBObjData *leftGrd, DBObjData *rightGrd, DBInt oper, DBInt mergeMissingVal) {
    DBInt leftID, rightID;
    DBFloat leftVal, rightVal;
    DBPosition pos;
    DBCoordinate coord;
    DBObjRecord *leftRec, *rightRec;
    DBGridIF *leftIF = new DBGridIF(leftGrd);
    DBGridIF *rightIF = new DBGridIF(rightGrd);

    for (leftID = 0; leftID < leftIF->LayerNum(); ++leftID) {
        leftRec = leftIF->Layer(leftID);
        if ((leftRec->Flags() & DBObjectFlagIdle) != DBObjectFlagIdle) break;
    }
    if (leftID == leftIF->LayerNum()) {
        CMmsgPrint(CMmsgAppError, "No Layer to Process in %s %d", __FILE__, __LINE__);
        return;
    }
    for (rightID = 0; rightID < rightIF->LayerNum(); ++rightID) {
        rightRec = rightIF->Layer(rightID);
        if ((rightRec->Flags() & DBObjectFlagIdle) != DBObjectFlagIdle) break;
    }
    if (rightID == rightIF->LayerNum()) {
        CMmsgPrint(CMmsgAppError, "No Layer to Process in %s %d", __FILE__, __LINE__);
        return;
    }

    rightID = (DBInt) 0;
    for (leftID = 0; leftID < leftIF->LayerNum(); ++leftID) {
        leftRec = leftIF->Layer(leftID);
        while ((leftRec->Flags() & DBObjectFlagIdle) == DBObjectFlagIdle) {
            ++leftID;
            if (leftID == leftIF->LayerNum()) goto Stop;
            leftRec = leftIF->Layer(leftID);
        }
        if ((rightRec = rightIF->Layer(rightID)) == (DBObjRecord *) NULL) {
            rightID = 0;
            rightRec = rightIF->Layer(rightID);
        }
        while ((rightRec->Flags() & DBObjectFlagIdle) == DBObjectFlagIdle) {
            ++rightID;
            if (rightID == rightIF->LayerNum()) rightID = 0;
            rightRec = rightIF->Layer(rightID);
        }

        for (pos.Row = 0; pos.Row < leftIF->RowNum(); pos.Row++) {
            if (DBPause((leftID * leftIF->RowNum() + pos.Row) * 100 / (leftIF->LayerNum() * leftIF->RowNum())))
                goto Stop;
            for (pos.Col = 0; pos.Col < leftIF->ColNum(); pos.Col++) {
                leftIF->Pos2Coord(pos, coord);
                if (leftIF->Value(leftRec, pos, &leftVal)) {
                    if (rightIF->Value(rightRec, coord, &rightVal))
                        switch (oper) {
                            case DBMathOperatorAdd:
                                leftIF->Value(leftRec, pos, leftVal + rightVal);
                                break;
                            case DBMathOperatorSub:
                                leftIF->Value(leftRec, pos, leftVal - rightVal);
                                break;
                            case DBMathOperatorMul:
                                leftIF->Value(leftRec, pos, leftVal * rightVal);
                                break;
                            case DBMathOperatorDiv:
                                if (fabs(rightVal) > 0.000001) leftIF->Value(leftRec, pos, leftVal / rightVal);
                                else leftIF->Value(leftRec, pos, leftIF->MissingValue());
                                break;
                        }
                    else if (mergeMissingVal) leftIF->Value(leftRec, pos, leftIF->MissingValue());
                }
            }
        }
        ++rightID;
        leftIF->RecalcStats(leftRec);
    }
    Stop:
    return;
}
Пример #5
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);
}
Пример #6
0
    DBObjData *Compute(char *title, CMthreadUserExecFunc userFunc) {
        DBInt i, layerID, dataLayerID;
        size_t threadId;
        DBPosition pos;
        DBCoordinate coord;
        char *layerName;
        DBObjData *data;
        DBObjRecord *record;
        size_t threadsNum = CMthreadProcessorNum();
        CMthreadTeam_t team;
        CMthreadJob_p job = (CMthreadJob_p) NULL;

        if (CMthreadTeamInitialize(&team, threadsNum) == (CMthreadTeam_p) NULL) {
            CMmsgPrint (CMmsgUsrError,"Team initialization error %s, %d",__FILE__,__LINE__);
            return ((DBObjData *) NULL);
        }
        if ((data = DBGridCreate(title, Extent, CellSize)) == (DBObjData *) NULL) return ((DBObjData *) NULL);
        data->Projection(GrdVar[0]->Projection()); // Taking projection from first grid variable

        GridIF = new DBGridIF(data);

        if (team.ThreadNum > 0) {
            if ((job = CMthreadJobCreate(GridIF->RowNum() * GridIF->ColNum(), userFunc, (void *) this)) ==
                (CMthreadJob_p) NULL) {
                CMmsgPrint(CMmsgAppError, "Job creation error in %s:%d", __FILE__, __LINE__);
                CMthreadTeamDestroy(&team);
                return ((DBObjData *) NULL);
            }
            for (threadId = 0; threadId < team.ThreadNum; ++threadId)
                if (Table->Add("TEMPRecord") == (DBObjRecord *) NULL) {
                    CMthreadTeamDestroy(&team);
                    return ((DBObjData *) NULL);
                }
        }
        else {
            if ((record = Table->Add("TEMPRecord")) == (DBObjRecord *) NULL) return ((DBObjData *) NULL);
        }
        for (layerID = 0; layerID < LayerNum; ++layerID) {
            layerName = GrdVar[MasterVar]->CurrentLayer(layerID);
            for (i = 0; i < (DBInt) VarNum; ++i) {
                if ((dataLayerID = GrdVar[i]->FindLayer(layerName)) != DBFault)
                    GrdVar[i]->CurrentLayer(dataLayerID);
                else {
                    if (GrdVar[i]->LayerIsDated(0)) continue;
                    GrdVar[i]->CurrentLayer(dataLayerID);
                }
            }
            if (layerID > 0) GridIF->AddLayer((char *) "New Layer");
            LayerRec = GridIF->Layer(layerID);
            GridIF->RenameLayer(LayerRec, layerName);

            if (job != (CMthreadJob_p) NULL) CMthreadJobExecute(&team, job);
            else
                for (pos.Row = 0; pos.Row < GridIF->RowNum(); ++pos.Row)
                    for (pos.Col = 0; pos.Col < GridIF->ColNum(); ++pos.Col) {
                        GridIF->Pos2Coord(pos, coord);
                        for (i = 0; i < (DBInt) VarNum; ++i) GrdVar[i]->GetVariable(record, coord);
                        for (i = 0; i < (DBInt) ExpNum; ++i) Expressions[i]->Evaluate(record);
                        GridIF->Value(LayerRec, pos, Operand->Float(record));
                    }
            GridIF->RecalcStats(LayerRec);
        }
        delete GridIF;
        CMthreadTeamDestroy(&team);
        return (data);
    }
Пример #7
0
DBInt DBGridCont2Network (DBObjData *gridData,DBObjData *netData, bool downhill)

	{
	DBInt basinID, layerID, zLayerID, zLayerNum, dir, maxDir, projection = gridData->Projection (), *zones;
	DBFloat elev0, elev1, delta, maxDelta, distance;
	DBCoordinate coord0, coord1;
	DBInt row, col;
	DBPosition pos, auxPos;
	char nameSTR [DBStringLength];
	DBObjTable *basinTable = netData->Table (DBrNItems);
	DBObjTable *cellTable  = netData->Table (DBrNCells);
	DBObjTable *layerTable = netData->Table (DBrNLayers);
	DBObjRecord *layerRec, *dataRec, *cellRec, *basinRec;

	DBObjTableField *mouthPosFLD = basinTable->Field (DBrNMouthPos);
	DBObjTableField *colorFLD		= basinTable->Field (DBrNColor);

	DBObjTableField *positionFLD	= cellTable->Field (DBrNPosition);
	DBObjTableField *toCellFLD		= cellTable->Field (DBrNToCell);
	DBObjTableField *fromCellFLD	= cellTable->Field (DBrNFromCell);
	DBObjTableField *orderFLD		= cellTable->Field (DBrNOrder);
	DBObjTableField *basinFLD		= cellTable->Field (DBrNBasin);
	DBObjTableField *basinCellsFLD= cellTable->Field (DBrNBasinCells);
	DBObjTableField *travelFLD		= cellTable->Field (DBrNTravel);
	DBObjTableField *upCellPosFLD	= cellTable->Field (DBrNUpCellPos);
	DBObjTableField *cellAreaFLD	= cellTable->Field (DBrNCellArea);
	DBObjTableField *subbasinLengthFLD = cellTable->Field (DBrNSubbasinLength);
	DBObjTableField *subbasinAreaFLD = cellTable->Field (DBrNSubbasinArea);

	DBObjTableField *rowNumFLD = layerTable->Field (DBrNRowNum);
	DBObjTableField *colNumFLD = layerTable->Field (DBrNColNum);
	DBObjTableField *cellWidthFLD = layerTable->Field (DBrNCellWidth);
	DBObjTableField *cellHeightFLD = layerTable->Field (DBrNCellHeight);
	DBObjTableField *valueTypeFLD = layerTable->Field (DBrNValueType);
	DBObjTableField *valueSizeFLD = layerTable->Field (DBrNValueSize);
	DBObjTableField *layerFLD = layerTable->Field (DBrNLayer);
	DBObjData *zGridData = gridData->LinkedData ();
	DBGridIF *gridIF = new DBGridIF (gridData), *zGridIF;
	DBNetworkIF *netIF;

	if ((zGridData != (DBObjData *) NULL) && ((zGridData->Type () == DBTypeGridDiscrete) || (zGridData->Type () == DBTypeGridContinuous)))
		{
		zGridIF = new DBGridIF (zGridData);
		zLayerNum = zGridIF->LayerNum () + 1;
		}
	else { zGridIF = (DBGridIF *) NULL; zLayerNum = 1; }

	if ((zones = (DBInt *) calloc (9 * zLayerNum,sizeof (DBInt))) == (DBInt *) NULL)
		{
		CMmsgPrint (CMmsgSysError, "Memory Allocation Error in: %s %d",__FILE__,__LINE__);
		if (zGridIF != (DBGridIF *) NULL) delete zGridIF;
		delete gridIF;
		return (DBFault);
		}
	layerTable->Add (DBrNLookupGrid);
	if ((layerRec = layerTable->Item (DBrNLookupGrid)) == (DBObjRecord *) NULL)
		{
		free (zones);
		if (zGridIF != (DBGridIF *) NULL) delete zGridIF;
		delete gridIF;
		return (DBFault);
		}

	netData->Projection (projection);
	netData->Extent (gridData->Extent ());
	cellWidthFLD->Float  (layerRec,gridIF->CellWidth ());
	cellHeightFLD->Float (layerRec,gridIF->CellHeight ());
	valueTypeFLD->Int (layerRec,DBTableFieldInt);
	valueSizeFLD->Int (layerRec,sizeof (DBInt));
	rowNumFLD->Int (layerRec,gridIF->RowNum ());
	colNumFLD->Int (layerRec,gridIF->ColNum ());

	dataRec = new DBObjRecord ("NetLookupGridRecord",((size_t) gridIF->RowNum ()) * gridIF->ColNum () * sizeof (DBInt),sizeof (DBInt));
	if (dataRec == (DBObjRecord *) NULL)
		{
		if (zGridIF != (DBGridIF *) NULL) delete zGridIF;
		return (DBFault);
		}
	layerFLD->Record (layerRec,dataRec);
	(netData->Arrays ())->Add (dataRec);
	for (pos.Row = 0;pos.Row < gridIF->RowNum ();pos.Row++)
		for (pos.Col = 0;pos.Col < gridIF->ColNum ();pos.Col++)
			((DBInt *) dataRec->Data ()) [pos.Row * gridIF->ColNum () + pos.Col] = DBFault;

	for (pos.Row = 0;pos.Row < gridIF->RowNum ();pos.Row++)
		{
		if (DBPause (10 * pos.Row / gridIF->RowNum ())) goto PauseStop;
		for (pos.Col = 0;pos.Col < gridIF->ColNum ();pos.Col++)
			{
			gridIF->Pos2Coord (pos,coord0);
			zLayerID = 0;
			if (zGridIF != (DBGridIF *) NULL)
				for ( ; zLayerID < zGridIF->LayerNum (); zLayerID++)
					{
					layerRec = zGridIF->Layer (zLayerID);
					if ((layerRec->Flags () & DBObjectFlagIdle) == DBObjectFlagIdle) continue;
					for (dir = 0;dir < 8;++dir)
						{
						row = pos.Row;
						col = pos.Col;
						if (((0x01 << dir) == DBNetDirNW) || ((0x01 << dir) == DBNetDirN) || ((0x01 << dir) == DBNetDirNE)) row++;
						if (((0x01 << dir) == DBNetDirSE) || ((0x01 << dir) == DBNetDirS) || ((0x01 << dir) == DBNetDirSW)) row--;
						if (((0x01 << dir) == DBNetDirNE) || ((0x01 << dir) == DBNetDirE) || ((0x01 << dir) == DBNetDirSE)) col++;
						if (((0x01 << dir) == DBNetDirNW) || ((0x01 << dir) == DBNetDirW) || ((0x01 << dir) == DBNetDirSW)) col--;
						if (row < 0) continue;
						if (col < 0) continue;
						if (row >= gridIF->RowNum ()) continue;
						if (col >= gridIF->ColNum ()) continue;
						auxPos.Row = row;
						auxPos.Col = col;
						gridIF->Pos2Coord (auxPos,coord1);
						switch (zGridData->Type ())
							{
							case DBTypeGridDiscrete:	basinID = zGridIF->GridValue (layerRec,coord1);	break;
							case DBTypeGridContinuous:	zGridIF->Value (layerRec,coord1,&basinID);		break;
							}
						zones [zLayerID * 9 + dir] = basinID;
						}
					switch (zGridData->Type ())
						{
						case DBTypeGridDiscrete:	basinID = zGridIF->GridValue (layerRec,coord0);	break;
						case DBTypeGridContinuous: zGridIF->Value (layerRec,coord0,&basinID);		break;
						}
					zones [zLayerID * 9 + 8] = basinID;
					}
			for (dir = 0;dir < 9;++dir) zones [zLayerID * 9 + dir] = 0;

			maxDir = DBFault;
			for (layerID = 0;layerID < gridIF->LayerNum ();++layerID)
				{
				layerRec = gridIF->Layer (layerID);
				if ((layerRec->Flags () & DBObjectFlagIdle) == DBObjectFlagIdle) continue;
				if (gridIF->Value (layerRec,pos,&elev0))
					{
					maxDelta = (DBFloat) 0.0;
					maxDir   = 0;

					for (zLayerID = 0;zLayerID < zLayerNum;++zLayerID)
						{
						for (dir = 0;dir < 8;++dir)
							{
							row = pos.Row;
							col = pos.Col;
							if (((0x01 << dir) == DBNetDirNW) || ((0x01 << dir) == DBNetDirN) || ((0x01 << dir) == DBNetDirNE)) row++;
							if (((0x01 << dir) == DBNetDirSE) || ((0x01 << dir) == DBNetDirS) || ((0x01 << dir) == DBNetDirSW)) row--;
							if (((0x01 << dir) == DBNetDirNE) || ((0x01 << dir) == DBNetDirE) || ((0x01 << dir) == DBNetDirSE)) col++;
							if (((0x01 << dir) == DBNetDirNW) || ((0x01 << dir) == DBNetDirW) || ((0x01 << dir) == DBNetDirSW)) col--;
							if (col < 0) continue;
							if (row < 0) continue;
							if (col >= gridIF->ColNum ()) continue;
							if (row >= gridIF->RowNum ()) continue;
							auxPos.Row = row;
							auxPos.Col = col;
							gridIF->Pos2Coord (auxPos,coord1);
							distance = DBMathCoordinateDistance (projection,coord0,coord1);
							if ((zones [zLayerID * 9 + dir] == zones [zLayerID * 9 + 8]) && (gridIF->Value (layerRec,auxPos,&elev1)))
								{
								delta = (downhill ? (elev1 - elev0) : (elev0 - elev1)) / distance;
								if (maxDelta > delta) { maxDelta = delta; maxDir = (0x01 << dir); }
								}
							}
						if (maxDir != 0) goto SlopeStop;
						}
					}
				}
SlopeStop:
			if (maxDir != DBFault)
				{
				sprintf (nameSTR,"GHAASCell:%d",cellTable->ItemNum ());
				cellRec = cellTable->Add (nameSTR);
				positionFLD->Position(cellRec,pos);
				toCellFLD->Int			(cellRec,(DBInt) maxDir);
				fromCellFLD->Int		(cellRec,(DBInt) 0);
				orderFLD->Int			(cellRec,(DBInt) 0);
				basinFLD->Int			(cellRec,(DBInt) 0);
				basinCellsFLD->Int	(cellRec,(DBInt) 0);
				travelFLD->Int			(cellRec,(DBInt) 0);
				upCellPosFLD->Position	(cellRec,pos);
				cellAreaFLD->Float	(cellRec,(DBFloat) 0.0);
				subbasinLengthFLD->Float(cellRec,(DBFloat) 0.0);
				subbasinAreaFLD->Float	(cellRec,(DBFloat) 0.0);
				((DBInt *) dataRec->Data ()) [pos.Row * gridIF->ColNum () + pos.Col] = cellRec->RowID ();
				}
			}
		}
PauseStop:
	if (pos.Row < gridIF->RowNum ())	return (DBFault);
	sprintf (nameSTR,"GHAASBasin%d",(DBInt) 0);
	basinRec = basinTable->Add (nameSTR);
	mouthPosFLD->Position	(basinRec,positionFLD->Position (cellTable->Item (0)));
	colorFLD->Int				(basinRec,0);

	free (zones);
	delete gridIF;
	if (zGridIF != (DBGridIF *) NULL) delete zGridIF;

	netData->Precision (DBMathMin (gridIF->CellWidth (),gridIF->CellHeight ()) / 25.0);
	netIF = new DBNetworkIF (netData);
	netIF->Build ();
	delete netIF;
	return (DBSuccess);
	}