void checkStream( QDataStream &stream ) { if ( stream.status() != QDataStream::Ok ) { if ( cf != -1 ) { Rast_unopen( cf ); G_fatal_error( "Cannot read data stream" ); } } }
/* ************************************************************************* */ void fatal_error(void *map, int *fd, int depths, char *errorMsg) { int i; /* Close files and exit */ if (map != NULL) { if (!Rast3d_close(map)) Rast3d_fatal_error(_("Unable to close 3D raster map")); } if (fd != NULL) { for (i = 0; i < depths; i++) Rast_unopen(fd[i]); } Rast3d_fatal_error("%s", errorMsg); exit(EXIT_FAILURE); }
/* ************************************************************************* */ void fatal_error(void *map, int elevfd, int outfd, char *errorMsg) { /* Close files and exit */ if (map != NULL) { if (!Rast3d_close(map)) Rast3d_fatal_error(_("Unable to close 3D raster map")); } /*unopen the output map */ if (outfd != -1) Rast_unopen(outfd); if (elevfd != -1) close_output_map(elevfd); Rast3d_fatal_error("%s", errorMsg); exit(EXIT_FAILURE); }
int main( int argc, char **argv ) { char *name = nullptr; struct Option *map; struct Cell_head window; G_gisinit( argv[0] ); G_define_module(); map = G_define_standard_option( G_OPT_R_OUTPUT ); if ( G_parser( argc, argv ) ) exit( EXIT_FAILURE ); name = map->answer; #ifdef Q_OS_WIN _setmode( _fileno( stdin ), _O_BINARY ); _setmode( _fileno( stdout ), _O_BINARY ); //setvbuf( stdin, NULL, _IONBF, BUFSIZ ); // setting _IONBF on stdout works on windows correctly, data written immediately even without fflush(stdout) //setvbuf( stdout, NULL, _IONBF, BUFSIZ ); #endif QgsGrassDataFile stdinFile; stdinFile.open( stdin ); QDataStream stdinStream( &stdinFile ); QFile stdoutFile; stdoutFile.open( stdout, QIODevice::WriteOnly | QIODevice::Unbuffered ); QDataStream stdoutStream( &stdoutFile ); qint32 proj, zone; stdinStream >> proj >> zone; QgsRectangle extent; qint32 rows, cols; stdinStream >> extent >> cols >> rows; checkStream( stdinStream ); QString err = QgsGrass::setRegion( &window, extent, rows, cols ); if ( !err.isEmpty() ) { G_fatal_error( "Cannot set region: %s", err.toUtf8().constData() ); } window.proj = ( int ) proj; window.zone = ( int ) zone; G_set_window( &window ); Qgis::DataType qgis_type; qint32 type; stdinStream >> type; checkStream( stdinStream ); qgis_type = ( Qgis::DataType )type; RASTER_MAP_TYPE grass_type; switch ( qgis_type ) { case Qgis::Int32: grass_type = CELL_TYPE; break; case Qgis::Float32: grass_type = FCELL_TYPE; break; case Qgis::Float64: grass_type = DCELL_TYPE; break; default: G_fatal_error( "QGIS data type %d not supported", qgis_type ); return 1; } cf = Rast_open_new( name, grass_type ); if ( cf < 0 ) { G_fatal_error( "Unable to create raster map <%s>", name ); return 1; } void *buf = Rast_allocate_buf( grass_type ); int expectedSize = cols * QgsRasterBlock::typeSize( qgis_type ); bool isCanceled = false; QByteArray byteArray; for ( int row = 0; row < rows; row++ ) { stdinStream >> isCanceled; checkStream( stdinStream ); if ( isCanceled ) { break; } double noDataValue; stdinStream >> noDataValue; stdinStream >> byteArray; checkStream( stdinStream ); if ( byteArray.size() != expectedSize ) { G_fatal_error( "Wrong byte array size, expected %d bytes, got %d, row %d / %d", expectedSize, byteArray.size(), row, rows ); return 1; } qint32 *cell = nullptr; float *fcell = nullptr; double *dcell = nullptr; if ( grass_type == CELL_TYPE ) cell = ( qint32 * ) byteArray.data(); else if ( grass_type == FCELL_TYPE ) fcell = ( float * ) byteArray.data(); else if ( grass_type == DCELL_TYPE ) dcell = ( double * ) byteArray.data(); void *ptr = buf; for ( int col = 0; col < cols; col++ ) { if ( grass_type == CELL_TYPE ) { if ( ( CELL )cell[col] == ( CELL )noDataValue ) { Rast_set_c_null_value( ( CELL * )ptr, 1 ); } else { Rast_set_c_value( ptr, ( CELL )( cell[col] ), grass_type ); } } else if ( grass_type == FCELL_TYPE ) { if ( ( FCELL )fcell[col] == ( FCELL )noDataValue ) { Rast_set_f_null_value( ( FCELL * )ptr, 1 ); } else { Rast_set_f_value( ptr, ( FCELL )( fcell[col] ), grass_type ); } } else if ( grass_type == DCELL_TYPE ) { if ( ( DCELL )dcell[col] == ( DCELL )noDataValue ) { Rast_set_d_null_value( ( DCELL * )ptr, 1 ); } else { Rast_set_d_value( ptr, ( DCELL )dcell[col], grass_type ); } } ptr = G_incr_void_ptr( ptr, Rast_cell_size( grass_type ) ); } Rast_put_row( cf, buf, grass_type ); #ifndef Q_OS_WIN // Because stdin is somewhere buffered on Windows (not clear if in QProcess or by Windows) // we cannot in QgsGrassImport wait for this because it hangs. Setting _IONBF on stdin does not help // and there is no flush() on QProcess. // OTOH, smaller stdin buffer is probably blocking QgsGrassImport so that the import can be canceled immediately. stdoutStream << ( bool )true; // row written stdoutFile.flush(); #endif } if ( isCanceled ) { Rast_unopen( cf ); } else { Rast_close( cf ); struct History history; Rast_short_history( name, "raster", &history ); Rast_command_history( &history ); Rast_write_history( name, &history ); } exit( EXIT_SUCCESS ); }
int main(int argc, char *argv[]) { int i, j; /* Loop control variables */ int bands; /* Number of image bands */ double *mu; /* Mean vector for image bands */ double **covar; /* Covariance Matrix */ double *eigval; double **eigmat; int *inp_fd; int scale, scale_max, scale_min; struct GModule *module; struct Option *opt_in, *opt_out, *opt_scale; /* initialize GIS engine */ G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("imagery")); G_add_keyword(_("image transformation")); G_add_keyword(_("PCA")); module->description = _("Principal components analysis (PCA) " "for image processing."); /* Define options */ opt_in = G_define_standard_option(G_OPT_R_INPUTS); opt_in->description = _("Name of two or more input raster maps"); opt_out = G_define_option(); opt_out->label = _("Base name for output raster maps"); opt_out->description = _("A numerical suffix will be added for each component map"); opt_out->key = "output_prefix"; opt_out->type = TYPE_STRING; opt_out->key_desc = "string"; opt_out->required = YES; opt_scale = G_define_option(); opt_scale->key = "rescale"; opt_scale->type = TYPE_INTEGER; opt_scale->key_desc = "min,max"; opt_scale->required = NO; opt_scale->answer = "0,255"; opt_scale->label = _("Rescaling range for output maps"); opt_scale->description = _("For no rescaling use 0,0"); opt_scale->guisection = _("Rescale"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); /* determine number of bands passed in */ for (bands = 0; opt_in->answers[bands] != NULL; bands++) ; if (bands < 2) G_fatal_error(_("Sorry, at least 2 input bands must be provided")); /* default values */ scale = 1; scale_min = 0; scale_max = 255; /* get scale parameters */ set_output_scale(opt_scale, &scale, &scale_min, &scale_max); /* allocate memory */ covar = G_alloc_matrix(bands, bands); mu = G_alloc_vector(bands); inp_fd = G_alloc_ivector(bands); eigmat = G_alloc_matrix(bands, bands); eigval = G_alloc_vector(bands); /* open and check input/output files */ for (i = 0; i < bands; i++) { char tmpbuf[128]; sprintf(tmpbuf, "%s.%d", opt_out->answer, i + 1); G_check_input_output_name(opt_in->answers[i], tmpbuf, GR_FATAL_EXIT); inp_fd[i] = Rast_open_old(opt_in->answers[i], ""); } G_verbose_message(_("Calculating covariance matrix...")); calc_mu(inp_fd, mu, bands); calc_covariance(inp_fd, covar, mu, bands); for (i = 0; i < bands; i++) { for (j = 0; j < bands; j++) { covar[i][j] = covar[i][j] / ((double)((Rast_window_rows() * Rast_window_cols()) - 1)); G_debug(3, "covar[%d][%d] = %f", i, j, covar[i][j]); } } G_math_d_copy(covar[0], eigmat[0], bands*bands); G_debug(1, "Calculating eigenvalues and eigenvectors..."); G_math_eigen(eigmat, eigval, bands); #ifdef PCA_DEBUG /* dump eigen matrix and eigen values */ dump_eigen(bands, eigmat, eigval); #endif G_debug(1, "Ordering eigenvalues in descending order..."); G_math_egvorder(eigval, eigmat, bands); G_debug(1, "Transposing eigen matrix..."); G_math_d_A_T(eigmat, bands); /* write output images */ write_pca(eigmat, inp_fd, opt_out->answer, bands, scale, scale_min, scale_max); /* write colors and history to output */ for (i = 0; i < bands; i++) { char outname[80]; sprintf(outname, "%s.%d", opt_out->answer, i + 1); /* write colors and history to file */ write_support(bands, outname, eigmat, eigval); /* close output file */ Rast_unopen(inp_fd[i]); } /* free memory */ G_free_matrix(covar); G_free_vector(mu); G_free_ivector(inp_fd); G_free_matrix(eigmat); G_free_vector(eigval); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { char *input; char *output; char *title; char *temp; FILE *fd, *ft; int cf, direction, sz; struct Cell_head cellhd; struct History history; void *rast, *rast_ptr; int row, col; int nrows, ncols; double x; char y[128]; struct GModule *module; struct { struct Option *input, *output, *title, *mult, *nv, *type; } parm; struct { struct Flag *s; } flag; char *null_val_str; DCELL mult; RASTER_MAP_TYPE data_type; double atof(); G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("raster")); G_add_keyword(_("import")); G_add_keyword(_("conversion")); G_add_keyword("ASCII"); module->description = _("Converts a GRASS ASCII raster file to binary raster map."); parm.input = G_define_standard_option(G_OPT_F_INPUT); parm.input->label = _("Name of input file to be imported"); parm.input->description = _("'-' for standard input"); parm.output = G_define_standard_option(G_OPT_R_OUTPUT); parm.type = G_define_option(); parm.type->key = "type"; parm.type->type = TYPE_STRING; parm.type->required = NO; parm.type->options = "CELL,FCELL,DCELL"; parm.type->label = _("Storage type for resultant raster map"); parm.type->description = _("Default: CELL for integer values, DCELL for floating-point values"); parm.title = G_define_option(); parm.title->key = "title"; parm.title->key_desc = "phrase"; parm.title->type = TYPE_STRING; parm.title->required = NO; parm.title->description = _("Title for resultant raster map"); parm.mult = G_define_option(); parm.mult->key = "multiplier"; parm.mult->type = TYPE_DOUBLE; parm.mult->description = _("Default: read from header"); parm.mult->required = NO; parm.mult->label = _("Multiplier for ASCII data"); parm.nv = G_define_standard_option(G_OPT_M_NULL_VALUE); parm.nv->description = _("Default: read from header"); parm.nv->label = _("String representing NULL value data cell"); parm.nv->guisection = _("NULL data"); flag.s = G_define_flag(); flag.s->key = 's'; flag.s->description = _("SURFER (Golden Software) ASCII file will be imported"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); input = parm.input->answer; output = parm.output->answer; temp = G_tempfile(); ft = fopen(temp, "w+"); if (ft == NULL) G_fatal_error(_("Unable to open temporary file <%s>"), temp); if ((title = parm.title->answer)) G_strip(title); if (!parm.mult->answer) Rast_set_d_null_value(&mult, 1); else if ((sscanf(parm.mult->answer, "%lf", &mult)) != 1) G_fatal_error(_("Wrong entry for multiplier: %s"), parm.mult->answer); null_val_str = parm.nv->answer; data_type = -1; if (parm.type->answer) { switch(parm.type->answer[0]) { case 'C': data_type = CELL_TYPE; break; case 'F': data_type = FCELL_TYPE; break; case 'D': data_type = DCELL_TYPE; break; } } if (strcmp(input, "-") == 0) { Tmp_file = G_tempfile(); if (NULL == (Tmp_fd = fopen(Tmp_file, "w+"))) G_fatal_error(_("Unable to open temporary file <%s>"), Tmp_file); unlink(Tmp_file); if (0 > file_cpy(stdin, Tmp_fd)) G_fatal_error(_("Unable to read input from stdin")); fd = Tmp_fd; } else fd = fopen(input, "r"); if (fd == NULL) { G_fatal_error(_("Unable to read input from <%s>"), input); } direction = 1; sz = 0; if (flag.s->answer) { sz = getgrdhead(fd, &cellhd); /* for Surfer files, the data type is always FCELL_TYPE, the multiplier and the null_val_str are never used */ data_type = FCELL_TYPE; mult = 1.; null_val_str = ""; /* rows in surfer files are ordered from bottom to top, opposite of normal GRASS ordering */ direction = -1; } else sz = gethead(fd, &cellhd, &data_type, &mult, &null_val_str); if (!sz) G_fatal_error(_("Can't get cell header")); nrows = cellhd.rows; ncols = cellhd.cols; Rast_set_window(&cellhd); if (nrows != Rast_window_rows()) G_fatal_error(_("OOPS: rows changed from %d to %d"), nrows, Rast_window_rows()); if (ncols != Rast_window_cols()) G_fatal_error(_("OOPS: cols changed from %d to %d"), ncols, Rast_window_cols()); rast_ptr = Rast_allocate_buf(data_type); rast = rast_ptr; cf = Rast_open_new(output, data_type); for (row = 0; row < nrows; row++) { G_percent(row, nrows, 2); for (col = 0; col < ncols; col++) { if (fscanf(fd, "%s", y) != 1) { Rast_unopen(cf); G_fatal_error(_("Data conversion failed at row %d, col %d"), row + 1, col + 1); } if (strcmp(y, null_val_str)) { x = atof(y); if ((float)x == GS_BLANK) { Rast_set_null_value(rast_ptr, 1, data_type); } else { Rast_set_d_value(rast_ptr, (DCELL) (x * mult), data_type); } } else { Rast_set_null_value(rast_ptr, 1, data_type); } rast_ptr = G_incr_void_ptr(rast_ptr, Rast_cell_size(data_type)); } fwrite(rast, Rast_cell_size(data_type), ncols, ft); rast_ptr = rast; } G_percent(nrows, nrows, 2); G_debug(1, "Creating support files for %s", output); sz = 0; if (direction < 0) { sz = -ncols * Rast_cell_size(data_type); G_fseek(ft, sz, SEEK_END); sz *= 2; } else { G_fseek(ft, 0L, SEEK_SET); } for (row = 0; row < nrows; row += 1) { fread(rast, Rast_cell_size(data_type), ncols, ft); Rast_put_row(cf, rast, data_type); G_fseek(ft, sz, SEEK_CUR); } fclose(ft); unlink(temp); Rast_close(cf); if (title) Rast_put_cell_title(output, title); Rast_short_history(output, "raster", &history); Rast_command_history(&history); Rast_write_history(output, &history); G_done_msg(" "); exit(EXIT_SUCCESS); }
int vect_to_rast(const char *vector_map, const char *raster_map, const char *field_name, const char *column, int cache_mb, int use, double value, int value_type, const char *rgbcolumn, const char *labelcolumn, int ftype, char *where, char *cats, int dense) { struct Map_info Map; struct line_pnts *Points; int i, field; struct cat_list *cat_list = NULL; int fd; /* for raster map */ int nareas, nlines; /* number of converted features */ int nareas_all, nplines_all; /* number of all areas, points/lines */ int stat; int format; int pass, npasses; /* Attributes */ int nrec; int ctype = 0; struct field_info *Fi; dbDriver *Driver; dbCatValArray cvarr; int is_fp = 0; nareas = 0; G_verbose_message(_("Loading data...")); Vect_set_open_level(2); if (Vect_open_old2(&Map, vector_map, "", field_name) < 0) G_fatal_error(_("Unable to open vector map <%s>"), vector_map); field = Vect_get_field_number(&Map, field_name); if (field > 0) cat_list = Vect_cats_set_constraint(&Map, field, where, cats); if ((use == USE_Z) && !(Vect_is_3d(&Map))) G_fatal_error(_("Vector map <%s> is not 3D"), Vect_get_full_name(&Map)); switch (use) { case USE_ATTR: db_CatValArray_init(&cvarr); if (!(Fi = Vect_get_field(&Map, field))) G_fatal_error(_("Database connection not defined for layer <%s>"), field_name); if ((Driver = db_start_driver_open_database(Fi->driver, Fi->database)) == NULL) G_fatal_error(_("Unable to open database <%s> by driver <%s>"), Fi->database, Fi->driver); db_set_error_handler_driver(Driver); /* Note do not check if the column exists in the table because it may be expression */ if ((nrec = db_select_CatValArray(Driver, Fi->table, Fi->key, column, NULL, &cvarr)) == -1) G_fatal_error(_("Column <%s> not found"), column); G_debug(3, "nrec = %d", nrec); ctype = cvarr.ctype; if (ctype != DB_C_TYPE_INT && ctype != DB_C_TYPE_DOUBLE) G_fatal_error(_("Column type (%s) not supported (did you mean 'labelcolumn'?)"), db_sqltype_name(ctype)); if (nrec < 0) G_fatal_error(_("No records selected from table <%s>"), Fi->table); G_debug(1, "%d records selected from table", nrec); db_close_database_shutdown_driver(Driver); for (i = 0; i < cvarr.n_values; i++) { if (ctype == DB_C_TYPE_INT) { G_debug(3, "cat = %d val = %d", cvarr.value[i].cat, cvarr.value[i].val.i); } else if (ctype == DB_C_TYPE_DOUBLE) { G_debug(3, "cat = %d val = %f", cvarr.value[i].cat, cvarr.value[i].val.d); } } switch (ctype) { case DB_C_TYPE_INT: format = CELL_TYPE; break; case DB_C_TYPE_DOUBLE: format = DCELL_TYPE; break; default: G_fatal_error(_("Unable to use column <%s>"), column); break; } break; case USE_CAT: format = CELL_TYPE; break; case USE_VAL: format = value_type; break; case USE_Z: format = DCELL_TYPE; is_fp = 1; break; case USE_D: format = DCELL_TYPE; break; default: G_fatal_error(_("Unknown use type: %d"), use); } fd = Rast_open_new(raster_map, format); Points = Vect_new_line_struct(); if (use != USE_Z && use != USE_D && (ftype & GV_AREA)) { if ((nareas = sort_areas(&Map, Points, field, cat_list)) == 0) G_warning(_("No areas selected from vector map <%s>"), vector_map); G_debug(1, "%d areas sorted", nareas); } if (nareas > 0 && dense) { G_warning(_("Area conversion and line densification are mutually exclusive, disabling line densification.")); dense = 0; } nlines = Vect_get_num_primitives(&Map, ftype); nplines_all = nlines; npasses = begin_rasterization(cache_mb, format, dense); pass = 0; nareas_all = Vect_get_num_areas(&Map); do { pass++; if (npasses > 1) G_message(_("Pass %d of %d:"), pass, npasses); stat = 0; if ((use != USE_Z && use != USE_D) && nareas) { if (do_areas (&Map, Points, &cvarr, ctype, use, value, value_type) < 0) { G_warning(_("Problem processing areas from vector map <%s>, continuing..."), vector_map); stat = -1; break; } } if (nlines) { if ((nlines = do_lines(&Map, Points, &cvarr, ctype, field, cat_list, use, value, value_type, ftype, &nplines_all, dense)) < 0) { G_warning(_("Problem processing lines from vector map <%s>, continuing..."), vector_map); stat = -1; break; } } G_important_message(_("Writing raster map...")); stat = output_raster(fd); } while (stat == 0); G_suppress_warnings(0); /* stat: 0 == repeat; 1 == done; -1 == error; */ Vect_destroy_line_struct(Points); if (stat < 0) { Rast_unopen(fd); return 1; } Vect_close(&Map); G_verbose_message(_("Creating support files for raster map...")); Rast_close(fd); update_hist(raster_map, vector_map, Map.head.orig_scale); /* colors */ if (rgbcolumn) { if (use != USE_ATTR && use != USE_CAT) { G_warning(_("Color can be updated from database only if use=attr")); update_colors(raster_map); } else { update_dbcolors(raster_map, vector_map, field, rgbcolumn, is_fp, column); } } else if (use == USE_D) update_fcolors(raster_map); else update_colors(raster_map); update_cats(raster_map); /* labels */ update_labels(raster_map, vector_map, field, labelcolumn, use, value, column); #if 0 /* maximum possible numer of areas: number of centroids * actual number of areas, currently unknown: * number of areas with centroid that are within cat constraint * and overlap with current region */ if (nareas_all > 0) G_message(_("Converted areas: %d of %d"), nareas, Vect_get_num_primitives(&Map, GV_CENTROID)); /* maximum possible numer of lines: number of GV_LINE + GV_POINT * actual number of lines, currently unknown: * number of lines are within cat constraint * and overlap with current region */ if (nlines > 0 && nplines_all > 0) G_message(_("Converted points/lines: %d of %d"), nlines, nplines_all); #endif return 0; }