1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
/*
* Copyright (C) 2005-2020 Centre National d'Etudes Spatiales (CNES)
*
* This file is part of Orfeo Toolbox
*
* https://www.orfeo-toolbox.org/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef otbOGRLayerStreamStitchingFilter_hxx
#define otbOGRLayerStreamStitchingFilter_hxx
#include "otbOGRLayerStreamStitchingFilter.h"
#include "itkContinuousIndex.h"
#include <iomanip>
#include "ogrsf_frmts.h"
#include <set>
namespace otb
{
template <class TImage>
OGRLayerStreamStitchingFilter<TImage>::OGRLayerStreamStitchingFilter() : m_Radius(2), m_OGRLayer(nullptr, false)
{
m_StreamSize.Fill(0);
}
template <class TInputImage>
void OGRLayerStreamStitchingFilter<TInputImage>::SetInput(const InputImageType* input)
{
this->Superclass::SetNthInput(0, const_cast<InputImageType*>(input));
}
template <class TInputImage>
const typename OGRLayerStreamStitchingFilter<TInputImage>::InputImageType* OGRLayerStreamStitchingFilter<TInputImage>::GetInput(void)
{
if (this->GetNumberOfInputs() < 1)
{
return nullptr;
}
return static_cast<const InputImageType*>(this->Superclass::GetInput(0));
}
template <class TInputImage>
void OGRLayerStreamStitchingFilter<TInputImage>::SetOGRLayer(const OGRLayerType& ogrLayer)
{
m_OGRLayer = ogrLayer;
this->Modified();
}
template <class TInputImage>
const typename OGRLayerStreamStitchingFilter<TInputImage>::OGRLayerType& OGRLayerStreamStitchingFilter<TInputImage>::GetOGRLayer(void) const
{
return m_OGRLayer;
}
template <class TInputImage>
double OGRLayerStreamStitchingFilter<TInputImage>::GetLengthOGRGeometryCollection(OGRGeometryCollection* intersection)
{
double dfLength = 0.0;
for (int iGeom = 0; iGeom < intersection->getNumGeometries(); iGeom++)
{
OGRGeometry* geom = intersection->getGeometryRef(iGeom);
switch (wkbFlatten(geom->getGeometryType()))
{
case wkbLinearRing:
case wkbLineString:
dfLength += ((OGRCurve*)geom)->get_Length();
break;
case wkbGeometryCollection:
dfLength += GetLengthOGRGeometryCollection(dynamic_cast<OGRGeometryCollection*>(geom));
break;
default:
break;
}
}
return dfLength;
}
template <class TInputImage>
void OGRLayerStreamStitchingFilter<TInputImage>::ProcessStreamingLine(bool line, itk::ProgressReporter& progress)
{
typename InputImageType::ConstPointer inputImage = this->GetInput();
// compute the number of stream division in row and column
SizeType imageSize = this->GetInput()->GetLargestPossibleRegion().GetSize();
unsigned int nbRowStream = static_cast<unsigned int>(imageSize[1] / m_StreamSize[1] + 1);
unsigned int nbColStream = static_cast<unsigned int>(imageSize[0] / m_StreamSize[0] + 1);
/*unsigned long startReporter;
unsigned long stopReporter;
if (!line)
{
startReporter = 0;
stopReporter = 50;
}
else
{
startReporter = 50;
stopReporter = 100;
}
itk::ProgressReporter progress(this,0,2*nbRowStream*nbColStream,100,startReporter); */
for (unsigned int x = 1; x <= nbColStream; x++)
{
OGRErr errStart = m_OGRLayer.ogr().StartTransaction();
if (errStart != OGRERR_NONE)
{
itkExceptionMacro(<< "Unable to start transaction for OGR layer " << m_OGRLayer.ogr().GetName() << ".");
}
for (unsigned int y = 1; y <= nbRowStream; y++)
{
// Compute Stream line
OGRLineString streamLine;
itk::ContinuousIndex<double, 2> startIndex;
itk::ContinuousIndex<double, 2> endIndex;
if (!line)
{
// Treat vertical stream line
startIndex[0] = static_cast<double>(m_StreamSize[0] * x) - 0.5;
startIndex[1] = static_cast<double>(m_StreamSize[1] * (y - 1)) - 0.5;
endIndex = startIndex;
endIndex[1] += static_cast<double>(m_StreamSize[1]);
}
else
{ // Treat horizontal stream line
startIndex[0] = static_cast<double>(m_StreamSize[0] * (x - 1)) - 0.5;
startIndex[1] = static_cast<double>(m_StreamSize[1] * y) - 0.5;
endIndex = startIndex;
endIndex[0] += static_cast<double>(m_StreamSize[0]);
}
OriginType startPoint;
inputImage->TransformContinuousIndexToPhysicalPoint(startIndex, startPoint);
OriginType endPoint;
inputImage->TransformContinuousIndexToPhysicalPoint(endIndex, endPoint);
streamLine.addPoint(startPoint[0], startPoint[1]);
streamLine.addPoint(endPoint[0], endPoint[1]);
// First we get all the feature that intersect the streaming line of the Upper/left stream
std::vector<FeatureStruct> upperStreamFeatureList;
upperStreamFeatureList.clear();
IndexType UpperLeftCorner;
IndexType LowerRightCorner;
if (!line)
{
// Treat Row stream
// Compute the spatial filter of the upper stream
UpperLeftCorner[0] = x * m_StreamSize[0] - 1 - m_Radius;
UpperLeftCorner[1] = m_StreamSize[1] * (y - 1);
LowerRightCorner[0] = m_StreamSize[0] * x - 1;
LowerRightCorner[1] = m_StreamSize[1] * y - 1;
}
else
{ // Treat Column stream
// Compute the spatial filter of the left stream
UpperLeftCorner[0] = (x - 1) * m_StreamSize[0];
UpperLeftCorner[1] = m_StreamSize[1] * y - 1 - m_Radius;
LowerRightCorner[0] = m_StreamSize[0] * x - 1;
LowerRightCorner[1] = m_StreamSize[1] * y - 1; //-1 to stop just before stream line
}
OriginType ulCorner;
inputImage->TransformIndexToPhysicalPoint(UpperLeftCorner, ulCorner);
OriginType lrCorner;
inputImage->TransformIndexToPhysicalPoint(LowerRightCorner, lrCorner);
m_OGRLayer.SetSpatialFilterRect(ulCorner[0], lrCorner[1], lrCorner[0], ulCorner[1]);
std::set<unsigned int> upperFIDs;
OGRLayerType::const_iterator featIt = m_OGRLayer.begin();
for (; featIt != m_OGRLayer.end(); ++featIt)
{
FeatureStruct s(m_OGRLayer.GetLayerDefn());
s.feat = *featIt;
s.fusioned = false;
upperStreamFeatureList.push_back(s);
upperFIDs.insert((*featIt).GetFID());
}
// Do the same thing for the lower/right stream
std::vector<FeatureStruct> lowerStreamFeatureList;
lowerStreamFeatureList.clear();
if (!line)
{
// Compute the spatial filter of the lower stream
UpperLeftCorner[0] = x * m_StreamSize[0];
UpperLeftCorner[1] = m_StreamSize[1] * (y - 1);
LowerRightCorner[0] = m_StreamSize[0] * x + m_Radius;
LowerRightCorner[1] = m_StreamSize[1] * y - 1;
}
else
{
// Compute the spatial filter of the right stream
UpperLeftCorner[0] = (x - 1) * m_StreamSize[0];
UpperLeftCorner[1] = m_StreamSize[1] * y;
LowerRightCorner[0] = m_StreamSize[0] * x - 1;
LowerRightCorner[1] = m_StreamSize[1] * y + m_Radius;
}
inputImage->TransformIndexToPhysicalPoint(UpperLeftCorner, ulCorner);
inputImage->TransformIndexToPhysicalPoint(LowerRightCorner, lrCorner);
m_OGRLayer.SetSpatialFilterRect(ulCorner[0], lrCorner[1], lrCorner[0], ulCorner[1]);
for (featIt = m_OGRLayer.begin(); featIt != m_OGRLayer.end(); ++featIt)
{
if (upperFIDs.find((*featIt).GetFID()) == upperFIDs.end())
{
FeatureStruct s(m_OGRLayer.GetLayerDefn());
s.feat = *featIt;
s.fusioned = false;
lowerStreamFeatureList.push_back(s);
}
}
unsigned int nbUpperPolygons = upperStreamFeatureList.size();
unsigned int nbLowerPolygons = lowerStreamFeatureList.size();
std::vector<FusionStruct> fusionList;
fusionList.clear();
for (unsigned int u = 0; u < nbUpperPolygons; u++)
{
for (unsigned int l = 0; l < nbLowerPolygons; l++)
{
FeatureStruct upper = upperStreamFeatureList[u];
FeatureStruct lower = lowerStreamFeatureList[l];
if (!(upper.feat == lower.feat) && upper.feat.GetGeometry()->IsValid() && lower.feat.GetGeometry()->IsValid())
{
if (ogr::Intersects(*upper.feat.GetGeometry(), *lower.feat.GetGeometry()))
{
ogr::UniqueGeometryPtr intersection2 = ogr::Intersection(*upper.feat.GetGeometry(), *lower.feat.GetGeometry());
ogr::UniqueGeometryPtr intersection = ogr::Intersection(*intersection2, streamLine);
// ogr::UniqueGeometryPtr intersection = ogr::Intersection(*upper.feat.GetGeometry(),*lower.feat.GetGeometry());
if (intersection)
{
FusionStruct fusion;
fusion.indStream1 = u;
fusion.indStream2 = l;
fusion.overlap = 0.;
if (intersection->getGeometryType() == wkbPolygon)
{
fusion.overlap = dynamic_cast<OGRPolygon*>(intersection.get())->get_Area();
}
else if (intersection->getGeometryType() == wkbMultiPolygon)
{
fusion.overlap = dynamic_cast<OGRMultiPolygon*>(intersection.get())->get_Area();
}
else if (intersection->getGeometryType() == wkbGeometryCollection)
{
fusion.overlap = dynamic_cast<OGRGeometryCollection*>(intersection.get())->get_Area();
}
else if (intersection->getGeometryType() == wkbLineString)
{
fusion.overlap = dynamic_cast<OGRLineString*>(intersection.get())->get_Length();
}
else if (intersection->getGeometryType() == wkbMultiLineString)
{
fusion.overlap = dynamic_cast<OGRMultiLineString*>(intersection.get())->get_Length();
}
/** -Wunused-variable
long upperFID = upper.feat.GetFID();
long lowerFID = lower.feat.GetFID();
**/
fusionList.push_back(fusion);
}
}
}
}
}
unsigned int fusionListSize = fusionList.size();
std::sort(fusionList.begin(), fusionList.end(), SortFeature);
for (unsigned int i = 0; i < fusionListSize; i++)
{
FeatureStruct upper = upperStreamFeatureList.at(fusionList.at(i).indStream1);
FeatureStruct lower = lowerStreamFeatureList.at(fusionList.at(i).indStream2);
if (!upper.fusioned && !lower.fusioned)
{
upperStreamFeatureList[fusionList[i].indStream1].fusioned = true;
lowerStreamFeatureList[fusionList[i].indStream2].fusioned = true;
ogr::UniqueGeometryPtr fusionPolygon = ogr::Union(*upper.feat.GetGeometry(), *lower.feat.GetGeometry());
OGRFeatureType fusionFeature(m_OGRLayer.GetLayerDefn());
fusionFeature.SetGeometry(fusionPolygon.get());
ogr::Field field = upper.feat[0];
try
{
switch (field.GetType())
{
case OFTInteger64:
{
fusionFeature[0].SetValue(field.GetValue<GIntBig>());
break;
}
default:
{
fusionFeature[0].SetValue(field.GetValue<int>());
}
}
m_OGRLayer.CreateFeature(fusionFeature);
m_OGRLayer.DeleteFeature(lower.feat.GetFID());
m_OGRLayer.DeleteFeature(upper.feat.GetFID());
}
catch (itk::ExceptionObject& err)
{
otbWarningMacro(<< "An exception was caught during fusion: " << err);
}
}
}
// Update progress
progress.CompletedPixel();
} // end for x
if (m_OGRLayer.ogr().TestCapability("Transactions"))
{
OGRErr errCommitX = m_OGRLayer.ogr().CommitTransaction();
if (errCommitX != OGRERR_NONE)
{
itkExceptionMacro(<< "Unable to commit transaction for OGR layer " << m_OGRLayer.ogr().GetName() << ".");
}
}
} // end for y
}
template <class TImage>
void OGRLayerStreamStitchingFilter<TImage>::GenerateData(void)
{
if (!m_OGRLayer)
{
itkExceptionMacro(<< "Input OGR layer is null!");
}
this->InvokeEvent(itk::StartEvent());
typename InputImageType::ConstPointer inputImage = this->GetInput();
// compute the number of stream division in row and column
SizeType imageSize = this->GetInput()->GetLargestPossibleRegion().GetSize();
unsigned int nbRowStream = static_cast<unsigned int>(imageSize[1] / m_StreamSize[1] + 1);
unsigned int nbColStream = static_cast<unsigned int>(imageSize[0] / m_StreamSize[0] + 1);
itk::ProgressReporter progress(this, 0, 2 * nbRowStream * nbColStream, 100, 0);
// Process column
this->ProcessStreamingLine(false, progress);
// Process row
this->ProcessStreamingLine(true, progress);
this->InvokeEvent(itk::EndEvent());
}
} // end namespace otb
#endif
|