From 39a8995b25d5f4730443adc9b9f6574d8cdc1eb3 Mon Sep 17 00:00:00 2001 From: Arnaud Jaen <arnaud.jaen@c-s.fr> Date: Thu, 19 Apr 2012 11:50:44 +0200 Subject: [PATCH] ENH: Speed up the writing of sqlite files for streaming segmentation. --- Code/OBIA/otbFusionOGRTileFilter.txx | 34 ++++++++++++++++++- Code/OBIA/otbPersistentImageToOGRDataFilter.h | 4 +++ .../otbPersistentImageToOGRDataFilter.txx | 24 +++++++++---- .../otbStreamingVectorizedSegmentationOGR.txx | 1 + 4 files changed, 55 insertions(+), 8 deletions(-) diff --git a/Code/OBIA/otbFusionOGRTileFilter.txx b/Code/OBIA/otbFusionOGRTileFilter.txx index 2623cdbd2b..16cce76db2 100644 --- a/Code/OBIA/otbFusionOGRTileFilter.txx +++ b/Code/OBIA/otbFusionOGRTileFilter.txx @@ -22,6 +22,8 @@ #include "ogrsf_frmts.h" #include <iomanip> +#include "itkTimeProbe.h" + namespace otb { @@ -78,10 +80,16 @@ FusionOGRTileFilter<TImage> unsigned int nbColStream = static_cast<unsigned int>(imageSize[0] / m_StreamSize[0] + 1); //process column + unsigned int nbTransaction = 0; + inputLayer->StartTransaction(); for(unsigned int x=1; x<=nbColStream; x++) { for(unsigned int y=1; y<=nbRowStream; y++) { + /*std::cout<< " column tile number : " << x*y <<std::endl; + itk::TimeProbe chrono; + chrono.Start();*/ + //First compute the intersection between polygons and the streaming line (upper stream) std::vector<FeatureStruct> upperStreamFeatureList; upperStreamFeatureList.clear(); @@ -226,6 +234,11 @@ FusionOGRTileFilter<TImage> inputLayer->DeleteFeature(lower.feat->GetFID()); inputLayer->DeleteFeature(upper.feat->GetFID()); OGRFeature::DestroyFeature( fusionFeature ); + if(++nbTransaction == 65536) + { + inputLayer->CommitTransaction(); + inputLayer->StartTransaction(); + } } } @@ -239,14 +252,24 @@ FusionOGRTileFilter<TImage> OGRFeature::DestroyFeature( lowerStreamFeatureList.at(l).feat ); } + /*chrono.Stop(); + std::cout<< "Column fusion tile took " << chrono.GetTotal() << " sec"<<std::endl;*/ + } //end for x } //end for y + inputLayer->CommitTransaction(); //Process line + nbTransaction = 0; + inputLayer->StartTransaction(); for(unsigned int y=1; y<=nbRowStream; y++) { for(unsigned int x=1; x<=nbColStream; x++) { + /*std::cout<< " line tile number : " << x*y <<std::endl; + itk::TimeProbe chrono; + chrono.Start();*/ + //First compute the intersection between polygons and the streaming line (upper stream) std::vector<FeatureStruct> upperStreamFeatureList; upperStreamFeatureList.clear(); @@ -388,6 +411,11 @@ FusionOGRTileFilter<TImage> inputLayer->DeleteFeature(lower.feat->GetFID()); inputLayer->DeleteFeature(upper.feat->GetFID()); OGRFeature::DestroyFeature( fusionFeature ); + if(++nbTransaction == 65536) + { + inputLayer->CommitTransaction(); + inputLayer->StartTransaction(); + } } } @@ -401,9 +429,13 @@ FusionOGRTileFilter<TImage> OGRFeature::DestroyFeature( lowerStreamFeatureList.at(l).feat ); } + + /*chrono.Stop(); + std::cout<< "line fusion tile took " << chrono.GetTotal() << " sec"<<std::endl;*/ + } //end for x } //end for y - + inputLayer->CommitTransaction(); //Free memory OGRDataSource::DestroyDataSource(inputDataSource); diff --git a/Code/OBIA/otbPersistentImageToOGRDataFilter.h b/Code/OBIA/otbPersistentImageToOGRDataFilter.h index 7e13c6e237..09528a6153 100644 --- a/Code/OBIA/otbPersistentImageToOGRDataFilter.h +++ b/Code/OBIA/otbPersistentImageToOGRDataFilter.h @@ -85,8 +85,12 @@ public: itkSetStringMacro(FileName); itkGetStringMacro(FileName); + /** Set/Get the Field Name of the ogr file in which labels will be written. (default is "DN")*/ itkSetMacro(FieldName, std::string); itkGetMacro(FieldName, std::string); + + /** Get the size of the tile used for streaming. */ + itkGetMacro(StreamSize, SizeType); protected: PersistentImageToOGRDataFilter(); diff --git a/Code/OBIA/otbPersistentImageToOGRDataFilter.txx b/Code/OBIA/otbPersistentImageToOGRDataFilter.txx index cca4197e9f..3a348ebe4e 100644 --- a/Code/OBIA/otbPersistentImageToOGRDataFilter.txx +++ b/Code/OBIA/otbPersistentImageToOGRDataFilter.txx @@ -117,13 +117,7 @@ void PersistentImageToOGRDataFilter<TImage> ::Synthetize() { - typedef FusionOGRTileFilter<InputImageType> FusionFilterType; - typename FusionFilterType::Pointer filter = FusionFilterType::New(); - - filter->SetInput(this->GetInput()); - filter->SetInputFileName(this->m_FileName); - filter->SetStreamSize(this->m_StreamSize); - filter->GenerateData(); + } template<class TImage> @@ -156,10 +150,18 @@ PersistentImageToOGRDataFilter<TImage> ogrDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(driverName.c_str()); m_DataSource = ogrDriver->CreateDataSource(this->m_FileName.c_str(), NULL); + + /*char ** options = NULL; + char * opt = "SPATIALITE=YES"; + options = CSLAddString(options, opt ); + m_DataSource = ogrDriver->CreateDataSource(this->m_FileName.c_str(), &options[0]);*/ + m_DataSource->CreateLayer("layer", oSRS ,wkbMultiPolygon, NULL); OGRFieldDefn field(m_FieldName.c_str(),OFTInteger); m_DataSource->GetLayer(0)->CreateField(&field, true); + + //CSLDestroy( options ); } else { @@ -187,6 +189,7 @@ PersistentImageToOGRDataFilter<TImage> unsigned int nbFeatures = poSrcLayer->GetFeatureCount(true); unsigned int i = 0; OGRFeature *poFeature; + poDstLayer->StartTransaction(); while (i<nbFeatures) { OGRFeature *poDstFeature = NULL; @@ -199,12 +202,19 @@ PersistentImageToOGRDataFilter<TImage> poDstFeature->SetFrom( poFeature, TRUE ); poDstLayer->CreateFeature( poDstFeature ); + if (i == nbFeatures-1) + { + poDstLayer->CommitTransaction(); + poDstLayer->StartTransaction(); + } + //free memory OGRFeature::DestroyFeature( poDstFeature ); OGRFeature::DestroyFeature( poFeature ); i++; } + poDstLayer->CommitTransaction(); chrono.Stop(); std::cout<< "write ogr tile took " << chrono.GetTotal() << " sec"<<std::endl; diff --git a/Code/OBIA/otbStreamingVectorizedSegmentationOGR.txx b/Code/OBIA/otbStreamingVectorizedSegmentationOGR.txx index c7265e020c..8ba7aa0820 100644 --- a/Code/OBIA/otbStreamingVectorizedSegmentationOGR.txx +++ b/Code/OBIA/otbStreamingVectorizedSegmentationOGR.txx @@ -159,6 +159,7 @@ PersistentStreamingLabelImageToOGRDataFilter<TImageType, TSegmentationFilter> i++; } m_TileMaxLabel = m_TileMaxLabel + relabelMap.size(); + chrono3.Stop(); std::cout<< "relabel took " << chrono3.GetTotal() << " sec"<<std::endl; return output; -- GitLab