2011-10-24 11:56:02 +02:00
|
|
|
/*****************************************************************************
|
2011-11-14 04:37:50 +01:00
|
|
|
*
|
2011-10-24 11:56:02 +02:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2017-03-27 17:14:51 +02:00
|
|
|
* Copyright (C) 2016 Artem Pavlenko
|
2011-10-24 11:56:02 +02:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef OGR_LAYER_PTR_HPP
|
|
|
|
#define OGR_LAYER_PTR_HPP
|
|
|
|
|
2012-04-08 02:20:56 +02:00
|
|
|
// mapnik
|
|
|
|
#include <mapnik/debug.hpp>
|
|
|
|
|
2011-10-24 11:56:02 +02:00
|
|
|
// stl
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2014-06-03 04:33:49 +02:00
|
|
|
// gdal
|
|
|
|
#include <gdal_version.h>
|
2011-10-24 11:56:02 +02:00
|
|
|
#include <ogrsf_frmts.h>
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2014-06-03 05:25:32 +02:00
|
|
|
#if GDAL_VERSION_MAJOR >= 2
|
2014-07-07 19:23:15 +02:00
|
|
|
using gdal_dataset_type = GDALDataset*;
|
2014-06-03 04:33:49 +02:00
|
|
|
#else
|
2014-07-07 19:23:15 +02:00
|
|
|
using gdal_dataset_type = OGRDataSource*;
|
2014-06-03 04:33:49 +02:00
|
|
|
#endif
|
|
|
|
|
2011-10-24 11:56:02 +02:00
|
|
|
class ogr_layer_ptr
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ogr_layer_ptr()
|
2013-11-27 16:52:47 +01:00
|
|
|
: datasource_(nullptr),
|
|
|
|
layer_(nullptr),
|
2011-11-14 04:37:50 +01:00
|
|
|
owns_layer_(false),
|
|
|
|
is_valid_(false)
|
2011-10-24 11:56:02 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~ogr_layer_ptr()
|
|
|
|
{
|
|
|
|
free_layer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_layer()
|
|
|
|
{
|
2013-11-27 16:52:47 +01:00
|
|
|
if (owns_layer_ && layer_ != nullptr && datasource_ != nullptr)
|
2011-10-24 11:56:02 +02:00
|
|
|
{
|
|
|
|
datasource_->ReleaseResultSet(layer_);
|
|
|
|
}
|
|
|
|
|
2013-11-27 16:52:47 +01:00
|
|
|
datasource_ = nullptr;
|
|
|
|
layer_ = nullptr;
|
2011-10-24 11:56:02 +02:00
|
|
|
layer_name_ = "";
|
|
|
|
owns_layer_ = false;
|
|
|
|
is_valid_ = false;
|
|
|
|
}
|
|
|
|
|
2014-06-03 04:33:49 +02:00
|
|
|
void layer_by_name(gdal_dataset_type const datasource,
|
2012-09-03 19:27:48 +02:00
|
|
|
std::string const& layer_name)
|
2011-10-24 11:56:02 +02:00
|
|
|
{
|
|
|
|
free_layer();
|
|
|
|
|
|
|
|
datasource_ = datasource;
|
|
|
|
|
|
|
|
OGRLayer* ogr_layer = datasource_->GetLayerByName(layer_name.c_str());
|
|
|
|
if (ogr_layer)
|
|
|
|
{
|
|
|
|
layer_name_ = layer_name;
|
|
|
|
layer_ = ogr_layer;
|
|
|
|
is_valid_ = true;
|
|
|
|
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_DEBUG(ogr) << "ogr_layer_ptr: layer_from_name layer=" << layer_name_;
|
2011-10-24 11:56:02 +02:00
|
|
|
}
|
|
|
|
|
2012-04-08 02:20:56 +02:00
|
|
|
#ifdef MAPNIK_LOG
|
2011-10-24 11:56:02 +02:00
|
|
|
debug_print_last_error();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-06-03 04:33:49 +02:00
|
|
|
void layer_by_index(gdal_dataset_type const datasource,
|
2011-10-24 11:56:02 +02:00
|
|
|
int layer_index)
|
|
|
|
{
|
|
|
|
free_layer();
|
|
|
|
|
|
|
|
datasource_ = datasource;
|
|
|
|
|
|
|
|
OGRLayer* ogr_layer = datasource_->GetLayer(layer_index);
|
|
|
|
if (ogr_layer)
|
|
|
|
{
|
|
|
|
OGRFeatureDefn* def = ogr_layer->GetLayerDefn();
|
|
|
|
if (def != 0)
|
|
|
|
{
|
|
|
|
layer_ = ogr_layer;
|
|
|
|
layer_name_ = def->GetName();
|
|
|
|
is_valid_ = true;
|
|
|
|
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_DEBUG(ogr) << "ogr_layer_ptr: layer_from_index layer=" << layer_name_;
|
2011-10-24 11:56:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-08 02:20:56 +02:00
|
|
|
#ifdef MAPNIK_LOG
|
2011-10-24 11:56:02 +02:00
|
|
|
debug_print_last_error();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-06-03 04:33:49 +02:00
|
|
|
void layer_by_sql(gdal_dataset_type const datasource,
|
2012-09-03 19:27:48 +02:00
|
|
|
std::string const& layer_sql)
|
2011-10-24 11:56:02 +02:00
|
|
|
{
|
|
|
|
free_layer();
|
|
|
|
|
|
|
|
datasource_ = datasource;
|
|
|
|
owns_layer_ = true;
|
|
|
|
|
|
|
|
// TODO - actually filter fields!
|
|
|
|
// http://trac.osgeo.org/gdal/wiki/rfc29_desired_fields
|
|
|
|
// http://trac.osgeo.org/gdal/wiki/rfc28_sqlfunc
|
|
|
|
|
2013-11-27 16:52:47 +01:00
|
|
|
OGRGeometry* spatial_filter = nullptr;
|
|
|
|
const char* sql_dialect = nullptr;
|
2011-10-24 11:56:02 +02:00
|
|
|
OGRLayer* ogr_layer = datasource_->ExecuteSQL(layer_sql.c_str(), spatial_filter, sql_dialect);
|
|
|
|
|
|
|
|
if (ogr_layer)
|
|
|
|
{
|
|
|
|
OGRFeatureDefn* def = ogr_layer->GetLayerDefn();
|
|
|
|
if (def != 0)
|
|
|
|
{
|
|
|
|
layer_ = ogr_layer;
|
|
|
|
layer_name_ = def->GetName();
|
|
|
|
is_valid_ = true;
|
|
|
|
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_DEBUG(ogr) << "ogr_layer_ptr: layer_from_sql layer=" << layer_name_;
|
2011-10-24 11:56:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-08 02:20:56 +02:00
|
|
|
#ifdef MAPNIK_LOG
|
2011-10-24 11:56:02 +02:00
|
|
|
debug_print_last_error();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-09-03 19:27:48 +02:00
|
|
|
std::string const& layer_name() const
|
2011-10-24 11:56:02 +02:00
|
|
|
{
|
|
|
|
return layer_name_;
|
|
|
|
}
|
|
|
|
|
|
|
|
OGRLayer* layer() const
|
|
|
|
{
|
|
|
|
return layer_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_valid() const
|
|
|
|
{
|
|
|
|
return is_valid_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2012-04-08 02:20:56 +02:00
|
|
|
#ifdef MAPNIK_LOG
|
2011-10-24 11:56:02 +02:00
|
|
|
void debug_print_last_error()
|
|
|
|
{
|
|
|
|
if (! is_valid_)
|
|
|
|
{
|
|
|
|
const std::string err = CPLGetLastErrorMsg();
|
|
|
|
if (err.size() == 0)
|
|
|
|
{
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_DEBUG(ogr) << "ogr_layer_ptr: Error getting layer";
|
2011-10-24 11:56:02 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_DEBUG(ogr) << "ogr_layer_ptr: " << err;
|
2011-10-24 11:56:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-06-03 04:33:49 +02:00
|
|
|
gdal_dataset_type datasource_;
|
2011-10-24 11:56:02 +02:00
|
|
|
OGRLayer* layer_;
|
|
|
|
std::string layer_name_;
|
|
|
|
bool owns_layer_;
|
|
|
|
bool is_valid_;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // OGR_LAYER_PTR_HPP
|