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$
|
|
|
|
|
|
|
|
#include "ogr_datasource.hpp"
|
|
|
|
#include "ogr_featureset.hpp"
|
2009-01-29 05:20:34 +01:00
|
|
|
#include <mapnik/ptree_helpers.hpp>
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
ogr_datasource::ogr_datasource(parameters const& params)
|
|
|
|
: datasource(params),
|
|
|
|
extent_(),
|
2009-01-29 05:20:34 +01:00
|
|
|
type_(datasource::Vector),
|
2009-01-28 21:16:31 +01:00
|
|
|
desc_(*params.get<std::string>("type"),"utf-8")
|
|
|
|
{
|
|
|
|
OGRRegisterAll();
|
|
|
|
|
|
|
|
boost::optional<std::string> file = params.get<std::string>("file");
|
|
|
|
if (!file) throw datasource_exception("missing <file> paramater");
|
|
|
|
|
|
|
|
boost::optional<std::string> layer = params.get<std::string>("layer");
|
|
|
|
if (!layer) throw datasource_exception("missing <layer> paramater");
|
|
|
|
|
2009-01-29 17:20:30 +01:00
|
|
|
layerName_ = *layer;
|
2009-01-29 05:20:34 +01:00
|
|
|
multiple_geometries_ = *params_.get<mapnik::boolean>("multiple_geometries",false);
|
|
|
|
|
2009-01-28 21:16:31 +01:00
|
|
|
dataset_ = OGRSFDriverRegistrar::Open ((*file).c_str(), FALSE);
|
|
|
|
if (!dataset_) throw datasource_exception(CPLGetLastErrorMsg());
|
|
|
|
|
2009-01-29 17:20:30 +01:00
|
|
|
layer_ = dataset_->GetLayerByName (layerName_.c_str());
|
|
|
|
if (! layer_) throw datasource_exception("cannot find <layer> in dataset");
|
|
|
|
|
|
|
|
OGREnvelope envelope;
|
|
|
|
layer_->GetExtent (&envelope);
|
|
|
|
extent_.init (envelope.MinX, envelope.MinY, envelope.MaxX, envelope.MaxY);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
std_string fld_name = fld->GetNameRef ();
|
|
|
|
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 const ogr_datasource::name_="ogr";
|
|
|
|
|
|
|
|
std::string ogr_datasource::name()
|
|
|
|
{
|
|
|
|
return name_;
|
|
|
|
}
|
|
|
|
|
|
|
|
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_)
|
|
|
|
{
|
|
|
|
mapnik::Envelope<double> const& query_extent = q.get_bbox();
|
|
|
|
|
2009-02-03 19:30:06 +01:00
|
|
|
layer_->SetSpatialFilterRect (query_extent.minx(),
|
|
|
|
query_extent.miny(),
|
|
|
|
query_extent.maxx(),
|
|
|
|
query_extent.maxy());
|
2009-01-29 17:20:30 +01:00
|
|
|
|
2009-01-28 21:16:31 +01:00
|
|
|
#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-01-29 17:20:30 +01:00
|
|
|
return featureset_ptr(new ogr_featureset(*dataset_, *layer_, 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-02-03 19:30:06 +01:00
|
|
|
OGRPoint point;
|
|
|
|
point.setX (pt.x);
|
|
|
|
point.setY (pt.y);
|
2009-01-29 17:20:30 +01:00
|
|
|
|
2009-02-03 19:30:06 +01:00
|
|
|
layer_->SetSpatialFilter (&point);
|
2009-01-29 17:20:30 +01:00
|
|
|
|
|
|
|
return featureset_ptr(new ogr_featureset(*dataset_, *layer_, desc_.get_encoding(), multiple_geometries_));
|
|
|
|
}
|
2009-01-28 21:16:31 +01:00
|
|
|
return featureset_ptr();
|
|
|
|
}
|
|
|
|
|