GDAL
gnm.h
00001 /******************************************************************************
00002  * $Id: gnm.h 36501 2016-11-25 14:09:24Z rouault $
00003  *
00004  * Project:  GDAL/OGR Geography Network support (Geographic Network Model)
00005  * Purpose:  GNM general public declarations.
00006  * Authors:  Mikhail Gusev (gusevmihs at gmail dot com)
00007  *           Dmitry Baryshnikov, polimax@mail.ru
00008  *
00009  ******************************************************************************
00010  * Copyright (c) 2014, Mikhail Gusev
00011  * Copyright (c) 2014-2015, NextGIS <info@nextgis.com>
00012  *
00013  * Permission is hereby granted, free of charge, to any person obtaining a
00014  * copy of this software and associated documentation files (the "Software"),
00015  * to deal in the Software without restriction, including without limitation
00016  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00017  * and/or sell copies of the Software, and to permit persons to whom the
00018  * Software is furnished to do so, subject to the following conditions:
00019  *
00020  * The above copyright notice and this permission notice shall be included
00021  * in all copies or substantial portions of the Software.
00022  *
00023  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00024  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00025  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00026  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00027  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00028  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00029  * DEALINGS IN THE SOFTWARE.
00030  ****************************************************************************/
00031 
00032 #ifndef GNM
00033 #define GNM
00034 
00035 #include "ogrsf_frmts.h"
00036 #include "gnmgraph.h"
00037 
00038 // Direction of an edge.
00039 typedef int GNMDirection; // We use int values in order to save them to the
00040                           // network data.
00041 
00042 // Network's metadata parameters names.
00043 #define GNM_MD_NAME     "net_name"
00044 #define GNM_MD_DESCR    "net_description"
00045 #define GNM_MD_SRS      "net_srs"
00046 #define GNM_MD_VERSION  "net_version"
00047 #define GNM_MD_RULE     "net_rule"
00048 #define GNM_MD_FORMAT   "FORMAT"
00049 #define GNM_MD_FETCHEDGES   "fetch_edge"
00050 #define GNM_MD_FETCHVERTEX  "fetch_vertex"
00051 #define GNM_MD_NUM_PATHS    "num_paths"
00052 #define GNM_MD_EMITTER   "emitter"
00053 
00054 // TODO: Constants for capabilities.
00055 //#define GNMCanChangeConnections "CanChangeConnections"
00056 
00057 typedef enum
00058 {           GATDijkstraShortestPath = 1,           GATKShortestPath,   GATConnectedComponents
00062 } GNMGraphAlgorithmType;
00063 
00070 class CPL_DLL GNMNetwork : public GDALDataset
00071 {
00072 public:
00073     GNMNetwork();
00074     virtual ~GNMNetwork();
00075 
00076     // GDALDataset Interface
00077     virtual const char *GetProjectionRef(void) CPL_OVERRIDE;
00078     virtual char      **GetFileList(void) CPL_OVERRIDE;
00079 
00080     // GNMNetwork Interface
00081 
00102     virtual CPLErr Create( const char* pszFilename, char** papszOptions ) = 0;
00103 
00109     virtual CPLErr Open( GDALOpenInfo* poOpenInfo ) = 0;
00110 
00115     virtual CPLErr Delete() = 0;
00116 
00123     virtual const char* GetName() const;
00124 
00129     virtual int GetVersion() const { return 0;}
00130 
00135     virtual CPLErr DisconnectAll () = 0;
00136 
00144     virtual OGRFeature *GetFeatureByGlobalFID (GNMGFID nGFID) = 0;
00145 
00156     virtual OGRLayer *GetPath (GNMGFID nStartFID, GNMGFID nEndFID,
00157                      GNMGraphAlgorithmType eAlgorithm, char** papszOptions) = 0;
00158 protected:
00165     virtual int CheckNetworkExist( const char* pszFilename,
00166                                    char** papszOptions ) = 0;
00167 
00168 protected:
00170     CPLString m_soName;
00171     CPLString m_soSRS;
00173 };
00174 
00175 class GNMRule;
00176 class OGRGNMWrappedResultLayer;
00177 
00184 class CPL_DLL GNMGenericNetwork: public GNMNetwork
00185 {
00186 public:
00187     GNMGenericNetwork();
00188     virtual ~GNMGenericNetwork();
00189 
00190     // GDALDataset Interface
00191 
00192     virtual int         GetLayerCount() CPL_OVERRIDE;
00193     virtual OGRLayer    *GetLayer(int) CPL_OVERRIDE;
00194     virtual OGRErr      DeleteLayer(int) CPL_OVERRIDE;
00195 
00196     virtual int         TestCapability( const char * ) CPL_OVERRIDE;
00197 
00198     virtual OGRLayer   *CopyLayer( OGRLayer *poSrcLayer,
00199                                    const char *pszNewName,
00200                                    char **papszOptions = NULL ) CPL_OVERRIDE;
00201 
00202     virtual int CloseDependentDatasets() CPL_OVERRIDE;
00203     virtual void FlushCache(void) CPL_OVERRIDE;
00204 
00205     // GNMNetwork Interface
00206 
00207     virtual CPLErr Create( const char* pszFilename, char** papszOptions ) CPL_OVERRIDE = 0;
00208     virtual CPLErr Delete() CPL_OVERRIDE;
00209 
00210     virtual int GetVersion() const CPL_OVERRIDE;
00215     virtual GNMGFID GetNewGlobalFID();
00216 
00223     virtual CPLString GetAlgorithmName(GNMDirection eAlgorithm, bool bShortName);
00224 
00232     virtual CPLErr AddFeatureGlobalFID(GNMGFID nFID, const char* pszLayerName);
00233 
00247     virtual CPLErr ConnectFeatures (GNMGFID nSrcFID,
00248                                     GNMGFID nTgtFID,
00249                                     GNMGFID nConFID = -1,
00250                                     double dfCost = 1,
00251                                     double dfInvCost = 1,
00252                                     GNMDirection eDir = GNM_EDGE_DIR_BOTH);
00253 
00261     virtual CPLErr DisconnectFeatures (GNMGFID nSrcFID,
00262                                        GNMGFID nTgtFID,
00263                                        GNMGFID nConFID);
00264 
00271     virtual CPLErr DisconnectFeaturesWithId(GNMGFID nFID);
00272 
00285     virtual CPLErr ReconnectFeatures (GNMGFID nSrcFID,
00286                                       GNMGFID nTgtFID,
00287                                       GNMGFID nConFID,
00288                                       double dfCost = 1,
00289                                       double dfInvCost = 1,
00290                                       GNMDirection eDir = GNM_EDGE_DIR_BOTH);
00291 
00292     virtual CPLErr DisconnectAll() CPL_OVERRIDE;
00293 
00294     virtual OGRFeature *GetFeatureByGlobalFID(GNMGFID nFID) CPL_OVERRIDE;
00295 
00330     virtual CPLErr CreateRule (const char *pszRuleStr);
00331 
00336     virtual CPLErr DeleteAllRules();
00337 
00343     virtual CPLErr DeleteRule(const char *pszRuleStr);
00344 
00349     virtual char** GetRules() const;
00350 
00375     virtual CPLErr ConnectPointsByLines (char **papszLayerList,
00376                                          double dfTolerance,
00377                                          double dfCost,
00378                                          double dfInvCost,
00379                                          GNMDirection eDir);
00380 
00387     virtual CPLErr ChangeBlockState (GNMGFID nFID, bool bIsBlock);
00388 
00397     virtual CPLErr ChangeAllBlockState (bool bIsBlock = false);
00398 
00399     virtual OGRLayer *GetPath (GNMGFID nStartFID, GNMGFID nEndFID,
00400                      GNMGraphAlgorithmType eAlgorithm, char** papszOptions) CPL_OVERRIDE;
00401 protected:
00408     virtual CPLErr CheckLayerDriver(const char* pszDefaultDriverName,
00409                                  char** papszOptions);
00415     virtual bool CheckStorageDriverSupport(const char* pszDriverName) = 0;
00416 protected:
00418     virtual CPLErr CreateMetadataLayer( GDALDataset* const pDS, int nVersion,
00419                                      size_t nFieldSize = 1024 );
00420     virtual CPLErr StoreNetworkSrs();
00421     virtual CPLErr LoadNetworkSrs();
00422     virtual CPLErr CreateGraphLayer( GDALDataset* const pDS );
00423     virtual CPLErr CreateFeaturesLayer( GDALDataset* const pDS );
00424     virtual CPLErr LoadMetadataLayer( GDALDataset* const pDS );
00425     virtual CPLErr LoadGraphLayer( GDALDataset* const pDS );
00426     virtual CPLErr LoadGraph();
00427     virtual CPLErr LoadFeaturesLayer( GDALDataset* const pDS );
00428     virtual CPLErr DeleteMetadataLayer() = 0;
00429     virtual CPLErr DeleteGraphLayer() = 0;
00430     virtual CPLErr DeleteFeaturesLayer() = 0;
00431     virtual CPLErr LoadNetworkLayer(const char* pszLayername) = 0;
00432     virtual CPLErr DeleteNetworkLayers() = 0;
00433     virtual void ConnectPointsByMultiline(GIntBig nFID,
00434                                   const OGRMultiLineString *poMultiLineString,
00435                                   const std::vector<OGRLayer *> &paPointLayers,
00436                                   double dfTolerance, double dfCost,
00437                                   double dfInvCost, GNMDirection eDir);
00438     virtual void ConnectPointsByLine(GIntBig nFID,
00439                                      const OGRLineString *poLineString,
00440                                      const std::vector<OGRLayer *> &paPointLayers,
00441                                      double dfTolerance, double dfCost,
00442                                      double dfInvCost, GNMDirection eDir);
00443     virtual GNMGFID FindNearestPoint(const OGRPoint* poPoint,
00444                                      const std::vector<OGRLayer*>& paPointLayers,
00445                                      double dfTolerance);
00446     virtual OGRFeature* FindConnection(GNMGFID nSrcFID, GNMGFID nTgtFID,
00447                                        GNMGFID nConFID);
00448     virtual void SaveRules();
00449     virtual GNMGFID GetNewVirtualFID();
00450     virtual void FillResultLayer(OGRGNMWrappedResultLayer* poResLayer,
00451                                  const GNMPATH &path, int nNoOfPath,
00452                                  bool bReturnVertices, bool bReturnEdges);
00454 protected:
00456     int m_nVersion;
00457     GNMGFID m_nGID;
00458     GNMGFID m_nVirtualConnectionGID;
00459     OGRLayer* m_poMetadataLayer;
00460     OGRLayer* m_poGraphLayer;
00461     OGRLayer* m_poFeaturesLayer;
00462 
00463     GDALDriver *m_poLayerDriver;
00464 
00465     std::map<GNMGFID, CPLString> m_moFeatureFIDMap;
00466     std::vector<OGRLayer*> m_apoLayers;
00467     std::vector<GNMRule> m_asRules;
00468     bool m_bIsRulesChanged;
00469 
00470     GNMGraph m_oGraph;
00471     bool m_bIsGraphLoaded;
00473 };
00474 
00482 class GNMGenericLayer : public OGRLayer
00483 {
00484 public:
00485     GNMGenericLayer(OGRLayer* poLayer, GNMGenericNetwork* poNetwork);
00486     virtual ~GNMGenericLayer();
00487 
00488     // OGRLayer Interface
00489 
00490     virtual OGRGeometry *GetSpatialFilter() CPL_OVERRIDE;
00491     virtual void        SetSpatialFilter( OGRGeometry * ) CPL_OVERRIDE;
00492     virtual void        SetSpatialFilterRect( double dfMinX, double dfMinY,
00493                                               double dfMaxX, double dfMaxY ) CPL_OVERRIDE;
00494 
00495     virtual void        SetSpatialFilter( int iGeomField, OGRGeometry * ) CPL_OVERRIDE;
00496     virtual void        SetSpatialFilterRect( int iGeomField,
00497                                             double dfMinX, double dfMinY,
00498                                             double dfMaxX, double dfMaxY ) CPL_OVERRIDE;
00499 
00500     virtual OGRErr      SetAttributeFilter( const char * ) CPL_OVERRIDE;
00501 
00502     virtual void        ResetReading() CPL_OVERRIDE;
00503     virtual OGRFeature *GetNextFeature() CPL_OVERRIDE;
00504     virtual OGRErr      SetNextByIndex( GIntBig nIndex ) CPL_OVERRIDE;
00505 
00506     virtual OGRErr      DeleteFeature( GIntBig nFID ) CPL_OVERRIDE;
00507 
00508     virtual const char *GetName() CPL_OVERRIDE;
00509     virtual OGRwkbGeometryType GetGeomType() CPL_OVERRIDE;
00510     virtual OGRFeatureDefn *GetLayerDefn() CPL_OVERRIDE;
00511     virtual int         FindFieldIndex( const char *pszFieldName, int bExactMatch ) CPL_OVERRIDE;
00512 
00513     virtual OGRSpatialReference *GetSpatialRef() CPL_OVERRIDE;
00514 
00515     virtual GIntBig     GetFeatureCount( int bForce = TRUE ) CPL_OVERRIDE;
00516     virtual OGRErr      GetExtent(OGREnvelope *psExtent, int bForce = TRUE) CPL_OVERRIDE;
00517     virtual OGRErr      GetExtent(int iGeomField, OGREnvelope *psExtent,
00518                                   int bForce = TRUE) CPL_OVERRIDE;
00519 
00520     virtual int         TestCapability( const char * ) CPL_OVERRIDE;
00521 
00522     virtual OGRErr      CreateField( OGRFieldDefn *poField,
00523                                      int bApproxOK = TRUE ) CPL_OVERRIDE;
00524     virtual OGRErr      DeleteField( int iField ) CPL_OVERRIDE;
00525     virtual OGRErr      ReorderFields( int* panMap ) CPL_OVERRIDE;
00526     virtual OGRErr      AlterFieldDefn( int iField, OGRFieldDefn* poNewFieldDefn,
00527                                         int nFlagsIn ) CPL_OVERRIDE;
00528 
00529     virtual OGRErr      CreateGeomField( OGRGeomFieldDefn *poField,
00530                                      int bApproxOK = TRUE ) CPL_OVERRIDE;
00531 
00532     virtual OGRErr      SyncToDisk() CPL_OVERRIDE;
00533 
00534     virtual OGRStyleTable *GetStyleTable() CPL_OVERRIDE;
00535     virtual void        SetStyleTableDirectly( OGRStyleTable *poStyleTable ) CPL_OVERRIDE;
00536 
00537     virtual void        SetStyleTable(OGRStyleTable *poStyleTable) CPL_OVERRIDE;
00538 
00539     virtual OGRErr      StartTransaction() CPL_OVERRIDE;
00540     virtual OGRErr      CommitTransaction() CPL_OVERRIDE;
00541     virtual OGRErr      RollbackTransaction() CPL_OVERRIDE;
00542 
00543     virtual const char *GetFIDColumn() CPL_OVERRIDE;
00544     virtual const char *GetGeometryColumn() CPL_OVERRIDE;
00545 
00546     virtual OGRErr      SetIgnoredFields( const char **papszFields ) CPL_OVERRIDE;
00547 
00549     OGRErr              Intersection( OGRLayer *pLayerMethod,
00550                                       OGRLayer *pLayerResult,
00551                                       char** papszOptions = NULL,
00552                                       GDALProgressFunc pfnProgress = NULL,
00553                                       void * pProgressArg = NULL );
00555     OGRErr              Union( OGRLayer *pLayerMethod,
00556                                OGRLayer *pLayerResult,
00557                                char** papszOptions = NULL,
00558                                GDALProgressFunc pfnProgress = NULL,
00559                                void * pProgressArg = NULL );
00561     OGRErr              SymDifference( OGRLayer *pLayerMethod,
00562                                        OGRLayer *pLayerResult,
00563                                        char** papszOptions,
00564                                        GDALProgressFunc pfnProgress,
00565                                        void * pProgressArg );
00567     OGRErr              Identity( OGRLayer *pLayerMethod,
00568                                   OGRLayer *pLayerResult,
00569                                   char** papszOptions = NULL,
00570                                   GDALProgressFunc pfnProgress = NULL,
00571                                   void * pProgressArg = NULL );
00573     OGRErr              Update( OGRLayer *pLayerMethod,
00574                                 OGRLayer *pLayerResult,
00575                                 char** papszOptions = NULL,
00576                                 GDALProgressFunc pfnProgress = NULL,
00577                                 void * pProgressArg = NULL );
00579     OGRErr              Clip( OGRLayer *pLayerMethod,
00580                               OGRLayer *pLayerResult,
00581                               char** papszOptions = NULL,
00582                               GDALProgressFunc pfnProgress = NULL,
00583                               void * pProgressArg = NULL );
00585     OGRErr              Erase( OGRLayer *pLayerMethod,
00586                                OGRLayer *pLayerResult,
00587                                char** papszOptions = NULL,
00588                                GDALProgressFunc pfnProgress = NULL,
00589                                void * pProgressArg = NULL );
00590 
00592     GIntBig             GetFeaturesRead();
00593 
00595     int                 AttributeFilterEvaluationNeedsGeometry();
00596 
00598     /* consider these private */
00599     OGRErr               InitializeIndexSupport( const char * );
00600     OGRLayerAttrIndex   *GetIndex();
00602 
00603 protected:
00605     virtual OGRErr      ISetFeature( OGRFeature *poFeature ) CPL_OVERRIDE;
00606     virtual OGRErr      ICreateFeature( OGRFeature *poFeature ) CPL_OVERRIDE;
00607 
00608 protected:
00609     CPLString m_soLayerName;
00610     OGRLayer *m_poLayer;
00611     GNMGenericNetwork* m_poNetwork;
00612     std::map<GNMGFID, GIntBig> m_mnFIDMap;
00614 };
00615 
00616 typedef enum
00617 {   GRTConnection = 0
00619 } GNMRuleType;
00620 
00630 class CPL_DLL GNMRule
00631 {
00632 public:
00634     GNMRule();
00636     explicit GNMRule(const std::string &oRule );
00638     explicit GNMRule(const char* pszRule);
00640     GNMRule(const GNMRule &oRule);
00641     virtual ~GNMRule();
00646     virtual bool IsValid() const;
00651     virtual bool IsAcceptAny() const;
00657     virtual GNMRuleType GetType() const;
00666     virtual bool CanConnect(const CPLString &soSrcLayerName,
00667                             const CPLString &soTgtLayerName,
00668                             const CPLString &soConnLayerName = "");
00670     virtual CPLString GetSourceLayerName() const;
00672     virtual CPLString GetTargetLayerName() const;
00674     virtual CPLString GetConnectorLayerName() const;
00676     const char* c_str() const;
00678     operator const char* (void) const;
00679 protected:
00681     virtual bool ParseRuleString();
00682 protected:
00683     CPLString m_soSrcLayerName;
00684     CPLString m_soTgtLayerName;
00685     CPLString m_soConnLayerName;
00686     bool m_bAllow;
00687     bool m_bValid;
00688     bool m_bAny;
00689     CPLString m_soRuleString;
00691 };
00692 
00699 class OGRGNMWrappedResultLayer : public OGRLayer
00700 {
00701 public:
00702     OGRGNMWrappedResultLayer(GDALDataset* poDS, OGRLayer* poLayer);
00703     ~OGRGNMWrappedResultLayer();
00704 
00705     // OGRLayer
00706     virtual void ResetReading() CPL_OVERRIDE;
00707     virtual OGRFeature *GetNextFeature() CPL_OVERRIDE;
00708     virtual OGRErr SetNextByIndex( GIntBig nIndex ) CPL_OVERRIDE;
00709     virtual OGRFeature *GetFeature( GIntBig nFID ) CPL_OVERRIDE;
00710     virtual OGRFeatureDefn *GetLayerDefn() CPL_OVERRIDE;
00711     virtual GIntBig GetFeatureCount( int bForce = TRUE ) CPL_OVERRIDE;
00712     virtual int TestCapability( const char * pszCap ) CPL_OVERRIDE;
00713     virtual OGRErr CreateField( OGRFieldDefn *poField, int bApproxOK = TRUE ) CPL_OVERRIDE;
00714     virtual OGRErr      CreateGeomField( OGRGeomFieldDefn *poField,
00715                                      int bApproxOK = TRUE ) CPL_OVERRIDE;
00716     virtual const char *GetFIDColumn() CPL_OVERRIDE;
00717     virtual const char *GetGeometryColumn() CPL_OVERRIDE;
00718     virtual OGRSpatialReference *GetSpatialRef() CPL_OVERRIDE;
00719 
00720     // OGRGNMWrappedResultLayer
00721     virtual OGRErr InsertFeature(OGRFeature* poFeature,
00722                                 const CPLString &soLayerName, int nPathNo,
00723                                 bool bIsEdge);
00724 protected:
00725     virtual OGRErr      ISetFeature( OGRFeature *poFeature ) CPL_OVERRIDE;
00726     virtual OGRErr      ICreateFeature( OGRFeature *poFeature ) CPL_OVERRIDE;
00727 protected:
00729     GDALDataset *poDS;
00730     OGRLayer    *poLayer;
00732 };
00733 
00734 #endif // GNM

Generated for GDAL by doxygen 1.7.6.1.