Skip to content

Commit fbe0015

Browse files
committed
fix GDAL output crash
1 parent 98b07c7 commit fbe0015

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

src/Map3d.cpp

+14-18
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
*/
3434

3535
#include "Map3d.h"
36+
#include <ogrsf_frmts.h>
3637

3738
Map3d::Map3d() {
3839
OGRRegisterAll();
@@ -488,7 +489,6 @@ bool Map3d::get_postgis_output(std::string connstr, bool pdok, bool citygml) {
488489
std::cerr << "ERROR: Cannot open database '" + connstr + "' for writing" << std::endl;
489490
dataSource->RollbackTransaction();
490491
GDALClose(dataSource);
491-
// GDALClose(driver);
492492
return false;
493493
}
494494
layers.emplace(layername, layer);
@@ -544,7 +544,6 @@ bool Map3d::get_postgis_output(std::string connstr, bool pdok, bool citygml) {
544544
return false;
545545
}
546546
GDALClose(dataSource);
547-
// GDALClose(driver);
548547
return true;
549548
#endif
550549
}
@@ -559,32 +558,30 @@ bool Map3d::get_gdal_output(std::string filename, std::string drivername, bool m
559558
GDALDriver *driver = GetGDALDriverManager()->GetDriverByName(drivername.c_str());
560559

561560
if (!multi) {
562-
OGRLayer *layer = create_gdal_layer(driver, NULL, filename, "my3dmap", AttributeMap(), true);
561+
GDALDataset* dataSource;
562+
OGRLayer *layer = create_gdal_layer(driver, dataSource, filename, "my3dmap", AttributeMap(), true);
563563
if (layer == NULL) {
564564
std::cerr << "ERROR: Cannot open file '" + filename + "' for writing" << std::endl;
565-
GDALClose(layer);
566-
// GDALClose(driver);
567565
return false;
568566
}
569567
for (auto& f : _lsFeatures) {
570568
f->get_shape(layer, false);
571569
}
572-
GDALClose(layer);
573-
// GDALClose(driver);
570+
GDALClose(dataSource);
574571
}
575572
else {
576573
std::unordered_map<std::string, OGRLayer*> layers;
574+
GDALDataset* dataSource;
577575
for (auto& f : _lsFeatures) {
578576
std::string layername = f->get_layername();
579577
if (layers.find(layername) == layers.end()) {
580578
std::string tmpFilename = filename;
581579
if (drivername == "ESRI Shapefile") {
582580
tmpFilename = filename + layername;
583581
}
584-
OGRLayer *layer = create_gdal_layer(driver, NULL, tmpFilename, layername, f->get_attributes(), f->get_class() == BUILDING);
582+
OGRLayer *layer = create_gdal_layer(driver, dataSource, tmpFilename, layername, f->get_attributes(), f->get_class() == BUILDING);
585583
if (layer == NULL) {
586584
std::cerr << "ERROR: Cannot open file '" + filename + "' for writing" << std::endl;
587-
close_gdal_resources(driver, layers);
588585
return false;
589586
}
590587
layers.emplace(layername, layer);
@@ -593,20 +590,19 @@ bool Map3d::get_gdal_output(std::string filename, std::string drivername, bool m
593590
return false;
594591
}
595592
}
596-
close_gdal_resources(driver, layers);
593+
GDALClose(dataSource);
597594
}
598595
return true;
599596
#endif
600597
}
601598

602-
#if GDAL_VERSION_MAJOR >= 2
603-
void Map3d::close_gdal_resources(GDALDriver* driver, std::unordered_map<std::string, OGRLayer*> layers) {
604-
for (auto& layer : layers) {
605-
GDALClose(layer.second);
606-
}
607-
// GDALClose(driver);
608-
}
609-
#endif
599+
// #if GDAL_VERSION_MAJOR >= 2
600+
// void Map3d::close_gdal_resources(GDALDriver* driver, std::unordered_map<std::string, OGRLayer*> layers) {
601+
// for (auto& layer : layers) {
602+
// GDALClose(layer.second);
603+
// }
604+
// }
605+
// #endif
610606

611607
#if GDAL_VERSION_MAJOR >= 2
612608
OGRLayer* Map3d::create_gdal_layer(GDALDriver* driver, GDALDataset* dataSource, std::string filename, std::string layername, AttributeMap attributes, bool addHeightAttributes) {

src/Map3d.h

-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ class Map3d {
159159
#else
160160
bool extract_and_add_polygon(GDALDataset* dataSource, PolygonFile* file);
161161
OGRLayer* create_gdal_layer(GDALDriver* driver, GDALDataset* dataSource, std::string filename, std::string layername, AttributeMap attributes, bool addHeightAttributes);
162-
void close_gdal_resources(GDALDriver* driver, std::unordered_map<std::string, OGRLayer*> layers);
163162
#endif
164163
void extract_feature(OGRFeature * f, std::string layerName, const char * idfield, const char * heightfield, std::string layertype, bool multiple_heights);
165164
void stitch_one_vertex(TopoFeature* f, int ringi, int pi, std::vector< std::tuple<TopoFeature*, int, int> >& star);

0 commit comments

Comments
 (0)