示例#1
0
void DBGridOperationAbs(DBObjData *grdData) {
    DBInt layerID;
    DBFloat value;
    DBPosition pos;
    DBObjRecord *layerRec;
    DBGridIF *gridIF = new DBGridIF(grdData);

    for (layerID = 0; layerID < gridIF->LayerNum(); ++layerID) {
        layerRec = gridIF->Layer(layerID);
        if ((layerRec->Flags() & DBObjectFlagIdle) != DBObjectFlagIdle) break;
    }
    if (layerID == gridIF->LayerNum()) {
        CMmsgPrint(CMmsgAppError, "No Layer to Process in %s %d", __FILE__, __LINE__);
        return;
    }

    for (layerID = 0; layerID < gridIF->LayerNum(); ++layerID) {
        layerRec = gridIF->Layer(layerID);
        if ((layerRec->Flags() & DBObjectFlagIdle) == DBObjectFlagIdle) continue;

        for (pos.Row = 0; pos.Row < gridIF->RowNum(); pos.Row++) {
            if (DBPause((layerID * gridIF->RowNum() + pos.Row) * 100 / (gridIF->LayerNum() * gridIF->RowNum())))
                goto Stop;
            for (pos.Col = 0; pos.Col < gridIF->ColNum(); pos.Col++)
                if (gridIF->Value(layerRec, pos, &value)) gridIF->Value(layerRec, pos, fabs(value));
        }
        gridIF->RecalcStats(layerRec);
    }
    Stop:
    return;
}
示例#2
0
void RGISEditGridStatsCBK (Widget widget, RGISWorkspace *workspace,XmAnyCallbackStruct *callData)

	{
	DBDataset *dataset = UIDataset ();
	DBObjData *dbData = dataset->Data ();
	DBGridIF *gridIF = new DBGridIF (dbData);
	UITable *tableCLS = (UITable *) dbData->Display (UITableName (dbData,dbData->Table (DBrNItems)));

	if (dbData->Type () == DBTypeGridContinuous)
			gridIF->RecalcStats ();
	else	gridIF->DiscreteStats ();
	if (tableCLS != (UITable *) NULL) tableCLS->Draw ();
	}
示例#3
0
void DBGridOperation(DBObjData *grdData, DBFloat constant, DBInt oper) {
    DBInt layerID;
    DBFloat value;
    DBPosition pos;
    DBObjRecord *layerRec;
    DBGridIF *gridIF = new DBGridIF(grdData);

    for (layerID = 0; layerID < gridIF->LayerNum(); ++layerID) {
        layerRec = gridIF->Layer(layerID);
        if ((layerRec->Flags() & DBObjectFlagIdle) != DBObjectFlagIdle) break;
    }
    if (layerID == gridIF->LayerNum()) {
        CMmsgPrint(CMmsgAppError, "No Layer to Process in %s %d", __FILE__, __LINE__);
        return;
    }

    for (layerID = 0; layerID < gridIF->LayerNum(); ++layerID) {
        layerRec = gridIF->Layer(layerID);
        if ((layerRec->Flags() & DBObjectFlagIdle) == DBObjectFlagIdle) continue;

        for (pos.Row = 0; pos.Row < gridIF->RowNum(); pos.Row++) {
            if (DBPause((layerID * gridIF->RowNum() + pos.Row) * 100 / (gridIF->LayerNum() * gridIF->RowNum())))
                goto Stop;
            for (pos.Col = 0; pos.Col < gridIF->ColNum(); pos.Col++) {
                if (gridIF->Value(layerRec, pos, &value))
                    switch (oper) {
                        case DBMathOperatorAdd:
                            gridIF->Value(layerRec, pos, value + constant);
                            break;
                        case DBMathOperatorSub:
                            gridIF->Value(layerRec, pos, value - constant);
                            break;
                        case DBMathOperatorMul:
                            gridIF->Value(layerRec, pos, value * constant);
                            break;
                        case DBMathOperatorDiv:
                            gridIF->Value(layerRec, pos, value / constant);
                            break;
                    }
            }
        }
        gridIF->RecalcStats(layerRec);
    }
    Stop:
    return;
}
示例#4
0
int _CMDnetErosion(DBObjData *netData, DBObjData *inData, DBObjData *weightData,
                   DBObjData *outData, DBFloat coeff, DBInt areaMult) {
    DBInt ret = DBSuccess, layerID, cellID, progress, maxProgress;
    DBFloat inValue, weight, outValue, *sumWeights;
    DBPosition pos;
    DBNetworkIF *netIF = new DBNetworkIF(netData);
    DBGridIF *inIF = new DBGridIF(inData);
    DBGridIF *outIF = new DBGridIF(outData);
    DBGridIF *weightIF = weightData != (DBObjData *) NULL ? new DBGridIF(weightData) : (DBGridIF *) NULL;
    DBObjRecord *inLayerRec, *outLayerRec, *weightLayerRec, *cellRec, *toCell;

    if ((sumWeights = (DBFloat *) calloc(netIF->CellNum(), sizeof(DBFloat))) == (DBFloat *) NULL) {
        CMmsgPrint(CMmsgSysError, "Memory allocation error in: %s %d", __FILE__, __LINE__);
        ret = DBFault;
        goto Stop;
    }

    layerID = 0;
    inLayerRec = inIF->Layer(layerID);

    outLayerRec = outIF->Layer(layerID);
    outIF->RenameLayer(outLayerRec, inLayerRec->Name());
    outValue = outIF->MissingValue(outLayerRec);
    for (pos.Row = 0; pos.Row < outIF->RowNum(); pos.Row++)
        for (pos.Col = 0; pos.Col < outIF->ColNum(); pos.Col++) outIF->Value(outLayerRec, pos, outValue);

    for (layerID = 1; layerID < inIF->LayerNum(); ++layerID) {
        inLayerRec = inIF->Layer(layerID);
        if ((outLayerRec = outIF->AddLayer(inLayerRec->Name())) == (DBObjRecord *) NULL) {
            ret = DBFault;
            goto Stop;
        }
        for (pos.Row = 0; pos.Row < outIF->RowNum(); pos.Row++)
            for (pos.Col = 0; pos.Col < outIF->ColNum(); pos.Col++) outIF->Value(outLayerRec, pos, outValue);
    }
    maxProgress = inIF->LayerNum() * netIF->CellNum();
    for (layerID = 0; layerID < inIF->LayerNum(); ++layerID) {
        inLayerRec = inIF->Layer(layerID);
        outLayerRec = outIF->Layer(layerID);
        if (weightIF != (DBGridIF *) NULL)
            weightLayerRec = weightIF->Layer(layerID % weightIF->LayerNum());
        for (cellID = 0; cellID < netIF->CellNum(); cellID++) {
            sumWeights[cellID] = 0.0;
            cellRec = netIF->Cell(cellID);
            if (inIF->Value(inLayerRec, netIF->Center(cellRec), &inValue) == false)
                outIF->Value(outLayerRec, netIF->CellPosition(cellRec), 0.0);
            else {
                if (weightIF != (DBGridIF *) NULL)
                    weight = weightIF->Value(weightLayerRec, netIF->Center(cellRec), &weight) == false ?
                             0.0 : weight * coeff;
                else weight = coeff;
                if (areaMult) weight = weight * netIF->CellArea(cellRec);
                sumWeights[cellID] = weight;
                outIF->Value(outLayerRec, netIF->CellPosition(cellRec), inValue * weight);
            }
        }

        for (cellID = netIF->CellNum() - 1; cellID >= 0; --cellID) {
            progress = layerID * netIF->CellNum() + (netIF->CellNum() - cellID);
            if (DBPause(progress * 100 / maxProgress)) goto Stop;
            cellRec = netIF->Cell(cellID);
            if ((toCell = netIF->ToCell(cellRec)) == (DBObjRecord *) NULL) continue;
            if (outIF->Value(outLayerRec, netIF->CellPosition(cellRec), &inValue) == false) continue;
            if (outIF->Value(outLayerRec, netIF->CellPosition(toCell), &outValue) == false) continue;

            sumWeights[toCell->RowID()] = sumWeights[toCell->RowID()] + weight;
            outIF->Value(outLayerRec, netIF->CellPosition(toCell), outValue + inValue);
        }
        outIF->RecalcStats(outLayerRec);
    }

    free(sumWeights);
    Stop:
    delete netIF;
    delete inIF;
    delete outIF;
    if (weightIF != (DBGridIF *) NULL) delete weightIF;
    return (ret);
}
示例#5
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);
}
示例#6
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;
}
示例#7
0
DBInt RGlibDataStream2RGIS(DBObjData *outData, DBObjData *tmplData, FILE *inFile) {
    DBInt layerID = 0, itemSize;
    DBPosition pos;
    DBFloat val;
    void *data = (void *) NULL;
    MFVarHeader_t header;
    DBObjRecord *record;


    switch (tmplData->Type()) {
        case DBTypeVectorPoint: {
            DBInt itemID;
            DBDate date;
            DBObjTable *itemTable = outData->Table(DBrNItems);
            DBObjTableField *idField = new DBObjTableField("ItemID", DBTableFieldInt, "%6d", sizeof(DBInt), false);
            DBObjTableField *dateField = new DBObjTableField("Date", DBTableFieldDate, "%s", sizeof(DBDate), false);
            DBObjTableField *valField;
            DBVPointIF *pntIF = new DBVPointIF(tmplData);

            itemTable->AddField(idField);
            itemTable->AddField(dateField);

            while (MFVarReadHeader(&header, inFile)) {
                if (header.ItemNum != pntIF->ItemNum()) {
                    CMmsgPrint(CMmsgUsrError, "Error: Datastream inconsistency %d %d!", header.ItemNum,
                               pntIF->ItemNum());
                    return (DBFault);
                }
                if (data == (void *) NULL) {
                    itemSize = MFVarItemSize(header.DataType);
                    if ((data = (void *) realloc(data, header.ItemNum * itemSize)) == (void *) NULL) {
                        CMmsgPrint(CMmsgSysError, "Memory allocation error in: %s %d", __FILE__, __LINE__);
                        return (DBFault);
                    }
                    switch (header.DataType) {
                        case MFByte:
                            valField = new DBObjTableField("Value", DBTableFieldInt, "%2d", sizeof(char), false);
                        case MFShort:
                            valField = new DBObjTableField("Value", DBTableFieldInt, "%4d", sizeof(DBShort), false);
                        case MFInt:
                            valField = new DBObjTableField("Value", DBTableFieldInt, "%8d", sizeof(DBInt), false);
                        case MFFloat:
                            valField = new DBObjTableField("Value", DBTableFieldFloat, "%8.2f", sizeof(DBFloat4),
                                                           false);
                        case MFDouble:
                            valField = new DBObjTableField("Value", DBTableFieldFloat, "%8.2f", sizeof(DBFloat), false);
                    }
                    itemTable->AddField(valField);
                }
                if ((int) fread(data, itemSize, header.ItemNum, inFile) != header.ItemNum) {
                    CMmsgPrint(CMmsgSysError, "Error: Data stream read in: %s %d", __FILE__, __LINE__);
                    return (DBFault);
                }
                for (itemID = 0; itemID < header.ItemNum; ++itemID) {
                    record = itemTable->Add(header.Date);
                    date.Set(header.Date);
                    idField->Int(record, itemID);
                    dateField->Date(record, date);
/*					decDateField->Float (record,date);
*/                    switch (header.DataType) {
                        case MFByte:
                            valField->Int(record, ((char *) data)[itemID]);
                            break;
                        case MFShort:
                            valField->Int(record, ((short *) data)[itemID]);
                            break;
                        case MFInt:
                            valField->Int(record, ((int *) data)[itemID]);
                            break;
                        case MFFloat:
                            valField->Float(record, ((float *) data)[itemID]);
                            break;
                        case MFDouble:
                            valField->Float(record, ((double *) data)[itemID]);
                            break;
                    }
                }
            }
            delete pntIF;
        }
            break;
        case DBTypeGridContinuous:
        case DBTypeGridDiscrete: {
            DBGridIF *gridIF = new DBGridIF(outData);

            while (MFVarReadHeader(&header, inFile)) {
                if (header.ItemNum != gridIF->RowNum() * gridIF->ColNum()) {
                    CMmsgPrint(CMmsgUsrError, "Error: Datastream inconsistency!");
                    return (DBFault);
                }
                if (layerID == 0) {
                    itemSize = MFVarItemSize(header.DataType);
                    if ((data = (void *) realloc(data, header.ItemNum * itemSize)) == (void *) NULL) {
                        CMmsgPrint(CMmsgSysError, "Memory allocation error in: %s %d", __FILE__, __LINE__);
                        return (DBFault);
                    }
                    record = gridIF->Layer(layerID);
                    gridIF->RenameLayer(header.Date);
                }
                else record = gridIF->AddLayer(header.Date);

                switch (header.DataType) {
                    case MFByte:
                    case MFShort:
                    case MFInt:
                        gridIF->MissingValue(record, header.Missing.Int);
                        break;
                    case MFFloat:
                    case MFDouble:
                        gridIF->MissingValue(record, header.Missing.Float);
                        break;
                }
                if ((int) fread(data, itemSize, header.ItemNum, inFile) != header.ItemNum) {
                    CMmsgPrint(CMmsgSysError, "Error: Data stream read in: %s %d", __FILE__, __LINE__);
                    return (DBFault);
                }

                for (pos.Row = 0; pos.Row < gridIF->RowNum(); ++pos.Row)
                    for (pos.Col = 0; pos.Col < gridIF->ColNum(); ++pos.Col) {
                        switch (header.DataType) {
                            case MFByte:
                                val = (DBFloat) (((char *) data)[pos.Row * gridIF->ColNum() + pos.Col]);
                                break;
                            case MFShort:
                                val = (DBFloat) (((short *) data)[pos.Row * gridIF->ColNum() + pos.Col]);
                                break;
                            case MFInt:
                                val = (DBFloat) (((int *) data)[pos.Row * gridIF->ColNum() + pos.Col]);
                                break;
                            case MFFloat:
                                val = (DBFloat) (((float *) data)[pos.Row * gridIF->ColNum() + pos.Col]);
                                break;
                            case MFDouble:
                                val = (DBFloat) (((double *) data)[pos.Row * gridIF->ColNum() + pos.Col]);
                                break;
                        }
                        gridIF->Value(record, pos, val);
                    }
                layerID++;
            }
            gridIF->RecalcStats();
        }
            break;
        case DBTypeNetwork: {
            DBInt cellID;
            DBGridIF *gridIF = new DBGridIF(outData);
            DBNetworkIF *netIF = new DBNetworkIF(tmplData);

            while (MFVarReadHeader(&header, inFile)) {
                if (header.ItemNum != netIF->CellNum()) {
                    CMmsgPrint(CMmsgUsrError, "Error: Datastream inconsistency!");
                    return (DBFault);
                }
                if (layerID == 0) {
                    itemSize = MFVarItemSize(header.DataType);
                    if ((data = (void *) realloc(data, header.ItemNum * itemSize)) == (void *) NULL) {
                        CMmsgPrint(CMmsgSysError, "Memory allocation error in: %s %d", __FILE__, __LINE__);
                        return (DBFault);
                    }
                    record = gridIF->Layer(layerID);
                    gridIF->RenameLayer(header.Date);
                }
                else record = gridIF->AddLayer(header.Date);
                if ((int) fread(data, itemSize, header.ItemNum, inFile) != header.ItemNum) {
                    CMmsgPrint(CMmsgSysError, "Error: Data stream read in: %s %d", __FILE__, __LINE__);
                    delete netIF;
                    return (DBFault);
                }

                for (pos.Row = 0; pos.Row < gridIF->RowNum(); ++pos.Row)
                    for (pos.Col = 0; pos.Col < gridIF->ColNum(); ++pos.Col)
                        gridIF->Value(record, pos, gridIF->MissingValue());

                for (cellID = 0; cellID < netIF->CellNum(); ++cellID) {
                    pos = netIF->CellPosition(netIF->Cell(cellID));

                    switch (header.DataType) {
                        case MFByte:
                            val = (DBFloat) (((char *) data)[cellID]);
                            break;
                        case MFShort:
                            val = (DBFloat) (((short *) data)[cellID]);
                            break;
                        case MFInt:
                            val = (DBFloat) (((int *) data)[cellID]);
                            break;
                        case MFFloat:
                            val = (DBFloat) (((float *) data)[cellID]);
                            break;
                        case MFDouble:
                            val = (DBFloat) (((double *) data)[cellID]);
                            break;
                    }
                    gridIF->Value(record, pos, val);
                }
                layerID++;
            }
            gridIF->RecalcStats();
        }
            break;
    }
    return (DBSuccess);
}
示例#8
0
		DBInt Read (FILE *file,DBObjData *data)
			{
			DBInt i, layer, swap, valueType, valueSize, docLength;
			DMLayerHeader dmLayerHeader;
			DBObjRecord *layerRec, *dataRec;
			DBObjTable *layerTable = data->Table (DBrNLayers);
			DBObjTable *itemTable	 = data->Table (DBrNItems);
			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);
			DBGridIF *gridIF;

			if ((swap = DMFileHeader::Read (file)) == DBFault) return (DBFault);
         data->Extent (Extent ());
         data->Projection (DBMathGuessProjection (data->Extent ()));
         data->Precision  (DBMathGuessPrecision  (data->Extent ()));
			if (FileType () != DMMatrix)
				{ CMmsgPrint (CMmsgAppError, "Wrong File Type in: %s %d",__FILE__,__LINE__); return (DBFault); }
			switch (DataType ())
				{
				case DMFloat:	valueType = DBTableFieldFloat;	valueSize = sizeof (DBFloat4);break;
				case DMInt:		valueType = DBTableFieldInt;		valueSize = sizeof (DBInt);	break;
				case DMByte:	valueType = DBTableFieldInt;		valueSize = sizeof (DBByte);	break;
				default: CMmsgPrint (CMmsgAppError, "Wrong Data Value Type in: %s %d",__FILE__,__LINE__); return (DBFault);
				}
			for (layer = 0;layer < LayerNum ();++layer)
				{
				if (dmLayerHeader.Read (file,swap) == DBFault) return (DBFault);
				if (strlen (dmLayerHeader.Description ()) > 0) layerName = dmLayerHeader.Description ();
				else { sprintf (layerNameSTR,"GHAASLayer%4d",layer + 1); layerName = layerNameSTR; }
				layerTable->Add (layerName);
				if ((layerRec = layerTable->Item ()) == (DBObjRecord *) NULL) return (DBFault);
				rowNumFLD->Int (layerRec,RowNum ());
				colNumFLD->Int (layerRec,ColNum ());
				cellWidthFLD->Float (layerRec,CellWidth ());
				cellHeightFLD->Float (layerRec,CellHeight ());
				valueTypeFLD->Int (layerRec,((DBInt) DBTypeGridDiscrete) == data->Type () ? DBTableFieldInt : valueType);
				valueSizeFLD->Int (layerRec,valueSize);
				if ((dataRec = new DBObjRecord (layerName,((size_t) ColNum ()) * RowNum () * valueSize,valueSize)) == (DBObjRecord *) NULL)
					return (DBFault);
				(data->Arrays ())->Add (dataRec);
				layerFLD->Record (layerRec,dataRec);
				}
			if (fread (&docLength,sizeof (int),1,file) != 1)
				{ CMmsgPrint (CMmsgSysError, "File Reading Error in: %s %d",__FILE__,__LINE__); return (DBFault); }
			if (swap) DBByteOrderSwapWord (&docLength);
			if (docLength > 0)
				{
				char *docString;
				if ((docString = (char *) calloc (docLength,sizeof (char))) == (char *) NULL)
					{ CMmsgPrint (CMmsgSysError, "Memory Allocation Error in: %s %d",__FILE__,__LINE__); return (DBFault); }
				if (fread (docString,docLength,1,file) != 1)
					{ CMmsgPrint (CMmsgSysError, "File Reading Error in: %s %d",__FILE__,__LINE__); return (DBFault); }
				data->Document (DBDocComment,docString);
				free (docString);
				}
			for (dataRec = (data->Arrays ())->First ();dataRec != (DBObjRecord *) NULL;dataRec = (data->Arrays ())->Next ())
				if (fread (dataRec->Data (),ColNum () * valueSize * RowNum (),1,file) != 1)
					{ CMmsgPrint (CMmsgSysError, "File Reading Error in: %s %d",__FILE__,__LINE__); return (DBFault); }
			if (swap && valueSize > 1)
				{
				int i;
				void (*swapFunc) (void *);
				switch (valueSize)
					{
					case 2: swapFunc = DBByteOrderSwapHalfWord; break;
					case 4: swapFunc = DBByteOrderSwapWord; break;
					case 8: swapFunc = DBByteOrderSwapLongWord; break;
					default: CMmsgPrint (CMmsgAppError, "Wrong Data Value Size in: %s %d",__FILE__,__LINE__); return (DBFault);
					}
				for (dataRec = (data->Arrays ())->First ();dataRec != (DBObjRecord *) NULL;dataRec = (data->Arrays ())->Next ())
					for (i = 0;i < ColNum () * RowNum ();++i) (*swapFunc) ((char *) dataRec->Data () + i * valueSize);
				}
			switch (data->Type ())
				{
				case DBTypeGridDiscrete:
					{
					DBInt value;
					char nameStr [DBStringLength];
					DBObjRecord *symRec = (data->Table (DBrNSymbols))->Add ("Default Symbol");
					DBObjRecord *itemRec;
					DBObjTableField *gridValueFLD  = itemTable->Field (DBrNGridValue);
					DBObjTableField *gridSymbolFLD = itemTable->Field (DBrNSymbol);
					DBObjTableField *symbolIDFLD	 = (data->Table (DBrNSymbols))->Field (DBrNSymbolID);
					DBObjTableField *foregroundFLD = (data->Table (DBrNSymbols))->Field (DBrNForeground);
					DBObjTableField *backgroundFLD = (data->Table (DBrNSymbols))->Field (DBrNBackground);
					DBObjTableField *styleFLD = (data->Table (DBrNSymbols))->Field (DBrNStyle);

					symbolIDFLD->Int (symRec,0);
					foregroundFLD->Int (symRec,1);
					backgroundFLD->Int (symRec,0);
					styleFLD->Int (symRec,0);
					for (dataRec = (data->Arrays ())->First ();dataRec != (DBObjRecord *) NULL;dataRec = (data->Arrays ())->Next ())
						{
						for (i = 0;i < ColNum () * RowNum ();++i)
							{
							switch (valueType)
								{
								case DBTableFieldFloat: value = (DBInt) rint (*((float *) ((char *) dataRec->Data () + i * valueSize))); break;
								case DBTableFieldInt:
									switch (valueSize)
										{
										case sizeof (DBByte):  value = (DBInt) (*((DBByte *)  ((char *) dataRec->Data () + i * valueSize))); break;
										case sizeof (DBShort): value = (DBInt) (*((DBShort *) ((char *) dataRec->Data () + i * valueSize))); break;
										case sizeof (DBInt):	  value = (DBInt) (*((DBInt *)	((char *) dataRec->Data () + i * valueSize))); break;
										default: CMmsgPrint (CMmsgAppError, "Wrong Data Size in: %s %d",__FILE__,__LINE__); return (DBFault);
										}
									break;
								default: CMmsgPrint (CMmsgAppError, "Wrong Data Type in: %s %d",__FILE__,__LINE__); return (DBFault);
								}
							sprintf (nameStr,"Category%04d",value);
							if ((itemRec = itemTable->Item (nameStr)) == (DBObjRecord *) NULL)
								{
								if ((itemRec = itemTable->Add (nameStr)) == (DBObjRecord *) NULL)
									{ CMmsgPrint (CMmsgAppError, "Item Object Creation Error in: %s %d",__FILE__,__LINE__); return (DBFault); }
								gridValueFLD->Int (itemRec,value);
								gridSymbolFLD->Record (itemRec,symRec);
								}
							value = itemRec->RowID ();
							switch (valueSize)
								{
								case sizeof (DBByte):  *((DBByte *)  ((char *) dataRec->Data () + i * valueSize)) = value; break;
								case sizeof (DBShort): *((DBShort *) ((char *) dataRec->Data () + i * valueSize)) = value; break;
								case sizeof (DBInt):	  *((DBInt *)	 ((char *) dataRec->Data () + i * valueSize)) = value; break;
								default: CMmsgPrint (CMmsgAppError, "Wrong Data Size in: %s %d",__FILE__,__LINE__); return (DBFault);
								}
							}
						}
					itemTable->ListSort (gridValueFLD);
					for (dataRec = (data->Arrays ())->First ();dataRec != (DBObjRecord *) NULL;dataRec = (data->Arrays ())->Next ())
						{
						for (i = 0;i < ColNum () * RowNum ();++i)
							{
							switch (valueSize)
								{
								case sizeof (DBByte):  value = (DBInt) (*((DBByte *)  ((char *) dataRec->Data () + i * valueSize))); break;
								case sizeof (DBShort): value = (DBInt) (*((DBShort *) ((char *) dataRec->Data () + i * valueSize))); break;
								case sizeof (DBInt):	  value = (DBInt) (*((DBInt *)	((char *) dataRec->Data () + i * valueSize))); break;
								default: CMmsgPrint (CMmsgAppError, "Wrong Data Size in: %s %d",__FILE__,__LINE__); return (DBFault);
								}
							itemRec = itemTable->Item (value);
							value = itemRec->ListPos ();
							switch (valueSize)
								{
								case sizeof (DBByte):  *((DBByte *)  ((char *) dataRec->Data () + i * valueSize)) = value; break;
								case sizeof (DBShort): *((DBShort *) ((char *) dataRec->Data () + i * valueSize)) = value; break;
								case sizeof (DBInt):	  *((DBInt *)	 ((char *) dataRec->Data () + i * valueSize)) = value; break;
								default: CMmsgPrint (CMmsgAppError, "Wrong Data Size in: %s %d",__FILE__,__LINE__); return (DBFault);
								}
							}
						}
					itemTable->ItemSort ();
					gridIF = new DBGridIF (data);
					gridIF->DiscreteStats ();
					delete gridIF;
					} break;
				case DBTypeGridContinuous:
					{
					DBObjTableField *missingValueFLD		= itemTable->Field (DBrNMissingValue);

					for (dataRec = (data->Arrays ())->First ();dataRec != (DBObjRecord *) NULL;dataRec = (data->Arrays ())->Next ())
						{
						itemTable->Add (dataRec->Name ());
						missingValueFLD->Float (itemTable->Item (),MissingValue ());
						}
					gridIF = new DBGridIF (data);
					gridIF->RecalcStats ();
					delete gridIF;
					data->Flags (DBDataFlagDispModeContBlueRed,DBSet);
					break;
					}
				default: break;
				}
			return (DBSuccess);
			}
示例#9
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);
    }
示例#10
0
int _CMDnetTransfer(DBObjData *netData,
                    DBObjData *inData,
                    DBObjData *weightData,
                    DBObjData *outData,
                    DBFloat coeff,
                    DBObjData *coeffData,
                    DBObjData *QData,
                    DBObjData *HLData,
                    DBFloat umax,
                    DBFloat ksat,
                    DBInt areaMult) {
    DBInt ret = DBSuccess, layerID, cellID, progress, maxProgress;
    DBFloat inValue, weight, outValue, *sumWeights, kCoeff, q, hl, Conc, Uptake, Vf;
    DBPosition pos;
    DBNetworkIF *netIF = new DBNetworkIF(netData);
    DBGridIF *inIF = new DBGridIF(inData);
    DBGridIF *outIF = new DBGridIF(outData);
    DBGridIF *weightIF = weightData != (DBObjData *) NULL ? new DBGridIF(weightData) : (DBGridIF *) NULL;
    DBGridIF *coeffIF = coeffData != (DBObjData *) NULL ? new DBGridIF(coeffData) : (DBGridIF *) NULL;
    DBGridIF *Q_IF = QData != (DBObjData *) NULL ? new DBGridIF(QData) : (DBGridIF *) NULL;
    DBGridIF *HL_IF = HLData != (DBObjData *) NULL ? new DBGridIF(HLData) : (DBGridIF *) NULL;
    DBObjRecord *inLayerRec, *outLayerRec, *weightLayerRec, *coeffLayerRec, *Q_LayerRec, *HL_LayerRec, *cellRec, *toCell;
    DBCoordinate coord;


    if ((sumWeights = (DBFloat *) calloc(netIF->CellNum(), sizeof(DBFloat))) == (DBFloat *) NULL) {
        CMmsgPrint(CMmsgSysError, "Memory allocation error in: %s %d", __FILE__, __LINE__);
        ret = DBFault;
        goto Stop;
    }

    layerID = 0;
    inLayerRec = inIF->Layer(layerID);

    outLayerRec = outIF->Layer(layerID);
    outIF->RenameLayer(outLayerRec, inLayerRec->Name());
    outValue = outIF->MissingValue(outLayerRec);
    for (pos.Row = 0; pos.Row < outIF->RowNum(); pos.Row++)
        for (pos.Col = 0; pos.Col < outIF->ColNum(); pos.Col++) outIF->Value(outLayerRec, pos, outValue);

    for (layerID = 1; layerID < inIF->LayerNum(); ++layerID) {
        inLayerRec = inIF->Layer(layerID);
        if ((outLayerRec = outIF->AddLayer(inLayerRec->Name())) == (DBObjRecord *) NULL) {
            ret = DBFault;
            goto Stop;
        }
        for (pos.Row = 0; pos.Row < outIF->RowNum(); pos.Row++)
            for (pos.Col = 0; pos.Col < outIF->ColNum(); pos.Col++) outIF->Value(outLayerRec, pos, outValue);
    }
    maxProgress = inIF->LayerNum() * netIF->CellNum();
    for (layerID = 0; layerID < inIF->LayerNum(); ++layerID) {
        inLayerRec = inIF->Layer(layerID);
        outLayerRec = outIF->Layer(layerID);
        if (weightIF != (DBGridIF *) NULL)
            weightLayerRec = weightIF->Layer(layerID % weightIF->LayerNum());
        if (coeffIF != (DBGridIF *) NULL)
            coeffLayerRec = coeffIF->Layer(layerID % coeffIF->LayerNum());
        if (Q_IF != (DBGridIF *) NULL)
            Q_LayerRec = Q_IF->Layer(layerID % Q_IF->LayerNum());
        if (HL_IF != (DBGridIF *) NULL)
            HL_LayerRec = HL_IF->Layer(layerID % HL_IF->LayerNum());

        for (cellID = 0; cellID < netIF->CellNum(); cellID++) {
            sumWeights[cellID] = 0.0;
            cellRec = netIF->Cell(cellID);
            coord = netIF->Center(cellRec);
            if (inIF->Value(inLayerRec, coord, &inValue) == false)
                outIF->Value(outLayerRec, netIF->CellPosition(cellRec), 0.0);
            else {
                if (weightIF != (DBGridIF *) NULL)
                    weight = weightIF->Value(weightLayerRec, coord, &weight) == false ?
                             0.0 : weight * coeff;
                else weight = coeff;

                if (areaMult) weight = weight * netIF->CellArea(cellRec);
                sumWeights[cellID] = weight;
                outIF->Value(outLayerRec, netIF->CellPosition(cellRec), inValue * weight);
            }
        }

        for (cellID = netIF->CellNum() - 1; cellID >= 0; --cellID) {
            progress = layerID * netIF->CellNum() + (netIF->CellNum() - cellID);
            if (DBPause(progress * 100 / maxProgress)) goto Stop;
            cellRec = netIF->Cell(cellID);
            coord = netIF->Center(cellRec);
            if ((toCell = netIF->ToCell(cellRec)) == (DBObjRecord *) NULL) continue;
            if (outIF->Value(outLayerRec, netIF->CellPosition(cellRec), &inValue) == false) continue;
            if (outIF->Value(outLayerRec, netIF->CellPosition(toCell), &outValue) == false) continue;

            sumWeights[toCell->RowID()] = sumWeights[toCell->RowID()] + weight;


            if (coeffIF != (DBGridIF *) NULL) {
                if (coeffIF->Value(coeffLayerRec, netIF->Center(cellRec), &kCoeff) == false) kCoeff = 1.0;
            }
            else {
                if (((Q_IF == (DBGridIF *) NULL) || (Q_IF->Value(Q_LayerRec, netIF->Center(cellRec), &q) == false)) ||
                        ((HL_IF == (DBGridIF *) NULL) ||
                         (HL_IF->Value(HL_LayerRec, netIF->Center(cellRec), &hl) == false)) ||
                        (umax == 0) || (ksat == 0))
                    kCoeff = 1.0;
                else {
                    if ((q > 0) && (hl > 0)) {
                        Conc = ((inValue / (q * 86400 * 365)) * 1000 *
                                1000); /* mg/m3 - assume input = kg/yr, Q is m3/s convert to m3/yr kg to mg */
                        Uptake = (umax * 24 * 365 * Conc) /
                                 (ksat * 1000 + Conc); /* mg/m2/yr - umax and ksat/Conc are in mg/m2/hr, mg/m3 */
                        if (Uptake > 0) {
                            Vf = Uptake / Conc; /* Vf in m/yr */
                        }
                        else Vf = 0;
                        kCoeff = pow(2.71828, (-1.0 * Vf / hl)); /* HL in m/yr */
                        /*
                         if ((cellID == 5390) || (cellID == 5015) || (cellID  == 4905) || (cellID == 4857))
                             printf("%i, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f \n", cellID, outValue, inValue, q, hl, umax, ksat, Conc, Uptake, Vf, kCoeff);
                        */
                    }
                    else kCoeff = 0;
                }
            }
            /*
            if ((cellID == 5390) || (cellID == 5015) || (cellID  == 4905) || (cellID == 4857))
                 printf("%i, %f \n", cellID, inValue);
            */
            inValue = kCoeff * inValue;

            outIF->Value(outLayerRec, netIF->CellPosition(toCell), outValue + inValue);
        }
        outIF->RecalcStats(outLayerRec);
    }

    free(sumWeights);
Stop:
    delete netIF;
    delete inIF;
    delete outIF;
    if (weightIF != (DBGridIF *) NULL) delete weightIF;
    return (ret);

    return (DBSuccess);
}
示例#11
0
void RGISEditGridNetFilterCBK (Widget widget, RGISWorkspace *workspace,XmAnyCallbackStruct *callData)

	{
	DBInt layerID, cellID, count, ret, kernel, kernelSize, maxProgress, dir;
	DBFloat elev, cellElev, prevElev, upElev [5], meanElev, minElev, dElev;
	DBDataset *dataset = UIDataset ();
	DBObjData *grdData = dataset->Data ();
	DBObjData *netData = grdData->LinkedData ();
	DBGridIF *gridIF = new DBGridIF (grdData);
	DBNetworkIF *netIF = new DBNetworkIF (netData);
	DBObjRecord *cellRec, *fromCell, *nextCell, *layerRec;

	UIPauseDialogOpen ((char *) "Network Filtering");
	maxProgress = netIF->CellNum () * gridIF->LayerNum ();
	for (layerID = 0;layerID < gridIF->LayerNum (); ++layerID)
		{
		layerRec = gridIF->Layer (layerID);

		for (cellID = 0;cellID < netIF->CellNum (); ++cellID)
			{
			if (UIPause (((layerID + 1) * netIF->CellNum () - cellID) * 100 / maxProgress)) goto Stop;
			fromCell = netIF->Cell (cellID);
			if (netIF->FromCell (fromCell) != (DBObjRecord *) NULL) continue;
			while (gridIF->Value (layerRec,netIF->Center (fromCell),&prevElev) == (DBInt) false)
				if ((fromCell = netIF->ToCell (fromCell)) == (DBObjRecord *) NULL) break;
			if (fromCell == (DBObjRecord *) NULL) continue;

			kernelSize = 0;
			for (cellRec = netIF->ToCell (fromCell); (cellRec != (DBObjRecord *) NULL) && (netIF->FromCell (cellRec) == fromCell); cellRec = netIF->ToCell (cellRec))
				{
				dElev = netIF->CellLength (fromCell) * RGlibMinSLOPE;
				if ((ret = gridIF->Value (layerRec,netIF->Center (cellRec),&cellElev)) == false) { count = 0; meanElev = 0.0; }
				else { count = 1, meanElev = cellElev; }

				if (kernelSize + 1 < (int) (sizeof (upElev) / sizeof (upElev [0]))) kernelSize++;
				for (kernel = kernelSize - 1;kernel > 0;--kernel) upElev [kernel] = upElev [kernel - 1]; upElev [0] = prevElev;
				for (kernel = 0;kernel < kernelSize;++kernel) { meanElev += upElev [kernel]; count++; }
				minElev = prevElev;
				for (dir = 0; dir < 8;++dir)
					if (((fromCell = netIF->FromCell (cellRec,0x01 << dir,true)) != (DBObjRecord *) NULL) &&
					    (gridIF->Value (layerRec,netIF->Center (fromCell),&elev) == true) && (minElev > elev))
						{ minElev = elev; dElev = netIF->CellLength (fromCell) * RGlibMinSLOPE; }

				nextCell = netIF->ToCell (cellRec);
				for (kernel = 0;(kernel < kernelSize) && (nextCell != (DBObjRecord *) NULL);++kernel)
					{
					if(gridIF->Value (layerRec,netIF->Center (nextCell),&elev) != (DBInt) false) { meanElev += elev; count++; }
					nextCell = netIF->ToCell (nextCell);
					}
				if (count > 0)
					{
					meanElev = meanElev / count;

					if (meanElev > minElev - dElev) meanElev = minElev - dElev;
					gridIF->Value (layerRec,netIF->Center (cellRec),meanElev);
					prevElev = meanElev;
					}
				else	gridIF->Value (layerRec,netIF->Center (cellRec),gridIF->MissingValue ());
				fromCell = cellRec;
				}
			}
		gridIF->RecalcStats (layerRec);
		}
Stop:
	UIPauseDialogClose ();
	delete gridIF;
	delete netIF;
	}