Skip to content

Commit f312a11

Browse files
committed
CLI: Check input CRS consistency
1 parent f9926f1 commit f312a11

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

src/exactextract.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include "utils_cli.h"
3333
#include "version.h"
3434

35+
#include <ogr_srs_api.h>
36+
3537
using exactextract::GDALDatasetWrapper;
3638
using exactextract::GDALRasterWrapper;
3739
using exactextract::Operation;
@@ -43,6 +45,9 @@ load_dataset(const std::string& descriptor,
4345
const std::string& dst_id_name,
4446
const std::string& dst_id_type);
4547

48+
static void
49+
check_crs_consistent(const GDALDatasetWrapper& features, const std::vector<std::unique_ptr<exactextract::RasterSource>>& rasters, const std::vector<std::unique_ptr<exactextract::RasterSource>>& weights);
50+
4651
int
4752
main(int argc, char** argv)
4853
{
@@ -114,6 +119,8 @@ main(int argc, char** argv)
114119

115120
GDALDatasetWrapper shp = load_dataset(poly_descriptor, include_cols, src_id_name, dst_id_name, dst_id_type);
116121

122+
check_crs_consistent(shp, rasters, weights);
123+
117124
std::unique_ptr<exactextract::GDALWriter> gdal_writer = std::make_unique<exactextract::GDALWriter>(
118125
output_filename, !nested_output, shp.srs());
119126

@@ -216,3 +223,21 @@ load_dataset(const std::string& descriptor,
216223

217224
return ds;
218225
}
226+
227+
static void
228+
check_crs_consistent(const GDALDatasetWrapper& features, const std::vector<std::unique_ptr<exactextract::RasterSource>>& rasters, const std::vector<std::unique_ptr<exactextract::RasterSource>>& weights)
229+
{
230+
OGRSpatialReferenceH expected = features.srs();
231+
for (const auto& raster : rasters) {
232+
OGRSpatialReferenceH srs = static_cast<const GDALRasterWrapper*>(raster.get())->srs();
233+
if (!OSRIsSame(srs, expected)) {
234+
std::cerr << "Input features do not have the same CRS as raster " << raster->name() << std::endl;
235+
}
236+
}
237+
for (const auto& raster : weights) {
238+
OGRSpatialReferenceH srs = static_cast<const GDALRasterWrapper*>(raster.get())->srs();
239+
if (!OSRIsSame(srs, expected)) {
240+
std::cerr << "Input features do not have the same CRS as raster " << raster->name() << std::endl;
241+
}
242+
}
243+
}

src/gdal_raster_wrapper.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,10 @@ GDALRasterWrapper::compute_raster_grid()
242242
m_grid = { box, dx, dy };
243243
}
244244

245+
OGRSpatialReferenceH
246+
GDALRasterWrapper::srs() const
247+
{
248+
return GDALGetSpatialRef(m_rast->get());
249+
}
250+
245251
}

src/gdal_raster_wrapper.h

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ class GDALRasterWrapper : public RasterSource
6666

6767
~GDALRasterWrapper() override;
6868

69+
OGRSpatialReferenceH srs() const;
70+
6971
bool cartesian() const;
7072

7173
private:

0 commit comments

Comments
 (0)