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
|
/*
* 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.
*/
#include "otbWrapperApplication.h"
#include "otbWrapperApplicationFactory.h"
#include "otbSpatialReference.h"
#include "otbOGRDataSourceToLabelImageFilter.h"
#include "otbGenericRSTransform.h"
namespace otb
{
namespace Wrapper
{
class Rasterization : public Application
{
public:
/** Standard class typedefs. */
typedef Rasterization Self;
typedef Application Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
/** Standard macro */
itkNewMacro(Self);
itkTypeMacro(Rasterization, otb::Application);
/** Filters typedef */
// the application produces a binary mask : no need to use a FloatVectorImageType
typedef UInt8ImageType::PointType PointType;
typedef UInt8ImageType::SizeType SizeType;
typedef UInt8ImageType::SpacingType SpacingType;
typedef UInt8ImageType::IndexType IndexType;
// Misc
typedef otb::GenericRSTransform<> RSTransformType;
typedef otb::PipelineMemoryPrintCalculator MemoryCalculatorType;
// Exact rasterization mode
typedef otb::OGRDataSourceToLabelImageFilter<FloatImageType> OGRDataSourceToMapFilterType;
private:
void DoInit() override
{
SetName("Rasterization");
SetDescription("Reproject and rasterize a vector dataset");
SetDocLongDescription(
"Reproject and rasterize a vector dataset. The grid of the rasterized output can be set by using a reference image, or by "
"setting all parmeters (origin, size, spacing) by hand. In the latter case, at least the spacing (ground sampling distance) is needed (other "
"parameters are computed automatically). The rasterized output can also be in a different projection reference system than the input dataset.\n\n"
"There are two rasterize mode available in the application. The first is the binary mode: it allows rendering all pixels belonging to a geometry of "
"the "
"input dataset in the foreground color, while rendering the other in background color. The second one allows rendering pixels belonging to a geometry "
"with respect to an attribute of this geometry. The field of the attribute to render can be set by the user. In the second mode, the background value "
"is still used for unassociated pixels.");
SetDocLimitations("None");
SetDocAuthors("OTB-Team");
SetDocSeeAlso("For now, support of input dataset with multiple layers having different projection reference system is limited.");
AddDocTag(Tags::Vector);
AddParameter(ParameterType_InputVectorData, "in", "Input vector dataset");
SetParameterDescription("in", "The input vector dataset to be rasterized");
AddParameter(ParameterType_OutputImage, "out", "Output image");
SetParameterDescription("out", "An output image containing the rasterized vector dataset");
AddParameter(ParameterType_InputImage, "im", "Input reference image");
SetParameterDescription("im", "A reference image from which to import output grid and projection reference system information.");
MandatoryOff("im");
AddParameter(ParameterType_Int, "szx", "Output size x");
SetParameterDescription("szx", "Output size along x axis (useless if support image is given)");
MandatoryOff("szx");
SetMinimumParameterIntValue("szx", 1);
AddParameter(ParameterType_Int, "szy", "Output size y");
SetParameterDescription("szy", "Output size along y axis (useless if support image is given)");
MandatoryOff("szy");
SetMinimumParameterIntValue("szy", 1);
AddParameter(ParameterType_Int, "epsg", "Output EPSG code");
SetParameterDescription(
"epsg", "EPSG code for the output projection reference system (EPSG 4326 for WGS84, 32631 for UTM31N...,useless if support image is given)");
MandatoryOff("epsg");
AddParameter(ParameterType_Float, "orx", "Output Upper-left x");
SetParameterDescription("orx", "Output upper-left corner x coordinate (useless if support image is given)");
MandatoryOff("orx");
AddParameter(ParameterType_Float, "ory", "Output Upper-left y");
SetParameterDescription("ory", "Output upper-left corner y coordinate (useless if support image is given)");
MandatoryOff("ory");
AddParameter(ParameterType_Float, "spx", "Spacing (GSD) x");
SetParameterDescription("spx", "Spacing (ground sampling distance) along x axis (useless if support image is given)");
MandatoryOff("spx");
AddParameter(ParameterType_Float, "spy", "Spacing (GSD) y");
SetParameterDescription("spy", "Spacing (ground sampling distance) along y axis (useless if support image is given)");
MandatoryOff("spy");
AddParameter(ParameterType_Float, "background", "Background value");
SetParameterDescription("background", "Default value for pixels not belonging to any geometry");
SetDefaultParameterFloat("background", 0.);
AddParameter(ParameterType_Choice, "mode", "Rasterization mode");
SetParameterDescription("mode", "Choice of rasterization modes");
AddChoice("mode.binary", "Binary mode");
SetParameterDescription("mode.binary", "In this mode, pixels within a geometry will hold the user-defined foreground value");
AddParameter(ParameterType_Float, "mode.binary.foreground", "Foreground value");
SetParameterDescription("mode.binary.foreground", "Value for pixels inside a geometry");
SetDefaultParameterFloat("mode.binary.foreground", 255);
AddChoice("mode.attribute", "Attribute burning mode");
SetParameterDescription("mode.attribute",
"In this mode, pixels within a geometry will hold the value of a user-defined field extracted from this geometry.");
AddParameter(ParameterType_String, "mode.attribute.field", "The attribute field to burn");
SetParameterDescription("mode.attribute.field", "Name of the attribute field to burn");
SetParameterString("mode.attribute.field", "DN");
AddRAMParameter();
SetDocExampleParameterValue("in", "qb_RoadExtract_classification.shp");
SetDocExampleParameterValue("out", "rasterImage.tif");
SetDocExampleParameterValue("spx", "1.");
SetDocExampleParameterValue("spy", "1.");
SetOfficialDocLink();
}
void DoUpdateParameters() override
{
// Nothing to do
}
void DoExecute() override
{
otb::ogr::DataSource::Pointer ogrDS;
UInt8ImageType::Pointer referenceImage;
ogrDS = otb::ogr::DataSource::New(GetParameterString("in"), otb::ogr::DataSource::Modes::Read);
bool validInputProjRef = false;
std::string inputProjectionRef = "";
// Retrieve extent
double ulx, uly, lrx, lry;
bool extentAvailable = true;
try
{
inputProjectionRef = ogrDS->GetGlobalExtent(ulx, uly, lrx, lry);
}
catch (const itk::ExceptionObject&)
{
extentAvailable = false;
}
if (!extentAvailable && (!(HasValue("spx") && HasValue("spy")) || (!(HasValue("orx") && HasValue("ory")))))
{
otbAppLogWARNING(<< "Failed to retrieve the spatial extent of the dataset. The application will retry in force mode, which means it might have to walk "
"the entire dataset to determine extent. This might be a long process for large datasets. Consider setting the orx, ory, spx and spy "
"parameters.");
try
{
inputProjectionRef = ogrDS->GetGlobalExtent(ulx, uly, lrx, lry, true);
extentAvailable = true;
}
catch (itk::ExceptionObject& err)
{
extentAvailable = false;
otbAppLogFATAL(<< "Failed to retrieve the spatial extent of the dataset in force mode. The spatial extent is mandatory when orx, ory, spx and spy "
"parameters are not set, consider setting them. Error from library: "
<< err.GetDescription());
}
}
if (extentAvailable)
{
otbAppLogINFO("Input dataset extent is (" << ulx << ", " << uly << ") (" << lrx << ", " << lry << ")");
}
if (inputProjectionRef == "")
{
otbAppLogWARNING(
"Failed to find a valid projection ref in dataset. The application will assume that the given reference image or origin, spacing and size are "
"consistent with the dataset geometry. Output EPSG code will be ignored.");
validInputProjRef = false;
}
else
{
validInputProjRef = true;
otbAppLogINFO("Input dataset projection reference system is: " << inputProjectionRef);
}
// region information
SizeType size;
PointType origin;
SpacingType spacing;
// reading projection information
// two choice :
std::string outputProjectionRef;
// a reference image is given as input
if (HasValue("im"))
{
if (HasValue("szx") || HasValue("szy") || HasValue("orx") || HasValue("ory") || HasValue("spx") || HasValue("spy") || HasValue("epsg"))
{
otbAppLogWARNING(
"A reference image has been given, other parameters "
"regarding the output image will be ignored");
}
referenceImage = GetParameterUInt8Image("im");
outputProjectionRef = referenceImage->GetProjectionRef();
size = referenceImage->GetLargestPossibleRegion().GetSize();
origin = referenceImage->GetOrigin();
spacing = referenceImage->GetSignedSpacing();
}
else if (HasValue("spx") && HasValue("spy"))
{
if (HasValue("epsg"))
{
unsigned int RSID = GetParameterInt("epsg");
outputProjectionRef = otb::SpatialReference::FromEPSG(RSID).ToWkt();
}
else
{
outputProjectionRef = inputProjectionRef;
}
PointType corner;
spacing[0] = GetParameterFloat("spx");
spacing[1] = GetParameterFloat("spy");
if (HasValue("orx") && HasValue("ory"))
{
corner[0] = GetParameterFloat("orx");
corner[1] = GetParameterFloat("ory");
}
else if (extentAvailable)
{
corner[0] = (spacing[0] > 0 ? ulx : lrx);
corner[1] = (spacing[1] > 0 ? uly : lry);
// Transform to output EPSG
if (validInputProjRef)
{
RSTransformType::Pointer rsTransform = RSTransformType::New();
rsTransform->SetInputProjectionRef(inputProjectionRef);
rsTransform->SetOutputProjectionRef(outputProjectionRef);
rsTransform->InstantiateTransform();
corner = rsTransform->TransformPoint(corner);
}
}
else
{
otbAppLogFATAL(<< "The orx and ory parameters are not set and the dataset extent could not be retrieved. The application can not determine the origin "
"of the output raster");
}
origin[0] = corner[0] + 0.5 * spacing[0];
origin[1] = corner[1] + 0.5 * spacing[1];
if (HasValue("szx") && HasValue("szy"))
{
size[0] = GetParameterInt("szx");
size[1] = GetParameterInt("szy");
}
else if (extentAvailable)
{
// Transform to output EPSG
PointType lrout;
lrout[0] = (spacing[0] > 0 ? lrx : ulx);
lrout[1] = (spacing[1] > 0 ? lry : uly);
if (validInputProjRef)
{
RSTransformType::Pointer rsTransform = RSTransformType::New();
rsTransform->SetInputProjectionRef(inputProjectionRef);
rsTransform->SetOutputProjectionRef(outputProjectionRef);
rsTransform->InstantiateTransform();
lrout = rsTransform->TransformPoint(lrout);
}
size[0] = static_cast<unsigned int>((lrout[0] - corner[0]) / spacing[0]);
size[1] = static_cast<unsigned int>((lrout[1] - corner[1]) / spacing[1]);
}
else
{
otbAppLogFATAL(<< "The szx and szy parameters are not set and the dataset extent could not be retrieved. The application can not deterimine the size "
"of the output raster");
}
}
else
{
otbAppLogFATAL("No reference image was given, at least spx and spy parameters must be set.");
}
m_OGRDataSourceRendering = OGRDataSourceToMapFilterType::New();
m_OGRDataSourceRendering->AddOGRDataSource(ogrDS);
m_OGRDataSourceRendering->SetOutputSize(size);
m_OGRDataSourceRendering->SetOutputOrigin(origin);
m_OGRDataSourceRendering->SetOutputSpacing(spacing);
m_OGRDataSourceRendering->SetBackgroundValue(GetParameterFloat("background"));
if (GetParameterString("mode") == "binary")
{
m_OGRDataSourceRendering->SetBurnAttributeMode(false);
m_OGRDataSourceRendering->SetForegroundValue(GetParameterFloat("mode.binary.foreground"));
}
else if (GetParameterString("mode") == "attribute")
{
m_OGRDataSourceRendering->SetBurnAttributeMode(true);
m_OGRDataSourceRendering->SetBurnAttribute(GetParameterString("mode.attribute.field"));
}
if (validInputProjRef)
{
m_OGRDataSourceRendering->SetOutputProjectionRef(outputProjectionRef);
}
otbAppLogINFO("Output projection reference system is: " << outputProjectionRef);
otbAppLogINFO("Output origin: " << origin);
otbAppLogINFO("Output size: " << size);
otbAppLogINFO("Output spacing: " << spacing);
SetParameterOutputImage<FloatImageType>("out", m_OGRDataSourceRendering->GetOutput());
}
OGRDataSourceToMapFilterType::Pointer m_OGRDataSourceRendering;
};
}
}
OTB_APPLICATION_EXPORT(otb::Wrapper::Rasterization)
|