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
|
|
|
|
|
|
|
//$Id: shape_io.cc 26 2005-03-29 19:18:59Z pavlenko $
|
|
|
|
|
2005-06-16 22:56:31 +02:00
|
|
|
#include "shape_io.hpp"
|
|
|
|
#include "shape.hpp"
|
2010-11-15 04:16:23 +01:00
|
|
|
#include <boost/filesystem/operations.hpp>
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-03-16 11:11:37 +01:00
|
|
|
using mapnik::datasource_exception;
|
2010-11-03 14:19:15 +01:00
|
|
|
using mapnik::geometry_type;
|
2010-09-02 22:20:42 +02:00
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
const std::string shape_io::SHP = ".shp";
|
|
|
|
const std::string shape_io::DBF = ".dbf";
|
2010-09-02 22:20:42 +02:00
|
|
|
const std::string shape_io::INDEX = ".index";
|
2005-06-14 17:06:59 +02:00
|
|
|
|
|
|
|
shape_io::shape_io(const std::string& shape_name)
|
2007-09-16 13:23:51 +02:00
|
|
|
: type_(shape_null),
|
2008-02-04 12:12:32 +01:00
|
|
|
shp_(shape_name + SHP),
|
|
|
|
dbf_(shape_name + DBF),
|
2007-09-16 13:23:51 +02:00
|
|
|
reclength_(0),
|
|
|
|
id_(0)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2010-09-06 21:20:59 +02:00
|
|
|
bool ok = (shp_.is_open() && dbf_.is_open());
|
|
|
|
if (!ok)
|
|
|
|
{
|
2010-09-14 17:43:38 +02:00
|
|
|
throw datasource_exception("Shape Plugin: cannot read shape file '" + shape_name + "'");
|
2010-09-06 21:20:59 +02:00
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
2010-11-15 04:16:23 +01:00
|
|
|
if (!boost::filesystem::exists(shape_name + INDEX))
|
|
|
|
{
|
2010-11-29 10:15:43 +01:00
|
|
|
throw datasource_exception("Shape Plugin: could not open index: '" + shape_name + INDEX + "' does not exist");
|
2010-11-15 04:16:23 +01:00
|
|
|
}
|
|
|
|
|
2010-09-06 21:20:59 +02:00
|
|
|
index_= boost::shared_ptr<shape_file>(new shape_file(shape_name + INDEX));
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2010-11-29 10:15:43 +01:00
|
|
|
std::cerr << "Shape Plugin: warning - could not open index: '" + shape_name + INDEX + "'" << std::endl;
|
2010-09-06 21:20:59 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
shape_io::~shape_io()
|
|
|
|
{
|
2007-09-16 13:23:51 +02:00
|
|
|
shp_.close();
|
|
|
|
dbf_.close();
|
2010-09-02 22:20:42 +02:00
|
|
|
if (index_) (*index_).close();
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void shape_io::move_to (int pos)
|
|
|
|
{
|
2007-09-16 13:23:51 +02:00
|
|
|
shp_.seek(pos);
|
|
|
|
id_ = shp_.read_xdr_integer();
|
|
|
|
reclength_ = shp_.read_xdr_integer();
|
|
|
|
type_ = shp_.read_ndr_integer();
|
2010-07-25 21:55:32 +02:00
|
|
|
|
2008-02-04 12:12:32 +01:00
|
|
|
if (shp_.is_eof()) {
|
|
|
|
id_ = 0;
|
|
|
|
reclength_ = 0;
|
|
|
|
type_ = shape_null;
|
|
|
|
}
|
|
|
|
|
2010-07-25 21:55:32 +02:00
|
|
|
if (type_!= shape_null && type_ != shape_point && type_ != shape_pointm && type_ != shape_pointz)
|
2007-09-16 13:23:51 +02:00
|
|
|
{
|
|
|
|
shp_.read_envelope(cur_extent_);
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int shape_io::type() const
|
|
|
|
{
|
2007-09-16 13:23:51 +02:00
|
|
|
return type_;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
2009-12-16 21:02:06 +01:00
|
|
|
const box2d<double>& shape_io::current_extent() const
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2007-09-16 13:23:51 +02:00
|
|
|
return cur_extent_;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
shape_file& shape_io::shp()
|
|
|
|
{
|
2007-09-16 13:23:51 +02:00
|
|
|
return shp_;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
shape_file& shape_io::shx()
|
|
|
|
{
|
2007-09-16 13:23:51 +02:00
|
|
|
return shx_;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
dbf_file& shape_io::dbf()
|
|
|
|
{
|
2007-09-16 13:23:51 +02:00
|
|
|
return dbf_;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
2010-11-03 14:19:15 +01:00
|
|
|
geometry_type * shape_io::read_polyline()
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2009-07-08 01:56:01 +02:00
|
|
|
shape_file::record_type record(reclength_*2-36);
|
2007-09-16 13:23:51 +02:00
|
|
|
shp_.read_record(record);
|
|
|
|
int num_parts=record.read_ndr_integer();
|
|
|
|
int num_points=record.read_ndr_integer();
|
2010-11-03 14:19:15 +01:00
|
|
|
geometry_type * line = new geometry_type(mapnik::LineString);
|
2007-09-16 13:23:51 +02:00
|
|
|
line->set_capacity(num_points + num_parts);
|
|
|
|
if (num_parts == 1)
|
|
|
|
{
|
|
|
|
line->set_capacity(num_points + 1);
|
|
|
|
record.skip(4);
|
|
|
|
double x=record.read_double();
|
|
|
|
double y=record.read_double();
|
|
|
|
line->move_to(x,y);
|
|
|
|
for (int i=1;i<num_points;++i)
|
|
|
|
{
|
|
|
|
x=record.read_double();
|
|
|
|
y=record.read_double();
|
|
|
|
line->line_to(x,y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::vector<int> parts(num_parts);
|
|
|
|
for (int i=0;i<num_parts;++i)
|
|
|
|
{
|
|
|
|
parts[i]=record.read_ndr_integer();
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
int start,end;
|
|
|
|
for (int k=0;k<num_parts;++k)
|
|
|
|
{
|
|
|
|
start=parts[k];
|
|
|
|
if (k==num_parts-1)
|
|
|
|
end=num_points;
|
|
|
|
else
|
|
|
|
end=parts[k+1];
|
2010-07-25 21:55:32 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
double x=record.read_double();
|
|
|
|
double y=record.read_double();
|
|
|
|
line->move_to(x,y);
|
2010-07-25 21:55:32 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
for (int j=start+1;j<end;++j)
|
|
|
|
{
|
|
|
|
x=record.read_double();
|
|
|
|
y=record.read_double();
|
|
|
|
line->line_to(x,y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return line;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
2010-11-03 14:19:15 +01:00
|
|
|
geometry_type * shape_io::read_polylinem()
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2009-07-08 01:56:01 +02:00
|
|
|
shape_file::record_type record(reclength_*2-36);
|
2007-09-16 13:23:51 +02:00
|
|
|
shp_.read_record(record);
|
|
|
|
int num_parts=record.read_ndr_integer();
|
|
|
|
int num_points=record.read_ndr_integer();
|
2010-11-03 14:19:15 +01:00
|
|
|
geometry_type * line = new geometry_type(mapnik::LineString);
|
2007-09-16 13:23:51 +02:00
|
|
|
line->set_capacity(num_points + num_parts);
|
|
|
|
if (num_parts == 1)
|
|
|
|
{
|
|
|
|
record.skip(4);
|
|
|
|
double x=record.read_double();
|
|
|
|
double y=record.read_double();
|
|
|
|
line->move_to(x,y);
|
|
|
|
for (int i=1;i<num_points;++i)
|
|
|
|
{
|
|
|
|
x=record.read_double();
|
|
|
|
y=record.read_double();
|
|
|
|
line->line_to(x,y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::vector<int> parts(num_parts);
|
|
|
|
for (int i=0;i<num_parts;++i)
|
|
|
|
{
|
|
|
|
parts[i]=record.read_ndr_integer();
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
int start,end;
|
|
|
|
for (int k=0;k<num_parts;++k)
|
|
|
|
{
|
|
|
|
start=parts[k];
|
|
|
|
if (k==num_parts-1)
|
|
|
|
end=num_points;
|
|
|
|
else
|
|
|
|
end=parts[k+1];
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
double x=record.read_double();
|
|
|
|
double y=record.read_double();
|
|
|
|
line->move_to(x,y);
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
for (int j=start+1;j<end;++j)
|
|
|
|
{
|
|
|
|
x=record.read_double();
|
|
|
|
y=record.read_double();
|
|
|
|
line->line_to(x,y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// m-range
|
|
|
|
//double m0=record.read_double();
|
|
|
|
//double m1=record.read_double();
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
//for (int i=0;i<num_points;++i)
|
|
|
|
//{
|
|
|
|
// double m=record.read_double();
|
|
|
|
//}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
return line;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
2010-11-03 14:19:15 +01:00
|
|
|
geometry_type * shape_io::read_polylinez()
|
2007-03-16 11:11:37 +01:00
|
|
|
{
|
2009-07-08 01:56:01 +02:00
|
|
|
shape_file::record_type record(reclength_*2-36);
|
2007-09-16 13:23:51 +02:00
|
|
|
shp_.read_record(record);
|
|
|
|
int num_parts=record.read_ndr_integer();
|
|
|
|
int num_points=record.read_ndr_integer();
|
2010-11-03 14:19:15 +01:00
|
|
|
geometry_type * line = new geometry_type(mapnik::LineString);
|
2007-09-16 13:23:51 +02:00
|
|
|
line->set_capacity(num_points + num_parts);
|
|
|
|
if (num_parts == 1)
|
|
|
|
{
|
|
|
|
record.skip(4);
|
|
|
|
double x=record.read_double();
|
|
|
|
double y=record.read_double();
|
|
|
|
line->move_to(x,y);
|
|
|
|
for (int i=1;i<num_points;++i)
|
|
|
|
{
|
|
|
|
x=record.read_double();
|
|
|
|
y=record.read_double();
|
|
|
|
line->line_to(x,y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::vector<int> parts(num_parts);
|
|
|
|
for (int i=0;i<num_parts;++i)
|
|
|
|
{
|
|
|
|
parts[i]=record.read_ndr_integer();
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
int start,end;
|
|
|
|
for (int k=0;k<num_parts;++k)
|
|
|
|
{
|
|
|
|
start=parts[k];
|
|
|
|
if (k==num_parts-1)
|
|
|
|
end=num_points;
|
|
|
|
else
|
|
|
|
end=parts[k+1];
|
2010-09-02 22:20:42 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
double x=record.read_double();
|
|
|
|
double y=record.read_double();
|
|
|
|
line->move_to(x,y);
|
2010-09-02 22:20:42 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
for (int j=start+1;j<end;++j)
|
|
|
|
{
|
|
|
|
x=record.read_double();
|
|
|
|
y=record.read_double();
|
|
|
|
line->line_to(x,y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// z-range
|
|
|
|
//double z0=record.read_double();
|
|
|
|
//double z1=record.read_double();
|
|
|
|
//for (int i=0;i<num_points;++i)
|
|
|
|
// {
|
|
|
|
// double z=record.read_double();
|
|
|
|
// }
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
// m-range
|
|
|
|
//double m0=record.read_double();
|
|
|
|
//double m1=record.read_double();
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
//for (int i=0;i<num_points;++i)
|
|
|
|
//{
|
|
|
|
// double m=record.read_double();
|
|
|
|
//}
|
|
|
|
return line;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
2010-11-03 14:19:15 +01:00
|
|
|
geometry_type * shape_io::read_polygon()
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2009-07-08 01:56:01 +02:00
|
|
|
shape_file::record_type record(reclength_*2-36);
|
2007-09-16 13:23:51 +02:00
|
|
|
shp_.read_record(record);
|
|
|
|
int num_parts=record.read_ndr_integer();
|
|
|
|
int num_points=record.read_ndr_integer();
|
|
|
|
std::vector<int> parts(num_parts);
|
2010-11-03 14:19:15 +01:00
|
|
|
geometry_type * poly = new geometry_type(mapnik::Polygon);
|
2007-09-16 13:23:51 +02:00
|
|
|
poly->set_capacity(num_points + num_parts);
|
|
|
|
for (int i=0;i<num_parts;++i)
|
|
|
|
{
|
|
|
|
parts[i]=record.read_ndr_integer();
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
for (int k=0;k<num_parts;k++)
|
|
|
|
{
|
|
|
|
int start=parts[k];
|
|
|
|
int end;
|
|
|
|
if (k==num_parts-1)
|
|
|
|
{
|
|
|
|
end=num_points;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
end=parts[k+1];
|
|
|
|
}
|
|
|
|
double x=record.read_double();
|
|
|
|
double y=record.read_double();
|
|
|
|
poly->move_to(x,y);
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
for (int j=start+1;j<end;j++)
|
|
|
|
{
|
|
|
|
x=record.read_double();
|
|
|
|
y=record.read_double();
|
|
|
|
poly->line_to(x,y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return poly;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
2010-11-03 14:19:15 +01:00
|
|
|
geometry_type * shape_io::read_polygonm()
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2009-07-08 01:56:01 +02:00
|
|
|
shape_file::record_type record(reclength_*2-36);
|
2007-09-16 13:23:51 +02:00
|
|
|
shp_.read_record(record);
|
|
|
|
int num_parts=record.read_ndr_integer();
|
|
|
|
int num_points=record.read_ndr_integer();
|
|
|
|
std::vector<int> parts(num_parts);
|
2010-11-03 14:19:15 +01:00
|
|
|
geometry_type * poly = new geometry_type(mapnik::Polygon);
|
2007-09-16 13:23:51 +02:00
|
|
|
poly->set_capacity(num_points + num_parts);
|
|
|
|
for (int i=0;i<num_parts;++i)
|
|
|
|
{
|
|
|
|
parts[i]=record.read_ndr_integer();
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
for (int k=0;k<num_parts;k++)
|
|
|
|
{
|
|
|
|
int start=parts[k];
|
|
|
|
int end;
|
|
|
|
if (k==num_parts-1)
|
|
|
|
{
|
|
|
|
end=num_points;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
end=parts[k+1];
|
|
|
|
}
|
|
|
|
double x=record.read_double();
|
|
|
|
double y=record.read_double();
|
|
|
|
poly->move_to(x,y);
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
for (int j=start+1;j<end;j++)
|
|
|
|
{
|
|
|
|
x=record.read_double();
|
|
|
|
y=record.read_double();
|
|
|
|
poly->line_to(x,y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// m-range
|
|
|
|
//double m0=record.read_double();
|
|
|
|
//double m1=record.read_double();
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
//for (int i=0;i<num_points;++i)
|
|
|
|
//{
|
|
|
|
// double m=record.read_double();
|
|
|
|
//}
|
|
|
|
return poly;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
2010-11-03 14:19:15 +01:00
|
|
|
geometry_type * shape_io::read_polygonz()
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2009-07-08 01:56:01 +02:00
|
|
|
shape_file::record_type record(reclength_*2-36);
|
2007-09-16 13:23:51 +02:00
|
|
|
shp_.read_record(record);
|
|
|
|
int num_parts=record.read_ndr_integer();
|
|
|
|
int num_points=record.read_ndr_integer();
|
|
|
|
std::vector<int> parts(num_parts);
|
2010-11-03 14:19:15 +01:00
|
|
|
geometry_type * poly=new geometry_type(mapnik::Polygon);
|
2007-09-16 13:23:51 +02:00
|
|
|
poly->set_capacity(num_points + num_parts);
|
|
|
|
for (int i=0;i<num_parts;++i)
|
|
|
|
{
|
|
|
|
parts[i]=record.read_ndr_integer();
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
for (int k=0;k<num_parts;k++)
|
|
|
|
{
|
|
|
|
int start=parts[k];
|
|
|
|
int end;
|
|
|
|
if (k==num_parts-1)
|
|
|
|
{
|
|
|
|
end=num_points;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
end=parts[k+1];
|
|
|
|
}
|
|
|
|
double x=record.read_double();
|
|
|
|
double y=record.read_double();
|
|
|
|
poly->move_to(x,y);
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
for (int j=start+1;j<end;j++)
|
|
|
|
{
|
|
|
|
x=record.read_double();
|
|
|
|
y=record.read_double();
|
|
|
|
poly->line_to(x,y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// z-range
|
|
|
|
//double z0=record.read_double();
|
|
|
|
//double z1=record.read_double();
|
|
|
|
//for (int i=0;i<num_points;++i)
|
|
|
|
//{
|
|
|
|
// double z=record.read_double();
|
|
|
|
//}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
// m-range
|
|
|
|
//double m0=record.read_double();
|
|
|
|
//double m1=record.read_double();
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-09-16 13:23:51 +02:00
|
|
|
//for (int i=0;i<num_points;++i)
|
|
|
|
//{
|
|
|
|
// double m=record.read_double();
|
|
|
|
//}
|
|
|
|
return poly;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|