Skip to content

Commit

Permalink
Ensure geojson outputs are correctly georeferenced
Browse files Browse the repository at this point in the history
  • Loading branch information
Chrismarsh committed Feb 10, 2025
1 parent 1dc5587 commit 21c4bb0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
5 changes: 0 additions & 5 deletions src/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1601,11 +1601,6 @@ void core::init(int argc, char **argv)
}
}

// needs to go here as both mesh needs to be loaded and the output dir needs to be known
boost::filesystem::create_directories(output_folder_path / "mesh_boundingbox");
auto mesh_boundingbox_path = output_folder_path / "mesh_boundingbox" / std::format("mesh_bbox_{}.geojson", _comm_world.rank());
_mesh->write_bbox_geojson(mesh_boundingbox_path.string());


pt::json_parser::write_json((output_folder_path / "config.json" ).string(),cfg); // output a full dump of the cfg, after all modifications, to the output directory
_cfg = cfg;
Expand Down
2 changes: 1 addition & 1 deletion src/mesh/triangulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1451,7 +1451,7 @@ void triangulation::reorder_faces(std::vector<size_t> permutation)

void triangulation::write_bbox_geojson(const std::string& filename)
{
gis::bbox2geojson(_bounding_box.x_min, _bounding_box.y_min, _bounding_box.x_max, _bounding_box.y_max, filename);
gis::bbox2geojson(_bounding_box.x_min, _bounding_box.y_min, _bounding_box.x_max, _bounding_box.y_max, filename, proj4());
}

void triangulation::load_partition_from_mesh(const std::string& mesh_filename)
Expand Down
41 changes: 26 additions & 15 deletions src/utility/gis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace gis
{

void bbox2geojson(double xmin, double ymin, double xmax, double ymax, const std::string& filepath)
void bbox2geojson(double xmin, double ymin, double xmax, double ymax, const std::string& filepath, const std::string& proj4)
{
GDALAllRegister();
const char *pszDriverName = "GeoJSON";
Expand All @@ -19,12 +19,22 @@ namespace gis
CHM_THROW_EXCEPTION(chm_error, "Creation of output file failed");
}

OGRLayer *poLayer = poDS->CreateLayer("bbox", nullptr, wkbPolygon, nullptr);
OGRSpatialReference oSRS;

if (oSRS.importFromProj4(proj4.c_str()) != OGRERR_NONE)
{
CHM_THROW_EXCEPTION(chm_error, "Failed to import PROJ.4 string");
}

char **papszOptions = nullptr;
papszOptions = CSLAddNameValue(papszOptions, "RFC7946", "TRUE");
OGRLayer *poLayer = poDS->CreateLayer("bbox", &oSRS, wkbPolygon, papszOptions);
if (poLayer == nullptr)
{
CHM_THROW_EXCEPTION(chm_error, "Layer creation failed");
}


// Create a polygon from the bounding box
OGRPolygon polygon;
OGRLinearRing ring;
Expand All @@ -49,11 +59,13 @@ namespace gis
CHM_THROW_EXCEPTION(chm_error, "Failed to create feature in GeoJSON");
}

CSLDestroy(papszOptions);
OGRFeature::DestroyFeature(poFeature);

GDALClose(poDS);
}

void xy2geojson(std::vector<std::tuple<float, float>> xy, std::string filepath, std::string proj4)
void xy2geojson(std::vector<std::tuple<float, float>> xy, const std::string& filepath, const std::string& proj4)
{
GDALAllRegister();
const char *pszDriverName = "GeoJSON";
Expand All @@ -69,23 +81,21 @@ namespace gis
CHM_THROW_EXCEPTION(chm_error, "Creation of output file failed");
}

OGRLayer *poLayer = poDS->CreateLayer("layer", nullptr, wkbPoint, nullptr);
OGRSpatialReference oSRS;

if (oSRS.importFromProj4(proj4.c_str()) != OGRERR_NONE)
{
CHM_THROW_EXCEPTION(chm_error, "Failed to import PROJ.4 string");
}
char **papszOptions = nullptr;
papszOptions = CSLAddNameValue(papszOptions, "RFC7946", "TRUE");

OGRLayer *poLayer = poDS->CreateLayer("layer", &oSRS, wkbPoint, papszOptions);
if (poLayer == nullptr)
{
CHM_THROW_EXCEPTION(chm_error, "Layer creation failed");
}

// OGRSpatialReference oSRS;

// if (oSRS.importFromProj4(proj4.c_str()) != OGRERR_NONE)
// {
// CHM_THROW_EXCEPTION(chm_error, "Failed to import PROJ.4 string");
// }
// if (poDS->SetProjection(proj4.c_str()) != CE_None)
// {
// CHM_THROW_EXCEPTION(chm_error, "Failed to set spatial reference");
// }

OGRFieldDefn oFieldX("X", OFTReal);
oFieldX.SetWidth(32);
poLayer->CreateField(&oFieldX);
Expand Down Expand Up @@ -121,6 +131,7 @@ namespace gis

OGRFeature::DestroyFeature(poFeature);
}
CSLDestroy(papszOptions);
GDALClose(poDS);
}

Expand Down
4 changes: 2 additions & 2 deletions src/utility/gis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

namespace gis
{
void bbox2geojson(double xmin, double ymin, double xmax, double ymax, const std::string& filepath);
void xy2geojson(std::vector<std::tuple<float, float>> xy, std::string filepath, std::string proj4);
void bbox2geojson(double xmin, double ymin, double xmax, double ymax, const std::string& filepath, const std::string& proj4);
void xy2geojson(std::vector<std::tuple<float, float>> xy, const std::string& filepath, const std::string& proj4);

}

0 comments on commit 21c4bb0

Please sign in to comment.