Skip to content

Commit

Permalink
fix GDAL output crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Ylannl committed Apr 2, 2024
1 parent 98b07c7 commit fbe0015
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
32 changes: 14 additions & 18 deletions src/Map3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
*/

#include "Map3d.h"
#include <ogrsf_frmts.h>

Map3d::Map3d() {
OGRRegisterAll();
Expand Down Expand Up @@ -488,7 +489,6 @@ bool Map3d::get_postgis_output(std::string connstr, bool pdok, bool citygml) {
std::cerr << "ERROR: Cannot open database '" + connstr + "' for writing" << std::endl;
dataSource->RollbackTransaction();
GDALClose(dataSource);
// GDALClose(driver);
return false;
}
layers.emplace(layername, layer);
Expand Down Expand Up @@ -544,7 +544,6 @@ bool Map3d::get_postgis_output(std::string connstr, bool pdok, bool citygml) {
return false;
}
GDALClose(dataSource);
// GDALClose(driver);
return true;
#endif
}
Expand All @@ -559,32 +558,30 @@ bool Map3d::get_gdal_output(std::string filename, std::string drivername, bool m
GDALDriver *driver = GetGDALDriverManager()->GetDriverByName(drivername.c_str());

if (!multi) {
OGRLayer *layer = create_gdal_layer(driver, NULL, filename, "my3dmap", AttributeMap(), true);
GDALDataset* dataSource;
OGRLayer *layer = create_gdal_layer(driver, dataSource, filename, "my3dmap", AttributeMap(), true);
if (layer == NULL) {
std::cerr << "ERROR: Cannot open file '" + filename + "' for writing" << std::endl;
GDALClose(layer);
// GDALClose(driver);
return false;
}
for (auto& f : _lsFeatures) {
f->get_shape(layer, false);
}
GDALClose(layer);
// GDALClose(driver);
GDALClose(dataSource);
}
else {
std::unordered_map<std::string, OGRLayer*> layers;
GDALDataset* dataSource;
for (auto& f : _lsFeatures) {
std::string layername = f->get_layername();
if (layers.find(layername) == layers.end()) {
std::string tmpFilename = filename;
if (drivername == "ESRI Shapefile") {
tmpFilename = filename + layername;
}
OGRLayer *layer = create_gdal_layer(driver, NULL, tmpFilename, layername, f->get_attributes(), f->get_class() == BUILDING);
OGRLayer *layer = create_gdal_layer(driver, dataSource, tmpFilename, layername, f->get_attributes(), f->get_class() == BUILDING);
if (layer == NULL) {
std::cerr << "ERROR: Cannot open file '" + filename + "' for writing" << std::endl;
close_gdal_resources(driver, layers);
return false;
}
layers.emplace(layername, layer);
Expand All @@ -593,20 +590,19 @@ bool Map3d::get_gdal_output(std::string filename, std::string drivername, bool m
return false;
}
}
close_gdal_resources(driver, layers);
GDALClose(dataSource);
}
return true;
#endif
}

#if GDAL_VERSION_MAJOR >= 2
void Map3d::close_gdal_resources(GDALDriver* driver, std::unordered_map<std::string, OGRLayer*> layers) {
for (auto& layer : layers) {
GDALClose(layer.second);
}
// GDALClose(driver);
}
#endif
// #if GDAL_VERSION_MAJOR >= 2
// void Map3d::close_gdal_resources(GDALDriver* driver, std::unordered_map<std::string, OGRLayer*> layers) {
// for (auto& layer : layers) {
// GDALClose(layer.second);
// }
// }
// #endif

#if GDAL_VERSION_MAJOR >= 2
OGRLayer* Map3d::create_gdal_layer(GDALDriver* driver, GDALDataset* dataSource, std::string filename, std::string layername, AttributeMap attributes, bool addHeightAttributes) {
Expand Down
1 change: 0 additions & 1 deletion src/Map3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ class Map3d {
#else
bool extract_and_add_polygon(GDALDataset* dataSource, PolygonFile* file);
OGRLayer* create_gdal_layer(GDALDriver* driver, GDALDataset* dataSource, std::string filename, std::string layername, AttributeMap attributes, bool addHeightAttributes);
void close_gdal_resources(GDALDriver* driver, std::unordered_map<std::string, OGRLayer*> layers);
#endif
void extract_feature(OGRFeature * f, std::string layerName, const char * idfield, const char * heightfield, std::string layertype, bool multiple_heights);
void stitch_one_vertex(TopoFeature* f, int ringi, int pi, std::vector< std::tuple<TopoFeature*, int, int> >& star);
Expand Down

0 comments on commit fbe0015

Please sign in to comment.