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)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Artem Pavlenko
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
|
|
|
// ogr
|
|
|
|
#include <ogrsf_frmts.h>
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2011-10-24 11:56:02 +02:00
|
|
|
class ogr_layer_ptr
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ogr_layer_ptr()
|
2011-11-14 04:37:50 +01:00
|
|
|
: datasource_(NULL),
|
|
|
|
layer_(NULL),
|
|
|
|
owns_layer_(false),
|
|
|
|
is_valid_(false)
|
2011-10-24 11:56:02 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~ogr_layer_ptr()
|
|
|
|
{
|
|
|
|
free_layer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_layer()
|
|
|
|
{
|
|
|
|
if (owns_layer_ && layer_ != NULL && datasource_ != NULL)
|
|
|
|
{
|
|
|
|
datasource_->ReleaseResultSet(layer_);
|
|
|
|
}
|
|
|
|
|
|
|
|
datasource_ = NULL;
|
|
|
|
layer_ = NULL;
|
|
|
|
layer_name_ = "";
|
|
|
|
owns_layer_ = false;
|
|
|
|
is_valid_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void layer_by_name(OGRDataSource* 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
|
|
|
|
}
|
|
|
|
|
|
|
|
void layer_by_index(OGRDataSource* const datasource,
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
void layer_by_sql(OGRDataSource* 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
|
|
|
|
|
|
|
|
OGRGeometry* spatial_filter = NULL;
|
|
|
|
const char* sql_dialect = NULL;
|
|
|
|
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
|
|
|
|
|
|
|
|
OGRDataSource* datasource_;
|
|
|
|
OGRLayer* layer_;
|
|
|
|
std::string layer_name_;
|
|
|
|
bool owns_layer_;
|
|
|
|
bool is_valid_;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // OGR_LAYER_PTR_HPP
|