Fast map matching  0.1.0
geometry.hpp
1 
10 #ifndef FMM_GEOMTYPES_HPP
11 #define FMM_GEOMTYPES_HPP
12 
13 #include <ogrsf_frmts.h> // C++ API for GDAL
14 #include <boost/geometry.hpp>
15 #include <string>
16 #include <sstream>
17 
18 namespace FMM {
22 namespace CORE{
23 
27 typedef boost::geometry::model::point<double, 2,
28  boost::geometry::cs::cartesian> Point; // Point for rtree box
34 class LineString {
35 public:
40  typedef boost::geometry::model::linestring<Point> linestring_t;
46  inline double get_x(int i) const{
47  return boost::geometry::get<0>(line.at(i));
48  };
55  inline double get_y(int i) const{
56  return boost::geometry::get<1>(line.at(i));
57  };
63  inline void set_x(int i, double v){
64  boost::geometry::set<0>(line.at(i),v);
65  };
71  inline void set_y(int i, double v){
72  boost::geometry::set<1>(line.at(i),v);
73  };
79  inline void add_point(double x,double y){
80  boost::geometry::append(line, Point(x,y));
81  };
86  inline void add_point(const Point& point){
87  boost::geometry::append(line, point);
88  };
98  inline Point get_point(int i) const{
99  return Point(boost::geometry::get<0>(
100  line.at(i)),boost::geometry::get<1>(line.at(i)));
101  };
108  inline const Point &at(int i) const{
109  return line.at(i);
110  }
115  inline int get_num_points() const{
116  return boost::geometry::num_points(line);
117  };
122  inline bool is_empty(){
123  return boost::geometry::num_points(line)==0;
124  };
128  inline void clear(){
129  boost::geometry::clear(line);
130  };
135  inline double get_length() const {
136  return boost::geometry::length(line);
137  };
144  inline std::string export_wkt() const{
145  std::ostringstream ss;
146  ss << boost::geometry::wkt(line);
147  return ss.str();
148  };
153  inline std::string export_json() const{
154  std::ostringstream ss;
155  int N = get_num_points();
156  if (N>0){
157  ss << "{\"type\":\"LineString\",\"coordinates\": [";
158  for (int i=0;i<N;++i){
159  ss << "[" << get_x(i) << "," << get_y(i) <<"]"
160  << (i==N-1 ? "": ",");
161  }
162  ss << "]}";
163  }
164  return ss.str();
165  };
170  inline const linestring_t &get_geometry_const() const{
171  return line;
172  };
178  return line;
179  };
180 
190  inline bool operator==(const LineString& rhs) const {
191  int N = get_num_points();
192  if (rhs.get_num_points()!=N)
193  return false;
194  bool result = true;
195  for (int i=0;i<N;++i){
196  if (boost::geometry::distance(get_point(i),rhs.get_point(i))>1e-6)
197  result = false;
198  }
199  return result;
200  };
207  friend std::ostream& operator<<(std::ostream& os, const LineString& rhs);
208 private:
209  linestring_t line;
210 }; // LineString
211 
212 std::ostream& operator<<(std::ostream& os,const FMM::CORE::LineString& rhs);
213 
219 LineString ogr2linestring(const OGRLineString *line);
220 
234 LineString ogr2linestring(const OGRMultiLineString *mline);
235 
241 LineString wkt2linestring(const std::string &wkt);
242 
249 OGRLineString *linestring2ogr(const LineString &line);
250 
257 OGRPoint *point2ogr(const Point &p);
258 
259 }; // CORE
260 
261 }; // FMM
262 
263 
264 #endif // FMM_GEOMTYPES_HPP
FMM::CORE::LineString::add_point
void add_point(double x, double y)
Add a point to the end of the current line.
Definition: geometry.hpp:79
FMM::CORE::ogr2linestring
LineString ogr2linestring(const OGRLineString *line)
Convert a OGRLineString to a linestring.
Definition: geometry.cpp:19
FMM::CORE::LineString::export_wkt
std::string export_wkt() const
Export a string containing WKT representation of the line.
Definition: geometry.hpp:144
FMM::CORE::LineString::operator==
bool operator==(const LineString &rhs) const
Compare if two linestring are the same.
Definition: geometry.hpp:190
FMM::CORE::LineString::get_geometry_const
const linestring_t & get_geometry_const() const
Get a const reference to the inner boost geometry linestring.
Definition: geometry.hpp:170
FMM::CORE::operator<<
std::ostream & operator<<(std::ostream &os, const FMM::CORE::LineString &rhs)
Definition: geometry.cpp:13
FMM::CORE::LineString::linestring_t
boost::geometry::model::linestring< Point > linestring_t
This is the boost geometry linestring class, stored inside the LineString class.
Definition: geometry.hpp:40
FMM::CORE::linestring2ogr
OGRLineString * linestring2ogr(const LineString &line)
Convert a linestring into a OGRLineString.
Definition: geometry.cpp:48
FMM
Fast map matching.
Definition: geom_algorithm.hpp:17
FMM::CORE::LineString::set_y
void set_y(int i, double v)
Set y coordinate of i-th point in the line.
Definition: geometry.hpp:71
FMM::CORE::LineString::operator<<
friend std::ostream & operator<<(std::ostream &os, const LineString &rhs)
Overwrite the operator of << of linestring.
FMM::CORE::LineString::get_point
Point get_point(int i) const
Get the i-th point in the line.
Definition: geometry.hpp:98
FMM::CORE::LineString::clear
void clear()
Remove all points in the current line.
Definition: geometry.hpp:128
FMM::CORE::LineString::add_point
void add_point(const Point &point)
Add a point to the end of the current line.
Definition: geometry.hpp:86
FMM::CORE::LineString::set_x
void set_x(int i, double v)
Set x coordinate of i-th point in the line.
Definition: geometry.hpp:63
FMM::CORE::LineString::export_json
std::string export_json() const
Export a string containing GeoJSON representation of the line.
Definition: geometry.hpp:153
FMM::CORE::LineString
Linestring geometry class.
Definition: geometry.hpp:34
FMM::CORE::LineString::is_empty
bool is_empty()
Check if the line is empty or not.
Definition: geometry.hpp:122
FMM::CORE::LineString::get_length
double get_length() const
Get the length of the line.
Definition: geometry.hpp:135
FMM::CORE::point2ogr
OGRPoint * point2ogr(const Point &p)
Convert a point into a OGRPoint.
Definition: geometry.cpp:56
FMM::CORE::wkt2linestring
LineString wkt2linestring(const std::string &wkt)
Convert a wkt into a linestring.
Definition: geometry.cpp:42
FMM::CORE::LineString::at
const Point & at(int i) const
Get a constance reference of the i-th point in the line.
Definition: geometry.hpp:108
FMM::CORE::LineString::get_num_points
int get_num_points() const
Get the number of points in a line.
Definition: geometry.hpp:115
FMM::CORE::Point
boost::geometry::model::point< double, 2, boost::geometry::cs::cartesian > Point
Point class.
Definition: geometry.hpp:28
FMM::CORE::LineString::get_y
double get_y(int i) const
Get the y coordinate of i-th point in the line.
Definition: geometry.hpp:55
FMM::CORE::LineString::get_x
double get_x(int i) const
Get the x coordinate of i-th point in the line.
Definition: geometry.hpp:46
FMM::CORE::LineString::get_geometry
linestring_t & get_geometry()
Get a reference to the inner boost geometry linestring.
Definition: geometry.hpp:177