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-
2722namespace MM
2823{
2924namespace 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
4241class TrajectoryReader
4342{
4443public:
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<< " \t Geometry 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<< " \t Geometry 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+ };
183156private:
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