Changeset 63378


Ignore:
Timestamp:
Dec 5, 2014, 8:26:14 AM (10 years ago)
Author:
mmetz
Message:

r.in.gdal: fix RAT import, do #2507

File:
1 edited

Legend:

Unmodified
Added
Removed
  • grass/trunk/raster/r.in.gdal/main.c

    r63019 r63378  
    6262    GDALRasterBandH hBand;
    6363    double adfGeoTransform[6];
     64    int n_bands;
    6465    int force_imagery = FALSE;
    6566    char error_msg[8096];
     
    9899    parm.band->key = "band";
    99100    parm.band->type = TYPE_INTEGER;
     101    parm.band->multiple = YES;
    100102    parm.band->required = NO;
    101     parm.band->description = _("Band to select (default is all bands)");
     103    parm.band->description = _("Band(s) to select (default is all bands)");
    102104
    103105    parm.memory = G_define_option();
     
    507509    /*      Do we want to generate a simple raster, or an imagery group?    */
    508510    /* -------------------------------------------------------------------- */
    509     if ((GDALGetRasterCount(hDS) > 1 && parm.band->answer == NULL)
     511    n_bands = 0;
     512    if (parm.band->answer != NULL) {
     513        while (parm.band->answers[n_bands])
     514            n_bands++;
     515    }
     516
     517    if ((GDALGetRasterCount(hDS) > 1 && n_bands != 1)
    510518        || GDALGetGCPCount(hDS) > 0)
    511519        force_imagery = TRUE;
     
    518526
    519527        if (parm.band->answer != NULL)
    520             nBand = atoi(parm.band->answer);
     528            nBand = atoi(parm.band->answers[0]);
    521529
    522530        hBand = GDALGetRasterBand(hDS, nBand);
     
    540548        struct Ref ref;
    541549        char szBandName[512];
    542         int nBand;
     550        int nBand = 0;
    543551        char colornamebuf[512], colornamebuf2[512];
    544552
     
    546554
    547555        colornamebuf2[0] = '\0';
    548         for (nBand = 1; nBand <= GDALGetRasterCount(hDS); nBand++) {
     556
     557        n_bands = 0;
     558        while (TRUE) {
     559            if (parm.band->answer != NULL) {
     560                if (parm.band->answers[n_bands] == NULL)
     561                    break;
     562                nBand = atoi(parm.band->answers[n_bands++]);
     563            }
     564            else {
     565                if (nBand >= GDALGetRasterCount(hDS))
     566                    break;
     567                nBand++;
     568            }
     569
    549570            G_debug(3, "Import raster band %d", nBand);
    550571            hBand = GDALGetRasterBand(hDS, nBand);
     
    11451166            struct Colors colors;
    11461167            DCELL val1, val2;
    1147             int r1, g1, b1, r2, g2, b2;
     1168            double r1, g1, b1, r2, g2, b2;
     1169            int cf;
    11481170
    11491171            Rast_init_colors(&colors);
     
    11801202                    bmaxc = indx;
    11811203            }
     1204           
     1205            /* guess color range 0, 1 or 0, 255 */
    11821206
    11831207            if (minc >= 0 && maxc >= 0 && rminc >= 0 && rmaxc >= 0 &&
    11841208                gminc >= 0 && gmaxc >= 0 && bminc >= 0 && bmaxc >= 0) {
     1209
     1210                cf = 1;
    11851211               
    1186                 /* fetch color rules */
     1212                /* analyze color rules */
    11871213                for (indx = 0; indx < nrows; indx++) {
    11881214                    val1 = GDALRATGetValueAsDouble(gdal_rat, indx, minc);
     
    11971223                    b2 = GDALRATGetValueAsDouble(gdal_rat, indx, bmaxc);
    11981224
    1199                     Rast_add_d_color_rule(&val1, r1, g1, b1,
    1200                                           &val2, r2, g2, b2,
    1201                                           &colors);
     1225                    if (r1 > 0.0 && r1 < 1.0)
     1226                        cf = 255;
     1227                    else if (cf == 255 && r1 > 1.0) {
     1228                        cf = 0;
     1229                        break;
     1230                    }
     1231
     1232                    if (g1 > 0.0 && g1 < 1.0)
     1233                        cf = 255;
     1234                    else if (cf == 255 && g1 > 1.0) {
     1235                        cf = 0;
     1236                        break;
     1237                    }
     1238
     1239                    if (b1 > 0.0 && b1 < 1.0)
     1240                        cf = 255;
     1241                    else if (cf == 255 && b1 > 1.0) {
     1242                        cf = 0;
     1243                        break;
     1244                    }
     1245
     1246                    if (r2 > 0.0 && r2 < 1.0)
     1247                        cf = 255;
     1248                    else if (cf == 255 && r2 > 1.0) {
     1249                        cf = 0;
     1250                        break;
     1251                    }
     1252
     1253                    if (g2 > 0.0 && g2 < 1.0)
     1254                        cf = 255;
     1255                    else if (cf == 255 && g2 > 1.0) {
     1256                        cf = 0;
     1257                        break;
     1258                    }
     1259
     1260                    if (b2 > 0.0 && b2 < 1.0)
     1261                        cf = 255;
     1262                    else if (cf == 255 && b2 > 1.0) {
     1263                        cf = 0;
     1264                        break;
     1265                    }
     1266                }
     1267
     1268                if (cf == 0)
     1269                    G_warning(_("Inconsistent color rules in RAT"));
     1270                else {
     1271                    /* fetch color rules */
     1272                    for (indx = 0; indx < nrows; indx++) {
     1273                        val1 = GDALRATGetValueAsDouble(gdal_rat, indx, minc);
     1274                        val2 = GDALRATGetValueAsDouble(gdal_rat, indx, maxc);
     1275
     1276                        r1 = GDALRATGetValueAsDouble(gdal_rat, indx, rminc);
     1277                        g1 = GDALRATGetValueAsDouble(gdal_rat, indx, gminc);
     1278                        b1 = GDALRATGetValueAsDouble(gdal_rat, indx, bminc);
     1279
     1280                        r2 = GDALRATGetValueAsDouble(gdal_rat, indx, rmaxc);
     1281                        g2 = GDALRATGetValueAsDouble(gdal_rat, indx, gmaxc);
     1282                        b2 = GDALRATGetValueAsDouble(gdal_rat, indx, bmaxc);
     1283
     1284                        Rast_add_d_color_rule(&val1, r1 * cf, g1 * cf, b1 * cf,
     1285                                              &val2, r2 * cf, g2 * cf, b2 * cf,
     1286                                              &colors);
     1287                    }
    12021288                }
    12031289            }
    12041290            else if (minmaxc >= 0 && rc >= 0 && gc >= 0 && bc >= 0) {
    1205                    
    1206                 if (minc < 0)
    1207                     minc = maxc;
    1208 
    1209                 /* fetch color table */
     1291               
     1292                cf = 1;
     1293
     1294                /* analyze color table */
    12101295                for (indx = 0; indx < nrows; indx++) {
    12111296                    val1 = GDALRATGetValueAsDouble(gdal_rat, indx, minmaxc);
     
    12141299                    g1 = GDALRATGetValueAsDouble(gdal_rat, indx, gc);
    12151300                    b1 = GDALRATGetValueAsDouble(gdal_rat, indx, bc);
    1216                    
    1217                     Rast_set_d_color(val1, r1, g1, b1, &colors);
     1301
     1302
     1303                    if (r1 > 0.0 && r1 < 1.0)
     1304                        cf = 255;
     1305                    else if (cf == 255 && r1 > 1.0) {
     1306                        cf = 0;
     1307                        break;
     1308                    }
     1309
     1310                    if (g1 > 0.0 && g1 < 1.0)
     1311                        cf = 255;
     1312                    else if (cf == 255 && g1 > 1.0) {
     1313                        cf = 0;
     1314                        break;
     1315                    }
     1316
     1317                    if (b1 > 0.0 && b1 < 1.0)
     1318                        cf = 255;
     1319                    else if (cf == 255 && b1 > 1.0) {
     1320                        cf = 0;
     1321                        break;
     1322                    }
     1323                }
     1324
     1325                if (cf == 0)
     1326                    G_warning(_("Inconsistent color rules in RAT"));
     1327                else {
     1328                    /* fetch color table */
     1329                    for (indx = 0; indx < nrows; indx++) {
     1330                        val1 = GDALRATGetValueAsDouble(gdal_rat, indx, minmaxc);
     1331
     1332                        r1 = GDALRATGetValueAsDouble(gdal_rat, indx, rc);
     1333                        g1 = GDALRATGetValueAsDouble(gdal_rat, indx, gc);
     1334                        b1 = GDALRATGetValueAsDouble(gdal_rat, indx, bc);
     1335                       
     1336                        Rast_set_d_color(val1, r1 * cf, g1 * cf, b1 * cf, &colors);
     1337                    }
    12181338                }
    12191339            }
Note: See TracChangeset for help on using the changeset viewer.