Opened 7 years ago

Last modified 6 years ago

#3415 new defect

Mark raster NULL values as None in PyGRASS

Reported by: marisn Owned by: grass-dev@…
Priority: normal Milestone: 7.4.5
Component: PyGRASS Version: unspecified
Keywords: Cc:
CPU: Unspecified Platform: Unspecified

Description

At the moment PyGRASS is providing raster NULL values as is (i.e. for CELL type they are marked as -2147483648). There is no function to check if value is NULL (in C it is done with Rast_is_c/f/d_null_value).

The best would be to replace raster NULL values with None when raster is read and None with NULL representation when it is written back. Such approach would greatly simplify use of rasters in PyGRASS (i.e. "if myrast[row][col] is not None:")

Change History (8)

in reply to:  description ; comment:1 by zarch, 7 years ago

Replying to marisn:

At the moment PyGRASS is providing raster NULL values as is (i.e. for CELL type they are marked as -2147483648). There is no function to check if value is NULL (in C it is done with Rast_is_c/f/d_null_value).

The best would be to replace raster NULL values with None when raster is read and None with NULL representation when it is written back. Such approach would greatly simplify use of rasters in PyGRASS (i.e. "if myrast[row][col] is not None:")

Well, but then for CELL raster instead to have an array of integer you will have an array of objects, I did not perform any benchmark but it seems not very intuitive as well. Perhaps we can define a CELL_NULL variable, and then you can write code as: if myrast[row] != CELL_NULL: or an equivalent function instead of numpy.isnan get pygrass.raster.isnull. What do you think?

comment:2 by marisn, 7 years ago

You have a good point. As long as loading/saving can cope with whatever is the NULL representation, a constant variable for grass.pygrass.raster would do. I.e. grass.pygrass.raster.NULL that gets set to representation of NULL for particular map when the map gets opened/created. Thus it wouldn't matter if map is CELL or FCELL. In code one could then use map[row][cell] != map.NULL to check for not NULL and map[row][col] = map.NULL to set to NULL. If nobody comes up with objection why it is a bad idea, could you, please, implement it?

in reply to:  1 comment:3 by glynn, 7 years ago

Replying to zarch:

Well, but then for CELL raster instead to have an array of integer you will have an array of objects, I did not perform any benchmark but it seems not very intuitive as well.

For FP data, NaN should be used for nulls. This avoids the need for explicit null handling in most cases.

For integer data, consider either (or both of):

  1. converting to a masked array. This isn't quite as transparent as NaN, but is typically easier than using a separate mask array.
  2. converting to double (np.float64), with NaN as null. But this isn't suitable if the data is to be used with e.g. bitwise operations, or used as indices.

comment:4 by neteler, 7 years ago

Milestone: 7.4.07.4.1

Ticket retargeted after milestone closed

comment:5 by neteler, 6 years ago

Milestone: 7.4.17.4.2

comment:6 by neteler, 6 years ago

Milestone: 7.4.27.4.3

Ticket retargeted after milestone closed

comment:7 by martinl, 6 years ago

Milestone: 7.4.37.4.4

Bump milestone to 7.4.4

comment:8 by neteler, 6 years ago

Milestone: 7.4.47.4.5

Ticket retargeted after milestone closed

Note: See TracTickets for help on using tickets.