Skip to content

Commit

Permalink
Add override_srs and default_srs options. (#3273)
Browse files Browse the repository at this point in the history
  • Loading branch information
abellgithub committed Oct 22, 2020
1 parent fb89eda commit 5708e7b
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
10 changes: 10 additions & 0 deletions doc/stages/writers.gdal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,18 @@ width
height
Number of cells in the Y direction. [Default: None]

override_srs
Write the raster with the provided SRS. [Default: None]

default_srs
Write the raster with the provided SRS if none exists. [Default: None]

.. include:: writer_opts.rst

.. note::
You may use the 'bounds' option, or 'origin_x', 'origin_y', 'width'
and 'height', but not both.

.. note::
Unless the raster being written is empty, the spatial reference will automatically
come from the data and does not need to be set with 'override_srs' or 'default_srs'.
14 changes: 14 additions & 0 deletions io/GDALWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ void GDALWriter::addArgs(ProgramArgs& args)
m_width);
m_heightArg = &args.add("height", "Number of cells in the Y direction.",
m_height);

args.add("override_srs", "Spatial reference to apply to data",
m_overrideSrs);
args.addSynonym("override_srs", "spatialreference");

args.add("default_srs", "Spatial reference to apply to data if one cannot be inferred",
m_defaultSrs);
}


Expand Down Expand Up @@ -117,6 +124,9 @@ void GDALWriter::initialize()
throwError("Invalid output type: '" + ts + "'.");
}

if (m_overrideSrs.valid() && m_defaultSrs.valid())
throwError("Can't set both 'override_srs' and 'default_srs'.");

if (!m_radiusArg->set())
m_radius = m_edgeLength * sqrt(2.0);

Expand Down Expand Up @@ -168,6 +178,10 @@ void GDALWriter::readyFile(const std::string& filename,
{
m_outputFilename = filename;
m_srs = srs;
if (!m_overrideSrs.empty())
m_srs = m_overrideSrs;
if (m_srs.empty())
m_srs = m_defaultSrs;
m_grid.reset();
if (m_fixedGrid)
createGrid(m_bounds.to2d());
Expand Down
2 changes: 2 additions & 0 deletions io/GDALWriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ class PDAL_DLL GDALWriter : public FlexWriter, public Streamable
Dimension::Type m_dataType;
bool m_expandByPoint;
bool m_fixedGrid;
SpatialReference m_defaultSrs;
SpatialReference m_overrideSrs;
};

}
46 changes: 46 additions & 0 deletions test/unit/io/GDALWriterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -850,4 +850,50 @@ TEST(GDALWriterTest, alternate_grid)
}
}

TEST(GDALWriterTest, srs)
{
auto test = [](const std::string& sourceSrs, const std::string& defaultSrs,
const std::string& overrideSrs, const std::string& testSrs)
{
std::string outfile(Support::temppath("out.tif"));

TextReader t;
Options to;
to.add("filename", Support::datapath("text/with_edgeofflightline.txt"));
if (sourceSrs.size())
to.add("override_srs", sourceSrs);
t.setOptions(to);

GDALWriter w;
Options wo;
wo.add("filename", outfile);
wo.add("origin_x", 0);
wo.add("origin_y", 0);
wo.add("width", 1000);
wo.add("height", 1000);
wo.add("resolution", 10);
if (defaultSrs.size())
wo.add("default_srs", defaultSrs);
if (overrideSrs.size())
wo.add("override_srs", overrideSrs);
w.setOptions(wo);
w.setInput(t);

FileUtils::deleteFile(outfile);
PointTable table;
w.prepare(table);
w.execute(table);

gdal::Raster raster(outfile);
raster.open();
EXPECT_EQ(raster.getSpatialRef(), testSrs);
};

test("", "EPSG:4326", "", "EPSG:4326");
test("", "", "EPSG:4326", "EPSG:4326");
test("EPSG:4326", "EPSG:2030", "", "EPSG:4326");
test("EPSG:4326", "", "EPSG:2030", "EPSG:2030");
EXPECT_THROW(test("EPSG:4326", "EPSG:4326", "EPSG:2030", "EPSG:2030"), pdal_error);
}

} // namespace pdal

0 comments on commit 5708e7b

Please sign in to comment.