Skip to content

Commit 520645f

Browse files
committed
Remove USE_BG_GEOMETRY from the reader class
1 parent c94e928 commit 520645f

File tree

4 files changed

+132
-161
lines changed

4 files changed

+132
-161
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ This project is an implementation of the fast map matching (FMM) algorithm intro
1717

1818
- Map matching with Shapefile: check the [example](https://github.com/cyang-kth/fmm/tree/master/example)
1919
- Map matching with OpenStreetMap: check the tutorial at [OSM map matching](https://github.com/cyang-kth/osm_mapmatching)
20-
- Interactive exploration of Map matching: a web application under the `web_demo` folder is designed for map matching on OpenStreetMap and screenshots are shown below.
20+
- Interactive exploration of map matching: a web application under the `web_demo` folder is designed for map matching on OpenStreetMap and screenshots are shown below.
2121

2222
<img src="img/demo1.gif" width="400"/> <img src="img/demo2.gif" width="400"/>
2323

src/geometry_type.hpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22
#define MM_GEOMTYPES_HPP
33

44
#ifdef USE_BG_GEOMETRY
5+
56
#include <ogrsf_frmts.h> // C++ API for GDAL
67
#include <boost/geometry.hpp>
78
#include <boost/geometry/geometries/geometries.hpp>
89
#include <boost/geometry/extensions/gis/io/wkb/read_wkb.hpp>
910
#include <iterator>
1011
#include <vector>
11-
#else
12+
13+
#else // USE_BG_GEOMETRY not defined
14+
1215
#include <ogrsf_frmts.h> // C++ API for GDAL
16+
1317
#endif
1418

1519
namespace MM {
@@ -59,7 +63,7 @@ typedef BGLineString LineString;
5963
* freeing the memory.
6064
*
6165
*/
62-
BGLineString *ogr2bg(OGRLineString *line){
66+
LineString *ogr2linestring(OGRLineString *line){
6367
int binary_size = line->WkbSize();
6468
std::vector<unsigned char> wkb(binary_size);
6569
// http://www.gdal.org/ogr__core_8h.html#a36cc1f4d807ba8f6fb8951f3adf251e2
@@ -69,10 +73,16 @@ BGLineString *ogr2bg(OGRLineString *line){
6973
return l;
7074
};
7175

72-
#else
76+
#else // USE_BG_GEOMETRY not defined
7377

7478
typedef OGRLineString LineString;
7579

80+
LineString *ogr2linestring(OGRLineString *line){
81+
LineString *linestring = (OGRLineString*) line->clone();
82+
return linestring;
83+
};
84+
85+
7686
#endif //USE_BG_GEOMETRY
7787

7888
}; // MM

src/network.hpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,7 @@ class Network
137137
e->source = ogrFeature->GetFieldAsInteger(source_idx);
138138
e->target = ogrFeature->GetFieldAsInteger(target_idx);
139139
OGRGeometry *rawgeometry = ogrFeature->GetGeometryRef();
140-
// CS_DEBUG(3) std::cout<<"Line "<< __LINE__<<" ID "<< e->id <<" Length "<<((OGRLineString*) rawgeometry)->get_Length()<<"\n";
141-
// CS_DEBUG(3) std::cout<<"Line "<< __LINE__<<" ID "<< e->id <<" Points "<<((OGRLineString*) rawgeometry)->getNumPoints()<<"\n";
142-
// The cloned geometry has to be freed by OGRGeometryFactory
143-
// https://github.com/OSGeo/gdal/blob/93fb17379bccba28a43a03bb2c19b868f264ebe1/gdal/ogr/ogrlinestring.cpp#L141
144-
#ifdef USE_BG_GEOMETRY
145-
e->geom = ogr2bg((OGRLineString*) rawgeometry);
146-
#else
147-
e->geom = (OGRLineString*) rawgeometry->clone();
148-
#endif
140+
e->geom = ogr2linestring((OGRLineString*) rawgeometry);
149141
e->length = e->geom->get_Length();
150142
if (e->source>max_node_id)
151143
{

src/reader.hpp

Lines changed: 117 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,18 @@
1919
#include "util.hpp"
2020
#include <iomanip>
2121

22-
23-
#if defined(GDAL_COMPUTE_VERSION) && GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(2,0,0)
24-
25-
#endif // defined
26-
2722
namespace MM
2823
{
2924
namespace IO
3025
{
3126
/**
3227
* According to the documentation at http://gdal.org/1.11/ogr/ogr_apitut.html
3328
*
34-
* Note that OGRFeature::GetGeometryRef() and OGRFeature::GetGeomFieldRef() return a pointer to the internal geometry owned by the OGRFeature. There we don't actually deleted the return geometry. However, the OGRLayer::GetNextFeature() method returns a copy of the feature that is now owned by us. So at the end of use we must free the feature.
29+
* Note that OGRFeature::GetGeometryRef() and OGRFeature::GetGeomFieldRef()
30+
* return a pointer to the internal geometry owned by the OGRFeature. There
31+
* we don't actually deleted the return geometry. However, the
32+
* OGRLayer::GetNextFeature() method returns a copy of the feature that is
33+
* now owned by us. So at the end of use we must free the feature.
3534
*
3635
* It implies that when we delete the feature, the geometry returned by
3736
* OGRFeature::GetGeometryRef() is also deleted. Therefore, we need to
@@ -42,154 +41,124 @@ namespace IO
4241
class TrajectoryReader
4342
{
4443
public:
45-
/**
46-
* Constructor of TrajectoryReader
47-
* @param filename, a GPS ESRI shapefile path
48-
* @param id_name, the ID column name in the GPS shapefile
49-
*/
50-
TrajectoryReader(const std::string & filename,const std::string & id_name)
51-
{
52-
std::cout<<"Reading meta data of GPS trajectories from: " << filename << '\n';
53-
OGRRegisterAll();
54-
#if GDAL_VERSION_MAJOR < 2
55-
poDS = OGRSFDriverRegistrar::Open(filename.c_str());
56-
#else
57-
poDS = (GDALDataset*) GDALOpenEx(filename.c_str(), GDAL_OF_VECTOR, NULL, NULL, NULL );
58-
#endif // GDAL_VERSION_MAJOR
59-
if( poDS == NULL )
60-
{
61-
printf( "Open failed.\n" );
62-
exit( 1 );
63-
}
64-
ogrlayer = poDS->GetLayer(0);
65-
_cursor=0;
66-
// Get the number of features first
67-
OGRFeatureDefn *ogrFDefn = ogrlayer->GetLayerDefn();
68-
// This should be a local field rather than a new variable
69-
id_idx=ogrFDefn->GetFieldIndex(id_name.c_str());
70-
NUM_FEATURES= ogrlayer->GetFeatureCount();
71-
if (id_idx<0)
72-
{
73-
std::cout<< "ERROR: id column not found with "<<id_name<< '\n';
74-
#if GDAL_VERSION_MAJOR < 2
75-
OGRDataSource::DestroyDataSource( poDS );
76-
#else
77-
GDALClose( poDS );
78-
#endif // GDAL_VERSION_MAJOR
79-
std::exit(EXIT_FAILURE);
80-
}
81-
if (wkbFlatten(ogrFDefn->GetGeomType()) != wkbLineString)
82-
{
83-
std::cout<<std::setw(12)<<""<< "Geometry type of trajectory is " <<OGRGeometryTypeToName(ogrFDefn->GetGeomType())<<'\n';
84-
std::cout<<std::setw(12)<<""<< "It should be LineString"<< '\n';
85-
#if GDAL_VERSION_MAJOR < 2
86-
OGRDataSource::DestroyDataSource( poDS );
87-
#else
88-
GDALClose( poDS );
89-
#endif // GDAL_VERSION_MAJOR
90-
std::cout<<"Program stop"<< '\n';
91-
std::exit(EXIT_FAILURE);
92-
} else {
93-
std::cout<< "\tGeometry type is " <<OGRGeometryTypeToName(ogrFDefn->GetGeomType())<<'\n';
94-
}
95-
std::cout<<" Index of ID column: " << id_idx<< '\n';
96-
std::cout<<" Total number of trajectories: " << NUM_FEATURES << '\n';
97-
std::cout<<"Finish reading meta data" << '\n';
98-
};
99-
// If there are still features not read
100-
bool has_next_feature()
101-
{
102-
return _cursor<NUM_FEATURES;
103-
};
104-
// Read the next trajectory in the shapefile
105-
Trajectory read_next_trajectory()
44+
/**
45+
* Constructor of TrajectoryReader
46+
* @param filename, a GPS ESRI shapefile path
47+
* @param id_name, the ID column name in the GPS shapefile
48+
*/
49+
TrajectoryReader(const std::string & filename,const std::string & id_name)
50+
{
51+
std::cout<<"Reading meta data of GPS trajectories from: " << filename << '\n';
52+
OGRRegisterAll();
53+
poDS = (GDALDataset*) GDALOpenEx(filename.c_str(),
54+
GDAL_OF_VECTOR, NULL, NULL, NULL );
55+
if( poDS == NULL )
10656
{
107-
OGRFeature *ogrFeature =ogrlayer->GetNextFeature();
108-
int trid = ogrFeature->GetFieldAsInteger(id_idx);
109-
DEBUG(2) std::cout<<"Read trajectory id : "<<trid<<'\n';
110-
OGRGeometry *rawgeometry = ogrFeature->GetGeometryRef();
111-
#ifdef USE_BG_GEOMETRY
112-
BGLineString *linestring = ogr2bg((OGRLineString*) rawgeometry);
113-
#else
114-
OGRLineString *linestring = (OGRLineString*) rawgeometry->clone();
115-
#endif
116-
OGRFeature::DestroyFeature(ogrFeature);
117-
++_cursor;
118-
return Trajectory(trid,linestring);
119-
};
120-
// Read the next N trajectories in the shapefile
121-
std::vector<Trajectory> read_next_N_trajectories(int N=30000)
57+
printf( "Open failed.\n" );
58+
exit( 1 );
59+
}
60+
ogrlayer = poDS->GetLayer(0);
61+
_cursor=0;
62+
// Get the number of features first
63+
OGRFeatureDefn *ogrFDefn = ogrlayer->GetLayerDefn();
64+
// This should be a local field rather than a new variable
65+
id_idx=ogrFDefn->GetFieldIndex(id_name.c_str());
66+
NUM_FEATURES= ogrlayer->GetFeatureCount();
67+
if (id_idx<0)
12268
{
123-
int trajectories_size = NUM_FEATURES-_cursor<N ? NUM_FEATURES-_cursor:N;
124-
// std::cout<<std::setw(4)<<""<<"Read features with buffer from : "<< _cursor << " to " << _cursor + N <<'\n';
125-
std::vector<Trajectory> trajectories(trajectories_size);
126-
int i=0;
127-
while(i<trajectories_size)
128-
{
129-
OGRFeature *ogrFeature =ogrlayer->GetNextFeature();
130-
int trid = ogrFeature->GetFieldAsInteger(id_idx);
131-
OGRGeometry *rawgeometry = ogrFeature->GetGeometryRef();
132-
#ifdef USE_BG_GEOMETRY
133-
BGLineString *linestring = ogr2bg((OGRLineString*) rawgeometry);
134-
#else
135-
OGRLineString *linestring = (OGRLineString*) rawgeometry->clone();
136-
#endif
137-
OGRFeature::DestroyFeature(ogrFeature);
138-
trajectories[i].id = trid;
139-
trajectories[i].geom = linestring;
140-
++i;
141-
}
142-
_cursor+=trajectories_size;
143-
return trajectories;
144-
};
145-
// Read all trajectories at once, which can consume a lot of
146-
// memories
147-
std::vector<Trajectory> read_all_trajectories()
69+
std::cout<< "ERROR: id column not found with "<<id_name<< '\n';
70+
GDALClose( poDS );
71+
std::exit(EXIT_FAILURE);
72+
}
73+
if (wkbFlatten(ogrFDefn->GetGeomType()) != wkbLineString)
14874
{
149-
std::cout<<"\t Read all trajectoires" << '\n';
150-
std::vector<Trajectory> trajectories(NUM_FEATURES);
151-
int i=0;
152-
while(i<NUM_FEATURES)
153-
{
154-
OGRFeature *ogrFeature =ogrlayer->GetNextFeature();
155-
int trid = ogrFeature->GetFieldAsInteger(id_idx);
156-
OGRGeometry *rawgeometry = ogrFeature->GetGeometryRef();
157-
#ifdef USE_BG_GEOMETRY
158-
BGLineString *linestring = ogr2bg((OGRLineString*) rawgeometry);
159-
#else
160-
OGRLineString *linestring = (OGRLineString*) rawgeometry->clone();
161-
#endif
162-
OGRFeature::DestroyFeature(ogrFeature);
163-
trajectories[i].id = trid;
164-
trajectories[i].geom = linestring;
165-
++i;
166-
}
167-
std::cout<<"\t Read trajectory set size : "<< i <<'\n';
168-
return trajectories;
169-
};
170-
// Get the number of trajectories in the file
171-
int get_num_trajectories()
75+
std::cout<<std::setw(12)<<""<< "Geometry type of trajectory is "
76+
<<OGRGeometryTypeToName(ogrFDefn->GetGeomType())<<'\n';
77+
std::cout<<std::setw(12)<<""<< "It should be LineString"<< '\n';
78+
GDALClose( poDS );
79+
std::cout<<"Program stop"<< '\n';
80+
std::exit(EXIT_FAILURE);
81+
} else {
82+
std::cout<< "\tGeometry type is " <<
83+
OGRGeometryTypeToName(ogrFDefn->GetGeomType())<<'\n';
84+
}
85+
std::cout<<" Index of ID column: " << id_idx<< '\n';
86+
std::cout<<" Total number of trajectories: " << NUM_FEATURES << '\n';
87+
std::cout<<"Finish reading meta data" << '\n';
88+
};
89+
// If there are still features not read
90+
bool has_next_feature()
91+
{
92+
return _cursor<NUM_FEATURES;
93+
};
94+
// Read the next trajectory in the shapefile
95+
Trajectory read_next_trajectory()
96+
{
97+
OGRFeature *ogrFeature =ogrlayer->GetNextFeature();
98+
int trid = ogrFeature->GetFieldAsInteger(id_idx);
99+
DEBUG(2) std::cout<<"Read trajectory id : "<<trid<<'\n';
100+
OGRGeometry *rawgeometry = ogrFeature->GetGeometryRef();
101+
LineString *linestring = ogr2linestring((OGRLineString*) rawgeometry);
102+
OGRFeature::DestroyFeature(ogrFeature);
103+
++_cursor;
104+
return Trajectory(trid,linestring);
105+
};
106+
// Read the next N trajectories in the shapefile
107+
std::vector<Trajectory> read_next_N_trajectories(int N=30000)
108+
{
109+
int trajectories_size = NUM_FEATURES-_cursor<N ? NUM_FEATURES-_cursor : N;
110+
std::vector<Trajectory> trajectories(trajectories_size);
111+
int i=0;
112+
while(i<trajectories_size)
172113
{
173-
return NUM_FEATURES;
174-
};
175-
~TrajectoryReader()
114+
OGRFeature *ogrFeature =ogrlayer->GetNextFeature();
115+
int trid = ogrFeature->GetFieldAsInteger(id_idx);
116+
OGRGeometry *rawgeometry = ogrFeature->GetGeometryRef();
117+
LineString *linestring = ogr2linestring((OGRLineString*) rawgeometry);
118+
OGRFeature::DestroyFeature(ogrFeature);
119+
trajectories[i].id = trid;
120+
trajectories[i].geom = linestring;
121+
++i;
122+
}
123+
_cursor+=trajectories_size;
124+
return trajectories;
125+
};
126+
// Read all trajectories at once, which can consume a lot of
127+
// memories
128+
std::vector<Trajectory> read_all_trajectories()
129+
{
130+
std::cout<<"\t Read all trajectoires" << '\n';
131+
std::vector<Trajectory> trajectories(NUM_FEATURES);
132+
int i=0;
133+
while(i<NUM_FEATURES)
176134
{
177-
#if GDAL_VERSION_MAJOR < 2
178-
OGRDataSource::DestroyDataSource( poDS );
179-
#else
180-
GDALClose( poDS );
181-
#endif // GDAL_VERSION_MAJOR
182-
};
135+
OGRFeature *ogrFeature =ogrlayer->GetNextFeature();
136+
int trid = ogrFeature->GetFieldAsInteger(id_idx);
137+
OGRGeometry *rawgeometry = ogrFeature->GetGeometryRef();
138+
LineString *linestring = ogr2linestring((OGRLineString*) rawgeometry);
139+
OGRFeature::DestroyFeature(ogrFeature);
140+
trajectories[i].id = trid;
141+
trajectories[i].geom = linestring;
142+
++i;
143+
}
144+
std::cout<<"\t Read trajectory set size : "<< i <<'\n';
145+
return trajectories;
146+
};
147+
// Get the number of trajectories in the file
148+
int get_num_trajectories()
149+
{
150+
return NUM_FEATURES;
151+
};
152+
~TrajectoryReader()
153+
{
154+
GDALClose( poDS );
155+
};
183156
private:
184-
int NUM_FEATURES=0;
185-
int id_idx; // Index of the id column in shapefile
186-
int _cursor=0; // Keep record of current features read
187-
#if GDAL_VERSION_MAJOR < 2
188-
OGRDataSource *poDS;
189-
#else
190-
GDALDataset *poDS; // GDAL 2.1.0
191-
#endif // GDAL_VERSION_MAJOR
192-
OGRLayer *ogrlayer;
157+
int NUM_FEATURES=0;
158+
int id_idx; // Index of the id column in shapefile
159+
int _cursor=0; // Keep record of current features read
160+
GDALDataset *poDS; // GDAL 2.1.0
161+
OGRLayer *ogrlayer;
193162
}; // TrajectoryReader
194163
} // IO
195164
} // MM

0 commit comments

Comments
 (0)