Skip to content

Add gdal raster as-features#12970

Merged
rouault merged 6 commits intoOSGeo:masterfrom
dbaston:gdal-raster-as-features
Sep 3, 2025
Merged

Add gdal raster as-features#12970
rouault merged 6 commits intoOSGeo:masterfrom
dbaston:gdal-raster-as-features

Conversation

@dbaston
Copy link
Copy Markdown
Member

@dbaston dbaston commented Aug 27, 2025

Adds a command to convert individual raster pixels into features (points, polygons, or non-spatial). Some of this could previously be done with gdal2ogr.

@dbaston dbaston added the gdal_cli Anything related to the new 3.11 "gdal" CLI frontend label Aug 27, 2025
@coveralls
Copy link
Copy Markdown
Collaborator

coveralls commented Aug 27, 2025

Coverage Status

Changes unknown
when pulling ebbb0ee on dbaston:gdal-raster-as-features
into ** on OSGeo:master**.

while (m_row < m_window.nYSize)
{
const double *pSrcVal = static_cast<double *>(m_buf) +
(m_row * m_window.nXSize * m_bands.size() +
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should make codeql happy:

Suggested change
(m_row * m_window.nXSize * m_bands.size() +
(m_bands.size() * m_row * m_window.nXSize +

m_bandFields.push_back(m_defn->GetFieldIndex(fieldName));
}

ResetReading();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to make cppcheck happy

Suggested change
ResetReading();
GDALRasterAsFeaturesLayer::ResetReading();

@jratike80
Copy link
Copy Markdown
Collaborator

Should we have some sort of test for pixel-is-area and pixel-is-point rasters? And should we warn users if they want to convert pixel-is-point data into polygons?

@dbaston
Copy link
Copy Markdown
Member Author

dbaston commented Aug 29, 2025

clang static analyzer failing with

Summary of errors found:
{
  "uri": "file:///home/runner/work/gdal/gdal/gcore/gdalrasterband.cpp",
  "msg": "Forming reference to null pointer",
  "location": {
    "importance": "essential",
    "location": {
      "message": {
        "text": "Forming reference to null pointer"
      },
      "physicalLocation": {
        "artifactLocation": {
          "index": 0,
          "uri": "file:///home/runner/work/gdal/gdal/gcore/gdalrasterband.cpp"
        },
        "region": {
          "endColumn": 52,
          "endLine": 9642,
          "startColumn": 21,
          "startLine": 9642
        }
      }
    }
  }
}

Code in question:

gdal/gcore/gdalrasterband.cpp

Lines 9634 to 9644 in 4eb6fd8

GDALRasterBand::WindowIteratorWrapper::WindowIteratorWrapper(
const GDALRasterBand &band)
: m_nRasterXSize(band.GetXSize()), m_nRasterYSize(band.GetYSize()),
m_nBlockXSize(-1), m_nBlockYSize(-1)
{
// If invalid block size is reported, just use a value of 1.
CPLErrorStateBackuper state(CPLQuietErrorHandler);
band.GetBlockSize(&m_nBlockXSize, &m_nBlockYSize);
m_nBlockXSize = std::max<int>(m_nBlockXSize, 1);
m_nBlockYSize = std::max<int>(m_nBlockYSize, 1);
}

I confess I don't understand the problem.

Comment on lines +209 to +210
m_it = m_ds.GetRasterBand(1)->IterateWindows().begin();
m_end = m_ds.GetRasterBand(1)->IterateWindows().end();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe try the following to make clang static analyzer happy ?

Suggested change
m_it = m_ds.GetRasterBand(1)->IterateWindows().begin();
m_end = m_ds.GetRasterBand(1)->IterateWindows().end();
#include <cassert>
Suggested change
m_it = m_ds.GetRasterBand(1)->IterateWindows().begin();
m_end = m_ds.GetRasterBand(1)->IterateWindows().end();
GDALRasterBand* poFirstBand = m_ds.GetRasterBand(1);
assert(poFirstBand);
m_it = poFirstBand->IterateWindows().begin();
m_end = poFirstBand->IterateWindows().end();

and this will need to be rebased on top of latest master to fix conflict

@dbaston dbaston force-pushed the gdal-raster-as-features branch from 4eb6fd8 to 01c34ca Compare August 29, 2025 17:56
@rouault
Copy link
Copy Markdown
Member

rouault commented Aug 29, 2025

hum, CSA still no happy. maybe try the following (using plain assert(), and adding a #include <cassert>) ?

GDALRasterBand::WindowIteratorWrapper GDALRasterBand::IterateWindows() const
{
#if defined(CSA_BUILD)
    assert(this != nullptr);
#endif
    return WindowIteratorWrapper(*this);
}

otherwise we'll need to add a suppression in ci/travis/csa_common/script.sh

// If invalid block size is reported, just use a value of 1.
CPLErrorStateBackuper state(CPLQuietErrorHandler);
#ifdef CSA_BUILD
assert(this);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt this will be effective here as we are in a constructor, so this cannot be null. Did you consider my proposal of putting it in GDALRasterBand::IterateWindows() instead ? There this could be null if calling IterateWindows() from a null pointer

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I tried that locally but it didn't seem to have an effect. This did, though I'm not confident in the diagnostic reporting from incremental builds.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, weird. You'll need to rebase on top of latest master, due to changes in const'ness of virtual methods

@dbaston dbaston force-pushed the gdal-raster-as-features branch from 755b83a to 5a44cc5 Compare September 2, 2025 17:23
@rouault rouault force-pushed the gdal-raster-as-features branch from 5a44cc5 to fc5cd1f Compare September 2, 2025 17:32
@rouault
Copy link
Copy Markdown
Member

rouault commented Sep 2, 2025

arg, I messed up something on master (now fixed) before you rebased on it. I've rebased for you and pushed on your branch, so nothing to do (but you'll need to "git fetch dbaston gdal-raster-as-features" and "git rebase dbaston/gdal-raster-as-features" if you need to apply further updates)

@rouault rouault added this to the 3.12.0 milestone Sep 3, 2025
@rouault rouault merged commit c056f97 into OSGeo:master Sep 3, 2025
50 of 51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gdal_cli Anything related to the new 3.11 "gdal" CLI frontend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants