diff --git a/plugins/input/shape/shape.cpp b/plugins/input/shape/shape.cpp index 3646ad388..fe55a935b 100644 --- a/plugins/input/shape/shape.cpp +++ b/plugins/input/shape/shape.cpp @@ -47,23 +47,23 @@ 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")) + : 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("missing parameter"); - - boost::optional base = params.get("base"); - if (base) - shape_name_ = *base + "/" + *file; - else - shape_name_ = *file; + boost::optional file = params.get("file"); + if (!file) throw datasource_exception("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",""); - boost::algorithm::ireplace_last(shape_name_,".shp",""); - if (bind) { this->bind(); @@ -73,204 +73,207 @@ shape_datasource::shape_datasource(const parameters ¶ms, bool bind) void shape_datasource::bind() const { if (is_bound_) return; - - if (!boost::filesystem::exists(shape_name_ + ".shp")) - { - throw datasource_exception(shape_name_ + " does not exist"); - } - try - { - shape_io shape(shape_name_); - // leave ABI alone - ((shape_datasource*)this)->init(shape); - for (int i=0;iinit(shape); + for (int i=0;i0) - { - desc_.add_descriptor(attribute_descriptor(fld_name,Double,false,8)); - } - else - { - desc_.add_descriptor(attribute_descriptor(fld_name,Integer,false,4)); - } - break; + 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 << "unknown type "<(filter, - shape_name_, - q.property_names(), - desc_.get_encoding())); - } - else - { - return featureset_ptr - (new shape_featureset(filter, - shape_name_, - q.property_names(), - desc_.get_encoding(), - file_length_)); - } + + filter_in_box filter(q.get_bbox()); + + if (indexed_) + { + return featureset_ptr + (new shape_index_featureset(filter, + shape_name_, + 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_) - { - return featureset_ptr - (new shape_index_featureset(filter, - shape_name_, - names, - desc_.get_encoding())); - } - else - { - return featureset_ptr - (new shape_featureset(filter, - shape_name_, - names, - desc_.get_encoding(), - file_length_)); - } + 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_) + { + return featureset_ptr + (new shape_index_featureset(filter, + shape_name_, + names, + desc_.get_encoding())); + } + else + { + return featureset_ptr + (new shape_featureset(filter, + shape_name_, + names, + desc_.get_encoding(), + file_length_)); + } } Envelope shape_datasource::envelope() const { if (!is_bound_) bind(); - - return extent_; + + return extent_; } diff --git a/plugins/input/shape/shape.hpp b/plugins/input/shape/shape.hpp index 85c40caed..078635d0f 100644 --- a/plugins/input/shape/shape.hpp +++ b/plugins/input/shape/shape.hpp @@ -42,14 +42,14 @@ class shape_datasource : public datasource public: shape_datasource(const parameters ¶ms, bool bind=true); virtual ~shape_datasource(); - + int type() const; static std::string name(); featureset_ptr features(const query& q) const; featureset_ptr features_at_point(coord2d const& pt) const; Envelope envelope() const; - layer_descriptor get_descriptor() const; - void bind() const; + layer_descriptor get_descriptor() const; + void bind() const; private: shape_datasource(const shape_datasource&); shape_datasource& operator=(const shape_datasource&); @@ -61,7 +61,6 @@ class shape_datasource : public datasource mutable Envelope extent_; mutable bool indexed_; mutable layer_descriptor desc_; - static const std::string name_; }; #endif //SHAPE_HPP diff --git a/plugins/input/shape/shapefile.hpp b/plugins/input/shape/shapefile.hpp index c4ec7a6b1..2602a51ee 100644 --- a/plugins/input/shape/shapefile.hpp +++ b/plugins/input/shape/shapefile.hpp @@ -51,7 +51,7 @@ struct RecordTag { return static_cast(::operator new(sizeof(char)*size)); } - + static void dealloc(data_type data) { ::operator delete(data); @@ -72,89 +72,89 @@ struct shape_record size_t size; mutable size_t pos; explicit shape_record(size_t size) - : + : data(Tag::alloc(size)), size(size), - pos(0) {} - + pos(0) {} + void set_data(typename Tag::data_type data_) { data = data_; } - typename Tag::data_type get_data() + typename Tag::data_type get_data() { - return data; + return data; } - + void skip(unsigned n) { pos+=n; } - + int read_ndr_integer() { - boost::int32_t val; + boost::int32_t val; read_int32_ndr(&data[pos],val); pos+=4; return val; } - + int read_xdr_integer() { - boost::int32_t val; + boost::int32_t val; read_int32_xdr(&data[pos],val); pos+=4; return val; } - + double read_double() { - double val; + double val; read_double_ndr(&data[pos],val); pos+=8; return val; } - long remains() + + long remains() { return (size-pos); } - - ~shape_record() + + ~shape_record() { Tag::dealloc(data); } - }; using namespace boost::iostreams; class shape_file : boost::noncopyable -{ +{ #ifdef SHAPE_MEMORY_MAPPED_FILE stream file_; #else stream file_; #endif - + public: #ifdef SHAPE_MEMORY_MAPPED_FILE typedef shape_record record_type; #else typedef shape_record record_type; #endif - + shape_file() {} shape_file(std::string const& file_name) - : + : #ifdef SHAPE_MEMORY_MAPPED_FILE file_(file_name) #else file_(file_name,std::ios::in | std::ios::binary) #endif {} - + ~shape_file() {} inline bool is_open() @@ -162,13 +162,12 @@ public: return file_.is_open(); } - inline void close() { if (file_ && file_.is_open()) file_.close(); } - + inline void read_record(record_type& rec) { #ifdef SHAPE_MEMORY_MAPPED_FILE @@ -178,7 +177,7 @@ public: file_.read(rec.get_data(),rec.size); #endif } - + inline int read_xdr_integer() { char b[4]; @@ -187,7 +186,7 @@ public: read_int32_xdr(b,val); return val; } - + inline int read_ndr_integer() { char b[4]; @@ -196,7 +195,7 @@ public: read_int32_ndr(b,val); return val; } - + inline double read_double() { double val; @@ -209,7 +208,7 @@ public: #endif return val; } - + inline void read_envelope(Envelope& envelope) { #ifndef MAPNIK_BIG_ENDIAN @@ -223,29 +222,29 @@ public: read_double_ndr(data + 2*8,maxx); read_double_ndr(data + 3*8,maxy); envelope.init(minx,miny,maxx,maxy); -#endif +#endif } - + inline void skip(std::streampos bytes) { file_.seekg(bytes,std::ios::cur); } - + inline void rewind() { seek(100); } - + inline void seek(std::streampos pos) { file_.seekg(pos,std::ios::beg); } - + inline std::streampos pos() { return file_.tellg(); } - + inline bool is_eof() { return file_.eof();