Commit 4047404e authored by guillaume pernot's avatar guillaume pernot

Merge branch '1993-VectorClassifier-update-mode' into 'develop'

Fixed lost reference of "buffer" in VectorPrediction update mode

Closes #1993

See merge request !640
parents 5c0e596a c48d2766
Pipeline #3137 passed with stages
in 91 minutes and 25 seconds
......@@ -104,9 +104,12 @@ private:
/** Normalize a list sample using the statistic file given */
typename ListSampleType::Pointer NormalizeListSample(ListSampleType::Pointer input);
/** Create the output DataSource, in update mode the input layer is buffered and the input
* data source is re opened in update mode. */
otb::ogr::DataSource::Pointer CreateOutputDataSource(otb::ogr::DataSource::Pointer source, otb::ogr::Layer& layer, bool updateMode);
/** Update the output DataSource : the input layer is buffered and the input data source is re opened in update mode. */
otb::ogr::DataSource::Pointer ReopenDataSourceInUpdateMode(ogr::DataSource::Pointer source, ogr::Layer& layer,
ogr::DataSource::Pointer buffer);
/** Create the output DataSource. */
otb::ogr::DataSource::Pointer CreateOutputDataSource(ogr::DataSource::Pointer source, ogr::Layer& layer);
/** Add a prediction field in the output layer if it does not exist.
* If computeConfidenceMap evaluates to true a confidence field will be
......
......@@ -147,41 +147,39 @@ typename VectorPrediction<RegressionMode>::ListSampleType::Pointer VectorPredict
template <bool RegressionMode>
otb::ogr::DataSource::Pointer VectorPrediction<RegressionMode>::CreateOutputDataSource(otb::ogr::DataSource::Pointer source, otb::ogr::Layer& layer,
bool updateMode)
otb::ogr::DataSource::Pointer VectorPrediction<RegressionMode>::ReopenDataSourceInUpdateMode(ogr::DataSource::Pointer source, ogr::Layer& layer,
ogr::DataSource::Pointer buffer)
{
ogr::DataSource::Pointer output;
ogr::DataSource::Pointer buffer = ogr::DataSource::New();
if (updateMode)
{
// Update mode
otbAppLogINFO("Update input vector data.");
// fill temporary buffer for the transfer
otb::ogr::Layer inputLayer = layer;
layer = buffer->CopyLayer(inputLayer, std::string("Buffer"));
// close input data source
source->Clear();
// Re-open input data source in update mode
output = otb::ogr::DataSource::New(GetParameterString("in"), otb::ogr::DataSource::Modes::Update_LayerUpdate);
}
else
// Update mode
otbAppLogINFO("Update input vector data.");
// fill temporary buffer for the transfer
otb::ogr::Layer inputLayer = layer;
layer = buffer->CopyLayer(inputLayer, std::string("Buffer"));
// close input data source
source->Clear();
// Re-open input data source in update mode
output = otb::ogr::DataSource::New(GetParameterString("in"), otb::ogr::DataSource::Modes::Update_LayerUpdate);
return output;
}
template <bool RegressionMode>
otb::ogr::DataSource::Pointer VectorPrediction<RegressionMode>::CreateOutputDataSource(ogr::DataSource::Pointer source, ogr::Layer& layer)
{
ogr::DataSource::Pointer output;
// Create new OGRDataSource
output = ogr::DataSource::New(GetParameterString("out"), ogr::DataSource::Modes::Overwrite);
otb::ogr::Layer newLayer = output->CreateLayer(GetParameterString("out"), const_cast<OGRSpatialReference*>(layer.GetSpatialRef()), layer.GetGeomType());
// Copy existing fields
OGRFeatureDefn& inLayerDefn = layer.GetLayerDefn();
for (int k = 0; k < inLayerDefn.GetFieldCount(); k++)
{
// Create new OGRDataSource
output = ogr::DataSource::New(GetParameterString("out"), ogr::DataSource::Modes::Overwrite);
otb::ogr::Layer newLayer = output->CreateLayer(GetParameterString("out"), const_cast<OGRSpatialReference*>(layer.GetSpatialRef()), layer.GetGeomType());
// Copy existing fields
OGRFeatureDefn& inLayerDefn = layer.GetLayerDefn();
for (int k = 0; k < inLayerDefn.GetFieldCount(); k++)
{
OGRFieldDefn fieldDefn(inLayerDefn.GetFieldDefn(k));
newLayer.CreateField(fieldDefn);
}
OGRFieldDefn fieldDefn(inLayerDefn.GetFieldDefn(k));
newLayer.CreateField(fieldDefn);
}
return output;
}
template <bool RegressionMode>
void VectorPrediction<RegressionMode>::AddPredictionField(otb::ogr::Layer& outLayer, otb::ogr::Layer const& layer, bool computeConfidenceMap)
{
......@@ -306,7 +304,21 @@ void VectorPrediction<RegressionMode>::DoExecute()
const bool updateMode = !(IsParameterEnabled("out") && HasValue("out"));
auto output = CreateOutputDataSource(source, layer, updateMode);
ogr::DataSource::Pointer buffer;
ogr::DataSource::Pointer output;
if (updateMode)
{
// in update mode, output is added to input data source.
// buffer needs to be allocated here, as its life-cycle is bound to "layer"
buffer = ogr::DataSource::New();
output = ReopenDataSourceInUpdateMode(source, layer, buffer);
}
else
{
output = CreateOutputDataSource(source, layer);
}
otb::ogr::Layer outLayer = output->GetLayer(0);
OGRErr errStart = outLayer.ogr().StartTransaction();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment