and another one :)

This commit is contained in:
Artem Pavlenko 2007-03-08 18:19:42 +00:00
parent d10a7eeb24
commit 6ed05bcf42

View file

@ -1,212 +1,211 @@
/***************************************************************************** /*****************************************************************************
* *
* This file is part of Mapnik (c++ mapping toolkit) * This file is part of Mapnik (c++ mapping toolkit)
* *
* Copyright (C) 2006 Artem Pavlenko * Copyright (C) 2006 Artem Pavlenko
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either * License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version. * 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, * This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details. * Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public * You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software * License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
* *
*****************************************************************************/ *****************************************************************************/
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <stdexcept> #include <stdexcept>
#include <mapnik/geom_util.hpp> #include <mapnik/geom_util.hpp>
#include "shape_featureset.hpp" #include "shape_featureset.hpp"
#include "shape_index_featureset.hpp" #include "shape_index_featureset.hpp"
#include "shape.hpp" #include "shape.hpp"
DATASOURCE_PLUGIN(shape_datasource) DATASOURCE_PLUGIN(shape_datasource)
shape_datasource::shape_datasource(const parameters &params) shape_datasource::shape_datasource(const parameters &params)
: datasource (params) , : datasource (params) ,
shape_name_(params.get("file")), shape_name_(params.get("file")),
type_(datasource::Vector), type_(datasource::Vector),
file_length_(0), file_length_(0),
indexed_(false), indexed_(false),
desc_(params.get("name"),"utf-8") desc_(params.get("name"),"utf-8")
{ {
try try
{ {
std::string encoding = params.get("encoding"); std::string encoding = params.get("encoding");
if (encoding.length() > 0) desc_.set_encoding(encoding); if (encoding.length() > 0) desc_.set_encoding(encoding);
shape_io shape(shape_name_); shape_io shape(shape_name_);
init(shape); init(shape);
for (int i=0;i<shape.dbf().num_fields();++i) for (int i=0;i<shape.dbf().num_fields();++i)
{ {
field_descriptor const& fd=shape.dbf().descriptor(i); field_descriptor const& fd=shape.dbf().descriptor(i);
std::string fld_name=fd.name_; std::string fld_name=fd.name_;
switch (fd.type_) switch (fd.type_)
{ {
case 'C': case 'C':
case 'D': case 'D':
case 'M': case 'M':
case 'L': case 'L':
desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::String)); desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::String));
break; break;
case 'N': case 'N':
case 'F': case 'F':
{ {
if (fd.dec_>0) if (fd.dec_>0)
{ {
desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::Double,false,8)); desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::Double,false,8));
} }
else else
{ {
desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::Integer,false,4)); desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::Integer,false,4));
} }
break; break;
} }
default: default:
#ifdef MAPNIK_DEBUG #ifdef MAPNIK_DEBUG
std::clog << "unknown type "<<fd.type_<<"\n"; std::clog << "unknown type "<<fd.type_<<"\n";
#endif #endif
break; break;
} }
} }
} }
catch (datasource_exception& ex) catch (datasource_exception& ex)
{ {
std::clog<<ex.what()<<std::endl; std::clog<<ex.what()<<std::endl;
throw; throw;
} }
} }
shape_datasource::~shape_datasource() shape_datasource::~shape_datasource()
{ {
} }
const std::string shape_datasource::name_="shape"; const std::string shape_datasource::name_="shape";
void shape_datasource::init(shape_io& shape) void shape_datasource::init(shape_io& shape)
{ {
//first read header from *.shp //first read header from *.shp
int file_code=shape.shp().read_xdr_integer(); int file_code=shape.shp().read_xdr_integer();
if (file_code!=9994) if (file_code!=9994)
{ {
//invalid //invalid
throw datasource_exception("wrong file code"); throw datasource_exception("wrong file code");
} }
shape.shp().skip(5*4); shape.shp().skip(5*4);
file_length_=shape.shp().read_xdr_integer(); file_length_=shape.shp().read_xdr_integer();
int version=shape.shp().read_ndr_integer(); int version=shape.shp().read_ndr_integer();
if (version!=1000) if (version!=1000)
{ {
//invalid version number //invalid version number
throw datasource_exception("invalid version number"); throw datasource_exception("invalid version number");
} }
#ifdef MAPNIK_DEBUG #ifdef MAPNIK_DEBUG
int shape_type = shape.shp().read_ndr_integer(); int shape_type = shape.shp().read_ndr_integer();
#else #else
shape.shp().skip(4); shape.shp().skip(4);
#endif #endif
shape.shp().read_envelope(extent_); shape.shp().read_envelope(extent_);
shape.shp().skip(4*8); shape.shp().skip(4*8);
// check if we have an index file around // check if we have an index file around
std::string index_name(shape_name_+".index"); std::string index_name(shape_name_+".index");
std::ifstream file(index_name.c_str(),std::ios::in | std::ios::binary); std::ifstream file(index_name.c_str(),std::ios::in | std::ios::binary);
if (file) if (file)
{ {
indexed_=true; indexed_=true;
file.close(); file.close();
} }
#ifdef MAPNIK_DEBUG #ifdef MAPNIK_DEBUG
int shape_type = shape.shp().read_ndr_integer(); std::clog << extent_ << std::endl;
std::clog << extent_ << std::endl; std::clog << "file_length=" << file_length_ << std::endl;
std::clog << "file_length=" << file_length_ << std::endl; std::clog << "shape_type=" << shape_type << std::endl;
std::clog << "shape_type=" << shape_type << std::endl; #endif
#endif
}
}
int shape_datasource::type() const
int shape_datasource::type() const {
{ return type_;
return type_; }
}
layer_descriptor shape_datasource::get_descriptor() const
layer_descriptor shape_datasource::get_descriptor() const {
{ return desc_;
return desc_; }
}
std::string shape_datasource::name()
std::string shape_datasource::name() {
{ return name_;
return name_; }
}
featureset_ptr shape_datasource::features(const query& q) const
featureset_ptr shape_datasource::features(const query& q) const {
{ filter_in_box filter(q.get_bbox());
filter_in_box filter(q.get_bbox()); if (indexed_)
if (indexed_) {
{ return featureset_ptr
return featureset_ptr (new shape_index_featureset<filter_in_box>(filter,
(new shape_index_featureset<filter_in_box>(filter, shape_name_,
shape_name_, q.property_names(),
q.property_names(), desc_.get_encoding()));
desc_.get_encoding())); }
} else
else {
{ return featureset_ptr
return featureset_ptr (new shape_featureset<filter_in_box>(filter,
(new shape_featureset<filter_in_box>(filter, shape_name_,
shape_name_, q.property_names(),
q.property_names(), desc_.get_encoding(),
desc_.get_encoding(), file_length_));
file_length_)); }
} }
}
featureset_ptr shape_datasource::features_at_point(coord2d const& pt) const
featureset_ptr shape_datasource::features_at_point(coord2d const& pt) const {
{ filter_at_point filter(pt);
filter_at_point filter(pt); // collect all attribute names
// collect all attribute names std::vector<attribute_descriptor> const& desc_vector = desc_.get_descriptors();
std::vector<attribute_descriptor> const& desc_vector = desc_.get_descriptors(); std::vector<attribute_descriptor>::const_iterator itr = desc_vector.begin();
std::vector<attribute_descriptor>::const_iterator itr = desc_vector.begin(); std::vector<attribute_descriptor>::const_iterator end = desc_vector.end();
std::vector<attribute_descriptor>::const_iterator end = desc_vector.end(); std::set<std::string> names;
std::set<std::string> names;
while (itr != end)
while (itr != end) {
{ names.insert(itr->get_name());
names.insert(itr->get_name()); ++itr;
++itr; }
}
if (indexed_)
if (indexed_) {
{ return featureset_ptr
return featureset_ptr (new shape_index_featureset<filter_at_point>(filter,
(new shape_index_featureset<filter_at_point>(filter, shape_name_,
shape_name_, names,
names, desc_.get_encoding()));
desc_.get_encoding())); }
} else
else {
{ return featureset_ptr
return featureset_ptr (new shape_featureset<filter_at_point>(filter,
(new shape_featureset<filter_at_point>(filter, shape_name_,
shape_name_, names,
names, desc_.get_encoding(),
desc_.get_encoding(), file_length_));
file_length_)); }
} }
}
Envelope<double> shape_datasource::envelope() const
Envelope<double> shape_datasource::envelope() const {
{ return extent_;
return extent_; }
}