Skip to content

Commit 21c4bb0

Browse files
committed
Ensure geojson outputs are correctly georeferenced
1 parent 1dc5587 commit 21c4bb0

File tree

4 files changed

+29
-23
lines changed

4 files changed

+29
-23
lines changed

src/core.cpp

-5
Original file line numberDiff line numberDiff line change
@@ -1601,11 +1601,6 @@ void core::init(int argc, char **argv)
16011601
}
16021602
}
16031603

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

16101605
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
16111606
_cfg = cfg;

src/mesh/triangulation.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1451,7 +1451,7 @@ void triangulation::reorder_faces(std::vector<size_t> permutation)
14511451

14521452
void triangulation::write_bbox_geojson(const std::string& filename)
14531453
{
1454-
gis::bbox2geojson(_bounding_box.x_min, _bounding_box.y_min, _bounding_box.x_max, _bounding_box.y_max, filename);
1454+
gis::bbox2geojson(_bounding_box.x_min, _bounding_box.y_min, _bounding_box.x_max, _bounding_box.y_max, filename, proj4());
14551455
}
14561456

14571457
void triangulation::load_partition_from_mesh(const std::string& mesh_filename)

src/utility/gis.cpp

+26-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace gis
44
{
55

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

22-
OGRLayer *poLayer = poDS->CreateLayer("bbox", nullptr, wkbPolygon, nullptr);
22+
OGRSpatialReference oSRS;
23+
24+
if (oSRS.importFromProj4(proj4.c_str()) != OGRERR_NONE)
25+
{
26+
CHM_THROW_EXCEPTION(chm_error, "Failed to import PROJ.4 string");
27+
}
28+
29+
char **papszOptions = nullptr;
30+
papszOptions = CSLAddNameValue(papszOptions, "RFC7946", "TRUE");
31+
OGRLayer *poLayer = poDS->CreateLayer("bbox", &oSRS, wkbPolygon, papszOptions);
2332
if (poLayer == nullptr)
2433
{
2534
CHM_THROW_EXCEPTION(chm_error, "Layer creation failed");
2635
}
2736

37+
2838
// Create a polygon from the bounding box
2939
OGRPolygon polygon;
3040
OGRLinearRing ring;
@@ -49,11 +59,13 @@ namespace gis
4959
CHM_THROW_EXCEPTION(chm_error, "Failed to create feature in GeoJSON");
5060
}
5161

62+
CSLDestroy(papszOptions);
5263
OGRFeature::DestroyFeature(poFeature);
64+
5365
GDALClose(poDS);
5466
}
5567

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

72-
OGRLayer *poLayer = poDS->CreateLayer("layer", nullptr, wkbPoint, nullptr);
84+
OGRSpatialReference oSRS;
85+
86+
if (oSRS.importFromProj4(proj4.c_str()) != OGRERR_NONE)
87+
{
88+
CHM_THROW_EXCEPTION(chm_error, "Failed to import PROJ.4 string");
89+
}
90+
char **papszOptions = nullptr;
91+
papszOptions = CSLAddNameValue(papszOptions, "RFC7946", "TRUE");
92+
93+
OGRLayer *poLayer = poDS->CreateLayer("layer", &oSRS, wkbPoint, papszOptions);
7394
if (poLayer == nullptr)
7495
{
7596
CHM_THROW_EXCEPTION(chm_error, "Layer creation failed");
7697
}
7798

78-
// OGRSpatialReference oSRS;
79-
80-
// if (oSRS.importFromProj4(proj4.c_str()) != OGRERR_NONE)
81-
// {
82-
// CHM_THROW_EXCEPTION(chm_error, "Failed to import PROJ.4 string");
83-
// }
84-
// if (poDS->SetProjection(proj4.c_str()) != CE_None)
85-
// {
86-
// CHM_THROW_EXCEPTION(chm_error, "Failed to set spatial reference");
87-
// }
88-
8999
OGRFieldDefn oFieldX("X", OFTReal);
90100
oFieldX.SetWidth(32);
91101
poLayer->CreateField(&oFieldX);
@@ -121,6 +131,7 @@ namespace gis
121131

122132
OGRFeature::DestroyFeature(poFeature);
123133
}
134+
CSLDestroy(papszOptions);
124135
GDALClose(poDS);
125136
}
126137

src/utility/gis.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

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

4141
}

0 commit comments

Comments
 (0)