2011-10-24 09:56:02 +00:00
|
|
|
/*****************************************************************************
|
2011-11-14 03:37:50 +00:00
|
|
|
*
|
2011-10-24 09:56:02 +00:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2021-01-05 14:39:07 +00:00
|
|
|
* Copyright (C) 2021 Artem Pavlenko
|
2011-10-24 09:56:02 +00: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 00:20:56 +00:00
|
|
|
// mapnik
|
|
|
|
#include <mapnik/debug.hpp>
|
|
|
|
|
2011-10-24 09:56:02 +00:00
|
|
|
// stl
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2014-06-03 02:33:49 +00:00
|
|
|
// gdal
|
|
|
|
#include <gdal_version.h>
|
2011-10-24 09:56:02 +00:00
|
|
|
#include <ogrsf_frmts.h>
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2014-06-03 03:25:32 +00:00
|
|
|
#if GDAL_VERSION_MAJOR >= 2
|
2014-07-07 17:23:15 +00:00
|
|
|
using gdal_dataset_type = GDALDataset*;
|
2014-06-03 02:33:49 +00:00
|
|
|
#else
|
2014-07-07 17:23:15 +00:00
|
|
|
using gdal_dataset_type = OGRDataSource*;
|
2014-06-03 02:33:49 +00:00
|
|
|
#endif
|
|
|
|
|
2011-10-24 09:56:02 +00:00
|
|
|
class ogr_layer_ptr
|
|
|
|
{
|
2022-01-26 09:43:31 +00:00
|
|
|
public:
|
2011-10-24 09:56:02 +00:00
|
|
|
ogr_layer_ptr()
|
2022-01-26 09:43:31 +00:00
|
|
|
: datasource_(nullptr)
|
|
|
|
, layer_(nullptr)
|
|
|
|
, owns_layer_(false)
|
|
|
|
, is_valid_(false)
|
|
|
|
{}
|
2011-10-24 09:56:02 +00:00
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
~ogr_layer_ptr() { free_layer(); }
|
2011-10-24 09:56:02 +00:00
|
|
|
|
|
|
|
void free_layer()
|
|
|
|
{
|
2013-11-27 15:52:47 +00:00
|
|
|
if (owns_layer_ && layer_ != nullptr && datasource_ != nullptr)
|
2011-10-24 09:56:02 +00:00
|
|
|
{
|
|
|
|
datasource_->ReleaseResultSet(layer_);
|
|
|
|
}
|
|
|
|
|
2013-11-27 15:52:47 +00:00
|
|
|
datasource_ = nullptr;
|
|
|
|
layer_ = nullptr;
|
2011-10-24 09:56:02 +00:00
|
|
|
layer_name_ = "";
|
|
|
|
owns_layer_ = false;
|
|
|
|
is_valid_ = false;
|
|
|
|
}
|
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
void layer_by_name(gdal_dataset_type const datasource, std::string const& layer_name)
|
2011-10-24 09:56:02 +00: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 01:00:51 +00:00
|
|
|
MAPNIK_LOG_DEBUG(ogr) << "ogr_layer_ptr: layer_from_name layer=" << layer_name_;
|
2011-10-24 09:56:02 +00:00
|
|
|
}
|
|
|
|
|
2012-04-08 00:20:56 +00:00
|
|
|
#ifdef MAPNIK_LOG
|
2011-10-24 09:56:02 +00:00
|
|
|
debug_print_last_error();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
void layer_by_index(gdal_dataset_type const datasource, int layer_index)
|
2011-10-24 09:56:02 +00:00
|
|
|
{
|
|
|
|
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 01:00:51 +00:00
|
|
|
MAPNIK_LOG_DEBUG(ogr) << "ogr_layer_ptr: layer_from_index layer=" << layer_name_;
|
2011-10-24 09:56:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-08 00:20:56 +00:00
|
|
|
#ifdef MAPNIK_LOG
|
2011-10-24 09:56:02 +00:00
|
|
|
debug_print_last_error();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
void layer_by_sql(gdal_dataset_type const datasource, std::string const& layer_sql)
|
2011-10-24 09:56:02 +00: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 15:52:47 +00:00
|
|
|
OGRGeometry* spatial_filter = nullptr;
|
|
|
|
const char* sql_dialect = nullptr;
|
2011-10-24 09:56:02 +00: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 01:00:51 +00:00
|
|
|
MAPNIK_LOG_DEBUG(ogr) << "ogr_layer_ptr: layer_from_sql layer=" << layer_name_;
|
2011-10-24 09:56:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-08 00:20:56 +00:00
|
|
|
#ifdef MAPNIK_LOG
|
2011-10-24 09:56:02 +00:00
|
|
|
debug_print_last_error();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-08-11 20:54:50 +00:00
|
|
|
std::string const& layer_name() const
|
|
|
|
{
|
|
|
|
return layer_name_;
|
|
|
|
}
|
2011-10-24 09:56:02 +00:00
|
|
|
|
2022-08-11 20:54:50 +00:00
|
|
|
OGRLayer* layer() const
|
|
|
|
{
|
|
|
|
return layer_;
|
|
|
|
}
|
2011-10-24 09:56:02 +00:00
|
|
|
|
2022-08-11 20:54:50 +00:00
|
|
|
bool is_valid() const
|
|
|
|
{
|
|
|
|
return is_valid_;
|
|
|
|
}
|
2011-10-24 09:56:02 +00:00
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
private:
|
2011-10-24 09:56:02 +00:00
|
|
|
|
2012-04-08 00:20:56 +00:00
|
|
|
#ifdef MAPNIK_LOG
|
2011-10-24 09:56:02 +00:00
|
|
|
void debug_print_last_error()
|
|
|
|
{
|
2022-01-26 09:43:31 +00:00
|
|
|
if (!is_valid_)
|
2011-10-24 09:56:02 +00:00
|
|
|
{
|
|
|
|
const std::string err = CPLGetLastErrorMsg();
|
|
|
|
if (err.size() == 0)
|
|
|
|
{
|
2012-04-09 01:00:51 +00:00
|
|
|
MAPNIK_LOG_DEBUG(ogr) << "ogr_layer_ptr: Error getting layer";
|
2011-10-24 09:56:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-09 01:00:51 +00:00
|
|
|
MAPNIK_LOG_DEBUG(ogr) << "ogr_layer_ptr: " << err;
|
2011-10-24 09:56:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-06-03 02:33:49 +00:00
|
|
|
gdal_dataset_type datasource_;
|
2011-10-24 09:56:02 +00:00
|
|
|
OGRLayer* layer_;
|
|
|
|
std::string layer_name_;
|
|
|
|
bool owns_layer_;
|
|
|
|
bool is_valid_;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // OGR_LAYER_PTR_HPP
|