Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature partial match #201

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions example/partial_match/fmm_config_csv.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<config>
<input>
<ubodt>
<file>../ubodt.txt</file>
</ubodt>
<network>
<file>../data/edges.shp</file>
<id>id</id>
</network>
<gps>
<file>trips_outlier.csv</file>
<id>id</id>
</gps>
</input>
<parameters>
<k>4</k>
<r>0.4</r>
<gps_error>0.5</gps_error>
</parameters>
<output>
<fields>
<opath/>
<cpath/>
<tpath/>
<mgeom/>
<ep/>
<tp/>
</fields>
<file>mr_outlier.txt</file>
</output>
<other>
<log_level>0</log_level>
</other>
</config>
5 changes: 5 additions & 0 deletions example/partial_match/mr_outlier.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
id;opath;cpath;tpath;mgeom;ep;tp
1;2,2,13,-1,14,23;2,5,13,-1,14,23;2|2,5,13|13,-1|-1,14|14,23;;0.792391,0.788102,0.896001,0,0.975345,0.966437;0.999961,0.756067,0,0,0.895804
2;2,2,13,-1,29,14,23;2,5,13,-1,29,-1,14,23;2|2,5,13|13,-1|-1,29|29,-1,14|14,23;;0.792391,0.788102,0.896001,0,0.923116,0.975345,0.966437;0.999961,0.756067,0,0,0,0.895804
3;2,2,13,30,29,14,23;2,5,13,-1,30,29,-1,14,23;2|2,5,13|13,-1,30|30,29|29,-1,14|14,23;;0.792391,0.788102,0.896001,0.980199,0.923116,0.975345,0.966437;0.999961,0.756067,0,0.948683,0,0.895804
4;-1,2,2,13,14,23;-1,2,5,13,14,23;-1,2|2|2,5,13|13,14|14,23;;0,0.792391,0.788102,0.896001,0.975345,0.966437;0,0.999961,0.756067,0.993327,0.895804
9 changes: 9 additions & 0 deletions example/partial_match/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### Partial match a trajectory

If a trajectory contains multiple parts that cannot be matched as a whole, parts of it
will be matched separately to a network.

The partial match is turned on with the option `--partial-match`, the output differs in the aspects
- mgeom: it will be a multiple linestring as opposed to single linestring
- opath:
- cpath:
5 changes: 5 additions & 0 deletions example/partial_match/trips_outlier.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
id;geom
1;LINESTRING(1.65889830508474 0.25098870056497,1.65494350282486 0.701836158192091,2.4933615819209 1.76567796610169,5 5,3.54929378531073 1.88827683615819,4.13064971751412 2.45776836158192)
2;LINESTRING(1.65889830508474 0.25098870056497,1.65494350282486 0.701836158192091,2.4933615819209 1.76567796610169,5 5,3.7 3.8,3.54929378531073 1.88827683615819,4.13064971751412 2.45776836158192)
3;LINESTRING(1.65889830508474 0.25098870056497,1.65494350282486 0.701836158192091,2.4933615819209 1.76567796610169,3.4 3.9,3.7 3.8,3.54929378531073 1.88827683615819,4.13064971751412 2.45776836158192)
4;LINESTRING(5 5,1.65889830508474 0.25098870056497,1.65494350282486 0.701836158192091,2.4933615819209 1.76567796610169,3.54929378531073 1.88827683615819,4.13064971751412 2.45776836158192)
6 changes: 6 additions & 0 deletions src/core/geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ std::ostream& FMM::CORE::operator<<(std::ostream& os,
return os;
};

std::ostream& FMM::CORE::operator<<(std::ostream& os,
const FMM::CORE::MultiLineString& rhs){
os<< std::setprecision(12) << boost::geometry::wkt(rhs.mline);
return os;
};

FMM::CORE::LineString FMM::CORE::ogr2linestring(const OGRLineString *line){
int binary_size = line->WkbSize();
std::vector<unsigned char> wkb(binary_size);
Expand Down
61 changes: 46 additions & 15 deletions src/core/geometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ namespace FMM {
/**
* Core data types
*/
namespace CORE{
namespace CORE {

/**
* Point class
*/
typedef boost::geometry::model::point<double, 2,
boost::geometry::cs::cartesian> Point; // Point for rtree box
boost::geometry::cs::cartesian> Point;

/**
* Linestring geometry class
*
Expand All @@ -43,7 +44,7 @@ class LineString {
* @param i point index
* @return x coordinate
*/
inline double get_x(int i) const{
inline double get_x(int i) const {
return boost::geometry::get<0>(line.at(i));
};
/**
Expand All @@ -52,7 +53,7 @@ class LineString {
* number of points in the line
* @return y coordinate
*/
inline double get_y(int i) const{
inline double get_y(int i) const {
return boost::geometry::get<1>(line.at(i));
};
/**
Expand Down Expand Up @@ -95,24 +96,24 @@ class LineString {
* Manipulating the returned point will not change the
* original line.
*/
inline Point get_point(int i) const{
inline Point get_point(int i) const {
return Point(boost::geometry::get<0>(
line.at(i)),boost::geometry::get<1>(line.at(i)));
line.at(i)),boost::geometry::get<1>(line.at(i)));
};
/**
* Get a constance reference of the i-th point in the line
* @param i point index
* @return A constant reference to the ith point of line, which
* avoids create a new point.
*/
inline const Point &at(int i) const{
inline const Point &at(int i) const {
return line.at(i);
}
/**
* Get the number of points in a line
* @return the point number
*/
inline int get_num_points() const{
inline int get_num_points() const {
return boost::geometry::num_points(line);
};
/**
Expand Down Expand Up @@ -141,7 +142,7 @@ class LineString {
*
* Example: LINESTRING (30 10, 10 30, 40 40)
*/
inline std::string export_wkt() const{
inline std::string export_wkt() const {
std::ostringstream ss;
ss << boost::geometry::wkt(line);
return ss.str();
Expand All @@ -150,14 +151,14 @@ class LineString {
* Export a string containing GeoJSON representation of the line.
* @return The GeoJSON of the line
*/
inline std::string export_json() const{
inline std::string export_json() const {
std::ostringstream ss;
int N = get_num_points();
if (N>0){
if (N>0) {
ss << "{\"type\":\"LineString\",\"coordinates\": [";
for (int i=0;i<N;++i){
for (int i=0; i<N; ++i) {
ss << "[" << get_x(i) << "," << get_y(i) <<"]"
<< (i==N-1 ? "": ",");
<< (i==N-1 ? "" : ",");
}
ss << "]}";
}
Expand All @@ -167,7 +168,7 @@ class LineString {
* Get a const reference to the inner boost geometry linestring
* @return const reference to the inner boost geometry linestring
*/
inline const linestring_t &get_geometry_const() const{
inline const linestring_t &get_geometry_const() const {
return line;
};
/**
Expand All @@ -192,7 +193,7 @@ class LineString {
if (rhs.get_num_points()!=N)
return false;
bool result = true;
for (int i=0;i<N;++i){
for (int i=0; i<N; ++i) {
if (boost::geometry::distance(get_point(i),rhs.get_point(i))>1e-6)
result = false;
}
Expand All @@ -211,6 +212,36 @@ class LineString {

std::ostream& operator<<(std::ostream& os,const FMM::CORE::LineString& rhs);

class MultiLineString {
public:
typedef boost::geometry::model::multi_linestring
<LineString::linestring_t> mlinestring_t;

MultiLineString(){
};
MultiLineString(const std::vector<LineString> &lines){
mline.resize(lines.size());
for (int i=0; i<lines.size(); ++i) {
int J = lines[i].getNumPoints();
for (int j=0; j<J; ++j) {
bg::append(mline[i],lines[i].getPoint(j));
}
}
};
inline bool isEmpty() const {
return bg::num_points(mline)==0;
};
inline bg::wkt_manipulator<mlinestring_t> exportToWkt() const {
return bg::wkt(mline);
};
friend std::ostream& operator<<(std::ostream& os,const MultiLineString& rhs);
private:
mlinestring_t mline;
};

std::ostream& operator<<(std::ostream& os,
const FMM::CORE::MultiLineString& rhs);

/**
* Convert a OGRLineString to a linestring
* @param line a pointer to OGRLineString
Expand Down
Loading