1 /******************************************************************************
2 * Copyright (c) 2015, Howard Butler (howard@hobu.co)
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following
8 * conditions are met:
9 *
10 *     * Redistributions of source code must retain the above copyright
11 *       notice, this list of conditions and the following disclaimer.
12 *     * Redistributions in binary form must reproduce the above copyright
13 *       notice, this list of conditions and the following disclaimer in
14 *       the documentation and/or other materials provided
15 *       with the distribution.
16 *     * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
17 *       names of its contributors may be used to endorse or promote
18 *       products derived from this software without specific prior
19 *       written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 * OF SUCH DAMAGE.
33 ****************************************************************************/
34 
35 #include "DensityKernel.hpp"
36 
37 #include "../filters/HexBinFilter.hpp"
38 #include "private/density/OGR.hpp"
39 
40 #include <pdal/util/FileUtils.hpp>
41 
42 namespace pdal
43 {
44 
45 static PluginInfo const s_info
46 {
47     "kernels.density",
48     "Density Kernel",
49     "http://pdal.io/apps/density.html"
50 };
51 
CREATE_STATIC_KERNEL(DensityKernel,s_info)52 CREATE_STATIC_KERNEL(DensityKernel, s_info)
53 
54 std::string DensityKernel::getName() const { return s_info.name; }
55 
addSwitches(ProgramArgs & args)56 void DensityKernel::addSwitches(ProgramArgs& args)
57 {
58     args.add("input,i", "input point cloud file name", m_inputFile).
59         setPositional();
60     args.add("output,o", "output vector data source", m_outputFile).
61         setPositional();
62     args.add("ogrdriver,f", "OGR driver name to use ", m_driverName,
63         "ESRI Shapefile");
64     args.add("lyr_name", "OGR layer name to use", m_layerName, "");
65     args.add("sample_size", "Sample size for auto-edge length calculation",
66         m_sampleSize, 5000U);
67     args.add("threshold", "Required cell density", m_density, 15);
68     args.add("edge_length", "Length of hex edge", m_edgeLength);
69     args.add("hole_cull_area_tolerance", "Tolerance area to "
70             "apply to holes before cull", m_cullArea);
71     args.add("smooth", "Smooth boundary output", m_doSmooth, true);
72 }
73 
74 
outputDensity(pdal::SpatialReference const & reference)75 void DensityKernel::outputDensity(pdal::SpatialReference const& reference)
76 {
77     HexBin* hexbin = dynamic_cast<HexBin*>(m_hexbinStage);
78     if (!hexbin)
79         throw pdal::pdal_error("unable to fetch filters.hexbin stage!");
80 
81     hexer::HexGrid* grid = hexbin->grid();
82 
83     OGR writer(m_outputFile, reference.getWKT(), m_driverName, m_layerName);
84     writer.writeDensity(grid);
85 }
86 
87 
execute()88 int DensityKernel::execute()
89 {
90     if (m_inputFile == "STDIN" ||
91         (FileUtils::extension(m_inputFile) == ".xml" ||
92         FileUtils::extension(m_inputFile) == ".json"))
93     {
94         m_manager.readPipeline(m_inputFile);
95     }
96     else
97     {
98         m_manager.makeReader(m_inputFile, "");
99     }
100     Options options;
101     options.add("sample_size", m_sampleSize);
102     options.add("threshold", m_density);
103     options.add("edge_length", m_edgeLength);
104     options.add("hole_cull_area_tolerance", m_cullArea);
105     options.add("smooth", m_doSmooth);
106     m_hexbinStage = &(m_manager.makeFilter("filters.hexbin",
107         *m_manager.getStage(), options));
108     m_manager.execute();
109     outputDensity(m_manager.pointTable().anySpatialReference());
110     return 0;
111 }
112 
113 } // namespace pdal
114