Skip to content

Commit

Permalink
Remove USE_BG_GEOMETRY from the reader class
Browse files Browse the repository at this point in the history
  • Loading branch information
cyang-kth committed Jan 20, 2020
1 parent c94e928 commit 520645f
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 161 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ This project is an implementation of the fast map matching (FMM) algorithm intro

- Map matching with Shapefile: check the [example](https://github.com/cyang-kth/fmm/tree/master/example)
- Map matching with OpenStreetMap: check the tutorial at [OSM map matching](https://github.com/cyang-kth/osm_mapmatching)
- 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.
- 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.

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

Expand Down
16 changes: 13 additions & 3 deletions src/geometry_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
#define MM_GEOMTYPES_HPP

#ifdef USE_BG_GEOMETRY

#include <ogrsf_frmts.h> // C++ API for GDAL
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/extensions/gis/io/wkb/read_wkb.hpp>
#include <iterator>
#include <vector>
#else

#else // USE_BG_GEOMETRY not defined

#include <ogrsf_frmts.h> // C++ API for GDAL

#endif

namespace MM {
Expand Down Expand Up @@ -59,7 +63,7 @@ typedef BGLineString LineString;
* freeing the memory.
*
*/
BGLineString *ogr2bg(OGRLineString *line){
LineString *ogr2linestring(OGRLineString *line){
int binary_size = line->WkbSize();
std::vector<unsigned char> wkb(binary_size);
// http://www.gdal.org/ogr__core_8h.html#a36cc1f4d807ba8f6fb8951f3adf251e2
Expand All @@ -69,10 +73,16 @@ BGLineString *ogr2bg(OGRLineString *line){
return l;
};

#else
#else // USE_BG_GEOMETRY not defined

typedef OGRLineString LineString;

LineString *ogr2linestring(OGRLineString *line){
LineString *linestring = (OGRLineString*) line->clone();
return linestring;
};


#endif //USE_BG_GEOMETRY

}; // MM
Expand Down
10 changes: 1 addition & 9 deletions src/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,7 @@ class Network
e->source = ogrFeature->GetFieldAsInteger(source_idx);
e->target = ogrFeature->GetFieldAsInteger(target_idx);
OGRGeometry *rawgeometry = ogrFeature->GetGeometryRef();
// CS_DEBUG(3) std::cout<<"Line "<< __LINE__<<" ID "<< e->id <<" Length "<<((OGRLineString*) rawgeometry)->get_Length()<<"\n";
// CS_DEBUG(3) std::cout<<"Line "<< __LINE__<<" ID "<< e->id <<" Points "<<((OGRLineString*) rawgeometry)->getNumPoints()<<"\n";
// The cloned geometry has to be freed by OGRGeometryFactory
// https://github.com/OSGeo/gdal/blob/93fb17379bccba28a43a03bb2c19b868f264ebe1/gdal/ogr/ogrlinestring.cpp#L141
#ifdef USE_BG_GEOMETRY
e->geom = ogr2bg((OGRLineString*) rawgeometry);
#else
e->geom = (OGRLineString*) rawgeometry->clone();
#endif
e->geom = ogr2linestring((OGRLineString*) rawgeometry);
e->length = e->geom->get_Length();
if (e->source>max_node_id)
{
Expand Down
265 changes: 117 additions & 148 deletions src/reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,18 @@
#include "util.hpp"
#include <iomanip>


#if defined(GDAL_COMPUTE_VERSION) && GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(2,0,0)

#endif // defined

namespace MM
{
namespace IO
{
/**
* According to the documentation at http://gdal.org/1.11/ogr/ogr_apitut.html
*
* 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.
* 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.
*
* It implies that when we delete the feature, the geometry returned by
* OGRFeature::GetGeometryRef() is also deleted. Therefore, we need to
Expand All @@ -42,154 +41,124 @@ namespace IO
class TrajectoryReader
{
public:
/**
* Constructor of TrajectoryReader
* @param filename, a GPS ESRI shapefile path
* @param id_name, the ID column name in the GPS shapefile
*/
TrajectoryReader(const std::string & filename,const std::string & id_name)
{
std::cout<<"Reading meta data of GPS trajectories from: " << filename << '\n';
OGRRegisterAll();
#if GDAL_VERSION_MAJOR < 2
poDS = OGRSFDriverRegistrar::Open(filename.c_str());
#else
poDS = (GDALDataset*) GDALOpenEx(filename.c_str(), GDAL_OF_VECTOR, NULL, NULL, NULL );
#endif // GDAL_VERSION_MAJOR
if( poDS == NULL )
{
printf( "Open failed.\n" );
exit( 1 );
}
ogrlayer = poDS->GetLayer(0);
_cursor=0;
// Get the number of features first
OGRFeatureDefn *ogrFDefn = ogrlayer->GetLayerDefn();
// This should be a local field rather than a new variable
id_idx=ogrFDefn->GetFieldIndex(id_name.c_str());
NUM_FEATURES= ogrlayer->GetFeatureCount();
if (id_idx<0)
{
std::cout<< "ERROR: id column not found with "<<id_name<< '\n';
#if GDAL_VERSION_MAJOR < 2
OGRDataSource::DestroyDataSource( poDS );
#else
GDALClose( poDS );
#endif // GDAL_VERSION_MAJOR
std::exit(EXIT_FAILURE);
}
if (wkbFlatten(ogrFDefn->GetGeomType()) != wkbLineString)
{
std::cout<<std::setw(12)<<""<< "Geometry type of trajectory is " <<OGRGeometryTypeToName(ogrFDefn->GetGeomType())<<'\n';
std::cout<<std::setw(12)<<""<< "It should be LineString"<< '\n';
#if GDAL_VERSION_MAJOR < 2
OGRDataSource::DestroyDataSource( poDS );
#else
GDALClose( poDS );
#endif // GDAL_VERSION_MAJOR
std::cout<<"Program stop"<< '\n';
std::exit(EXIT_FAILURE);
} else {
std::cout<< "\tGeometry type is " <<OGRGeometryTypeToName(ogrFDefn->GetGeomType())<<'\n';
}
std::cout<<" Index of ID column: " << id_idx<< '\n';
std::cout<<" Total number of trajectories: " << NUM_FEATURES << '\n';
std::cout<<"Finish reading meta data" << '\n';
};
// If there are still features not read
bool has_next_feature()
{
return _cursor<NUM_FEATURES;
};
// Read the next trajectory in the shapefile
Trajectory read_next_trajectory()
/**
* Constructor of TrajectoryReader
* @param filename, a GPS ESRI shapefile path
* @param id_name, the ID column name in the GPS shapefile
*/
TrajectoryReader(const std::string & filename,const std::string & id_name)
{
std::cout<<"Reading meta data of GPS trajectories from: " << filename << '\n';
OGRRegisterAll();
poDS = (GDALDataset*) GDALOpenEx(filename.c_str(),
GDAL_OF_VECTOR, NULL, NULL, NULL );
if( poDS == NULL )
{
OGRFeature *ogrFeature =ogrlayer->GetNextFeature();
int trid = ogrFeature->GetFieldAsInteger(id_idx);
DEBUG(2) std::cout<<"Read trajectory id : "<<trid<<'\n';
OGRGeometry *rawgeometry = ogrFeature->GetGeometryRef();
#ifdef USE_BG_GEOMETRY
BGLineString *linestring = ogr2bg((OGRLineString*) rawgeometry);
#else
OGRLineString *linestring = (OGRLineString*) rawgeometry->clone();
#endif
OGRFeature::DestroyFeature(ogrFeature);
++_cursor;
return Trajectory(trid,linestring);
};
// Read the next N trajectories in the shapefile
std::vector<Trajectory> read_next_N_trajectories(int N=30000)
printf( "Open failed.\n" );
exit( 1 );
}
ogrlayer = poDS->GetLayer(0);
_cursor=0;
// Get the number of features first
OGRFeatureDefn *ogrFDefn = ogrlayer->GetLayerDefn();
// This should be a local field rather than a new variable
id_idx=ogrFDefn->GetFieldIndex(id_name.c_str());
NUM_FEATURES= ogrlayer->GetFeatureCount();
if (id_idx<0)
{
int trajectories_size = NUM_FEATURES-_cursor<N ? NUM_FEATURES-_cursor:N;
// std::cout<<std::setw(4)<<""<<"Read features with buffer from : "<< _cursor << " to " << _cursor + N <<'\n';
std::vector<Trajectory> trajectories(trajectories_size);
int i=0;
while(i<trajectories_size)
{
OGRFeature *ogrFeature =ogrlayer->GetNextFeature();
int trid = ogrFeature->GetFieldAsInteger(id_idx);
OGRGeometry *rawgeometry = ogrFeature->GetGeometryRef();
#ifdef USE_BG_GEOMETRY
BGLineString *linestring = ogr2bg((OGRLineString*) rawgeometry);
#else
OGRLineString *linestring = (OGRLineString*) rawgeometry->clone();
#endif
OGRFeature::DestroyFeature(ogrFeature);
trajectories[i].id = trid;
trajectories[i].geom = linestring;
++i;
}
_cursor+=trajectories_size;
return trajectories;
};
// Read all trajectories at once, which can consume a lot of
// memories
std::vector<Trajectory> read_all_trajectories()
std::cout<< "ERROR: id column not found with "<<id_name<< '\n';
GDALClose( poDS );
std::exit(EXIT_FAILURE);
}
if (wkbFlatten(ogrFDefn->GetGeomType()) != wkbLineString)
{
std::cout<<"\t Read all trajectoires" << '\n';
std::vector<Trajectory> trajectories(NUM_FEATURES);
int i=0;
while(i<NUM_FEATURES)
{
OGRFeature *ogrFeature =ogrlayer->GetNextFeature();
int trid = ogrFeature->GetFieldAsInteger(id_idx);
OGRGeometry *rawgeometry = ogrFeature->GetGeometryRef();
#ifdef USE_BG_GEOMETRY
BGLineString *linestring = ogr2bg((OGRLineString*) rawgeometry);
#else
OGRLineString *linestring = (OGRLineString*) rawgeometry->clone();
#endif
OGRFeature::DestroyFeature(ogrFeature);
trajectories[i].id = trid;
trajectories[i].geom = linestring;
++i;
}
std::cout<<"\t Read trajectory set size : "<< i <<'\n';
return trajectories;
};
// Get the number of trajectories in the file
int get_num_trajectories()
std::cout<<std::setw(12)<<""<< "Geometry type of trajectory is "
<<OGRGeometryTypeToName(ogrFDefn->GetGeomType())<<'\n';
std::cout<<std::setw(12)<<""<< "It should be LineString"<< '\n';
GDALClose( poDS );
std::cout<<"Program stop"<< '\n';
std::exit(EXIT_FAILURE);
} else {
std::cout<< "\tGeometry type is " <<
OGRGeometryTypeToName(ogrFDefn->GetGeomType())<<'\n';
}
std::cout<<" Index of ID column: " << id_idx<< '\n';
std::cout<<" Total number of trajectories: " << NUM_FEATURES << '\n';
std::cout<<"Finish reading meta data" << '\n';
};
// If there are still features not read
bool has_next_feature()
{
return _cursor<NUM_FEATURES;
};
// Read the next trajectory in the shapefile
Trajectory read_next_trajectory()
{
OGRFeature *ogrFeature =ogrlayer->GetNextFeature();
int trid = ogrFeature->GetFieldAsInteger(id_idx);
DEBUG(2) std::cout<<"Read trajectory id : "<<trid<<'\n';
OGRGeometry *rawgeometry = ogrFeature->GetGeometryRef();
LineString *linestring = ogr2linestring((OGRLineString*) rawgeometry);
OGRFeature::DestroyFeature(ogrFeature);
++_cursor;
return Trajectory(trid,linestring);
};
// Read the next N trajectories in the shapefile
std::vector<Trajectory> read_next_N_trajectories(int N=30000)
{
int trajectories_size = NUM_FEATURES-_cursor<N ? NUM_FEATURES-_cursor : N;
std::vector<Trajectory> trajectories(trajectories_size);
int i=0;
while(i<trajectories_size)
{
return NUM_FEATURES;
};
~TrajectoryReader()
OGRFeature *ogrFeature =ogrlayer->GetNextFeature();
int trid = ogrFeature->GetFieldAsInteger(id_idx);
OGRGeometry *rawgeometry = ogrFeature->GetGeometryRef();
LineString *linestring = ogr2linestring((OGRLineString*) rawgeometry);
OGRFeature::DestroyFeature(ogrFeature);
trajectories[i].id = trid;
trajectories[i].geom = linestring;
++i;
}
_cursor+=trajectories_size;
return trajectories;
};
// Read all trajectories at once, which can consume a lot of
// memories
std::vector<Trajectory> read_all_trajectories()
{
std::cout<<"\t Read all trajectoires" << '\n';
std::vector<Trajectory> trajectories(NUM_FEATURES);
int i=0;
while(i<NUM_FEATURES)
{
#if GDAL_VERSION_MAJOR < 2
OGRDataSource::DestroyDataSource( poDS );
#else
GDALClose( poDS );
#endif // GDAL_VERSION_MAJOR
};
OGRFeature *ogrFeature =ogrlayer->GetNextFeature();
int trid = ogrFeature->GetFieldAsInteger(id_idx);
OGRGeometry *rawgeometry = ogrFeature->GetGeometryRef();
LineString *linestring = ogr2linestring((OGRLineString*) rawgeometry);
OGRFeature::DestroyFeature(ogrFeature);
trajectories[i].id = trid;
trajectories[i].geom = linestring;
++i;
}
std::cout<<"\t Read trajectory set size : "<< i <<'\n';
return trajectories;
};
// Get the number of trajectories in the file
int get_num_trajectories()
{
return NUM_FEATURES;
};
~TrajectoryReader()
{
GDALClose( poDS );
};
private:
int NUM_FEATURES=0;
int id_idx; // Index of the id column in shapefile
int _cursor=0; // Keep record of current features read
#if GDAL_VERSION_MAJOR < 2
OGRDataSource *poDS;
#else
GDALDataset *poDS; // GDAL 2.1.0
#endif // GDAL_VERSION_MAJOR
OGRLayer *ogrlayer;
int NUM_FEATURES=0;
int id_idx; // Index of the id column in shapefile
int _cursor=0; // Keep record of current features read
GDALDataset *poDS; // GDAL 2.1.0
OGRLayer *ogrlayer;
}; // TrajectoryReader
} // IO
} // MM
Expand Down

0 comments on commit 520645f

Please sign in to comment.