GDAL
gnm.h
1 /******************************************************************************
2  * $Id$
3  *
4  * Project: GDAL/OGR Geography Network support (Geographic Network Model)
5  * Purpose: GNM general public declarations.
6  * Authors: Mikhail Gusev (gusevmihs at gmail dot com)
7  * Dmitry Baryshnikov, polimax@mail.ru
8  *
9  ******************************************************************************
10  * Copyright (c) 2014, Mikhail Gusev
11  * Copyright (c) 2014-2015, NextGIS <info@nextgis.com>
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a
14  * copy of this software and associated documentation files (the "Software"),
15  * to deal in the Software without restriction, including without limitation
16  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  * and/or sell copies of the Software, and to permit persons to whom the
18  * Software is furnished to do so, subject to the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included
21  * in all copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29  * DEALINGS IN THE SOFTWARE.
30  ****************************************************************************/
31 
32 #ifndef GNM
33 #define GNM
34 
35 #include "ogrsf_frmts.h"
36 #include "gnmgraph.h"
37 
38 // Direction of an edge.
39 typedef int GNMDirection; // We use int values in order to save them to the
40  // network data.
41 
42 // Network's metadata parameters names.
43 #define GNM_MD_NAME "net_name"
44 #define GNM_MD_DESCR "net_description"
45 #define GNM_MD_SRS "net_srs"
46 #define GNM_MD_VERSION "net_version"
47 #define GNM_MD_RULE "net_rule"
48 #define GNM_MD_FORMAT "FORMAT"
49 #define GNM_MD_FETCHEDGES "fetch_edge"
50 #define GNM_MD_FETCHVERTEX "fetch_vertex"
51 #define GNM_MD_NUM_PATHS "num_paths"
52 #define GNM_MD_EMITTER "emitter"
53 
54 // TODO: Constants for capabilities.
55 //#define GNMCanChangeConnections "CanChangeConnections"
56 
57 typedef enum
58 { GATDijkstraShortestPath = 1, GATKShortestPath, GATConnectedComponents
62 } GNMGraphAlgorithmType;
63 
70 class CPL_DLL GNMNetwork : public GDALDataset
71 {
72 public:
73  GNMNetwork();
74  virtual ~GNMNetwork();
75 
76  // GDALDataset Interface
77  virtual const char *GetProjectionRef(void);
78  virtual char **GetFileList(void);
79 
80  // GNMNetwork Interface
81 
102  virtual CPLErr Create( const char* pszFilename, char** papszOptions ) = 0;
103 
109  virtual CPLErr Open( GDALOpenInfo* poOpenInfo ) = 0;
110 
115  virtual CPLErr Delete() = 0;
116 
123  virtual const char* GetName() const;
124 
129  virtual int GetVersion() const { return 0;}
130 
135  virtual CPLErr DisconnectAll () = 0;
136 
144  virtual OGRFeature *GetFeatureByGlobalFID (GNMGFID nGFID) = 0;
145 
156  virtual OGRLayer *GetPath (GNMGFID nStartFID, GNMGFID nEndFID,
157  GNMGraphAlgorithmType eAlgorithm, char** papszOptions) = 0;
158 protected:
165  virtual int CheckNetworkExist( const char* pszFilename,
166  char** papszOptions ) = 0;
167 
168 protected:
169  CPLString m_soName;
170  CPLString m_soSRS;
171 };
172 
173 class GNMRule;
175 
182 class CPL_DLL GNMGenericNetwork: public GNMNetwork
183 {
184 public:
186  virtual ~GNMGenericNetwork();
187 
188  // GDALDataset Interface
189 
190  virtual int GetLayerCount();
191  virtual OGRLayer *GetLayer(int);
192  virtual OGRErr DeleteLayer(int);
193 
194  virtual int TestCapability( const char * );
195 
196  virtual OGRLayer *CopyLayer( OGRLayer *poSrcLayer,
197  const char *pszNewName,
198  char **papszOptions = NULL );
199 
200  virtual int CloseDependentDatasets();
201  virtual void FlushCache(void);
202 
203  // GNMNetwork Interface
204 
205  virtual CPLErr Create( const char* pszFilename, char** papszOptions ) = 0;
206  virtual CPLErr Delete();
207 
208  virtual int GetVersion() const;
213  virtual GNMGFID GetNewGlobalFID();
214 
221  virtual CPLString GetAlgorithmName(GNMDirection eAlgorithm, bool bShortName);
222 
230  virtual CPLErr AddFeatureGlobalFID(GNMGFID nFID, const char* pszLayerName);
231 
245  virtual CPLErr ConnectFeatures (GNMGFID nSrcFID,
246  GNMGFID nTgtFID,
247  GNMGFID nConFID = -1,
248  double dfCost = 1,
249  double dfInvCost = 1,
250  GNMDirection eDir = GNM_EDGE_DIR_BOTH);
251 
259  virtual CPLErr DisconnectFeatures (GNMGFID nSrcFID,
260  GNMGFID nTgtFID,
261  GNMGFID nConFID);
262 
269  virtual CPLErr DisconnectFeaturesWithId(GNMGFID nFID);
270 
283  virtual CPLErr ReconnectFeatures (GNMGFID nSrcFID,
284  GNMGFID nTgtFID,
285  GNMGFID nConFID,
286  double dfCost = 1,
287  double dfInvCost = 1,
288  GNMDirection eDir = GNM_EDGE_DIR_BOTH);
289 
290  virtual CPLErr DisconnectAll();
291 
292  virtual OGRFeature *GetFeatureByGlobalFID(GNMGFID nFID);
293 
328  virtual CPLErr CreateRule (const char *pszRuleStr);
329 
334  virtual CPLErr DeleteAllRules();
335 
341  virtual CPLErr DeleteRule(const char *pszRuleStr);
342 
347  virtual char** GetRules() const;
348 
370  virtual CPLErr ConnectPointsByLines (char **papszLayerList,
371  double dfTolerance,
372  double dfCost,
373  double dfInvCost,
374  GNMDirection eDir);
375 
382  virtual CPLErr ChangeBlockState (GNMGFID nFID, bool bIsBlock);
383 
392  virtual CPLErr ChangeAllBlockState (bool bIsBlock = false);
393 
394  virtual OGRLayer *GetPath (GNMGFID nStartFID, GNMGFID nEndFID,
395  GNMGraphAlgorithmType eAlgorithm, char** papszOptions);
396 protected:
403  virtual CPLErr CheckLayerDriver(const char* pszDefaultDriverName,
404  char** papszOptions);
410  virtual bool CheckStorageDriverSupport(const char* pszDriverName) = 0;
411 protected:
412  virtual CPLErr CreateMetadataLayer( GDALDataset* const pDS, int nVersion,
413  size_t nFieldSize = 1024 );
414  virtual CPLErr StoreNetworkSrs();
415  virtual CPLErr LoadNetworkSrs();
416  virtual CPLErr CreateGraphLayer( GDALDataset* const pDS );
417  virtual CPLErr CreateFeaturesLayer( GDALDataset* const pDS );
418  virtual CPLErr LoadMetadataLayer( GDALDataset* const pDS );
419  virtual CPLErr LoadGraphLayer( GDALDataset* const pDS );
420  virtual CPLErr LoadGraph();
421  virtual CPLErr LoadFeaturesLayer( GDALDataset* const pDS );
422  virtual CPLErr DeleteMetadataLayer() = 0;
423  virtual CPLErr DeleteGraphLayer() = 0;
424  virtual CPLErr DeleteFeaturesLayer() = 0;
425  virtual CPLErr LoadNetworkLayer(const char* pszLayername) = 0;
426  virtual CPLErr DeleteNetworkLayers() = 0;
427  virtual void ConnectPointsByMultiline(GIntBig nFID,
428  const OGRMultiLineString *poMultiLineString,
429  const std::vector<OGRLayer *> &paPointLayers,
430  double dfTolerance, double dfCost,
431  double dfInvCost, GNMDirection eDir);
432  virtual void ConnectPointsByLine(GIntBig nFID,
433  const OGRLineString *poLineString,
434  const std::vector<OGRLayer *> &paPointLayers,
435  double dfTolerance, double dfCost,
436  double dfInvCost, GNMDirection eDir);
437  virtual GNMGFID FindNearestPoint(const OGRPoint* poPoint,
438  const std::vector<OGRLayer*>& paPointLayers,
439  double dfTolerance);
440  virtual OGRFeature* FindConnection(GNMGFID nSrcFID, GNMGFID nTgtFID,
441  GNMGFID nConFID);
442  virtual void SaveRules();
443  virtual GNMGFID GetNewVirtualFID();
444  virtual void FillResultLayer(OGRGNMWrappedResultLayer* poResLayer,
445  const GNMPATH &path, int nNoOfPath,
446  bool bReturnVertices, bool bReturnEdges);
447 protected:
448  int m_nVersion;
449  GNMGFID m_nGID;
450  GNMGFID m_nVirtualConnectionGID;
451  OGRLayer* m_poMetadataLayer;
452  OGRLayer* m_poGraphLayer;
453  OGRLayer* m_poFeaturesLayer;
454 
455  GDALDriver *m_poLayerDriver;
456 
457  std::map<GNMGFID, CPLString> m_moFeatureFIDMap;
458  std::vector<OGRLayer*> m_apoLayers;
459  std::vector<GNMRule> m_asRules;
460  bool m_bIsRulesChanged;
461 
462  GNMGraph m_oGraph;
463  bool m_bIsGraphLoaded;
464 };
465 
473 class GNMGenericLayer : public OGRLayer
474 {
475 public:
476  GNMGenericLayer(OGRLayer* poLayer, GNMGenericNetwork* poNetwork);
477  virtual ~GNMGenericLayer();
478 
479  // OGRLayer Interface
480 
481  virtual OGRGeometry *GetSpatialFilter();
482  virtual void SetSpatialFilter( OGRGeometry * );
483  virtual void SetSpatialFilterRect( double dfMinX, double dfMinY,
484  double dfMaxX, double dfMaxY );
485 
486  virtual void SetSpatialFilter( int iGeomField, OGRGeometry * );
487  virtual void SetSpatialFilterRect( int iGeomField,
488  double dfMinX, double dfMinY,
489  double dfMaxX, double dfMaxY );
490 
491  virtual OGRErr SetAttributeFilter( const char * );
492 
493  virtual void ResetReading();
494  virtual OGRFeature *GetNextFeature();
495  virtual OGRErr SetNextByIndex( GIntBig nIndex );
496 
497  virtual OGRErr DeleteFeature( GIntBig nFID );
498 
499  virtual const char *GetName();
500  virtual OGRwkbGeometryType GetGeomType();
501  virtual OGRFeatureDefn *GetLayerDefn();
502  virtual int FindFieldIndex( const char *pszFieldName, int bExactMatch );
503 
504  virtual OGRSpatialReference *GetSpatialRef();
505 
506  virtual GIntBig GetFeatureCount( int bForce = TRUE );
507  virtual OGRErr GetExtent(OGREnvelope *psExtent, int bForce = TRUE);
508  virtual OGRErr GetExtent(int iGeomField, OGREnvelope *psExtent,
509  int bForce = TRUE);
510 
511  virtual int TestCapability( const char * );
512 
513  virtual OGRErr CreateField( OGRFieldDefn *poField,
514  int bApproxOK = TRUE );
515  virtual OGRErr DeleteField( int iField );
516  virtual OGRErr ReorderFields( int* panMap );
517  virtual OGRErr AlterFieldDefn( int iField, OGRFieldDefn* poNewFieldDefn,
518  int nFlagsIn );
519 
520  virtual OGRErr CreateGeomField( OGRGeomFieldDefn *poField,
521  int bApproxOK = TRUE );
522 
523  virtual OGRErr SyncToDisk();
524 
525  virtual OGRStyleTable *GetStyleTable();
526  virtual void SetStyleTableDirectly( OGRStyleTable *poStyleTable );
527 
528  virtual void SetStyleTable(OGRStyleTable *poStyleTable);
529 
530  virtual OGRErr StartTransaction();
531  virtual OGRErr CommitTransaction();
532  virtual OGRErr RollbackTransaction();
533 
534  virtual const char *GetFIDColumn();
535  virtual const char *GetGeometryColumn();
536 
537  virtual OGRErr SetIgnoredFields( const char **papszFields );
538 
539  OGRErr Intersection( OGRLayer *pLayerMethod,
540  OGRLayer *pLayerResult,
541  char** papszOptions = NULL,
542  GDALProgressFunc pfnProgress = NULL,
543  void * pProgressArg = NULL );
544  OGRErr Union( OGRLayer *pLayerMethod,
545  OGRLayer *pLayerResult,
546  char** papszOptions = NULL,
547  GDALProgressFunc pfnProgress = NULL,
548  void * pProgressArg = NULL );
549  OGRErr SymDifference( OGRLayer *pLayerMethod,
550  OGRLayer *pLayerResult,
551  char** papszOptions,
552  GDALProgressFunc pfnProgress,
553  void * pProgressArg );
554  OGRErr Identity( OGRLayer *pLayerMethod,
555  OGRLayer *pLayerResult,
556  char** papszOptions = NULL,
557  GDALProgressFunc pfnProgress = NULL,
558  void * pProgressArg = NULL );
559  OGRErr Update( OGRLayer *pLayerMethod,
560  OGRLayer *pLayerResult,
561  char** papszOptions = NULL,
562  GDALProgressFunc pfnProgress = NULL,
563  void * pProgressArg = NULL );
564  OGRErr Clip( OGRLayer *pLayerMethod,
565  OGRLayer *pLayerResult,
566  char** papszOptions = NULL,
567  GDALProgressFunc pfnProgress = NULL,
568  void * pProgressArg = NULL );
569  OGRErr Erase( OGRLayer *pLayerMethod,
570  OGRLayer *pLayerResult,
571  char** papszOptions = NULL,
572  GDALProgressFunc pfnProgress = NULL,
573  void * pProgressArg = NULL );
574 
575  GIntBig GetFeaturesRead();
576 
577  int AttributeFilterEvaluationNeedsGeometry();
578 
579  /* consider these private */
580  OGRErr InitializeIndexSupport( const char * );
581  OGRLayerAttrIndex *GetIndex();
582 
583 protected:
584  virtual OGRErr ISetFeature( OGRFeature *poFeature );
585  virtual OGRErr ICreateFeature( OGRFeature *poFeature );
586 
587 protected:
588  CPLString m_soLayerName;
589  OGRLayer *m_poLayer;
590  GNMGenericNetwork* m_poNetwork;
591  std::map<GNMGFID, GIntBig> m_mnFIDMap;
592 };
593 
594 typedef enum
595 { GRTConnection = 0
597 } GNMRuleType;
598 
608 class CPL_DLL GNMRule
609 {
610 public:
611  GNMRule();
612  GNMRule(const std::string &oRule );
613  GNMRule(const char* pszRule);
614  GNMRule(const GNMRule &oRule);
615  virtual ~GNMRule();
620  virtual bool IsValid() const;
625  virtual bool IsAcceptAny() const;
631  virtual GNMRuleType GetType() const;
640  virtual bool CanConnect(const CPLString &soSrcLayerName,
641  const CPLString &soTgtLayerName,
642  const CPLString &soConnLayerName = "");
643  virtual CPLString GetSourceLayerName() const;
644  virtual CPLString GetTargetLayerName() const;
645  virtual CPLString GetConnectorLayerName() const;
646  const char* c_str() const;
647  operator const char* (void) const;
648 protected:
649  virtual bool ParseRuleString();
650 protected:
651  CPLString m_soSrcLayerName;
652  CPLString m_soTgtLayerName;
653  CPLString m_soConnLayerName;
654  bool m_bAllow;
655  bool m_bValid;
656  bool m_bAny;
657  CPLString m_soRuleString;
658 };
659 
667 {
668 public:
671 
672  // OGRLayer
673  virtual void ResetReading();
674  virtual OGRFeature *GetNextFeature();
675  virtual OGRErr SetNextByIndex( GIntBig nIndex );
676  virtual OGRFeature *GetFeature( GIntBig nFID );
677  virtual OGRFeatureDefn *GetLayerDefn();
678  virtual GIntBig GetFeatureCount( int bForce = TRUE );
679  virtual int TestCapability( const char * pszCap );
680  virtual OGRErr CreateField( OGRFieldDefn *poField, int bApproxOK = TRUE );
681  virtual OGRErr CreateGeomField( OGRGeomFieldDefn *poField,
682  int bApproxOK = TRUE );
683  virtual const char *GetFIDColumn();
684  virtual const char *GetGeometryColumn();
685  virtual OGRSpatialReference *GetSpatialRef();
686 
687  // OGRGNMWrappedResultLayer
688  virtual OGRErr InsertFeature(OGRFeature* poFeature,
689  const CPLString &soLayerName, int nPathNo,
690  bool bIsEdge);
691 protected:
692  virtual OGRErr ISetFeature( OGRFeature *poFeature );
693  virtual OGRErr ICreateFeature( OGRFeature *poFeature );
694 protected:
695  GDALDataset *poDS;
696  OGRLayer *poLayer;
697 };
698 
699 #endif // GNM
GNM layer which represents a geography network layer of generic format.
Definition: gnm.h:473
virtual void FlushCache(void)
Flush all write cached data to disk.
Definition: gdaldataset.cpp:382
Concrete representation of a multi-vertex line.
Definition: ogr_geometry.h:527
Definition of a geometry field of an OGRFeatureDefn.
Definition: ogr_feature.h:143
virtual const char * GetProjectionRef(void)
Fetch the projection definition string for this dataset.
Definition: gdaldataset.cpp:811
GNM class which represents a geography network of generic format.
Definition: gnm.h:182
virtual OGRFeature * GetFeatureByGlobalFID(GNMGFID nGFID)=0
GetFeatureByGlobalFID search all network layers for given feature identificator.
Convenient string class based on std::string.
Definition: cpl_string.h:283
Definition of a feature class or feature layer.
Definition: ogr_feature.h:206
virtual CPLErr Delete()=0
Delete network.
virtual OGRLayer * GetLayer(int)
Fetch a layer by index.
Definition: gdaldataset.cpp:5820
virtual OGRErr DeleteLayer(int)
Delete the indicated layer from the datasource.
Definition: gdaldataset.cpp:4377
Definition of an attribute of an OGRFeatureDefn.
Definition: ogr_feature.h:62
virtual OGRLayer * CopyLayer(OGRLayer *poSrcLayer, const char *pszNewName, char **papszOptions=NULL)
Duplicate an existing layer.
Definition: gdaldataset.cpp:4022
OGRwkbGeometryType
List of well known binary geometry types.
Definition: ogr_core.h:333
A collection of OGRLineString.
Definition: ogr_geometry.h:1292
The simple graph class, which holds the appropriate for calculations graph in memory (based on STL co...
Definition: gnmgraph.h:74
Abstract base class for all geometry classes.
Definition: ogr_geometry.h:104
Classes related to registration of format support, and opening datasets.
Definition: gdal_priv.h:238
General GNM class which represents a geography network of common format.
Definition: gnm.h:70
virtual int CloseDependentDatasets()
Drop references to any other datasets referenced by this dataset.
Definition: gdaldataset.cpp:3288
virtual int GetVersion() const
GetVersion return the network version if applicable.
Definition: gnm.h:129
virtual char ** GetFileList(void)
Fetch files forming dataset.
Definition: gdaldataset.cpp:2311
This class represents an OpenGIS Spatial Reference System, and contains methods for converting betwee...
Definition: ogr_spatialref.h:129
virtual CPLErr Create(const char *pszFilename, char **papszOptions)=0
Create network system layers.
The simple class for rules.
Definition: gnm.h:608
virtual OGRLayer * GetPath(GNMGFID nStartFID, GNMGFID nEndFID, GNMGraphAlgorithmType eAlgorithm, char **papszOptions)=0
Create path between start and end GFIDs.
Point class.
Definition: ogr_geometry.h:264
Simple container for a bounding region.
Definition: ogr_core.h:48
This class represents a layer of simple features, with access methods.
Definition: ogrsf_frmts.h:66
A simple feature, including geometry and attributes.
Definition: ogr_feature.h:274
This class represents a style table.
Definition: ogr_featurestyle.h:82
virtual CPLErr DisconnectAll()=0
DisconnectAll method clears the network graph.
A set of associated raster bands, usually from one file.
Definition: gdal_priv.h:296
virtual int GetLayerCount()
Get the number of layers in this dataset.
Definition: gdaldataset.cpp:5795
Definition: ogr_attrind.h:66
The OGRGNMWrappedResultLayer class for search paths queries results.
Definition: gnm.h:666
virtual int TestCapability(const char *)
Test if capability is available.
Definition: gdaldataset.cpp:5859
Format specific driver.
Definition: gdal_priv.h:1036

Generated for GDAL by doxygen 1.8.11.