Esempio n. 1
0
void p_nearest(struct cache *ibuffer,	/* input buffer                  */
	       void *obufptr,	/* ptr in output buffer          */
	       int cell_type,	/* raster map type of obufptr    */
	       double *col_idx,	/* column index in input matrix  */
	       double *row_idx,	/* row index in input matrix     */
	       struct Cell_head *cellhd	/* cell header of input layer    */
    )
{
    int row, col;		/* row/col of nearest neighbor   */
    FCELL *cellp;

    /* cut indices to integer */
    row = (int)floor(*row_idx);
    col = (int)floor(*col_idx);

    /* check for out of bounds - if out of bounds set NULL value     */
    if (row < 0 || row >= cellhd->rows || col < 0 || col >= cellhd->cols) {
	G_set_null_value(obufptr, 1, cell_type);
	return;
    }

    cellp = CPTR(ibuffer, row, col);

    if (G_is_f_null_value(cellp)) {
	G_set_null_value(obufptr, 1, cell_type);
	return;
    }

    G_set_raster_value_f(obufptr, *cellp, cell_type);
}
Esempio n. 2
0
int G3d_isNullValueNum(const void *n, int type)
{
    if (type == FCELL_TYPE)
	return G_is_f_null_value(n);
    else
	return G_is_d_null_value(n);
}
Esempio n. 3
0
/*!
   \brief Load raster map as floating point map

   Calling function must have already allocated space in buff for
   wind->rows * wind->cols floats.

   This routine simply loads the map into a 2d array by repetitve calls
   to get_f_raster_row.

   \param wind current window
   \param map_name raster map name
   \param[out] buff data buffer
   \param[out] nullmap null map buffer
   \param[out] has_null indicates if raster map contains null-data

   \return 1 on success
   \return 0 on failure
 */
int Gs_loadmap_as_float(struct Cell_head *wind, const char *map_name,
			float *buff, struct BM *nullmap, int *has_null)
{
    FILEDESC cellfile;
    const char *map_set;
    char *nullflags;
    int offset, row, col;

    G_debug(3, "Gs_loadmap_as_float(): name=%s", map_name);

    map_set = G_find_cell2(map_name, "");
    if (!map_set) {
	G_warning(_("Raster map <%s> not found"), map_name);
	return 0;
    }
    *has_null = 0;

    nullflags = G_allocate_null_buf();	/* G_fatal_error */
    if (!nullflags) {
	G_fatal_error(_("Unable to allocate memory for a null buffer"));
    }

    if ((cellfile = G_open_cell_old(map_name, map_set)) == -1) {
	G_fatal_error(_("Unable to open raster map <%s>"), map_name);
    }

    G_message(_("Loading raster map <%s>..."),
	      G_fully_qualified_name(map_name, map_set));

    for (row = 0; row < wind->rows; row++) {
	offset = row * wind->cols;
	G_get_f_raster_row(cellfile, &(buff[offset]), row);
	G_get_null_value_row(cellfile, nullflags, row);

	G_percent(row, wind->rows, 2);

	for (col = 0; col < wind->cols; col++) {
	    if (nullflags[col] || G_is_f_null_value(buff + offset + col)) {
		*has_null = 1;
		BM_set(nullmap, col, row, 1);
	    }
	    /* set nm */
	}
    }
    G_percent(1, 1, 1);

    G_debug(4, "  has_null=%d", *has_null);

    G_close_cell(cellfile);

    G_free(nullflags);

    return (1);
}
Esempio n. 4
0
int QgsGrassGisLib::G_get_null_value_row( int fd, char *flags, int row )
{
    FCELL *buf = G_allocate_f_raster_buf();
    QgsGrassGisLib::instance()->readRasterRow( fd, buf, row, FCELL_TYPE, false );

    for ( int i = 0; i < mColumns; i++ )
    {
        flags[i] = G_is_f_null_value( &buf[i] ) ? 1 : 0;
    }
    G_free( buf );
    return 1;
}
Esempio n. 5
0
int QgsGrassGisLib::putRasterRow( int fd, const void *buf, RASTER_MAP_TYPE data_type )
{
    Raster rast = mRasters.value( fd );
    if ( rast.row < 0 || rast.row >= mRows )
    {
        QgsDebugMsg( QString( "row %1 out of range 0 - %2" ).arg( rast.row ).arg( mRows ) );
        return -1;
    }

    QGis::DataType inputType = qgisRasterType( data_type );
    //QgsDebugMsg( QString("data_type = %1").arg(data_type) );
    //QgsDebugMsg( QString("inputType = %1").arg(inputType) );
    //QgsDebugMsg( QString("provider->dataType = %1").arg( rast.provider->dataType( rast.band ) ) );

    //double noDataValue = rast.provider->noDataValue( rast.band );
    QgsRasterBlock block( inputType, mColumns, 1, rast.noDataValue );

    memcpy( block.bits( 0 ), buf, QgsRasterBlock::typeSize( inputType )*mColumns );
    block.convert( rast.provider->dataType( rast.band ) );

    // Set no data after converting to output type
    for ( int i = 0; i < mColumns; i++ )
    {
        bool isNoData = false;
        switch ( data_type )
        {
        case CELL_TYPE:
            isNoData = G_is_c_null_value( &(( CELL * ) buf )[i] ) != 0;
            break;
        case FCELL_TYPE:
            isNoData = G_is_f_null_value( &(( FCELL * ) buf )[i] ) != 0;
            break;
        case DCELL_TYPE:
            isNoData = G_is_d_null_value( &(( DCELL * ) buf )[i] ) != 0;
            break;
        default:
            break;
        }
        if ( isNoData )
        {
            block.setIsNoData( i );
        }
    }

    if ( !rast.provider->write( block.bits( 0 ), rast.band, mColumns, 1, 0, rast.row ) )
    {
        fatal( "Cannot write block" );
    }
    mRasters[fd].row++;

    return 1;
}
Esempio n. 6
0
void p_bilinear(struct cache *ibuffer,	/* input buffer                  */
		void *obufptr,	/* ptr in output buffer          */
		int cell_type,	/* raster map type of obufptr    */
		double *col_idx,	/* column index          */
		double *row_idx,	/* row index                     */
		struct Cell_head *cellhd	/* information of output map     */
    )
{
    int row;			/* row indices for interp        */
    int col;			/* column indices for interp     */
    int i, j;
    FCELL t, u;			/* intermediate slope            */
    FCELL result;		/* result of interpolation       */
    FCELL c[2][2];

    /* cut indices to integer */
    row = (int)floor(*row_idx - 0.5);
    col = (int)floor(*col_idx - 0.5);

    /* check for out of bounds - if out of bounds set NULL value and return */
    if (row < 0 || row + 1 >= cellhd->rows || col < 0 || col + 1 >= cellhd->cols) {
	G_set_null_value(obufptr, 1, cell_type);
	return;
    }

    for (i = 0; i < 2; i++)
	for (j = 0; j < 2; j++) {
	    const FCELL *cellp = CPTR(ibuffer, row + i, col + j);
	    if (G_is_f_null_value(cellp)) {
		G_set_null_value(obufptr, 1, cell_type);
		return;
	    }
	    c[i][j] = *cellp;
	}

    /* do the interpolation  */
    t = *col_idx - 0.5 - col;
    u = *row_idx - 0.5 - row;

    result = G_interp_bilinear(t, u, c[0][0], c[0][1], c[1][0], c[1][1]);

    G_set_raster_value_f(obufptr, result, cell_type);
}
Esempio n. 7
0
/* THIS IS CALLED IF ONLY THE TARGET POINT UNDER CURRENT CONSIDERATION IS TO
   BE RAISED BY 'OFFSETB' AMOUNT */
double find_inclination2(int x, int y, double viewpt_elev, 
			SEGMENT *seg_in_p,
			int row_viewpt, int col_viewpt, RASTER_MAP_TYPE data_type)
{
    double del_x, del_y,dist,atan(),sqrt();
    int abs();
    double dest_elev = 0.0;
    extern struct Cell_head window;
    void *value = NULL;	
	/* these vars can store one data value from the elevation input map, */
	/* which could be CELL, DCELL or FCELL type. */
    CELL c_value;
    FCELL f_value;
    DCELL d_value;
    
    del_x = abs(x) ;
    del_y = abs(y) ;
        
    dist=sqrt(del_x * del_x + del_y * del_y)*window.ns_res;
    
    	/* this takes care, that target elevation has the right format, 
	   depending on type of input DEM */
    	
	if (data_type == CELL_TYPE) {
		value = (CELL*) &c_value;
	}
	if (data_type == FCELL_TYPE) {
		value = (FCELL*) &f_value;
	}
	if (data_type == DCELL_TYPE) {
		value = (DCELL*) &d_value;
	}
	
    /* read value from elevation input map, convert to appropriate */
	/* map type */
    segment_get(seg_in_p,value,row_viewpt-y,x+col_viewpt);

	if (data_type == CELL_TYPE) {	
	  if ( G_is_c_null_value (&c_value) ) {
	  	return (NULLPT);
	  } else {
	  	dest_elev = c_value + OFFSETB;
	  }
	}

	if (data_type == FCELL_TYPE) {
	  if ( G_is_f_null_value (&f_value) ) {
	  	return (NULLPT);
	  } else {
	  	dest_elev = f_value + OFFSETB;
	  }
	}

	if (data_type == DCELL_TYPE) {
	  if ( G_is_d_null_value (&d_value) ) {
	  	return (NULLPT);
	  } else {
	  	dest_elev = d_value + OFFSETB;	
	  }
	}


	/* CURVATURE CORRECTION */
	/* decrease height of target point */
	if (DIST_CC > 0.0) {
		if (dist >= DIST_CC) {
			dest_elev = dest_elev - ((dist*dist) / (2 * Re));
		}
	}
        	
    return(atan((dest_elev - viewpt_elev) / dist));
}
Esempio n. 8
0
/* NULL cell handling added by Benjamin Ducke, May 2004 */
void Close_segmented_outfile (char* map_name, int map_fd,
			             char* seg_name, int seg_fd, SEGMENT* seg, 
                         CELL* cell_buf, int terse,
                         SEGMENT* elevation_seg, RASTER_MAP_TYPE data_type, int make_nulls)
{
  unsigned long row, nrows, col, ncols;
  void *value = NULL;
  char *null_flags;
  /* the following are used to store different raster map types */
  CELL c_value;
  FCELL f_value;
  DCELL d_value;  	

	
  /* Find number of rows and columns in elevation map */
  nrows = G_window_rows();
  ncols = G_window_cols();
	
  /* allocate memory for a NULL data row */	
  null_flags = G_calloc ((unsigned) ncols, sizeof (char));
    
  /* Write pending updates by segment_put() to output map */
  segment_flush(seg);
  
  if (!terse) {	
    fprintf (stdout, "\nWriting raster map '%s': \n", map_name);	
  }
	
  /* Convert output submatrices to full cell overlay */
  for(row=0; row< nrows; row++) {
    segment_get_row(seg, cell_buf, (signed) row);
	/* check for NULL values in the corresponding row of the */
	/* elevation input file. If there are any, set the CELLs */
	/* in the output file to NULL, as well */
	
	/* initialize null data row */
	for (col=0; col<ncols; col++) {
		null_flags[col] = 0;
	}
	
	/* convert all -1 cells to NULL, if user wants it so */
    if ( make_nulls == 1 ) {
	    for (col=0; col<ncols; col++) {
		   if ( cell_buf[col] == -1 ) {
		     null_flags[col] = 1;
		   }
		}
	}
	
	/* update NULL flags in row */
    for (col=0; col<ncols; col++) {
		if ( data_type == CELL_TYPE ) {
		    value = (CELL*) &c_value;
		}		  
		if ( data_type == FCELL_TYPE ) {
		    value = (FCELL*) &f_value;
		}		  
		if ( data_type == DCELL_TYPE ) {
		    value = (DCELL*) &d_value;
		}
		
        segment_get (elevation_seg, value, (signed) row, (signed) col);
		
		/* check for NULL value and record in null_flags row */
		if ( data_type == CELL_TYPE ) {
		    if (G_is_c_null_value (&c_value) ) {
			  null_flags[col] = 1; /* signal NULL value in null_flags row */
			}			
		}		  
		if ( data_type == FCELL_TYPE ) {
		    if (G_is_f_null_value (&f_value) ) {
			  null_flags[col] = 1; /* signal NULL value in null_flags row */
			}
		}		  
		if ( data_type == DCELL_TYPE ) {
		    if (G_is_d_null_value (&d_value) ) {
			  null_flags[col] = 1; /* signal NULL value in null_flags row */
			}						
		}
	}
    /* set all NULL cells according to null_flags row */
	G_insert_c_null_values (cell_buf, null_flags, (signed) ncols);
	
	/* now, write this row to disk */  
    if(G_put_raster_row (map_fd, cell_buf, CELL_TYPE) < 0) {
	  G_fatal_error ("Failed to convert output submatrices ");	  
	}
	
	/* progress display */
	if (! terse) {
		G_percent ((signed) row, (signed) nrows-1,2);
	}	
  }
  
  G_free (null_flags);
  
/* Close files */
  segment_release (seg);
  close (seg_fd);
  unlink (seg_name);
  G_close_cell (map_fd);
}