1 /******************************************************************************
2  * $Id: ogrgrass.h a832da5b936bac8438f9c50e7c20e563fd94c9ff 2020-09-22 13:06:27 +0200 Markus Neteler $
3  *
4  * Project:  OpenGIS Simple Features Reference Implementation
5  * Purpose:  Private definitions for OGR/GRASS driver.
6  * Author:   Radim Blazek, radim.blazek@gmail.com
7  *
8  ******************************************************************************
9  * Copyright (c) 2005, Radim Blazek <radim.blazek@gmail.com>
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included
19  * in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  ****************************************************************************/
29 
30 #ifndef OGRGRASS_H_INCLUDED
31 #define OGRGRASS_H_INCLUDED
32 
33 #include "ogrsf_frmts.h"
34 
35 extern "C" {
36     #include <grass/version.h>
37     #include <grass/gprojects.h>
38     #include <grass/gis.h>
39     #include <grass/dbmi.h>
40     #include <grass/vector.h>
41 }
42 
43 /************************************************************************/
44 /*                            OGRGRASSLayer                             */
45 /************************************************************************/
46 class OGRGRASSLayer final: public OGRLayer
47 {
48   public:
49                         OGRGRASSLayer( int layer, struct Map_info * map );
50                         virtual ~OGRGRASSLayer();
51 
52     // Layer info
GetLayerDefn()53     OGRFeatureDefn *    GetLayerDefn() override { return poFeatureDefn; }
54     GIntBig             GetFeatureCount( int ) override;
55     OGRErr              GetExtent(OGREnvelope *psExtent, int bForce) override;
GetExtent(int iGeomField,OGREnvelope * psExtent,int bForce)56     virtual OGRErr      GetExtent(int iGeomField, OGREnvelope *psExtent, int bForce) override
57                 { return OGRLayer::GetExtent(iGeomField, psExtent, bForce); }
58     virtual OGRSpatialReference *GetSpatialRef() override;
59     int                 TestCapability( const char * ) override;
60 
61     // Reading
62     void                ResetReading() override;
63     virtual OGRErr      SetNextByIndex( GIntBig nIndex ) override;
64     OGRFeature *        GetNextFeature() override;
65     OGRFeature         *GetFeature( GIntBig nFeatureId ) override;
66 
67     // Filters
68     virtual OGRErr      SetAttributeFilter( const char *query ) override;
69     virtual void        SetSpatialFilter( OGRGeometry * poGeomIn ) override;
SetSpatialFilter(int iGeomField,OGRGeometry * poGeom)70     virtual void        SetSpatialFilter( int iGeomField, OGRGeometry *poGeom ) override
71                 { OGRLayer::SetSpatialFilter(iGeomField, poGeom); }
72 
73   private:
74     char                *pszName;
75     OGRSpatialReference *poSRS;
76     OGRFeatureDefn      *poFeatureDefn;
77     char                *pszQuery;      // Attribute filter string
78 
79     int                 iNextId;
80     int                 nTotalCount;
81     int                 iLayer;         // Layer number
82     int                 iLayerIndex;    // Layer index (in GRASS category index)
83     int                 iCatField;      // Field where category (key) is stored
84     int                 nFields;
85     int                 *paFeatureIndex; // Array of indexes to category index array
86 
87     // Vector map
88     struct Map_info     *poMap;
89     struct field_info   *poLink;
90 
91     // Database connection
92     bool                bHaveAttributes;
93 
94     dbString            *poDbString;
95     dbDriver            *poDriver;
96     dbCursor            *poCursor;
97 
98     bool                bCursorOpened;  // Sequential database cursor opened
99     int                 iCurrentCat;    // Current category in select cursor
100 
101     struct line_pnts    *poPoints;
102     struct line_cats    *poCats;
103 
104     bool                StartDbDriver ();
105     bool                StopDbDriver ();
106 
107     OGRGeometry         *GetFeatureGeometry ( long nFeatureId, int *cat );
108     bool                SetAttributes ( OGRFeature *feature, dbTable *table );
109 
110     // Features matching spatial filter for ALL features/elements in GRASS
111     char                *paSpatialMatch;
112     bool                SetSpatialMatch();
113 
114     // Features matching attribute filter for ALL features/elements in GRASS
115     char                *paQueryMatch;
116     bool                OpenSequentialCursor();
117     bool                ResetSequentialCursor();
118     bool                SetQueryMatch();
119 };
120 
121 /************************************************************************/
122 /*                          OGRGRASSDataSource                          */
123 /************************************************************************/
124 class OGRGRASSDataSource final: public OGRDataSource
125 {
126   public:
127                         OGRGRASSDataSource();
128                         virtual ~OGRGRASSDataSource();
129 
130     int                 Open( const char *, int bUpdate, int bTestOpen,
131                               int bSingleNewFile = FALSE );
132 
GetName()133     const char          *GetName() override { return pszName; }
GetLayerCount()134     int                 GetLayerCount() override { return nLayers; }
135     OGRLayer            *GetLayer( int ) override;
136 
137     int                 TestCapability( const char * ) override;
138 
139   private:
140     OGRGRASSLayer     **papoLayers;
141     char                *pszName;       // Date source name
142     char                *pszGisdbase;   // GISBASE
143     char                *pszLocation;   // location name
144     char                *pszMapset;     // mapset name
145     char                *pszMap;        // name of vector map
146 
147     struct Map_info     map;
148     int                 nLayers;
149 
150     int                 bOpened;
151 
152     static bool SplitPath ( char *, char **, char **, char **, char ** );
153 };
154 
155 /************************************************************************/
156 /*                            OGRGRASSDriver                            */
157 /************************************************************************/
158 class OGRGRASSDriver final: public OGRSFDriver
159 {
160   public:
161                         virtual ~OGRGRASSDriver();
162 
163     const char          *GetName() override;
164     OGRDataSource       *Open( const char *, int ) override;
165 
166     int                 TestCapability( const char * ) override;
167 };
168 
169 #endif /* ndef OGRGRASS_H_INCLUDED */
170