1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
/**********************************************************************
* $Id: CoordinateArraySequence.cpp 1978 2007-04-22 04:44:14Z mloskot $
*
* GEOS - Geometry Engine Open Source
* http://geos.refractions.net
*
* Copyright (C) 2001-2002 Vivid Solutions Inc.
* Copyright (C) 2005 Refractions Research Inc.
*
* This is free software; you can redistribute and/or modify it under
* the terms of the GNU Lesser General Public Licence as published
* by the Free Software Foundation.
* See the COPYING file for more information.
*
**********************************************************************/
#include <geos/geom/Envelope.h>
#include <geos/geom/CoordinateArraySequence.h>
#include <geos/geom/Coordinate.h>
#include <geos/geom/CoordinateFilter.h>
#include <cassert>
#include <algorithm>
#include <vector>
using namespace std;
namespace geos {
namespace geom { // geos::geom
CoordinateArraySequence::CoordinateArraySequence():
vect(new vector<Coordinate>())
{
}
CoordinateArraySequence::CoordinateArraySequence(size_t n):
vect(new vector<Coordinate>(n))
{
}
CoordinateArraySequence::CoordinateArraySequence(
vector<Coordinate> *coords): vect(coords)
{
if ( ! vect ) vect = new vector<Coordinate>();
}
CoordinateArraySequence::CoordinateArraySequence(
const CoordinateArraySequence &c)
:
CoordinateSequence(c),
vect(new vector<Coordinate>(*(c.vect)))
{
}
CoordinateSequence *
CoordinateArraySequence::clone() const
{
return new CoordinateArraySequence(*this);
}
void
CoordinateArraySequence::setPoints(const vector<Coordinate> &v)
{
vect->assign(v.begin(), v.end());
}
const vector<Coordinate>*
CoordinateArraySequence::toVector() const
{
return vect; //new vector<Coordinate>(vect->begin(),vect->end());
}
bool
CoordinateArraySequence::isEmpty() const
{
return vect->empty();
}
void
CoordinateArraySequence::add(const Coordinate& c)
{
vect->push_back(c);
}
void
CoordinateArraySequence::add(const Coordinate& c, bool allowRepeated)
{
if (!allowRepeated && ! vect->empty() )
{
const Coordinate& last=vect->back();
if (last.equals2D(c)) return;
}
vect->push_back(c);
}
size_t
CoordinateArraySequence::getSize() const
{
return vect->size();
}
const Coordinate &
CoordinateArraySequence::getAt(size_t pos) const
{
assert(pos<vect->size());
return (*vect)[pos];
}
void
CoordinateArraySequence::getAt(size_t pos, Coordinate &c) const
{
assert(pos<vect->size());
c=(*vect)[pos];
}
void
CoordinateArraySequence::setAt(const Coordinate& c, size_t pos)
{
assert(pos<vect->size());
(*vect)[pos]=c;
}
void
CoordinateArraySequence::deleteAt(size_t pos)
{
assert(pos<vect->size());
vect->erase(vect->begin()+pos);
}
string
CoordinateArraySequence::toString() const
{
string result("(");
if (getSize()>0) {
//char buffer[100];
for (size_t i=0, n=vect->size(); i<n; i++)
{
Coordinate& c=(*vect)[i];
if ( i ) result.append(", ");
result.append(c.toString());
}
}
result.append(")");
return result;
}
CoordinateArraySequence::~CoordinateArraySequence()
{
delete vect;
}
void
CoordinateArraySequence::expandEnvelope(Envelope &env) const
{
size_t size = vect->size();
for (size_t i=0; i<size; i++) env.expandToInclude((*vect)[i]);
}
double
CoordinateArraySequence::getOrdinate(size_t index, size_t ordinateIndex) const
{
assert(index<vect->size());
switch (ordinateIndex)
{
case CoordinateSequence::X:
return (*vect)[index].x;
case CoordinateSequence::Y:
return (*vect)[index].y;
case CoordinateSequence::Z:
return (*vect)[index].z;
default:
return DoubleNotANumber;
}
}
void
CoordinateArraySequence::setOrdinate(size_t index, size_t ordinateIndex,
double value)
{
assert(index<vect->size());
assert(ordinateIndex == CoordinateSequence::X
|| ordinateIndex == CoordinateSequence::Y
|| ordinateIndex == CoordinateSequence::Z);
switch (ordinateIndex)
{
case CoordinateSequence::X:
(*vect)[index].x = value;
break;
case CoordinateSequence::Y:
(*vect)[index].y = value;
break;
case CoordinateSequence::Z:
(*vect)[index].z = value;
break;
default:
break;
}
}
void
CoordinateArraySequence::apply_rw(const CoordinateFilter *filter)
{
for (vector<Coordinate>::iterator i=vect->begin(), e=vect->end(); i!=e; ++i)
{
filter->filter_rw(&(*i));
}
}
void
CoordinateArraySequence::apply_ro(CoordinateFilter *filter) const
{
for (vector<Coordinate>::const_iterator i=vect->begin(), e=vect->end(); i!=e; ++i)
{
filter->filter_ro(&(*i));
}
}
CoordinateSequence&
CoordinateArraySequence::removeRepeatedPoints()
{
// We use == operator, which is 2D only
vector<Coordinate>::iterator new_end = \
std::unique(vect->begin(), vect->end());
vect->erase(new_end, vect->end());
return *this;
}
} // namespace geos::geom
} //namespace geos
/**********************************************************************
* $Log$
* Revision 1.10 2006/06/12 15:07:47 strk
* explicitly invoked CoordinateSequence (copy) ctor - suggested by GCC warning.
*
* Revision 1.9 2006/06/12 10:10:39 strk
* Fixed getGeometryN() to take size_t rather then int, changed unsigned int parameters to size_t.
*
* Revision 1.8 2006/05/03 08:58:34 strk
* added new non-static CoordinateSequence::removeRepeatedPoints() mutator.
*
* Revision 1.7 2006/03/27 09:00:50 strk
* Bug #79 - Small fix in CoordinateArraySequence::toString()
*
* Revision 1.6 2006/03/22 16:58:34 strk
* Removed (almost) all inclusions of geom.h.
* Removed obsoleted .cpp files.
* Fixed a bug in WKTReader not using the provided CoordinateSequence
* implementation, optimized out some memory allocations.
*
**********************************************************************/
|