Skip to content
Snippets Groups Projects
Commit ac152a4c authored by Jordi Inglada's avatar Jordi Inglada
Browse files

Example VectorDataIO

parent 291ea39f
No related branches found
No related tags found
No related merge requests found
......@@ -18,7 +18,25 @@
// Software Guide : BeginLatex
//
// Although \ref{DXFExample}
// Although specific vector data import approaches, as the one
// presented in \ref{DXFExample}, can be useful, it is even more
// interesting to have available approaches which are independent of
// the input format. Unfortunately, many vector data formats do not
// share the models for the data they represent. However, in some
// cases, when simple data is stored, it can be decomposed in simple
// objects as for instance polylines, polygons and points. This is
// the case for the Shapefile and the KML (Keyhole Markup Language),
// for instance.
//
// Even though specific reader/writer for Shapefile (and soon KML)
// are available in OTB, we designed a generic approach for the IO of
// this kind of data.
//
// This example illustrates the use of OTB's vector data IO
// framework.
//
// We will start by including the header files for the classes
// describing the vector data and the corresponding reader and writer.
//
// Software Guide : EndLatex
......@@ -26,6 +44,16 @@
#include "otbVectorData.h"
#include "otbVectorDataFileReader.h"
#include "otbVectorDataFileWriter.h"
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We will also need to include the header files for the classes
// which model the individual objects that we get from the vector
// data structure.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
#include "itkPreOrderTreeIterator.h"
#include "otbObjectList.h"
#include "otbPolygon.h"
......@@ -42,21 +70,58 @@ int main(int argc, char * argv[])
}
typedef short unsigned int PixelType;
// Software Guide : BeginLatex
//
// We define the types for the vector data structure and the
// corresponding file reader.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef otb::VectorData<PixelType,2> VectorDataType;
typedef VectorDataType::Pointer VectorDataPointerType;
typedef otb::VectorDataFileReader<VectorDataType>
VectorDataFileReaderType;
typedef VectorDataFileReaderType::Pointer VectorDataFileReaderPointerType;
typedef VectorDataType::DataTreeType DataTreeType;
typedef DataTreeType::Pointer DataTreePointerType;
typedef itk::PreOrderTreeIterator<DataTreeType> TreeIteratorType;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We can now instantiate the reader and read the data.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
VectorDataFileReaderPointerType reader = VectorDataFileReaderType::New();
VectorDataFileReaderType::Pointer reader = VectorDataFileReaderType::New();
reader->SetFileName(argv[1]);
reader->Update();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The vector data obtained from the reader wil provide a tree of
// nodes containing the actual objects of the scene. This tree will
// be accessed using an \doxygen{itk}{PreOrderTreeIterator}.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef VectorDataType::DataTreeType DataTreeType;
typedef itk::PreOrderTreeIterator<DataTreeType> TreeIteratorType;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// In this example we will only read polygon objects from the input
// file before writing them to the output file. We define the type
// for the polygon object as well as an iterator to the vertices. The
// polygons obtained will be stored in an \doxygen{otb}{ObjectList}.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef otb::Polygon<PixelType> PolygonType;
typedef PolygonType::VertexListIteratorType PolygonIteratorType;
......@@ -64,12 +129,31 @@ int main(int argc, char * argv[])
typedef PolygonListType::Iterator PolygonListIteratorType;
PolygonListType::Pointer polygonList = PolygonListType::New();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We get the data tree and instantiate an iterator to walk through it.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
TreeIteratorType it(reader->GetOutput()->GetDataTree());
it.GoToBegin();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We check that the current object is a polygon using the
// \code{IsPolygonFeature()} method and get its exterior ring in
// order to sore it into the list.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
while(!it.IsAtEnd())
{
......@@ -79,69 +163,96 @@ int main(int argc, char * argv[])
}
++it;
}
// Software Guide : EndCodeSnippet
polygonList->PushBack(PolygonType::New());
//polygonList->Back()->SetValue(m_LeftViewer->GetNextROILabel());
typedef VectorDataType::DataNodeType DataNodeType;
typedef DataNodeType::PointType PointType;
typedef DataNodeType::LineType LineType;
typedef LineType::VertexType VertexType;
// Software Guide : BeginLatex
//
// Before writing the polygons to the output file, we have to build
// the vector data structure. This structure will be build up of
// nodes. We define the types needed for that.
//
// Software Guide : EndLatex
VectorDataType::Pointer data = VectorDataType::New();
DataNodeType::Pointer document = DataNodeType::New();
DataNodeType::Pointer folder1 = DataNodeType::New();
DataNodeType::Pointer folder2 = DataNodeType::New();
DataNodeType::Pointer folder3 = DataNodeType::New();
DataNodeType::Pointer line = DataNodeType::New();
// Software Guide : BeginCodeSnippet
VectorDataType::Pointer outVectorData = VectorDataType::New();
document->SetNodeType(otb::DOCUMENT);
folder1->SetNodeType(otb::FOLDER);
folder2->SetNodeType(otb::FOLDER);
folder3->SetNodeType(otb::FOLDER);
line->SetNodeType(otb::FEATURE_LINE);
document->SetNodeId("DOCUMENT");
folder1->SetNodeId("FOLDER1");
folder2->SetNodeId("FOLDER2");
folder3->SetNodeId("FOLDER3");
line->SetNodeId("FEATURE_LINE");
VertexType p1;
p1.Fill(5);
VertexType p3;
p3.Fill(0);
typedef VectorDataType::DataNodeType DataNodeType;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We fill the data structure with the nodes. The root node is a
// document which is composed of folders. A list of polygons can be
// seen as a multi polygon object.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
VertexType p2;
p2[0]=0;
p2[1]=10;
DataNodeType::Pointer document = DataNodeType::New();
document->SetNodeType(otb::DOCUMENT);
document->SetNodeId("polygon");
DataNodeType::Pointer folder = DataNodeType::New();
folder->SetNodeType(otb::FOLDER);
DataNodeType::Pointer multiPolygon = DataNodeType::New();
multiPolygon->SetNodeType(otb::FEATURE_MULTIPOLYGON);
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We assign these objects to the data tree stored by the vector data object.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
LineType::Pointer l = LineType::New();
l->AddVertex(p1);
l->AddVertex(p2);
l->AddVertex(p3);
line->SetLine(l);
DataTreeType::Pointer tree = outVectorData->GetDataTree();
DataNodeType::Pointer root = tree->GetRoot()->Get();
tree->Add(document,root);
tree->Add(folder,document);
tree->Add(multiPolygon,folder);
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We can now iterate through the polygon list and fill the vector
// data structure.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
DataNodeType::Pointer root = data->GetDataTree()->GetRoot()->Get();
for(PolygonListType::Iterator it = polygonList->Begin();
it != polygonList->End(); ++it)
{
DataNodeType::Pointer newPolygon = DataNodeType::New();
newPolygon->SetPolygonExteriorRing(it.Get());
tree->Add(newPolygon,multiPolygon);
}
data->GetDataTree()->Add(document,root);
data->GetDataTree()->Add(folder1,document);
data->GetDataTree()->Add(folder2,document);
data->GetDataTree()->Add(folder3,document);
data->GetDataTree()->Add(line,folder2);
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// An finally we write the vector data to a file using a generic
// \doxygen{otb}{VectorDataFileWriter}.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef otb::VectorDataFileWriter<VectorDataType> WriterType;
WriterType::Pointer writer = WriterType::New();
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(argv[2]);
writer->SetInput(data);
writer->Update();
// Software Guide : EndCodeSnippet
return EXIT_SUCCESS;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment