2006-03-31 12:32:02 +02:00
|
|
|
/*****************************************************************************
|
2011-11-14 04:37:50 +01:00
|
|
|
*
|
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
|
|
|
*
|
2011-10-22 15:27:28 +02:00
|
|
|
* Copyright (C) 2011 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
|
|
|
|
2005-06-16 22:56:31 +02:00
|
|
|
#include "shape_io.hpp"
|
2011-05-10 17:03:09 +02:00
|
|
|
|
2011-10-22 15:27:28 +02:00
|
|
|
// mapnik
|
2012-04-08 02:20:56 +02:00
|
|
|
#include <mapnik/debug.hpp>
|
2011-05-10 17:03:09 +02:00
|
|
|
#include <mapnik/datasource.hpp>
|
2011-05-17 01:41:34 +02:00
|
|
|
|
2011-05-10 17:03:09 +02:00
|
|
|
// boost
|
2011-05-17 01:41:34 +02:00
|
|
|
#include <boost/make_shared.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
|
|
|
|
2012-09-03 19:27:48 +02:00
|
|
|
shape_io::shape_io(std::string const& shape_name, bool open_index)
|
2011-11-14 04:37:50 +01:00
|
|
|
: type_(shape_null),
|
|
|
|
shp_(shape_name + SHP),
|
|
|
|
dbf_(shape_name + DBF),
|
|
|
|
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());
|
2011-10-22 15:27:28 +02:00
|
|
|
if (! ok)
|
2011-11-14 04:37:50 +01:00
|
|
|
{
|
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
|
|
|
}
|
2011-10-22 15:27:28 +02:00
|
|
|
|
2010-12-10 02:25:43 +01:00
|
|
|
if (open_index)
|
2010-09-06 21:20:59 +02:00
|
|
|
{
|
2011-11-14 04:37:50 +01:00
|
|
|
try
|
2010-11-15 04:16:23 +01:00
|
|
|
{
|
2011-05-17 01:41:34 +02:00
|
|
|
index_= boost::make_shared<shape_file>(shape_name + INDEX);
|
2010-12-10 02:25:43 +01:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_WARN(shape) << "shape_io: Could not open index=" << shape_name << INDEX;
|
2010-11-15 04:16:23 +01:00
|
|
|
}
|
2010-09-06 21:20:59 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
2011-04-06 15:02:31 +02:00
|
|
|
shape_io::~shape_io() {}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2012-12-11 10:37:53 +01:00
|
|
|
void shape_io::move_to(std::streampos pos)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2011-10-22 15:27:28 +02:00
|
|
|
shp_.seek(pos);
|
|
|
|
id_ = shp_.read_xdr_integer();
|
|
|
|
reclength_ = shp_.read_xdr_integer();
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
shape_file& shape_io::shp()
|
|
|
|
{
|
2011-10-22 15:27:28 +02:00
|
|
|
return shp_;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dbf_file& shape_io::dbf()
|
|
|
|
{
|
2011-10-22 15:27:28 +02:00
|
|
|
return dbf_;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
2012-12-11 10:37:53 +01:00
|
|
|
void shape_io::read_bbox(shape_file::record_type & record, mapnik::box2d<double> & bbox)
|
2011-11-14 04:37:50 +01:00
|
|
|
{
|
2012-12-11 10:37:53 +01:00
|
|
|
double lox = record.read_double();
|
|
|
|
double loy = record.read_double();
|
|
|
|
double hix = record.read_double();
|
|
|
|
double hiy = record.read_double();
|
|
|
|
bbox.init(lox, loy, hix, hiy);
|
|
|
|
}
|
2012-03-13 15:59:22 +01:00
|
|
|
|
2012-12-11 10:37:53 +01:00
|
|
|
void shape_io::read_polyline( shape_file::record_type & record, mapnik::geometry_container & geom)
|
|
|
|
{
|
2011-11-14 04:37:50 +01:00
|
|
|
int num_parts = record.read_ndr_integer();
|
|
|
|
int num_points = record.read_ndr_integer();
|
|
|
|
if (num_parts == 1)
|
|
|
|
{
|
2012-07-26 12:57:41 +02:00
|
|
|
std::auto_ptr<geometry_type> line(new geometry_type(mapnik::LineString));
|
2011-11-14 04:37:50 +01:00
|
|
|
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);
|
|
|
|
}
|
2011-12-09 13:25:13 +01:00
|
|
|
geom.push_back(line);
|
2011-11-14 04:37:50 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::vector<int> parts(num_parts);
|
|
|
|
for (int i = 0; i < num_parts; ++i)
|
|
|
|
{
|
|
|
|
parts[i] = record.read_ndr_integer();
|
|
|
|
}
|
|
|
|
|
|
|
|
int start, end;
|
|
|
|
for (int k = 0; k < num_parts; ++k)
|
|
|
|
{
|
2012-07-26 12:57:41 +02:00
|
|
|
std::auto_ptr<geometry_type> line(new geometry_type(mapnik::LineString));
|
2011-10-22 15:27:28 +02:00
|
|
|
start = parts[k];
|
|
|
|
if (k == num_parts - 1)
|
|
|
|
{
|
|
|
|
end = num_points;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
end = parts[k + 1];
|
|
|
|
}
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2011-10-22 15:27:28 +02:00
|
|
|
double x = record.read_double();
|
|
|
|
double y = record.read_double();
|
|
|
|
line->move_to(x, y);
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2011-10-22 15:27:28 +02:00
|
|
|
for (int j = start + 1; j < end; ++j)
|
|
|
|
{
|
|
|
|
x = record.read_double();
|
|
|
|
y = record.read_double();
|
|
|
|
line->line_to(x, y);
|
|
|
|
}
|
2011-12-09 13:25:13 +01:00
|
|
|
geom.push_back(line);
|
2011-10-22 15:27:28 +02:00
|
|
|
}
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
2012-12-11 10:37:53 +01:00
|
|
|
void shape_io::read_polygon(shape_file::record_type & record, mapnik::geometry_container & geom)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2011-10-22 15:27:28 +02:00
|
|
|
int num_parts = record.read_ndr_integer();
|
|
|
|
int num_points = record.read_ndr_integer();
|
|
|
|
std::vector<int> parts(num_parts);
|
2012-03-13 15:59:22 +01:00
|
|
|
|
2011-10-22 15:27:28 +02:00
|
|
|
for (int i = 0; i < num_parts; ++i)
|
|
|
|
{
|
|
|
|
parts[i] = record.read_ndr_integer();
|
|
|
|
}
|
|
|
|
|
2012-07-26 12:58:48 +02:00
|
|
|
for (int k = 0; k < num_parts; ++k)
|
2011-10-22 15:27:28 +02:00
|
|
|
{
|
2012-07-26 12:57:41 +02:00
|
|
|
std::auto_ptr<geometry_type> poly(new geometry_type(mapnik::Polygon));
|
2011-10-22 15:27:28 +02:00
|
|
|
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);
|
2013-03-01 17:06:44 +01:00
|
|
|
for (int j=start+1;j<end;j++)
|
2011-10-22 15:27:28 +02:00
|
|
|
{
|
|
|
|
x = record.read_double();
|
|
|
|
y = record.read_double();
|
|
|
|
poly->line_to(x, y);
|
|
|
|
}
|
2013-03-01 17:06:44 +01:00
|
|
|
poly->close_path();
|
2012-02-27 07:11:22 +01:00
|
|
|
geom.push_back(poly);
|
2011-10-22 15:27:28 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|