+ backports cosmetics
+ backports avoid static string as datasource name
This commit is contained in:
parent
980098e2d5
commit
b4421bc39c
3 changed files with 201 additions and 200 deletions
|
@ -47,22 +47,22 @@ using mapnik::filter_at_point;
|
||||||
using mapnik::attribute_descriptor;
|
using mapnik::attribute_descriptor;
|
||||||
|
|
||||||
shape_datasource::shape_datasource(const parameters ¶ms, bool bind)
|
shape_datasource::shape_datasource(const parameters ¶ms, bool bind)
|
||||||
: datasource (params),
|
: datasource (params),
|
||||||
type_(datasource::Vector),
|
type_(datasource::Vector),
|
||||||
file_length_(0),
|
file_length_(0),
|
||||||
indexed_(false),
|
indexed_(false),
|
||||||
desc_(*params.get<std::string>("type"), *params.get<std::string>("encoding","utf-8"))
|
desc_(*params.get<std::string>("type"), *params.get<std::string>("encoding","utf-8"))
|
||||||
{
|
{
|
||||||
boost::optional<std::string> file = params.get<std::string>("file");
|
boost::optional<std::string> file = params.get<std::string>("file");
|
||||||
if (!file) throw datasource_exception("missing <file> parameter");
|
if (!file) throw datasource_exception("missing <file> parameter");
|
||||||
|
|
||||||
boost::optional<std::string> base = params.get<std::string>("base");
|
boost::optional<std::string> base = params.get<std::string>("base");
|
||||||
if (base)
|
if (base)
|
||||||
shape_name_ = *base + "/" + *file;
|
shape_name_ = *base + "/" + *file;
|
||||||
else
|
else
|
||||||
shape_name_ = *file;
|
shape_name_ = *file;
|
||||||
|
|
||||||
boost::algorithm::ireplace_last(shape_name_,".shp","");
|
boost::algorithm::ireplace_last(shape_name_,".shp","");
|
||||||
|
|
||||||
if (bind)
|
if (bind)
|
||||||
{
|
{
|
||||||
|
@ -74,203 +74,206 @@ void shape_datasource::bind() const
|
||||||
{
|
{
|
||||||
if (is_bound_) return;
|
if (is_bound_) return;
|
||||||
|
|
||||||
if (!boost::filesystem::exists(shape_name_ + ".shp"))
|
if (!boost::filesystem::exists(shape_name_ + ".shp"))
|
||||||
{
|
{
|
||||||
throw datasource_exception(shape_name_ + " does not exist");
|
throw datasource_exception("shapefile '" + shape_name_ + ".shp' does not exist");
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
if (boost::filesystem::is_directory(shape_name_ + ".shp"))
|
||||||
{
|
{
|
||||||
shape_io shape(shape_name_);
|
throw datasource_exception("shapefile '" + shape_name_ + ".shp' appears to be a directory not a file");
|
||||||
// leave ABI alone
|
}
|
||||||
((shape_datasource*)this)->init(shape);
|
|
||||||
for (int i=0;i<shape.dbf().num_fields();++i)
|
try
|
||||||
{
|
{
|
||||||
field_descriptor const& fd=shape.dbf().descriptor(i);
|
shape_io shape(shape_name_);
|
||||||
std::string fld_name=fd.name_;
|
// leave ABI alone
|
||||||
switch (fd.type_)
|
((shape_datasource*)this)->init(shape);
|
||||||
{
|
for (int i=0;i<shape.dbf().num_fields();++i)
|
||||||
|
{
|
||||||
|
field_descriptor const& fd=shape.dbf().descriptor(i);
|
||||||
|
std::string fld_name=fd.name_;
|
||||||
|
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, String));
|
desc_.add_descriptor(attribute_descriptor(fld_name, 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,Double,false,8));
|
desc_.add_descriptor(attribute_descriptor(fld_name,Double,false,8));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
desc_.add_descriptor(attribute_descriptor(fld_name,Integer,false,4));
|
desc_.add_descriptor(attribute_descriptor(fld_name,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;
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
std::clog << " got exception ... \n";
|
std::clog << " got exception ... \n";
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
is_bound_ = true;
|
is_bound_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
shape_datasource::~shape_datasource() {}
|
shape_datasource::~shape_datasource() {}
|
||||||
|
|
||||||
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 file code
|
//invalid file code
|
||||||
throw datasource_exception((boost::format("wrong file code : %d") % file_code).str());
|
throw datasource_exception((boost::format("wrong file code : %d") % file_code).str());
|
||||||
}
|
}
|
||||||
|
|
||||||
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((boost::format("invalid version number: %d") % version).str());
|
throw datasource_exception((boost::format("invalid version number: %d") % version).str());
|
||||||
}
|
}
|
||||||
|
|
||||||
#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_);
|
|
||||||
|
|
||||||
#ifdef MAPNIK_DEBUG
|
#ifdef MAPNIK_DEBUG
|
||||||
double zmin = shape.shp().read_double();
|
double zmin = shape.shp().read_double();
|
||||||
double zmax = shape.shp().read_double();
|
double zmax = shape.shp().read_double();
|
||||||
double mmin = shape.shp().read_double();
|
double mmin = shape.shp().read_double();
|
||||||
double mmax = shape.shp().read_double();
|
double mmax = shape.shp().read_double();
|
||||||
|
|
||||||
std::clog << "Z min/max " << zmin << "," << zmax << "\n";
|
std::clog << "Z min/max " << zmin << "," << zmax << "\n";
|
||||||
std::clog << "M min/max " << mmin << "," << mmax << "\n";
|
std::clog << "M min/max " << mmin << "," << mmax << "\n";
|
||||||
#else
|
#else
|
||||||
shape.shp().skip(4*8);
|
shape.shp().skip(4*8);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// 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
|
||||||
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
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string shape_datasource::name()
|
||||||
|
{
|
||||||
|
return "shape";
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
{
|
{
|
||||||
if (!is_bound_) bind();
|
if (!is_bound_) bind();
|
||||||
return desc_;
|
return desc_;
|
||||||
}
|
|
||||||
|
|
||||||
std::string shape_datasource::name()
|
|
||||||
{
|
|
||||||
return name_;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
featureset_ptr shape_datasource::features(const query& q) const
|
featureset_ptr shape_datasource::features(const query& q) const
|
||||||
{
|
{
|
||||||
if (!is_bound_) bind();
|
if (!is_bound_) bind();
|
||||||
|
|
||||||
filter_in_box filter(q.get_bbox());
|
filter_in_box filter(q.get_bbox());
|
||||||
if (indexed_)
|
|
||||||
{
|
if (indexed_)
|
||||||
return featureset_ptr
|
{
|
||||||
(new shape_index_featureset<filter_in_box>(filter,
|
return featureset_ptr
|
||||||
shape_name_,
|
(new shape_index_featureset<filter_in_box>(filter,
|
||||||
q.property_names(),
|
shape_name_,
|
||||||
desc_.get_encoding()));
|
q.property_names(),
|
||||||
}
|
desc_.get_encoding()));
|
||||||
else
|
}
|
||||||
{
|
else
|
||||||
return featureset_ptr
|
{
|
||||||
(new shape_featureset<filter_in_box>(filter,
|
return featureset_ptr
|
||||||
shape_name_,
|
(new shape_featureset<filter_in_box>(filter,
|
||||||
q.property_names(),
|
shape_name_,
|
||||||
desc_.get_encoding(),
|
q.property_names(),
|
||||||
file_length_));
|
desc_.get_encoding(),
|
||||||
}
|
file_length_));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
featureset_ptr shape_datasource::features_at_point(coord2d const& pt) const
|
featureset_ptr shape_datasource::features_at_point(coord2d const& pt) const
|
||||||
{
|
{
|
||||||
if (!is_bound_) bind();
|
if (!is_bound_) bind();
|
||||||
|
|
||||||
filter_at_point filter(pt);
|
filter_at_point filter(pt);
|
||||||
// collect all attribute names
|
|
||||||
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 end = desc_vector.end();
|
|
||||||
std::set<std::string> names;
|
|
||||||
|
|
||||||
while (itr != end)
|
// collect all attribute names
|
||||||
{
|
std::vector<attribute_descriptor> const& desc_vector = desc_.get_descriptors();
|
||||||
names.insert(itr->get_name());
|
std::vector<attribute_descriptor>::const_iterator itr = desc_vector.begin();
|
||||||
++itr;
|
std::vector<attribute_descriptor>::const_iterator end = desc_vector.end();
|
||||||
}
|
std::set<std::string> names;
|
||||||
|
|
||||||
if (indexed_)
|
while (itr != end)
|
||||||
{
|
{
|
||||||
return featureset_ptr
|
names.insert(itr->get_name());
|
||||||
(new shape_index_featureset<filter_at_point>(filter,
|
++itr;
|
||||||
shape_name_,
|
}
|
||||||
names,
|
|
||||||
desc_.get_encoding()));
|
if (indexed_)
|
||||||
}
|
{
|
||||||
else
|
return featureset_ptr
|
||||||
{
|
(new shape_index_featureset<filter_at_point>(filter,
|
||||||
return featureset_ptr
|
shape_name_,
|
||||||
(new shape_featureset<filter_at_point>(filter,
|
names,
|
||||||
shape_name_,
|
desc_.get_encoding()));
|
||||||
names,
|
}
|
||||||
desc_.get_encoding(),
|
else
|
||||||
file_length_));
|
{
|
||||||
}
|
return featureset_ptr
|
||||||
|
(new shape_featureset<filter_at_point>(filter,
|
||||||
|
shape_name_,
|
||||||
|
names,
|
||||||
|
desc_.get_encoding(),
|
||||||
|
file_length_));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Envelope<double> shape_datasource::envelope() const
|
Envelope<double> shape_datasource::envelope() const
|
||||||
{
|
{
|
||||||
if (!is_bound_) bind();
|
if (!is_bound_) bind();
|
||||||
|
|
||||||
return extent_;
|
return extent_;
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,7 +61,6 @@ class shape_datasource : public datasource
|
||||||
mutable Envelope<double> extent_;
|
mutable Envelope<double> extent_;
|
||||||
mutable bool indexed_;
|
mutable bool indexed_;
|
||||||
mutable layer_descriptor desc_;
|
mutable layer_descriptor desc_;
|
||||||
static const std::string name_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //SHAPE_HPP
|
#endif //SHAPE_HPP
|
||||||
|
|
|
@ -94,7 +94,7 @@ struct shape_record
|
||||||
|
|
||||||
int read_ndr_integer()
|
int read_ndr_integer()
|
||||||
{
|
{
|
||||||
boost::int32_t val;
|
boost::int32_t val;
|
||||||
read_int32_ndr(&data[pos],val);
|
read_int32_ndr(&data[pos],val);
|
||||||
pos+=4;
|
pos+=4;
|
||||||
return val;
|
return val;
|
||||||
|
@ -102,7 +102,7 @@ struct shape_record
|
||||||
|
|
||||||
int read_xdr_integer()
|
int read_xdr_integer()
|
||||||
{
|
{
|
||||||
boost::int32_t val;
|
boost::int32_t val;
|
||||||
read_int32_xdr(&data[pos],val);
|
read_int32_xdr(&data[pos],val);
|
||||||
pos+=4;
|
pos+=4;
|
||||||
return val;
|
return val;
|
||||||
|
@ -115,6 +115,7 @@ struct shape_record
|
||||||
pos+=8;
|
pos+=8;
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
long remains()
|
long remains()
|
||||||
{
|
{
|
||||||
return (size-pos);
|
return (size-pos);
|
||||||
|
@ -124,7 +125,6 @@ struct shape_record
|
||||||
{
|
{
|
||||||
Tag::dealloc(data);
|
Tag::dealloc(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
using namespace boost::iostreams;
|
using namespace boost::iostreams;
|
||||||
|
@ -162,7 +162,6 @@ public:
|
||||||
return file_.is_open();
|
return file_.is_open();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
inline void close()
|
inline void close()
|
||||||
{
|
{
|
||||||
if (file_ && file_.is_open())
|
if (file_ && file_.is_open())
|
||||||
|
|
Loading…
Reference in a new issue