Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VRT: add a NoDataFromMaskSource source #9128

Merged
merged 3 commits into from
Feb 18, 2024

Conversation

rouault
Copy link
Member

@rouault rouault commented Jan 24, 2024

The NoDataFromMaskSource is derived from the SimpleSource and shares the same properties except that it replaces the value of the source with the value of the NODATA child element when the value of the mask band of the source is less or equal to the MaskValueThreshold child element.

Also add a --nodata_max_mask_threshold option to gdalbuildvrt

Example:

$ gdal_translate autotest/gcore/data/stefan_full_rgba.tif in.tif -a_srs EPSG:4326 -a_ullr -180 90 180 -90
$ gdalbuildvrt nodatafrommask.vrt in.tif -nodata_if_mask_less_or_equal 128 -vrtnodata 0
$ cat nodatafrommask.vrt
<VRTDataset rasterXSize="162" rasterYSize="150">
  <SRS dataAxisToSRSAxisMapping="2,1">GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]</SRS>
  <GeoTransform> -1.8000000000000000e+02,  2.2222222222222223e+00,  0.0000000000000000e+00,  9.0000000000000000e+01,  0.0000000000000000e+00, -1.2000000000000000e+00</GeoTransform>
  <VRTRasterBand dataType="Byte" band="1">
    <NoDataValue>0</NoDataValue>
    <ColorInterp>Red</ColorInterp>
    <NoDataFromMaskSource>
      <SourceFilename relativeToVRT="1">in.tif</SourceFilename>
      <SourceBand>1</SourceBand>
      <SourceProperties RasterXSize="162" RasterYSize="150" DataType="Byte" BlockXSize="162" BlockYSize="12" />
      <SrcRect xOff="0" yOff="0" xSize="162" ySize="150" />
      <DstRect xOff="0" yOff="0" xSize="162" ySize="150" />
      <MaskValueThreshold>128</MaskValueThreshold>
      <NODATA>0</NODATA>
    </NoDataFromMaskSource>
  </VRTRasterBand>
  <VRTRasterBand dataType="Byte" band="2">
    <NoDataValue>0</NoDataValue>
    <ColorInterp>Green</ColorInterp>
    <NoDataFromMaskSource>
      <SourceFilename relativeToVRT="1">in.tif</SourceFilename>
      <SourceBand>2</SourceBand>
      <SourceProperties RasterXSize="162" RasterYSize="150" DataType="Byte" BlockXSize="162" BlockYSize="12" />
      <SrcRect xOff="0" yOff="0" xSize="162" ySize="150" />
      <DstRect xOff="0" yOff="0" xSize="162" ySize="150" />
      <MaskValueThreshold>128</MaskValueThreshold>
      <NODATA>0</NODATA>
    </NoDataFromMaskSource>
  </VRTRasterBand>
  <VRTRasterBand dataType="Byte" band="3">
    <NoDataValue>0</NoDataValue>
    <ColorInterp>Blue</ColorInterp>
    <NoDataFromMaskSource>
      <SourceFilename relativeToVRT="1">in.tif</SourceFilename>
      <SourceBand>3</SourceBand>
      <SourceProperties RasterXSize="162" RasterYSize="150" DataType="Byte" BlockXSize="162" BlockYSize="12" />
      <SrcRect xOff="0" yOff="0" xSize="162" ySize="150" />
      <DstRect xOff="0" yOff="0" xSize="162" ySize="150" />
      <MaskValueThreshold>128</MaskValueThreshold>
      <NODATA>0</NODATA>
    </NoDataFromMaskSource>
  </VRTRasterBand>
</VRTDataset>

@rouault rouault added this to the 3.9.0 milestone Jan 24, 2024
@coveralls
Copy link
Collaborator

coveralls commented Jan 24, 2024

Coverage Status

coverage: 68.811% (+0.006%) from 68.805%
when pulling f72d17b on rouault:NoDataFromMaskSource
into 5f9ffa3 on OSGeo:master.

@rouault rouault force-pushed the NoDataFromMaskSource branch 6 times, most recently from 16409c3 to c1a0a89 Compare January 24, 2024 16:57
apps/gdalbuildvrt_bin.cpp Outdated Show resolved Hide resolved
The NoDataFromMaskSource is derived from the SimpleSource and shares
the same properties except that it replaces the value of the source with
the value of the NODATA child element when the value of the mask band of
the source is less or equal to the MaskValueThreshold child element.

Also add a -nodata_if_mask_less_or_equal <threshol> option to
gdalbuildvrt

Example:
```
$ gdal_translate autotest/gcore/data/stefan_full_rgba.tif in.tif -a_srs EPSG:4326 -a_ullr -180 90 180 -90
$ gdalbuildvrt nodatafrommask.vrt in.tif -nodata_if_mask_less_or_equal 128 -vrtnodata 0
$ cat nodatafrommask.vrt
<VRTDataset rasterXSize="162" rasterYSize="150">
  <SRS dataAxisToSRSAxisMapping="2,1">GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]</SRS>
  <GeoTransform> -1.8000000000000000e+02,  2.2222222222222223e+00,  0.0000000000000000e+00,  9.0000000000000000e+01,  0.0000000000000000e+00, -1.2000000000000000e+00</GeoTransform>
  <VRTRasterBand dataType="Byte" band="1">
    <NoDataValue>0</NoDataValue>
    <ColorInterp>Red</ColorInterp>
    <NoDataFromMaskSource>
      <SourceFilename relativeToVRT="1">in.tif</SourceFilename>
      <SourceBand>1</SourceBand>
      <SourceProperties RasterXSize="162" RasterYSize="150" DataType="Byte" BlockXSize="162" BlockYSize="12" />
      <SrcRect xOff="0" yOff="0" xSize="162" ySize="150" />
      <DstRect xOff="0" yOff="0" xSize="162" ySize="150" />
      <MaskValueThreshold>128</MaskValueThreshold>
      <NODATA>0</NODATA>
    </NoDataFromMaskSource>
  </VRTRasterBand>
  <VRTRasterBand dataType="Byte" band="2">
    <NoDataValue>0</NoDataValue>
    <ColorInterp>Green</ColorInterp>
    <NoDataFromMaskSource>
      <SourceFilename relativeToVRT="1">in.tif</SourceFilename>
      <SourceBand>2</SourceBand>
      <SourceProperties RasterXSize="162" RasterYSize="150" DataType="Byte" BlockXSize="162" BlockYSize="12" />
      <SrcRect xOff="0" yOff="0" xSize="162" ySize="150" />
      <DstRect xOff="0" yOff="0" xSize="162" ySize="150" />
      <MaskValueThreshold>128</MaskValueThreshold>
      <NODATA>0</NODATA>
    </NoDataFromMaskSource>
  </VRTRasterBand>
  <VRTRasterBand dataType="Byte" band="3">
    <NoDataValue>0</NoDataValue>
    <ColorInterp>Blue</ColorInterp>
    <NoDataFromMaskSource>
      <SourceFilename relativeToVRT="1">in.tif</SourceFilename>
      <SourceBand>3</SourceBand>
      <SourceProperties RasterXSize="162" RasterYSize="150" DataType="Byte" BlockXSize="162" BlockYSize="12" />
      <SrcRect xOff="0" yOff="0" xSize="162" ySize="150" />
      <DstRect xOff="0" yOff="0" xSize="162" ySize="150" />
      <MaskValueThreshold>128</MaskValueThreshold>
      <NODATA>0</NODATA>
    </NoDataFromMaskSource>
  </VRTRasterBand>
</VRTDataset>
```
@rouault
Copy link
Member Author

rouault commented Feb 6, 2024

@rcoup did you get a chance to test this PR?

@rouault rouault merged commit a766289 into OSGeo:master Feb 18, 2024
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants