コード例 #1
0
ファイル: DBObjTables.C プロジェクト: ztessler/RGIS
DBObjTableField::DBObjTableField(DBObjTableField &field) : DBObject(field) {
    TypeVAR = field.TypeVAR;
    UnitTypeVAR = field.UnitTypeVAR;
    UnitVAR = field.UnitVAR;
    RequiredVAR = field.RequiredVAR;

    StartByteVAR = field.StartByteVAR;
    LengthVAR = field.LengthVAR;
    strcpy(FormatSTR, field.FormatSTR);
    Flags(field.Flags());
    switch (TypeVAR) {
        case DBTableFieldInt:
            switch (LengthVAR) {
                case sizeof(DBByte):
                    BytePROP = field.BytePROP;
                    break;
                case sizeof(DBShort):
                    ShortPROP = field.ShortPROP;
                    break;
                default:
                    IntPROP = field.IntPROP;
                    break;
            }
            break;
        case DBTableFieldFloat:
            switch (LengthVAR) {
                case sizeof(DBFloat4):
                    Float4PROP = field.Float4PROP;
                    break;
                default:
                    FloatPROP = field.FloatPROP;
                    break;
            }
            break;
        case DBTableFieldDate:
            DatePROP = field.DatePROP;
            break;
        case DBTableFieldTableRec:
        case DBTableFieldDataRec:
            RecordPROP = field.RecordPROP;
            break;
    }
}
コード例 #2
0
ファイル: DBObjTables.C プロジェクト: ztessler/RGIS
static int _DBObjTableListSort(const DBObjRecord **obj0, const DBObjRecord **obj1) {
    DBInt ret = 0;
    DBObjTableField *field;
    DBDate date0, date1;

    for (field = _DBObjTableSortFields->First();
         field != (DBObjTableField *) NULL;
         field = _DBObjTableSortFields->Next()) {
        switch (field->Type()) {
            case DBTableFieldString:
                ret = strcmp(field->String(*obj0), field->String(*obj1));
                break;
            case DBTableFieldInt:
                ret = field->Int(*obj0) - field->Int(*obj1);
                break;
            case DBTableFieldFloat:
                if (field->Float(*obj0) - field->Float(*obj1) > 0.0) ret = 1;
                else if (field->Float(*obj0) - field->Float(*obj1) < 0.0) ret = -1;
                else ret = 0;
                break;
            case DBTableFieldDate:
                date0 = field->Date(*obj0);
                date1 = field->Date(*obj1);
                if (date0 > date1) ret = 1;
                else if (date0 < date1) ret = -1;
                else ret = 0;
                break;
            case DBTableFieldTableRec:
                ret = strcmp((*obj0)->Name(), (*obj1)->Name());
                break;
            case DBTableFieldDataRec:
                ret = (*obj0)->RowID() - (*obj1)->RowID();
                break;
        }
        if ((field->Flags() & DBObjectFlagSortReversed) == DBObjectFlagSortReversed) ret *= -1;
        if (ret != 0) return (ret);
    }
    if (ret == 0) ret = (*obj0)->ListPos() - (*obj1)->ListPos();
    return (ret);
}
コード例 #3
0
int main(int argc, char *argv[]) {
    int argPos, argNum = argc, ret, fieldNum = 0, verbose = false;
    char *tableName = (char *) NULL;
    char *title = (char *) NULL, *subject = (char *) NULL;
    char *domain = (char *) NULL, *version = (char *) NULL;
    DBObjData *data;
    DBObjectLIST<DBObjTableField> *fieldList;
    DBObjTable *table;
    DBObjTableField *fieldPTR;
    CMDtblSortField *fields = (CMDtblSortField *) NULL;

    for (argPos = 1; argPos < argNum;) {
        if (CMargTest (argv[argPos], "-a", "--table")) {
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) {
                CMmsgPrint(CMmsgUsrError, "Missing table name!");
                if (fields != (CMDtblSortField *) NULL) free(fields);
                return (CMfailed);
            }
            tableName = argv[argPos];
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) break;
            continue;
        }
        if (CMargTest (argv[argPos], "-A", "--ascending")) {
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) {
                CMmsgPrint(CMmsgUsrError, "Missing sort field!");
                if (fields != (CMDtblSortField *) NULL) free(fields);
                return (CMfailed);
            }
            if ((fields = (CMDtblSortField *) realloc(fields, (fieldNum + 1) * sizeof(CMDtblSortField))) ==
                (CMDtblSortField *) NULL) {
                CMmsgPrint(CMmsgSysError, "Memory reallocation error in: %s, %n", __FILE__, __LINE__);
                return (CMfailed);
            }
            fields[fieldNum].FieldName(argv[argPos]);
            fields[fieldNum].Ascending(true);
            fieldNum++;
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) break;
            continue;
        }
        if (CMargTest (argv[argPos], "-D", "--descending")) {
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) {
                CMmsgPrint(CMmsgUsrError, "Missing selection mode!");
                if (fields != (CMDtblSortField *) NULL) free(fields);
                return (CMfailed);
            }
            if ((fields = (CMDtblSortField *) realloc(fields, (fieldNum + 1) * sizeof(CMDtblSortField))) ==
                (CMDtblSortField *) NULL) {
                CMmsgPrint(CMmsgSysError, "Memory reallocation error in: %s, %n", __FILE__, __LINE__);
                return (CMfailed);
            }
            fields[fieldNum].FieldName(argv[argPos]);
            fields[fieldNum].Ascending(false);
            fieldNum++;
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) break;
            continue;
        }
        if (CMargTest (argv[argPos], "-t", "--title")) {
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) {
                CMmsgPrint(CMmsgUsrError, "Missing title!");
                return (CMfailed);
            }
            title = argv[argPos];
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) break;
            continue;
        }
        if (CMargTest (argv[argPos], "-u", "--subject")) {
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) {
                CMmsgPrint(CMmsgUsrError, "Missing subject!");
                return (CMfailed);
            }
            subject = argv[argPos];
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) break;
            continue;
        }
        if (CMargTest (argv[argPos], "-d", "--domain")) {
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) {
                CMmsgPrint(CMmsgUsrError, "Missing domain!");
                return (CMfailed);
            }
            domain = argv[argPos];
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) break;
            continue;
        }
        if (CMargTest (argv[argPos], "-v", "--version")) {
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) {
                CMmsgPrint(CMmsgUsrError, "Missing version!");
                return (CMfailed);
            }
            version = argv[argPos];
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) break;
            continue;
        }
        if (CMargTest (argv[argPos], "-V", "--verbose")) {
            verbose = true;
            if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) break;
            continue;
        }
        if (CMargTest (argv[argPos], "-h", "--help")) {
            CMmsgPrint(CMmsgInfo, "%s [options] <input file> <output file>", CMfileName(argv[0]));
            CMmsgPrint(CMmsgInfo, "     -a,--table      [ table name ]");
            CMmsgPrint(CMmsgInfo, "     -A,--ascending  [ fieldname ]");
            CMmsgPrint(CMmsgInfo, "     -D,--descending [ fieldname ]");
            CMmsgPrint(CMmsgInfo, "     -t,--title      [dataset title]");
            CMmsgPrint(CMmsgInfo, "     -u,--subject    [subject]");
            CMmsgPrint(CMmsgInfo, "     -d,--domain     [domain]");
            CMmsgPrint(CMmsgInfo, "     -v,--version    [version]");
            CMmsgPrint(CMmsgInfo, "     -V,--verbose");
            CMmsgPrint(CMmsgInfo, "     -h,--help");
            if (fields != (CMDtblSortField *) NULL) free(fields);
            return (DBSuccess);
        }
        if ((argv[argPos][0] == '-') && ((int) strlen(argv[argPos]) > 1)) {
            CMmsgPrint(CMmsgUsrError, "Unknown option: %s!", argv[argPos]);
            if (fields != (CMDtblSortField *) NULL) free(fields);
            return (CMfailed);
        }
        argPos++;
    }

    if (argNum > 3) {
        CMmsgPrint(CMmsgUsrError, "Extra arguments!");
        if (fields != (CMDtblSortField *) NULL) free(fields);
        return (CMfailed);
    }
    if (verbose) RGlibPauseOpen(argv[0]);


    data = new DBObjData();
    if (((argNum > 1) && (strcmp(argv[1], "-") != 0) ? data->Read(argv[1]) : data->Read(stdin)) == DBFault) {
        if (fields != (CMDtblSortField *) NULL)free(fields);
        delete data;
        return (CMfailed);
    }

    if (title != (char *) NULL) data->Name(title);
    if (subject != (char *) NULL) data->Document(DBDocSubject, subject);
    if (domain != (char *) NULL) data->Document(DBDocGeoDomain, domain);
    if (version != (char *) NULL) data->Document(DBDocVersion, version);

    if (tableName == (char *) NULL) tableName = DBrNItems;

    if ((table = data->Table(tableName)) == (DBObjTable *) NULL) {
        CMmsgPrint(CMmsgUsrError, "Invalid table!");
        if (fields != (CMDtblSortField *) NULL) free(fields);
        delete data;
        return (CMfailed);
    }

    if (fields != (CMDtblSortField *) NULL) {
        DBInt fieldID;
        fieldList = new DBObjectLIST<DBObjTableField>("Fields");
        for (fieldID = 0; fieldID < fieldNum; ++fieldID) {
            if ((fieldPTR = table->Field(fields[fieldID].FieldName())) == (DBObjTableField *) NULL) {
                CMmsgPrint(CMmsgUsrError, "Invalid field: %s!", fields[fieldID].FieldName());
                free(fields);
                return (CMfailed);
            }
            if (fields[fieldID].Ascending())
                fieldPTR->Flags(DBObjectFlagSortReversed, DBClear);
            else fieldPTR->Flags(DBObjectFlagSortReversed, DBSet);
            fieldList->Add(new DBObjTableField(*fieldPTR));
        }
        table->ListSort(fieldList);
        table->ItemSort();
        delete fieldList;
        free(fields);
    }
    ret = (argNum > 2) && (strcmp(argv[2], "-") != 0) ? data->Write(argv[2]) : data->Write(stdout);

    delete data;
    if (verbose) RGlibPauseClose();
    return (ret);
}
コード例 #4
0
ファイル: RGlibPoint.C プロジェクト: bmfekete/RGIS
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);
}