Orfeo Toolbox  3.16
RasterizationExample.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 
19 
20 // Software Guide : BeginCommandLineArgs
21 // INPUTS: {w002n44e.shp}
22 // OUTPUTS: {ArcachonRasterizedCoastline.png}
23 // Software Guide : EndCommandLineArgs
24 
25 // Software Guide : BeginLatex
26 //
27 // The \doxygen{otb}{VectorDataToMapFilter} allows to perform
28 // rasterization of a given vector data as a binary mask. This example
29 // will demonstrate how to use this filter to perform rasterization of
30 // the SRTM water body masks available here:
31 // \url{http://dds.cr.usgs.gov/srtm/version2_1/SWBD/}.
32 //
33 // First step to use this filter is to include the appropriate headers:
34 //
35 // Software Guide : EndLatex
36 
37 // Software Guide : BeginCodeSnippet
38 #include "otbVectorData.h"
39 #include "otbImage.h"
41 // Software Guide : EndCodeSnippet
42 
43 #include "otbImageFileWriter.h"
46 #include "itkRGBAPixel.h"
49 
50 int main(int argc, char * argv[])
51 {
52  // Software Guide : BeginLatex
53  //
54  // Then, we need to define the appropriate VectorData and Image
55  // type.
56  //
57  // Software Guide : EndLatex
58 
59  // Software Guide : BeginCodeSnippet
60 
61  typedef unsigned char PixelType;
62  typedef otb::Image<PixelType, 2> ImageType;
64  // Software Guide : EndCodeSnippet
65 
66  // Software Guide : BeginLatex
67  //
68  // Using these typedefs, we can define and instantiate the
69  // \doxygen{otb}{VectorDataToMapFilter}.
70  //
71  // Software Guide : EndLatex
72 
73  // Software Guide : BeginCodeSnippet
75  ImageType> VectorDataToMapFilterType;
76  VectorDataToMapFilterType::Pointer vectorDataRendering
77  = VectorDataToMapFilterType::New();
78  // Software Guide : EndCodeSnippet
79 
80  // Software Guide : BeginLatex
81  //
82  // We will also define a \doxygen{otb}{VectorDataFileReader} to read
83  // the VectorData, as well as a
84  // \doxygen{otb}{VectorDataProjectionFilter} to reproject our data
85  // in a given map projection.
86  //
87  // Software Guide : EndLatex
88 
89  // Software Guide : BeginCodeSnippet
90  typedef otb::VectorDataFileReader<VectorDataType> VectorDataFileReaderType;
91  VectorDataFileReaderType::Pointer reader = VectorDataFileReaderType::New();
92  reader->SetFileName(argv[1]);
93 
95  VectorDataType> ProjectionFilterType;
96  ProjectionFilterType::Pointer projection = ProjectionFilterType::New();
97  projection->SetInput(reader->GetOutput());
98  // Software Guide : EndCodeSnippet
99 
100  std::string projectionRefWkt = "PROJCS[\"WGS 84 / UTM zone 30N\", GEOGCS[\"WGS 84\", DATUM[\"WGS_1984\", SPHEROID[\"WGS 84\", 6378137, 298.257223563, AUTHORITY[\"EPSG\",\"7030\"]], AUTHORITY[\"EPSG\",\"6326\"]], PRIMEM[\"Greenwich\", 0, AUTHORITY[\"EPSG\",\"8901\"]], UNIT[\"degree\", 0.01745329251994328, AUTHORITY[\"EPSG\",\"9122\"]], AUTHORITY[\"EPSG\",\"4326\"]], UNIT[\"metre\", 1, AUTHORITY[\"EPSG\",\"9001\"]], PROJECTION[\"Transverse_Mercator\"], PARAMETER[\"latitude_of_origin\", 0], PARAMETER[\"central_meridian\", -3], PARAMETER[\"scale_factor\", 0.9996], PARAMETER[\"false_easting\", 500000], PARAMETER[\"false_northing\", 0], AUTHORITY[\"EPSG\",\"32630\"], AXIS[\"Easting\", EAST], AXIS[\"Northing\", NORTH]]";
101 
102  // Software Guide : BeginLatex
103  //
104  // Next step is to specify the map projection in which to reproject
105  // our vector.
106  //
107  // Software Guide : EndLatex
108 
109  // Software Guide : BeginCodeSnippet
110 
111  projection->SetOutputProjectionRef(projectionRefWkt);
112  // Software Guide : EndCodeSnippet
113 
114  // Software Guide : BeginLatex
115  //
116  // Since the input vector can be pretty big, we will perform an
117  // extract of the region of interest using the
118  // \doxygen{otb}{VectorDataExtractROI}.
119  //
120  // The first step is to define the region of interest.
121  //
122  // Software Guide : EndLatex
123 
124  // Software Guide : BeginCodeSnippet
125  typedef otb::RemoteSensingRegion<double> RegionType;
126  ImageType::SizeType size;
127  size[0] = 500;
128  size[1] = 500;
129 
130  ImageType::PointType origin;
131  origin[0] = 633602; //UL easting
132  origin[1] = 4961679; //UL northing
133 
134  ImageType::SpacingType spacing;
135  spacing[0] = 56;
136  spacing[1] = -56;
137 
138  RegionType region;
139  RegionType::SizeType sizeInUnit;
140  sizeInUnit[0] = size[0] * spacing[0];
141  sizeInUnit[1] = size[1] * spacing[1];
142  region.SetSize(sizeInUnit);
143  region.SetOrigin(origin);
144  region.SetRegionProjection(projectionRefWkt);
145 
146  // Software Guide : EndCodeSnippet
147 
148  // Software Guide : BeginLatex
149  //
150  // Then, we define and set-up the
151  // \doxygen{otb}{VectorDataExtractROI} filter using the region.
152  //
153  // Software Guide : EndLatex
154 
155  // Software Guide :: BeginCodeSnippet
156 
157  typedef otb::VectorDataExtractROI<VectorDataType> ExtractROIType;
158  ExtractROIType::Pointer extractROI = ExtractROIType::New();
159  extractROI->SetRegion(region);
160  extractROI->SetInput(projection->GetOutput());
161  // Software Guide : EndCodeSnippet
162 
163  // Software Guide : BeginLatex
164  //
165  // Now, we can plug the the ROI filter to the
166  // \doxygen{otb}{VectorDataToMapFilter}.
167  //
168  // Software Guide : EndLatex
169 
170  // Software Guide : BeginCodeSnippet
171 
172  vectorDataRendering->SetInput(extractROI->GetOutput());
173  vectorDataRendering->SetSize(size);
174  vectorDataRendering->SetOrigin(origin);
175  vectorDataRendering->SetSpacing(spacing);
176 
177  // Software Guide : EndCodeSnippet
178 
179  // Software Guide : BeginLatex
180  //
181  // Since we are interested in binary rendering, we need to set the
182  // appropriate rendering style.
183  //
184  // Software Guide : EndLatex
185 
186  // Software Guide : BeginCodeSnippet
187  vectorDataRendering->SetRenderingStyleType(VectorDataToMapFilterType::Binary);
188  // Software Guide : EndCodeSnippet
189 
190  // Software Guide : BeginLatex
191  //
192  // The rendering filter will return a binary image with label 0 when
193  // outside the rasterized vector features and 255 when inside. To
194  // get a fancier rendering we will substitute a blue color to the
195  // foreground value and green to the background value.
196  //
197  // Software Guide : EndLatex
198 
199  // Software Guide : BeginCodeSnippet
200 
201  typedef itk::RGBAPixel<unsigned char> RGBAPixelType;
202  typedef otb::Image<RGBAPixelType, 2> RGBAImageType;
203  typedef itk::ChangeLabelImageFilter<ImageType,
204  RGBAImageType> ChangeLabelImageFilterType;
205 
206  ChangeLabelImageFilterType::Pointer
207  changeLabelFilter = ChangeLabelImageFilterType::New();
208 
209  RGBAPixelType green, blue;
210  green.SetAlpha(255);
211  green.SetGreen(255);
212  blue.SetAlpha(255);
213  blue.SetBlue(255);
214 
215  changeLabelFilter->SetChange(0, blue);
216  changeLabelFilter->SetChange(255, green);
217  changeLabelFilter->SetInput(vectorDataRendering->GetOutput());
218 
219  // Software Guide : EndCodeSnippet
220 
221  // Software Guide : BeginLatex
222  //
223  // Last step is to write the image to the disk using a
224  // \doxygen{otb}{ImageFileWriter}.
225  //
226  // Software Guide : EndLatex
227 
228  // Software Guide : BeginCodeSnippet
229 
230  typedef otb::ImageFileWriter<RGBAImageType> WriterType;
231  WriterType::Pointer writer = WriterType::New();
232  writer->SetInput(changeLabelFilter->GetOutput());
233  writer->SetFileName(argv[2]);
234  writer->Update();
235  // Software Guide : EndCodeSnippet
236 
237  // Software Guide : BeginLatex
238  //
239  // \begin{figure}
240  // \center
241  // \includegraphics[width=0.4\textwidth]{ArcachonRasterizedCoastline.eps}
242  // \itkcaption[Rasterized SRTM water bodies near Arcachon, France.]{Rasterized SRTM water bodies near Arcachon, France.}
243  // \label{fig:RasterizationFilterOutput}
244  // \end{figure}
245  //
246  // Figure \ref{fig:RasterizationFilterOutput} illustrates the use
247  // of the rasterization filter on SRTM water bodies mask near
248  // Arcachon in France. Ocean appears in blue while land appears in green.
249  //
250  // Software Guide : EndLatex
251 
252 
253  return EXIT_SUCCESS;
254 }

Generated at Sun Feb 3 2013 00:59:33 for Orfeo Toolbox with doxygen 1.8.1.1