Skip to content

Commit

Permalink
Add matching functionality for stmatch
Browse files Browse the repository at this point in the history
  • Loading branch information
cyang-kth committed Jul 20, 2020
1 parent 982834e commit 37c37ee
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 30 deletions.
2 changes: 1 addition & 1 deletion example/notebook/fmm_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Run map matching on a file"
"### Run map matching on a GPS file"
]
},
{
Expand Down
159 changes: 141 additions & 18 deletions example/notebook/stmatch_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -25,7 +25,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand All @@ -43,48 +43,55 @@
"model = STMATCH(network,graph)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Define STMATCH configuration"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"wkt = \"LINESTRING(0.200812146892656 2.14088983050848,1.44262005649717 2.14879943502825,3.06408898305084 2.16066384180791,3.06408898305084 2.7103813559322,3.70872175141242 2.97930790960452,4.11606638418078 2.62337570621469)\""
"k = 4\n",
"gps_error = 0.5\n",
"radius = 0.4\n",
"vmax = 30\n",
"factor = 1.5\n",
"stmatch_config = STMATCHConfig(k, radius, gps_error, vmax, factor)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Define STMATCH configuration"
"### Match trajectory in wkt format"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"k = 4\n",
"gps_error = 0.5\n",
"radius = 0.4\n",
"vmax = 30\n",
"factor = 1.5\n",
"config = STMATCHConfig(k, radius, gps_error, vmax, factor)"
"wkt = \"LINESTRING(0.200812146892656 2.14088983050848,1.44262005649717 2.14879943502825,3.06408898305084 2.16066384180791,3.06408898305084 2.7103813559322,3.70872175141242 2.97930790960452,4.11606638418078 2.62337570621469)\""
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"result = model.match_wkt(wkt,config)"
"result = model.match_wkt(wkt,stmatch_config)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 11,
"metadata": {},
"outputs": [
{
Expand All @@ -94,8 +101,8 @@
"Matched path: [8, 11, 13, 18, 20, 24]\n",
"Matched edge for each point: [8, 11, 18, 18, 20, 24]\n",
"Matched edge index [0, 1, 3, 3, 4, 5]\n",
"Matched geometry: LINESTRING(0.200812 2,1 2,2 2,3 2,3 3,4 3,4 2.62338)\n",
"Matched point LINESTRING(0.200812 2,1.44262 2,3 2.16066,3 2.71038,3.70872 3,4 2.62338)\n"
"Matched geometry: LINESTRING(0.20081215 2,1 2,2 2,3 2,3 3,4 3,4 2.6233757)\n",
"Matched point LINESTRING(0.20081215 2,1.4426201 2,3 2.1606638,3 2.7103814,3.7087218 3,4 2.6233757)\n"
]
}
],
Expand All @@ -107,6 +114,109 @@
"print \"Matched point \", result.pgeom.export_wkt()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Match GPS data in a file"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"from fmm import GPSConfig,ResultConfig"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"# Define input data configuration\n",
"input_config = GPSConfig()\n",
"input_config.file = \"../data/trips.csv\"\n",
"input_config.id = \"id\""
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"gps file : ../data/trips.csv\n",
"id column : id\n",
"geom column : geom\n",
"timestamp column : timestamp\n",
"x column : x\n",
"y column : y\n",
"GPS point : false\n",
"\n"
]
}
],
"source": [
"print input_config.to_string()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Result file : ../data/mr.txt\n",
"Output fields: opath cpath mgeom \n"
]
}
],
"source": [
"# Define output configuration\n",
"result_config = ResultConfig()\n",
"result_config.file = \"../data/mr.txt\"\n",
"result_config.output_config.write_opath = True\n",
"print result_config.to_string()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"status = model.match_gps_file(input_config, result_config, stmatch_config)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Time takes 0.001 seconds\n",
"Total points 17 matched 17\n",
"Map match speed 17000 points/s \n",
"\n"
]
}
],
"source": [
"print status"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -131,7 +241,20 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.15"
"version": "2.7.16"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": true
}
},
"nbformat": 4,
Expand Down
59 changes: 59 additions & 0 deletions src/mm/stmatch/stmatch_algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "algorithm/geom_algorithm.hpp"
#include "util/debug.hpp"
#include "util/util.hpp"
#include "io/gps_reader.hpp"
#include "io/mm_writer.hpp"

#include <limits>

Expand Down Expand Up @@ -155,6 +157,63 @@ MatchResult STMATCH::match_traj(const Trajectory &traj,
traj.id, matched_candidate_path, opath, cpath, indices, mgeom};
}

std::string STMATCH::match_gps_file(
const FMM::CONFIG::GPSConfig &gps_config,
const FMM::CONFIG::ResultConfig &result_config,
const STMATCHConfig &stmatch_config
){
std::ostringstream oss;
std::string status;
bool validate = true;
if (!gps_config.validate()) {
oss<<"gps_config invalid\n";
validate = false;
}
if (!result_config.validate()) {
oss<<"result_config invalid\n";
validate = false;
}
if (!stmatch_config.validate()) {
oss<<"stmatch_config invalid\n";
validate = false;
}
if (!validate){
oss<<"match_gps_file canceled\n";
return oss.str();
}
// Start map matching
int progress = 0;
int points_matched = 0;
int total_points = 0;
int step_size = 1000;
UTIL::TimePoint begin_time = std::chrono::steady_clock::now();
FMM::IO::GPSReader reader(gps_config);
FMM::IO::CSVMatchResultWriter writer(result_config.file,
result_config.output_config);
while (reader.has_next_trajectory()) {
if (progress % step_size == 0) {
SPDLOG_INFO("Progress {}", progress);
}
Trajectory trajectory = reader.read_next_trajectory();
int points_in_tr = trajectory.geom.get_num_points();
MM::MatchResult result = match_traj(
trajectory, stmatch_config);
writer.write_result(trajectory,result);
if (!result.cpath.empty()) {
points_matched += points_in_tr;
}
total_points += points_in_tr;
++progress;
}
UTIL::TimePoint end_time = std::chrono::steady_clock::now();
double duration = std::chrono::duration_cast<
std::chrono::milliseconds>(end_time - begin_time).count() / 1000.;
oss<<"Time takes " << duration << " seconds\n";
oss<<"Total points " << total_points << " matched "<< points_matched <<"\n";
oss<<"Map match speed " << points_matched / duration << " points/s \n";
return oss.str();
};

void STMATCH::update_tg(TransitionGraph *tg,
const CompositeGraph &cg,
const Trajectory &traj,
Expand Down
Loading

0 comments on commit 37c37ee

Please sign in to comment.