/***************************************************************************** * * This file is part of Mapnik (c++ mapping toolkit) * * Copyright (C) 2006 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 * *****************************************************************************/ #include #include #include #include // boost #include #include #include #include #include "shape_featureset.hpp" #include "shape_index_featureset.hpp" #include "shape.hpp" DATASOURCE_PLUGIN(shape_datasource) using mapnik::String; using mapnik::Double; using mapnik::Integer; using mapnik::datasource_exception; using mapnik::filter_in_box; using mapnik::filter_at_point; using mapnik::attribute_descriptor; shape_datasource::shape_datasource(const parameters ¶ms, bool bind) : datasource (params), type_(datasource::Vector), file_length_(0), indexed_(false), desc_(*params.get("type"), *params.get("encoding","utf-8")) { boost::optional file = params.get("file"); if (!file) throw datasource_exception("Shape Plugin: missing parameter"); boost::optional base = params.get("base"); if (base) shape_name_ = *base + "/" + *file; else shape_name_ = *file; boost::algorithm::ireplace_last(shape_name_,".shp",""); if (bind) { this->bind(); } } void shape_datasource::bind() const { if (is_bound_) return; if (!boost::filesystem::exists(shape_name_ + ".shp")) { throw datasource_exception("Shape Plugin: shapefile '" + shape_name_ + ".shp' does not exist"); } if (boost::filesystem::is_directory(shape_name_ + ".shp")) { throw datasource_exception("Shape Plugin: shapefile '" + shape_name_ + ".shp' appears to be a directory not a file"); } try { shape_ = boost::shared_ptr(new shape_io(shape_name_)); init(*shape_); for (int i=0;idbf().num_fields();++i) { field_descriptor const& fd=shape_->dbf().descriptor(i); std::string fld_name=fd.name_; switch (fd.type_) { case 'C': case 'D': case 'M': case 'L': desc_.add_descriptor(attribute_descriptor(fld_name, String)); break; case 'N': case 'F': { if (fd.dec_>0) { desc_.add_descriptor(attribute_descriptor(fld_name,Double,false,8)); } else { desc_.add_descriptor(attribute_descriptor(fld_name,Integer,false,4)); } break; } default: #ifdef MAPNIK_DEBUG std::clog << "Shape Plugin: unknown type " << fd.type_ << std::endl; #endif break; } } } catch (datasource_exception& ex) { std::clog<shp().seek(0); return featureset_ptr (new shape_index_featureset(filter, *shape_, q.property_names(), desc_.get_encoding())); } else { return featureset_ptr (new shape_featureset(filter, shape_name_, q.property_names(), desc_.get_encoding(), file_length_)); } } featureset_ptr shape_datasource::features_at_point(coord2d const& pt) const { if (!is_bound_) bind(); filter_at_point filter(pt); // collect all attribute names std::vector const& desc_vector = desc_.get_descriptors(); std::vector::const_iterator itr = desc_vector.begin(); std::vector::const_iterator end = desc_vector.end(); std::set names; while (itr != end) { names.insert(itr->get_name()); ++itr; } if (indexed_) { shape_->shp().seek(0); return featureset_ptr (new shape_index_featureset(filter, *shape_, names, desc_.get_encoding())); } else { return featureset_ptr (new shape_featureset(filter, shape_name_, names, desc_.get_encoding(), file_length_)); } } box2d shape_datasource::envelope() const { if (!is_bound_) bind(); return extent_; }