Skip to content

Commit

Permalink
Add WKT function
Browse files Browse the repository at this point in the history
  • Loading branch information
mpartio committed Jan 5, 2024
1 parent f0d38bc commit 5e1e807
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions himan-lib/include/grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ class regular_grid : public grid
virtual double Dj() const;

virtual std::string Proj4String() const;
virtual std::string WKT(const std::map<std::string, std::string>& opts = {}) const;
virtual std::unique_ptr<OGRPolygon> Geometry() const;
virtual earth_shape<double> EarthShape() const override;

Expand Down
28 changes: 28 additions & 0 deletions himan-lib/source/grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,34 @@ std::string regular_grid::Proj4String() const
return proj;
}

std::string regular_grid::WKT(const std::map<std::string, std::string>& opts) const
{
if (itsSpatialReference == nullptr)
{
throw std::runtime_error("Spatial reference instance not initialized");
}

char** projopts = nullptr;

// https://gdal.org/doxygen/classOGRSpatialReference.html#ae986da88649783b5c194de55c46890a5
for (const auto& opt : opts)
{
projopts = CSLAddNameValue(projopts, opt.first.c_str(), opt.second.c_str());
}

char* wkt;
if (itsSpatialReference->exportToWkt(&wkt) != OGRERR_NONE)
{
throw std::runtime_error("Failed to get wkt");
}

std::string w(wkt);
CPLFree(wkt);
CSLDestroy(projopts);

return w;
}

point regular_grid::Projected(const point& latlon) const
{
double projX = latlon.X(), projY = latlon.Y();
Expand Down

0 comments on commit 5e1e807

Please sign in to comment.