Skip to content

Commit

Permalink
Merge pull request #3395 from mapnik/register-gdal-once
Browse files Browse the repository at this point in the history
register gdal once at plugin load
  • Loading branch information
Dane Springmeyer committed Apr 6, 2016
2 parents 61cb4ef + 8339202 commit 585de59
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions include/mapnik/plugin.hpp
Expand Up @@ -39,6 +39,7 @@ class PluginInfo : util::noncopyable
{
public:
using callable_returning_string = const char* (*) ();
using callable_returning_void = void (*) ();
PluginInfo (std::string const& filename,
std::string const& library_name);
~PluginInfo();
Expand Down
11 changes: 6 additions & 5 deletions plugins/input/gdal/gdal_datasource.cpp
Expand Up @@ -44,11 +44,14 @@ using mapnik::featureset_ptr;
using mapnik::layer_descriptor;
using mapnik::datasource_exception;

static std::once_flag once_flag;

static bool GDALAllRegister_once_()
extern "C" MAPNIK_EXP void on_plugin_load()
{
static bool const quiet_unused = (GDALAllRegister(), true);
return quiet_unused;
// initialize gdal formats
std::call_once(once_flag,[](){
GDALAllRegister();
});
}

gdal_datasource::gdal_datasource(parameters const& params)
Expand All @@ -60,8 +63,6 @@ gdal_datasource::gdal_datasource(parameters const& params)
{
MAPNIK_LOG_DEBUG(gdal) << "gdal_datasource: Initializing...";

GDALAllRegister_once_();

#ifdef MAPNIK_STATS
mapnik::progress_timer __stats__(std::clog, "gdal_datasource::init");
#endif
Expand Down
14 changes: 10 additions & 4 deletions plugins/input/ogr/ogr_datasource.cpp
Expand Up @@ -59,6 +59,16 @@ using mapnik::datasource_exception;
using mapnik::filter_in_box;
using mapnik::filter_at_point;

static std::once_flag once_flag;

extern "C" MAPNIK_EXP void on_plugin_load()
{
// initialize ogr formats
// NOTE: in GDAL >= 2.0 this is the same as GDALAllRegister()
std::call_once(once_flag,[](){
OGRRegisterAll();
});
}

ogr_datasource::ogr_datasource(parameters const& params)
: datasource(params),
Expand Down Expand Up @@ -87,10 +97,6 @@ void ogr_datasource::init(mapnik::parameters const& params)
mapnik::progress_timer __stats__(std::clog, "ogr_datasource::init");
#endif

// initialize ogr formats
// NOTE: in GDAL >= 2.0 this is the same as GDALAllRegister()
OGRRegisterAll();

boost::optional<std::string> file = params.get<std::string>("file");
boost::optional<std::string> string = params.get<std::string>("string");
if (!string) string = params.get<std::string>("inline");
Expand Down
8 changes: 8 additions & 0 deletions src/plugin.cpp
Expand Up @@ -60,6 +60,10 @@ PluginInfo::PluginInfo(std::string const& filename,
{
callable_returning_string name = reinterpret_cast<callable_returning_string>(dlsym(module_->dl, library_name.c_str()));
if (name) name_ = name();
callable_returning_void init_once = reinterpret_cast<callable_returning_void>(dlsym(module_->dl, "on_plugin_load"));;
if (init_once) {
init_once();
}
}
#else
#ifdef MAPNIK_HAS_DLCFN
Expand All @@ -68,6 +72,10 @@ PluginInfo::PluginInfo(std::string const& filename,
{
callable_returning_string name = reinterpret_cast<callable_returning_string>(dlsym(module_->dl, library_name.c_str()));
if (name) name_ = name();
callable_returning_void init_once = reinterpret_cast<callable_returning_void>(dlsym(module_->dl, "on_plugin_load"));;
if (init_once) {
init_once();
}
}
#else
throw std::runtime_error("no support for loading dynamic objects (Mapnik not compiled with -DMAPNIK_HAS_DLCFN)");
Expand Down

0 comments on commit 585de59

Please sign in to comment.