2009-01-28 21:16:31 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
// $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 std::clog;
|
|
|
|
using std::endl;
|
|
|
|
|
|
|
|
using mapnik::datasource;
|
|
|
|
using mapnik::parameters;
|
|
|
|
|
|
|
|
DATASOURCE_PLUGIN(ogr_datasource)
|
|
|
|
|
|
|
|
using mapnik::Envelope;
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
ogr_datasource::ogr_datasource(parameters const& params)
|
|
|
|
: datasource(params),
|
|
|
|
extent_(),
|
2009-01-29 05:20:34 +01:00
|
|
|
type_(datasource::Vector),
|
2009-05-12 00:06:48 +02:00
|
|
|
desc_(*params.get<std::string>("type"), *params.get<std::string>("encoding","utf-8")),
|
|
|
|
indexed_(false)
|
2009-01-28 21:16:31 +01:00
|
|
|
{
|
|
|
|
OGRRegisterAll();
|
|
|
|
|
|
|
|
boost::optional<std::string> file = params.get<std::string>("file");
|
2009-03-30 22:19:57 +02:00
|
|
|
if (!file) throw datasource_exception("missing <file> parameter");
|
2009-01-28 21:16:31 +01:00
|
|
|
|
2009-01-29 05:20:34 +01:00
|
|
|
multiple_geometries_ = *params_.get<mapnik::boolean>("multiple_geometries",false);
|
|
|
|
|
2009-03-30 22:19:57 +02:00
|
|
|
boost::optional<std::string> base = params.get<std::string>("base");
|
|
|
|
if (base)
|
|
|
|
dataset_name_ = *base + "/" + *file;
|
|
|
|
else
|
|
|
|
dataset_name_ = *file;
|
2009-05-12 00:06:48 +02:00
|
|
|
|
|
|
|
// open ogr driver
|
2009-03-30 22:19:57 +02:00
|
|
|
dataset_ = OGRSFDriverRegistrar::Open ((dataset_name_).c_str(), FALSE);
|
2009-05-08 23:12:53 +02:00
|
|
|
if (!dataset_)
|
|
|
|
{
|
|
|
|
std::string err = CPLGetLastErrorMsg();
|
|
|
|
if( err.size() == 0 )
|
|
|
|
{
|
|
|
|
throw datasource_exception("Connection failed: " + dataset_name_ + " was not found or is not a supported format");
|
|
|
|
} else {
|
|
|
|
throw datasource_exception(err);
|
|
|
|
}
|
|
|
|
}
|
2009-01-28 21:16:31 +01:00
|
|
|
|
2009-05-12 00:06:48 +02:00
|
|
|
// initialize layer
|
2009-05-08 23:12:53 +02:00
|
|
|
boost::optional<std::string> layer = params.get<std::string>("layer");
|
|
|
|
if (!layer)
|
|
|
|
{
|
|
|
|
std::string s ("missing <layer> parameter, available layers are: ");
|
|
|
|
unsigned num_layers = dataset_->GetLayerCount();
|
|
|
|
for (unsigned i = 0; i < num_layers; ++i )
|
|
|
|
{
|
|
|
|
OGRLayer *ogr_layer = dataset_->GetLayer(i);
|
|
|
|
OGRFeatureDefn* def = ogr_layer->GetLayerDefn();
|
|
|
|
if (def != 0) {
|
|
|
|
s += " '";
|
|
|
|
s += def->GetName();
|
|
|
|
s += "' ";
|
|
|
|
} else {
|
|
|
|
s += "No layers found!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw datasource_exception(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
layerName_ = *layer;
|
2009-01-29 17:20:30 +01:00
|
|
|
layer_ = dataset_->GetLayerByName (layerName_.c_str());
|
|
|
|
if (! layer_) throw datasource_exception("cannot find <layer> in dataset");
|
|
|
|
|
2009-05-12 00:06:48 +02:00
|
|
|
// initialize envelope
|
2009-01-29 17:20:30 +01:00
|
|
|
OGREnvelope envelope;
|
|
|
|
layer_->GetExtent (&envelope);
|
|
|
|
extent_.init (envelope.MinX, envelope.MinY, envelope.MaxX, envelope.MaxY);
|
|
|
|
|
2009-05-12 00:06:48 +02:00
|
|
|
// scan for index file
|
2009-05-12 06:32:42 +02:00
|
|
|
unsigned breakpoint = dataset_name_.find_last_of (".");
|
2009-05-12 00:06:48 +02:00
|
|
|
if (breakpoint == std::string::npos) breakpoint = dataset_name_.length();
|
|
|
|
index_name_ = dataset_name_.substr(0, breakpoint) + ".index";
|
|
|
|
std::ifstream index_file (index_name_.c_str(), std::ios::in | std::ios::binary);
|
|
|
|
if (index_file)
|
|
|
|
{
|
|
|
|
indexed_=true;
|
|
|
|
index_file.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
// deal with attributes descriptions
|
2009-01-28 21:16:31 +01:00
|
|
|
OGRFeatureDefn* def = layer_->GetLayerDefn ();
|
|
|
|
if (def != 0)
|
|
|
|
{
|
2009-01-29 17:20:30 +01:00
|
|
|
int fld_count = def->GetFieldCount ();
|
|
|
|
for (int i = 0; i < fld_count; i++)
|
2009-01-28 21:16:31 +01:00
|
|
|
{
|
|
|
|
OGRFieldDefn* fld = def->GetFieldDefn (i);
|
|
|
|
|
2009-02-18 23:50:12 +01:00
|
|
|
std::string fld_name = fld->GetNameRef ();
|
2009-01-28 21:16:31 +01:00
|
|
|
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:
|
2009-01-29 17:20:30 +01:00
|
|
|
case OFTWideString: // deprecated
|
2009-01-28 21:16:31 +01:00
|
|
|
desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::String));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OFTBinary:
|
|
|
|
desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::Object));
|
|
|
|
break;
|
2009-01-29 17:20:30 +01:00
|
|
|
|
|
|
|
case OFTIntegerList:
|
|
|
|
case OFTRealList:
|
|
|
|
case OFTStringList:
|
|
|
|
case OFTWideStringList: // deprecated !
|
2009-01-28 21:16:31 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2009-01-29 17:20:30 +01:00
|
|
|
clog << "unhandled type_oid=" << type_oid << endl;
|
2009-01-28 21:16:31 +01:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
2009-01-29 17:20:30 +01:00
|
|
|
case OFTDate:
|
|
|
|
case OFTTime:
|
|
|
|
case OFTDateTime: // unhandled !
|
2009-01-28 21:16:31 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2009-01-29 17:20:30 +01:00
|
|
|
clog << "unhandled type_oid=" << type_oid << endl;
|
2009-01-28 21:16:31 +01:00
|
|
|
#endif
|
2009-01-29 17:20:30 +01:00
|
|
|
desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::Object));
|
2009-01-28 21:16:31 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default: // unknown
|
|
|
|
#ifdef MAPNIK_DEBUG
|
2009-01-29 17:20:30 +01:00
|
|
|
clog << "unknown type_oid=" << type_oid << endl;
|
2009-01-28 21:16:31 +01:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ogr_datasource::~ogr_datasource()
|
|
|
|
{
|
|
|
|
OGRDataSource::DestroyDataSource (dataset_);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ogr_datasource::name()
|
|
|
|
{
|
2009-05-08 23:12:53 +02:00
|
|
|
return "ogr";
|
2009-01-28 21:16:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int ogr_datasource::type() const
|
|
|
|
{
|
2009-01-29 05:20:34 +01:00
|
|
|
return type_;
|
2009-01-28 21:16:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Envelope<double> ogr_datasource::envelope() const
|
|
|
|
{
|
|
|
|
return extent_;
|
|
|
|
}
|
|
|
|
|
|
|
|
layer_descriptor ogr_datasource::get_descriptor() const
|
|
|
|
{
|
|
|
|
return desc_;
|
|
|
|
}
|
|
|
|
|
|
|
|
featureset_ptr ogr_datasource::features(query const& q) const
|
|
|
|
{
|
|
|
|
if (dataset_ && layer_)
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
std::ostringstream s;
|
|
|
|
|
|
|
|
s << "select ";
|
|
|
|
std::set<std::string> const& props=q.property_names();
|
|
|
|
std::set<std::string>::const_iterator pos=props.begin();
|
|
|
|
std::set<std::string>::const_iterator end=props.end();
|
|
|
|
while (pos != end)
|
|
|
|
{
|
|
|
|
s <<",\""<<*pos<<"\"";
|
|
|
|
++pos;
|
|
|
|
}
|
2009-01-29 17:20:30 +01:00
|
|
|
s << " from " << layerName_ ;
|
|
|
|
|
|
|
|
// execute existing SQL
|
|
|
|
OGRLayer* layer = dataset_->ExecuteSQL (s.str(), poly);
|
|
|
|
|
|
|
|
// layer must be freed
|
|
|
|
dataset_->ReleaseResultSet (layer);
|
2009-01-28 21:16:31 +01:00
|
|
|
#endif
|
|
|
|
|
2009-05-12 00:06:48 +02:00
|
|
|
if (indexed_)
|
|
|
|
{
|
|
|
|
filter_in_box filter(q.get_bbox());
|
|
|
|
|
|
|
|
return featureset_ptr(new ogr_index_featureset<filter_in_box> (*dataset_,
|
|
|
|
*layer_,
|
|
|
|
filter,
|
|
|
|
index_name_,
|
|
|
|
desc_.get_encoding(),
|
|
|
|
multiple_geometries_));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return featureset_ptr(new ogr_featureset (*dataset_,
|
|
|
|
*layer_,
|
|
|
|
q.get_bbox(),
|
|
|
|
desc_.get_encoding(),
|
|
|
|
multiple_geometries_));
|
|
|
|
}
|
2009-01-28 21:16:31 +01:00
|
|
|
}
|
|
|
|
return featureset_ptr();
|
|
|
|
}
|
|
|
|
|
|
|
|
featureset_ptr ogr_datasource::features_at_point(coord2d const& pt) const
|
|
|
|
{
|
2009-01-29 17:20:30 +01:00
|
|
|
if (dataset_ && 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_,
|
|
|
|
*layer_,
|
|
|
|
filter,
|
|
|
|
index_name_,
|
|
|
|
desc_.get_encoding(),
|
|
|
|
multiple_geometries_));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
OGRPoint point;
|
|
|
|
point.setX (pt.x);
|
|
|
|
point.setY (pt.y);
|
|
|
|
|
|
|
|
return featureset_ptr(new ogr_featureset (*dataset_,
|
|
|
|
*layer_,
|
|
|
|
point,
|
|
|
|
desc_.get_encoding(),
|
|
|
|
multiple_geometries_));
|
|
|
|
}
|
2009-01-29 17:20:30 +01:00
|
|
|
}
|
2009-01-28 21:16:31 +01:00
|
|
|
return featureset_ptr();
|
|
|
|
}
|
|
|
|
|