2006-03-31 12:32:02 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* Copyright (C) 2006 Artem Pavlenko
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* 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,
|
2005-06-14 17:06:59 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2006-03-31 12:32:02 +02:00
|
|
|
* 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
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
*****************************************************************************/
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2006-10-04 13:22:18 +02:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <mapnik/geom_util.hpp>
|
2005-06-16 22:56:31 +02:00
|
|
|
#include "shape_featureset.hpp"
|
|
|
|
#include "shape_index_featureset.hpp"
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2006-10-04 13:22:18 +02:00
|
|
|
#include "shape.hpp"
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2006-03-09 00:02:28 +01:00
|
|
|
DATASOURCE_PLUGIN(shape_datasource)
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-02-14 20:54:39 +01:00
|
|
|
shape_datasource::shape_datasource(const parameters ¶ms)
|
|
|
|
: datasource (params) ,
|
|
|
|
shape_name_(params.get("file")),
|
|
|
|
type_(datasource::Vector),
|
|
|
|
file_length_(0),
|
|
|
|
indexed_(false),
|
2007-02-14 22:55:04 +01:00
|
|
|
desc_(params.get("name"),"utf-8")
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2007-02-06 15:27:21 +01:00
|
|
|
try
|
|
|
|
{
|
2007-02-14 22:55:04 +01:00
|
|
|
std::string encoding = params.get("encoding");
|
|
|
|
if (encoding.length() > 0) desc_.set_encoding(encoding);
|
2007-02-06 15:27:21 +01:00
|
|
|
shape_io shape(shape_name_);
|
|
|
|
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_)
|
|
|
|
{
|
2006-03-22 16:55:58 +01:00
|
|
|
case 'C':
|
|
|
|
case 'D':
|
|
|
|
case 'M':
|
|
|
|
case 'L':
|
2007-02-06 15:27:21 +01:00
|
|
|
desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::String));
|
|
|
|
break;
|
2006-03-22 16:55:58 +01:00
|
|
|
case 'N':
|
|
|
|
case 'F':
|
2007-02-06 15:27:21 +01:00
|
|
|
{
|
|
|
|
if (fd.dec_>0)
|
|
|
|
{
|
|
|
|
desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::Double,false,8));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::Integer,false,4));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2006-03-22 16:55:58 +01:00
|
|
|
default:
|
2006-11-19 18:13:33 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2007-02-06 15:27:21 +01:00
|
|
|
std::clog << "unknown type "<<fd.type_<<"\n";
|
2006-11-19 18:13:33 +01:00
|
|
|
#endif
|
2007-02-06 15:27:21 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (datasource_exception& ex)
|
|
|
|
{
|
|
|
|
std::clog<<ex.what()<<std::endl;
|
|
|
|
throw;
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
shape_datasource::~shape_datasource()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2006-11-29 13:12:23 +01:00
|
|
|
const std::string shape_datasource::name_="shape";
|
2005-06-14 17:06:59 +02:00
|
|
|
|
|
|
|
void shape_datasource::init(shape_io& shape)
|
|
|
|
{
|
2007-02-06 15:27:21 +01:00
|
|
|
//first read header from *.shp
|
|
|
|
int file_code=shape.shp().read_xdr_integer();
|
|
|
|
if (file_code!=9994)
|
|
|
|
{
|
|
|
|
//invalid
|
|
|
|
throw datasource_exception("wrong file code");
|
|
|
|
}
|
|
|
|
shape.shp().skip(5*4);
|
|
|
|
file_length_=shape.shp().read_xdr_integer();
|
|
|
|
int version=shape.shp().read_ndr_integer();
|
|
|
|
if (version!=1000)
|
|
|
|
{
|
|
|
|
//invalid version number
|
|
|
|
throw datasource_exception("invalid version number");
|
|
|
|
}
|
2007-02-14 22:55:04 +01:00
|
|
|
int shape_type = shape.shp().read_ndr_integer();
|
2007-02-06 15:27:21 +01:00
|
|
|
shape.shp().read_envelope(extent_);
|
|
|
|
shape.shp().skip(4*8);
|
|
|
|
|
|
|
|
// check if we have an index file around
|
|
|
|
std::string index_name(shape_name_+".index");
|
|
|
|
std::ifstream file(index_name.c_str(),std::ios::in | std::ios::binary);
|
|
|
|
if (file)
|
|
|
|
{
|
|
|
|
indexed_=true;
|
|
|
|
file.close();
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2006-11-19 18:13:33 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2007-02-06 15:27:21 +01:00
|
|
|
std::clog << extent_ << std::endl;
|
|
|
|
std::clog << "file_length=" << file_length_ << std::endl;
|
|
|
|
std::clog << "shape_type=" << shape_type << std::endl;
|
2006-11-19 18:13:33 +01:00
|
|
|
#endif
|
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int shape_datasource::type() const
|
|
|
|
{
|
2007-02-06 15:27:21 +01:00
|
|
|
return type_;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
1. hit_test implementation for geometry objects:
bool hit_test(double x, double y, double tol);
2. added image_view(unsigned x, unsigned y, unsigned width, unsigned height)
allowing to select region from image data e.g (in Python):
im = Image(2048,2048)
view = im.view(0,0,256,256)
save_to_file(filename,type, view)
3. changed envelope method to return vy value in datasource classes
4. features_at_point impl for shape and postgis plug-ins
2006-11-25 12:02:59 +01:00
|
|
|
layer_descriptor shape_datasource::get_descriptor() const
|
2005-06-16 22:56:31 +02:00
|
|
|
{
|
2007-02-06 15:27:21 +01:00
|
|
|
return desc_;
|
2005-06-16 22:56:31 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
|
|
|
std::string shape_datasource::name()
|
|
|
|
{
|
2007-02-06 15:27:21 +01:00
|
|
|
return name_;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
featureset_ptr shape_datasource::features(const query& q) const
|
|
|
|
{
|
2007-02-06 15:27:21 +01:00
|
|
|
filter_in_box filter(q.get_bbox());
|
|
|
|
if (indexed_)
|
|
|
|
{
|
|
|
|
return featureset_ptr
|
2007-02-14 22:55:04 +01:00
|
|
|
(new shape_index_featureset<filter_in_box>(filter,
|
|
|
|
shape_name_,
|
|
|
|
q.property_names(),
|
|
|
|
desc_.get_encoding()));
|
2007-02-06 15:27:21 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return featureset_ptr
|
2007-02-14 22:55:04 +01:00
|
|
|
(new shape_featureset<filter_in_box>(filter,
|
|
|
|
shape_name_,
|
|
|
|
q.property_names(),
|
|
|
|
desc_.get_encoding(),
|
|
|
|
file_length_));
|
2007-02-06 15:27:21 +01:00
|
|
|
}
|
2006-10-16 23:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
featureset_ptr shape_datasource::features_at_point(coord2d const& pt) const
|
|
|
|
{
|
2007-02-06 15:27:21 +01:00
|
|
|
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;
|
1. hit_test implementation for geometry objects:
bool hit_test(double x, double y, double tol);
2. added image_view(unsigned x, unsigned y, unsigned width, unsigned height)
allowing to select region from image data e.g (in Python):
im = Image(2048,2048)
view = im.view(0,0,256,256)
save_to_file(filename,type, view)
3. changed envelope method to return vy value in datasource classes
4. features_at_point impl for shape and postgis plug-ins
2006-11-25 12:02:59 +01:00
|
|
|
|
2007-02-06 15:27:21 +01:00
|
|
|
while (itr != end)
|
|
|
|
{
|
|
|
|
names.insert(itr->get_name());
|
|
|
|
++itr;
|
|
|
|
}
|
1. hit_test implementation for geometry objects:
bool hit_test(double x, double y, double tol);
2. added image_view(unsigned x, unsigned y, unsigned width, unsigned height)
allowing to select region from image data e.g (in Python):
im = Image(2048,2048)
view = im.view(0,0,256,256)
save_to_file(filename,type, view)
3. changed envelope method to return vy value in datasource classes
4. features_at_point impl for shape and postgis plug-ins
2006-11-25 12:02:59 +01:00
|
|
|
|
2007-02-06 15:27:21 +01:00
|
|
|
if (indexed_)
|
|
|
|
{
|
|
|
|
return featureset_ptr
|
2007-02-14 22:55:04 +01:00
|
|
|
(new shape_index_featureset<filter_at_point>(filter,
|
|
|
|
shape_name_,
|
|
|
|
names,
|
|
|
|
desc_.get_encoding()));
|
2007-02-06 15:27:21 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return featureset_ptr
|
2007-02-14 22:55:04 +01:00
|
|
|
(new shape_featureset<filter_at_point>(filter,
|
|
|
|
shape_name_,
|
|
|
|
names,
|
|
|
|
desc_.get_encoding(),
|
|
|
|
file_length_));
|
2007-02-06 15:27:21 +01:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
1. hit_test implementation for geometry objects:
bool hit_test(double x, double y, double tol);
2. added image_view(unsigned x, unsigned y, unsigned width, unsigned height)
allowing to select region from image data e.g (in Python):
im = Image(2048,2048)
view = im.view(0,0,256,256)
save_to_file(filename,type, view)
3. changed envelope method to return vy value in datasource classes
4. features_at_point impl for shape and postgis plug-ins
2006-11-25 12:02:59 +01:00
|
|
|
Envelope<double> shape_datasource::envelope() const
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2007-02-06 15:27:21 +01:00
|
|
|
return extent_;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|