1 /**********************************************************************
2  *
3  * GEOS - Geometry Engine Open Source
4  * http://geos.osgeo.org
5  *
6  * Copyright (C) 2011 Sandro Santilli <strk@kbt.io>
7  * Copyright (C) 2001-2002 Vivid Solutions Inc.
8  * Copyright (C) 2005 2006 Refractions Research Inc.
9  *
10  * This is free software; you can redistribute and/or modify it under
11  * the terms of the GNU Lesser General Public Licence as published
12  * by the Free Software Foundation.
13  * See the COPYING file for more information.
14  *
15  **********************************************************************
16  *
17  * Last port: geom/LineString.java r320 (JTS-1.12)
18  *
19  **********************************************************************/
20 
21 #ifndef GEOS_GEOS_LINESTRING_H
22 #define GEOS_GEOS_LINESTRING_H
23 
24 #include <geos/export.h>
25 #include <geos/geom/Geometry.h> // for inheritance
26 #include <geos/geom/CoordinateSequence.h> // for proper use of unique_ptr<>
27 #include <geos/geom/Envelope.h> // for proper use of unique_ptr<>
28 #include <geos/geom/Dimension.h> // for Dimension::DimensionType
29 
30 #include <string>
31 #include <vector>
32 #include <memory> // for unique_ptr
33 
34 #include <geos/inline.h>
35 
36 #ifdef _MSC_VER
37 #pragma warning(push)
38 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
39 #endif
40 
41 namespace geos {
42 namespace geom {
43 class Coordinate;
44 class CoordinateArraySequence;
45 class CoordinateSequenceFilter;
46 }
47 }
48 
49 namespace geos {
50 namespace geom { // geos::geom
51 
52 /**
53  *  Models an OGC-style <code>LineString</code>.
54  *
55  *  A LineString consists of a sequence of two or more vertices,
56  *  along with all points along the linearly-interpolated curves
57  *  (line segments) between each
58  *  pair of consecutive vertices.
59  *  Consecutive vertices may be equal.
60  *  The line segments in the line may intersect each other (in other words,
61  *  the linestring may "curl back" in itself and self-intersect).
62  *  Linestrings with exactly two identical points are invalid.
63  *
64  *  A linestring must have either 0 or 2 or more points.
65  *  If these conditions are not met, the constructors throw
66  *  an {@link util::IllegalArgumentException}.
67  */
68 class GEOS_DLL LineString: public Geometry {
69 
70 public:
71 
72     friend class GeometryFactory;
73 
74     /// A vector of const LineString pointers
75     typedef std::vector<const LineString*> ConstVect;
76 
77     ~LineString() override;
78 
79     /**
80      * \brief
81      * Creates and returns a full copy of this {@link LineString} object
82      * (including all coordinates contained by it)
83      *
84      * @return A clone of this instance
85      */
86     std::unique_ptr<Geometry> clone() const override;
87 
88     std::unique_ptr<CoordinateSequence> getCoordinates() const override;
89 
90     /// Returns a read-only pointer to internal CoordinateSequence
91     const CoordinateSequence* getCoordinatesRO() const;
92 
93     virtual const Coordinate& getCoordinateN(size_t n) const;
94 
95     /// Returns line dimension (1)
96     Dimension::DimensionType getDimension() const override;
97 
98     /**
99      * \brief
100      * Returns Dimension::False for a closed LineString,
101      * 0 otherwise (LineString boundary is a MultiPoint)
102      */
103     int getBoundaryDimension() const override;
104 
105     /// Returns coordinate dimension.
106     uint8_t getCoordinateDimension() const override;
107 
108     /**
109      * \brief
110      * Returns a MultiPoint.
111      * Empty for closed LineString, a Point for each vertex otherwise.
112      */
113     std::unique_ptr<Geometry> getBoundary() const override;
114 
115     bool isEmpty() const override;
116 
117     std::size_t getNumPoints() const override;
118 
119     virtual std::unique_ptr<Point> getPointN(std::size_t n) const;
120 
121     /// \brief
122     /// Return the start point of the LineString
123     /// or NULL if this is an EMPTY LineString.
124     ///
125     virtual std::unique_ptr<Point> getStartPoint() const;
126 
127     /// \brief
128     /// Return the end point of the LineString
129     /// or NULL if this is an EMPTY LineString.
130     ///
131     virtual std::unique_ptr<Point> getEndPoint() const;
132 
133     virtual bool isClosed() const;
134 
135     virtual bool isRing() const;
136 
137     std::string getGeometryType() const override;
138 
139     GeometryTypeId getGeometryTypeId() const override;
140 
141     virtual bool isCoordinate(Coordinate& pt) const;
142 
143     bool equalsExact(const Geometry* other, double tolerance = 0)
144     const override;
145 
146     void apply_rw(const CoordinateFilter* filter) override;
147 
148     void apply_ro(CoordinateFilter* filter) const override;
149 
150     void apply_rw(GeometryFilter* filter) override;
151 
152     void apply_ro(GeometryFilter* filter) const override;
153 
154     void apply_rw(GeometryComponentFilter* filter) override;
155 
156     void apply_ro(GeometryComponentFilter* filter) const override;
157 
158     void apply_rw(CoordinateSequenceFilter& filter) override;
159 
160     void apply_ro(CoordinateSequenceFilter& filter) const override;
161 
162     /** \brief
163      * Normalizes a LineString.
164      *
165      * A normalized linestring
166      * has the first point which is not equal to its reflected point
167      * less than the reflected point.
168      */
169     void normalize() override;
170 
171     //was protected
172     int compareToSameClass(const Geometry* ls) const override;
173 
174     const Coordinate* getCoordinate() const override;
175 
176     double getLength() const override;
177 
178     /**
179      * Creates a LineString whose coordinates are in the reverse
180      * order of this object's
181      *
182      * @return a LineString with coordinates in the reverse order
183      */
184     std::unique_ptr<Geometry> reverse() const override;
185 
186 protected:
187 
188     LineString(const LineString& ls);
189 
190     /// \brief
191     /// Constructs a LineString taking ownership the
192     /// given CoordinateSequence.
193     LineString(CoordinateSequence* pts, const GeometryFactory* newFactory);
194 
195     /// Hopefully cleaner version of the above
196     LineString(CoordinateSequence::Ptr && pts,
197                const GeometryFactory& newFactory);
198 
199     Envelope::Ptr computeEnvelopeInternal() const override;
200 
201     CoordinateSequence::Ptr points;
202 
203     int
getSortIndex()204     getSortIndex() const override
205     {
206         return SORTINDEX_LINESTRING;
207     };
208 
209 private:
210 
211     void validateConstruction();
212     void normalizeClosed();
213 
214 
215 };
216 
217 struct GEOS_DLL  LineStringLT {
218     bool
operatorLineStringLT219     operator()(const LineString* ls1, const LineString* ls2) const
220     {
221         return ls1->compareTo(ls2) < 0;
222     }
223 };
224 
225 
226 inline std::unique_ptr<Geometry>
clone()227 LineString::clone() const
228 {
229     return std::unique_ptr<Geometry>(new LineString(*this));
230 }
231 
232 } // namespace geos::geom
233 } // namespace geos
234 
235 #ifdef _MSC_VER
236 #pragma warning(pop)
237 #endif
238 
239 #endif // ndef GEOS_GEOS_LINESTRING_H
240