2009-01-28 21:16:31 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2011-10-22 02:27:06 +02:00
|
|
|
* Copyright (C) 2011 Artem Pavlenko
|
2009-01-28 21:16:31 +01: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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
// $Id$
|
|
|
|
|
2009-05-12 00:06:48 +02:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2009-01-28 21:16:31 +01:00
|
|
|
#include "ogr_datasource.hpp"
|
|
|
|
#include "ogr_featureset.hpp"
|
2009-05-12 00:06:48 +02:00
|
|
|
#include "ogr_index_featureset.hpp"
|
|
|
|
|
|
|
|
// mapnik
|
2009-01-29 05:20:34 +01:00
|
|
|
#include <mapnik/ptree_helpers.hpp>
|
2009-05-12 00:06:48 +02:00
|
|
|
#include <mapnik/geom_util.hpp>
|
2009-01-28 21:16:31 +01:00
|
|
|
|
2009-03-30 22:19:57 +02:00
|
|
|
// boost
|
2009-05-12 00:06:48 +02:00
|
|
|
#include <boost/algorithm/string.hpp>
|
2009-03-30 22:19:57 +02:00
|
|
|
|
2009-01-28 21:16:31 +01:00
|
|
|
using mapnik::datasource;
|
|
|
|
using mapnik::parameters;
|
|
|
|
|
|
|
|
DATASOURCE_PLUGIN(ogr_datasource)
|
|
|
|
|
2009-12-16 21:02:06 +01:00
|
|
|
using mapnik::box2d;
|
2009-01-28 21:16:31 +01:00
|
|
|
using mapnik::coord2d;
|
|
|
|
using mapnik::query;
|
|
|
|
using mapnik::featureset_ptr;
|
|
|
|
using mapnik::layer_descriptor;
|
|
|
|
using mapnik::attribute_descriptor;
|
|
|
|
using mapnik::datasource_exception;
|
2009-05-12 00:06:48 +02:00
|
|
|
using mapnik::filter_in_box;
|
|
|
|
using mapnik::filter_at_point;
|
2009-01-28 21:16:31 +01:00
|
|
|
|
|
|
|
|
2010-10-24 08:34:18 +02:00
|
|
|
ogr_datasource::ogr_datasource(parameters const& params, bool bind)
|
2011-10-22 02:27:06 +02:00
|
|
|
: datasource(params),
|
|
|
|
extent_(),
|
|
|
|
type_(datasource::Vector),
|
2011-10-24 11:53:33 +02:00
|
|
|
desc_(*params.get<std::string>("type"), *params.get<std::string>("encoding", "utf-8")),
|
2011-10-22 02:27:06 +02:00
|
|
|
indexed_(false)
|
2009-01-28 21:16:31 +01:00
|
|
|
{
|
2011-10-22 02:27:06 +02:00
|
|
|
boost::optional<std::string> file = params.get<std::string>("file");
|
|
|
|
boost::optional<std::string> string = params.get<std::string>("string");
|
|
|
|
if (! file && ! string)
|
|
|
|
{
|
|
|
|
throw datasource_exception("missing <file> or <string> parameter");
|
|
|
|
}
|
2009-01-28 21:16:31 +01:00
|
|
|
|
2011-10-24 11:53:33 +02:00
|
|
|
multiple_geometries_ = *params.get<mapnik::boolean>("multiple_geometries", false);
|
2009-01-29 05:20:34 +01:00
|
|
|
|
2011-10-22 02:27:06 +02:00
|
|
|
if (string)
|
|
|
|
{
|
2010-11-16 18:14:22 +01:00
|
|
|
dataset_name_ = *string;
|
2011-10-22 02:27:06 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
boost::optional<std::string> base = params.get<std::string>("base");
|
|
|
|
if (base)
|
|
|
|
{
|
|
|
|
dataset_name_ = *base + "/" + *file;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dataset_name_ = *file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bind)
|
|
|
|
{
|
|
|
|
this->bind();
|
|
|
|
}
|
2010-10-24 08:34:18 +02:00
|
|
|
}
|
2009-05-12 00:06:48 +02:00
|
|
|
|
2010-11-16 18:14:22 +01:00
|
|
|
ogr_datasource::~ogr_datasource()
|
|
|
|
{
|
|
|
|
if (is_bound_)
|
|
|
|
{
|
2011-10-24 11:53:33 +02:00
|
|
|
// free layer before destroying the datasource
|
|
|
|
layer_.free_layer();
|
|
|
|
|
2010-11-16 18:14:22 +01:00
|
|
|
OGRDataSource::DestroyDataSource (dataset_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-24 08:34:18 +02:00
|
|
|
void ogr_datasource::bind() const
|
|
|
|
{
|
2011-10-22 02:27:06 +02:00
|
|
|
if (is_bound_) return;
|
|
|
|
|
|
|
|
// initialize ogr formats
|
|
|
|
OGRRegisterAll();
|
|
|
|
|
|
|
|
// open ogr driver
|
|
|
|
dataset_ = OGRSFDriverRegistrar::Open((dataset_name_).c_str(), FALSE);
|
|
|
|
if (! dataset_)
|
|
|
|
{
|
|
|
|
const std::string err = CPLGetLastErrorMsg();
|
|
|
|
if (err.size() == 0)
|
|
|
|
{
|
|
|
|
throw datasource_exception("OGR Plugin: connection failed: " + dataset_name_ + " was not found or is not a supported format");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw datasource_exception("OGR Plugin: " + err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize layer
|
|
|
|
boost::optional<std::string> layer_by_name = params_.get<std::string>("layer");
|
|
|
|
boost::optional<unsigned> layer_by_index = params_.get<unsigned>("layer_by_index");
|
2011-10-24 11:53:33 +02:00
|
|
|
boost::optional<std::string> layer_by_sql = params_.get<std::string>("layer_by_sql");
|
|
|
|
|
|
|
|
int passed_parameters = 0;
|
|
|
|
passed_parameters += layer_by_name ? 1 : 0;
|
|
|
|
passed_parameters += layer_by_index ? 1 : 0;
|
|
|
|
passed_parameters += layer_by_sql ? 1 : 0;
|
2011-10-22 02:27:06 +02:00
|
|
|
|
2011-10-24 11:53:33 +02:00
|
|
|
if (passed_parameters > 1)
|
2011-10-22 02:27:06 +02:00
|
|
|
{
|
2011-10-24 11:53:33 +02:00
|
|
|
throw datasource_exception("OGR Plugin: you can only select an ogr layer by name "
|
|
|
|
"('layer' parameter), by number ('layer_by_index' parameter), "
|
|
|
|
"or by sql ('layer_by_sql' parameter), "
|
|
|
|
"do not supply 2 or more of them at the same time" );
|
2011-10-22 02:27:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (layer_by_name)
|
|
|
|
{
|
2011-10-24 11:53:33 +02:00
|
|
|
layer_name_ = *layer_by_name;
|
2011-10-24 12:05:01 +02:00
|
|
|
layer_.layer_by_name(dataset_, layer_name_);
|
2011-10-22 02:27:06 +02:00
|
|
|
}
|
|
|
|
else if (layer_by_index)
|
|
|
|
{
|
|
|
|
const unsigned num_layers = dataset_->GetLayerCount();
|
|
|
|
if (*layer_by_index >= num_layers)
|
|
|
|
{
|
|
|
|
std::ostringstream s;
|
|
|
|
s << "OGR Plugin: only ";
|
|
|
|
s << num_layers;
|
|
|
|
s << " layer(s) exist, cannot find layer by index '" << *layer_by_index << "'";
|
|
|
|
|
|
|
|
throw datasource_exception(s.str());
|
|
|
|
}
|
|
|
|
|
2011-10-24 12:05:01 +02:00
|
|
|
layer_.layer_by_index(dataset_, *layer_by_index);
|
2011-10-24 11:53:33 +02:00
|
|
|
layer_name_ = layer_.layer_name();
|
|
|
|
}
|
|
|
|
else if (layer_by_sql)
|
|
|
|
{
|
2011-10-24 12:05:01 +02:00
|
|
|
layer_.layer_by_sql(dataset_, *layer_by_sql);
|
2011-10-24 11:53:33 +02:00
|
|
|
layer_name_ = layer_.layer_name();
|
2011-10-22 02:27:06 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::ostringstream s;
|
2011-10-24 11:53:33 +02:00
|
|
|
s << "OGR Plugin: missing <layer> or <layer_by_index> or <layer_by_sql> "
|
|
|
|
<< "parameter, available layers are: ";
|
2011-10-22 02:27:06 +02:00
|
|
|
|
|
|
|
unsigned num_layers = dataset_->GetLayerCount();
|
|
|
|
bool layer_found = false;
|
|
|
|
for (unsigned i = 0; i < num_layers; ++i )
|
|
|
|
{
|
|
|
|
OGRLayer* ogr_layer = dataset_->GetLayer(i);
|
|
|
|
OGRFeatureDefn* ogr_layer_def = ogr_layer->GetLayerDefn();
|
|
|
|
if (ogr_layer_def != 0)
|
|
|
|
{
|
|
|
|
layer_found = true;
|
|
|
|
s << " '" << ogr_layer_def->GetName() << "' ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! layer_found)
|
|
|
|
{
|
|
|
|
s << "None (no layers were found in dataset)";
|
|
|
|
}
|
|
|
|
|
|
|
|
throw datasource_exception(s.str());
|
|
|
|
}
|
|
|
|
|
2011-10-24 11:53:33 +02:00
|
|
|
if (! layer_.is_valid())
|
2011-10-22 02:27:06 +02:00
|
|
|
{
|
|
|
|
std::string s("OGR Plugin: ");
|
|
|
|
|
|
|
|
if (layer_by_name)
|
|
|
|
{
|
|
|
|
s += "cannot find layer by name '" + *layer_by_name;
|
|
|
|
}
|
|
|
|
else if (layer_by_index)
|
|
|
|
{
|
|
|
|
s += "cannot find layer by index number '" + *layer_by_index;
|
|
|
|
}
|
2011-10-24 11:53:33 +02:00
|
|
|
else if (layer_by_sql)
|
|
|
|
{
|
|
|
|
s += "cannot find layer by sql query '" + *layer_by_sql;
|
|
|
|
}
|
2011-10-22 02:27:06 +02:00
|
|
|
|
|
|
|
s += "' in dataset '" + dataset_name_ + "'";
|
|
|
|
|
|
|
|
throw datasource_exception(s);
|
|
|
|
}
|
|
|
|
|
2011-10-24 11:53:33 +02:00
|
|
|
// work with real OGR layer
|
|
|
|
OGRLayer* layer = layer_.layer();
|
|
|
|
|
2011-10-22 02:27:06 +02:00
|
|
|
// initialize envelope
|
|
|
|
OGREnvelope envelope;
|
2011-10-24 11:53:33 +02:00
|
|
|
layer->GetExtent(&envelope);
|
2011-10-22 02:27:06 +02:00
|
|
|
extent_.init(envelope.MinX, envelope.MinY, envelope.MaxX, envelope.MaxY);
|
|
|
|
|
|
|
|
// scan for index file
|
|
|
|
// TODO - layer names don't match dataset name, so this will break for
|
|
|
|
// any layer types of ogr than shapefiles, etc
|
|
|
|
// fix here and in ogrindex
|
|
|
|
size_t breakpoint = dataset_name_.find_last_of(".");
|
|
|
|
if (breakpoint == std::string::npos)
|
|
|
|
{
|
|
|
|
breakpoint = dataset_name_.length();
|
|
|
|
}
|
|
|
|
index_name_ = dataset_name_.substr(0, breakpoint) + ".ogrindex";
|
|
|
|
|
2011-10-24 11:53:33 +02:00
|
|
|
std::ifstream index_file(index_name_.c_str(), std::ios::in | std::ios::binary);
|
2011-10-22 02:27:06 +02:00
|
|
|
if (index_file)
|
|
|
|
{
|
|
|
|
indexed_ = true;
|
|
|
|
index_file.close();
|
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
// TODO - enable this warning once the ogrindex tool is a bit more stable/mature
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::clog << "### Notice: no ogrindex file found for " << dataset_name_
|
|
|
|
<< ", use the 'ogrindex' program to build an index for faster rendering"
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// deal with attributes descriptions
|
2011-10-24 11:53:33 +02:00
|
|
|
OGRFeatureDefn* def = layer->GetLayerDefn();
|
2011-10-22 02:27:06 +02:00
|
|
|
if (def != 0)
|
|
|
|
{
|
|
|
|
const int fld_count = def->GetFieldCount();
|
|
|
|
for (int i = 0; i < fld_count; i++)
|
|
|
|
{
|
|
|
|
OGRFieldDefn* fld = def->GetFieldDefn(i);
|
|
|
|
|
|
|
|
const std::string fld_name = fld->GetNameRef();
|
|
|
|
const OGRFieldType type_oid = fld->GetType();
|
|
|
|
|
|
|
|
switch (type_oid)
|
|
|
|
{
|
|
|
|
case OFTInteger:
|
|
|
|
desc_.add_descriptor(attribute_descriptor(fld_name, mapnik::Integer));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OFTReal:
|
|
|
|
desc_.add_descriptor(attribute_descriptor(fld_name, mapnik::Double));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OFTString:
|
|
|
|
case OFTWideString: // deprecated
|
|
|
|
desc_.add_descriptor(attribute_descriptor(fld_name, mapnik::String));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OFTBinary:
|
|
|
|
desc_.add_descriptor(attribute_descriptor(fld_name, mapnik::Object));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OFTIntegerList:
|
|
|
|
case OFTRealList:
|
|
|
|
case OFTStringList:
|
|
|
|
case OFTWideStringList: // deprecated !
|
2009-01-28 21:16:31 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2011-10-22 02:27:06 +02:00
|
|
|
std::clog << "OGR Plugin: unhandled type_oid=" << type_oid << std::endl;
|
2009-01-28 21:16:31 +01:00
|
|
|
#endif
|
2011-10-22 02:27:06 +02:00
|
|
|
break;
|
2009-01-28 21:16:31 +01:00
|
|
|
|
2011-10-22 02:27:06 +02:00
|
|
|
case OFTDate:
|
|
|
|
case OFTTime:
|
|
|
|
case OFTDateTime: // unhandled !
|
2009-01-28 21:16:31 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2011-10-22 02:27:06 +02:00
|
|
|
std::clog << "OGR Plugin: unhandled type_oid=" << type_oid << std::endl;
|
2009-01-28 21:16:31 +01:00
|
|
|
#endif
|
2011-10-22 02:27:06 +02:00
|
|
|
desc_.add_descriptor(attribute_descriptor(fld_name, mapnik::Object));
|
|
|
|
break;
|
2009-01-28 21:16:31 +01:00
|
|
|
|
2011-10-22 02:27:06 +02:00
|
|
|
default: // unknown
|
2009-01-28 21:16:31 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2011-10-22 02:27:06 +02:00
|
|
|
std::clog << "OGR Plugin: unknown type_oid=" << type_oid << std::endl;
|
2009-01-28 21:16:31 +01:00
|
|
|
#endif
|
2011-10-22 02:27:06 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
is_bound_ = true;
|
2009-01-28 21:16:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string ogr_datasource::name()
|
|
|
|
{
|
2011-10-22 02:27:06 +02:00
|
|
|
return "ogr";
|
2009-01-28 21:16:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int ogr_datasource::type() const
|
|
|
|
{
|
2011-10-22 02:27:06 +02:00
|
|
|
return type_;
|
2009-01-28 21:16:31 +01:00
|
|
|
}
|
|
|
|
|
2009-12-16 21:02:06 +01:00
|
|
|
box2d<double> ogr_datasource::envelope() const
|
2009-01-28 21:16:31 +01:00
|
|
|
{
|
2011-10-22 02:27:06 +02:00
|
|
|
if (! is_bound_) bind();
|
|
|
|
return extent_;
|
2009-01-28 21:16:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
layer_descriptor ogr_datasource::get_descriptor() const
|
|
|
|
{
|
2011-10-22 02:27:06 +02:00
|
|
|
if (! is_bound_) bind();
|
|
|
|
return desc_;
|
2009-01-28 21:16:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
featureset_ptr ogr_datasource::features(query const& q) const
|
|
|
|
{
|
2011-10-22 02:27:06 +02:00
|
|
|
if (! is_bound_) bind();
|
2010-10-24 08:34:18 +02:00
|
|
|
|
2011-10-24 11:53:33 +02:00
|
|
|
if (dataset_ && layer_.is_valid())
|
2011-10-22 02:27:06 +02:00
|
|
|
{
|
2011-10-24 11:53:33 +02:00
|
|
|
OGRLayer* layer = layer_.layer();
|
2009-01-28 21:16:31 +01:00
|
|
|
|
2009-05-12 00:06:48 +02:00
|
|
|
if (indexed_)
|
|
|
|
{
|
|
|
|
filter_in_box filter(q.get_bbox());
|
|
|
|
|
2011-10-22 02:27:06 +02:00
|
|
|
return featureset_ptr(new ogr_index_featureset<filter_in_box>(*dataset_,
|
2011-10-24 11:53:33 +02:00
|
|
|
*layer,
|
2011-10-22 02:27:06 +02:00
|
|
|
filter,
|
|
|
|
index_name_,
|
|
|
|
desc_.get_encoding(),
|
|
|
|
multiple_geometries_));
|
2009-05-12 00:06:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return featureset_ptr(new ogr_featureset (*dataset_,
|
2011-10-24 11:53:33 +02:00
|
|
|
*layer,
|
2009-05-12 00:06:48 +02:00
|
|
|
q.get_bbox(),
|
|
|
|
desc_.get_encoding(),
|
|
|
|
multiple_geometries_));
|
|
|
|
}
|
2011-10-22 02:27:06 +02:00
|
|
|
}
|
2011-10-24 11:53:33 +02:00
|
|
|
|
2011-10-22 02:27:06 +02:00
|
|
|
return featureset_ptr();
|
2009-01-28 21:16:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
featureset_ptr ogr_datasource::features_at_point(coord2d const& pt) const
|
|
|
|
{
|
2011-10-22 02:27:06 +02:00
|
|
|
if (!is_bound_) bind();
|
2010-10-24 08:34:18 +02:00
|
|
|
|
2011-10-24 11:53:33 +02:00
|
|
|
if (dataset_ && layer_.is_valid())
|
2011-10-22 02:27:06 +02:00
|
|
|
{
|
2011-10-24 11:53:33 +02:00
|
|
|
OGRLayer* layer = layer_.layer();
|
|
|
|
|
2009-05-12 00:06:48 +02:00
|
|
|
if (indexed_)
|
|
|
|
{
|
|
|
|
filter_at_point filter(pt);
|
|
|
|
|
|
|
|
return featureset_ptr(new ogr_index_featureset<filter_at_point> (*dataset_,
|
2011-10-24 11:53:33 +02:00
|
|
|
*layer,
|
2009-05-12 00:06:48 +02:00
|
|
|
filter,
|
|
|
|
index_name_,
|
|
|
|
desc_.get_encoding(),
|
|
|
|
multiple_geometries_));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
OGRPoint point;
|
2011-04-02 05:45:50 +02:00
|
|
|
point.setX (pt.x);
|
|
|
|
point.setY (pt.y);
|
2009-05-12 00:06:48 +02:00
|
|
|
|
|
|
|
return featureset_ptr(new ogr_featureset (*dataset_,
|
2011-10-24 11:53:33 +02:00
|
|
|
*layer,
|
2009-05-12 00:06:48 +02:00
|
|
|
point,
|
|
|
|
desc_.get_encoding(),
|
|
|
|
multiple_geometries_));
|
|
|
|
}
|
2011-10-22 02:27:06 +02:00
|
|
|
}
|
2011-10-24 11:53:33 +02:00
|
|
|
|
2011-10-22 02:27:06 +02:00
|
|
|
return featureset_ptr();
|
2009-01-28 21:16:31 +01:00
|
|
|
}
|