Skip to content

Commit

Permalink
Fixed gdal set_projection api usage
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkvdb committed Sep 4, 2024
1 parent 3794d14 commit d31ee35
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
28 changes: 23 additions & 5 deletions gdalgeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ Layer::~Layer()
}
}

Layer& Layer::operator=(Layer&& other)
Layer& Layer::operator=(Layer&& other) noexcept
{
_layer = other._layer;
other._layer = nullptr;
Expand Down Expand Up @@ -622,23 +622,41 @@ std::optional<int32_t> Layer::epsg() const
return std::optional<int32_t>();
}

void Layer::set_projection(SpatialReference& srs)
void Layer::set_active_projection(SpatialReference& srs)
{
const int geomCount = _layer->GetLayerDefn()->GetGeomFieldCount();
for (int i = 0; i < geomCount; ++i) {
_layer->GetLayerDefn()->GetGeomFieldDefn(i)->SetSpatialRef(srs.get());
check_error(_layer->SetActiveSRS(i, srs.get()), "Failed to set the active projection");
}
}

void Layer::set_projection_from_epsg(int32_t epsg)
void Layer::set_active_projection_from_epsg(int32_t epsg)
{
SpatialReference srs(epsg);
set_active_projection(srs);
}

void Layer::set_projection(SpatialReference& srs)
{
const int geomCount = _layer->GetLayerDefn()->GetGeomFieldCount();

if (_layer->TestCapability(OLCAlterGeomFieldDefn) == 0) {
throw RuntimeError("Layer does not support altering geometry field definitions");
}

for (int i = 0; i < geomCount; ++i) {
_layer->GetLayerDefn()->GetGeomFieldDefn(i)->SetSpatialRef(srs.get());
OGRGeomFieldDefn def(_layer->GetLayerDefn()->GetGeomFieldDefn(i));
def.SetSpatialRef(srs.get());
check_error(_layer->AlterGeomFieldDefn(i, &def, ALTER_GEOM_FIELD_DEFN_SRS_FLAG), "Failed to alter geometry field");
}
}

void Layer::set_projection_from_epsg(int32_t epsg)
{
SpatialReference srs(epsg);
set_projection(srs);
}

std::optional<SpatialReference> Layer::projection() const
{
if (auto* srs = _layer->GetSpatialRef(); srs != nullptr) {
Expand Down
7 changes: 6 additions & 1 deletion include/infra/gdalgeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -1032,10 +1032,15 @@ class Layer
Layer(Layer&&);
~Layer();

Layer& operator=(Layer&&);
Layer& operator=(Layer&&) noexcept;
Layer& operator=(const Layer&);

std::optional<int32_t> epsg() const;

// Changes the layers project while working with but will not persist this change after reopening the layer
void set_active_projection(SpatialReference& srs);
void set_active_projection_from_epsg(int32_t epsg);

//! Make sure the spatial reference stays in scope while using the layer!
void set_projection(SpatialReference& srs);
void set_projection_from_epsg(int32_t epsg);
Expand Down

0 comments on commit d31ee35

Please sign in to comment.