~ubuntu-branches/ubuntu/quantal/qgis/quantal

« back to all changes in this revision

Viewing changes to providers/grass/qgsgrassprovider.h

  • Committer: Bazaar Package Importer
  • Author(s): Steve Halasz
  • Date: 2005-11-05 16:04:45 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20051105160445-l0g4isz5bc9yehet
Tags: 0.7.4-1
* New upstream release
* Build GRASS support in qgis-plugin-grass package (Closes: #248649)

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
 */
46
46
 
47
47
/* Attributes. Cache of database attributes (because selection from database is slow). */
48
 
struct GATT {
49
 
    int cat;       // category
50
 
    char **values; // pointer to array of pointers to values
 
48
struct GATT
 
49
{
 
50
  int cat;       // category
 
51
  char **values; // pointer to array of pointers to values
51
52
};
52
53
 
53
54
/* Grass layer (unique vector+field). */
54
 
struct GLAYER {
55
 
    QString path;                  // path to the layer gisdbase+location+mapset+mapName
56
 
    int     field;                 // field number
57
 
    bool    valid;                 // valid is true if layer is opened, once the layer is closed, 
58
 
                                   // valid is set to false and no more used
59
 
    int     mapId;                 // map ID in maps vector
60
 
    struct  Map_info   *map;       // map header
61
 
    struct  field_info *fieldInfo; // field info
62
 
    int     nColumns;              // number of columns in database table, if 0, attributes are not available
63
 
                                   // and category (column name 'cat') is used instead
64
 
    int     keyColumn;             // number of key column
65
 
    std::vector<QgsField> fields;  // description of layer fields
66
 
    int     nAttributes;           // number of attributes read to the memory (may be < nRecords)
67
 
    GATT    *attributes;           // vector of attributes
68
 
    double  (*minmax)[2];          // minimum and maximum values of attributes
69
 
    int     nUsers;                // number of instances using this layer, increased by open(), 
70
 
                                   // decreased by close()
 
55
struct GLAYER
 
56
{
 
57
  QString path;                  // path to the layer gisdbase+location+mapset+mapName
 
58
  int     field;                 // field number
 
59
  bool    valid;                 // valid is true if layer is opened, once the layer is closed,
 
60
  // valid is set to false and no more used
 
61
  int     mapId;                 // map ID in maps vector
 
62
  struct  Map_info   *map;       // map header
 
63
  struct  field_info *fieldInfo; // field info
 
64
  int     nColumns;              // number of columns in database table, if 0, attributes are not available
 
65
  // and category (column name 'cat') is used instead
 
66
  int     keyColumn;             // number of key column
 
67
  std::vector<QgsField> fields;  // description of layer fields
 
68
  int     nAttributes;           // number of attributes read to the memory (may be < nRecords)
 
69
  GATT    *attributes;           // vector of attributes
 
70
  double  (*minmax)[2];          // minimum and maximum values of attributes
 
71
  int     nUsers;                // number of instances using this layer, increased by open(),
 
72
  // decreased by close()
71
73
};
72
74
 
73
75
/* Grass vector map. */
74
 
struct GMAP {
75
 
    QString gisdbase;      // map gisdabase
76
 
    QString location;      // map location name (not path!)
77
 
    QString mapset;        // map mapset
78
 
    QString mapName;       // map name
79
 
    QString path;          // path to the layer gisdbase+location+mapset+mapName
80
 
    bool    valid;         // true if map is opened, once the map is closed, 
81
 
                           // valid is set to false and no more used
82
 
    struct  Map_info *map; // map header
83
 
    int     nUsers;        // number layers using this map
84
 
    int     update;        // true if the map is opened in update mode -> disabled standard reading
85
 
                           // through getNextFeature(), featureCount() returns 0
86
 
    QDateTime lastModified; // lastModified time of the vector directory, when the map was opened
87
 
    int     version;       // version, increased by each closeEdit() and updateMap()
 
76
struct GMAP
 
77
{
 
78
  QString gisdbase;      // map gisdabase
 
79
  QString location;      // map location name (not path!)
 
80
  QString mapset;        // map mapset
 
81
  QString mapName;       // map name
 
82
  QString path;          // path to the layer gisdbase+location+mapset+mapName
 
83
  bool    valid;         // true if map is opened, once the map is closed,
 
84
  // valid is set to false and no more used
 
85
  struct  Map_info *map; // map header
 
86
  int     nUsers;        // number layers using this map
 
87
  int     update;        // true if the map is opened in update mode -> disabled standard reading
 
88
  // through getNextFeature(), featureCount() returns 0
 
89
  QDateTime lastModified; // last modified time of the vector directory, when the map was opened
 
90
  QDateTime lastAttributesModified; // last modified time of the vector 'dbln' file, when the map was opened
 
91
                            // or attributes were updated. The 'dbln' file is updated by v.to.db etc.
 
92
                            // when attributes are changed
 
93
  int     version;       // version, increased by each closeEdit() and updateMap()
88
94
};
89
95
 
90
96
/**
91
97
  \class QgsGrassProvider
92
98
  \brief Data provider for GRASS vectors
93
99
*/
94
 
class QgsGrassProvider : public QgsVectorDataProvider {
 
100
class QgsGrassProvider : public QgsVectorDataProvider
 
101
{
95
102
public:
96
 
        QgsGrassProvider(QString uri=0);
97
 
        virtual ~QgsGrassProvider();
98
 
 
99
 
        /**
100
 
        * Get the first feature resulting from a select operation
101
 
        * @return QgsFeature
102
 
        */
103
 
        QgsFeature * getFirstFeature(bool fetchAttributes=false);
104
 
 
105
 
        /** 
106
 
        * Get the next feature resutling from a select operation
107
 
        * @return QgsFeature
108
 
        */
109
 
        QgsFeature * getNextFeature(bool fetchAttributes=false);
110
 
        bool getNextFeature(QgsFeature &feature, bool fetchAttributes=false);
111
 
        QgsFeature* getNextFeature(std::list<int>& attlist);
112
 
        
113
 
        /** 
114
 
        * Get the feature type as defined in WKBTYPE (qgis.h). 
115
 
        * @return int representing the feature type
116
 
        */
117
 
        int geometryType();
118
 
 
119
 
        /** 
120
 
        * Get the number of features in the layer
121
 
        */
122
 
        long featureCount();
123
 
 
124
 
        /** 
125
 
        * Get the number of fields in the layer
126
 
        */
127
 
        int fieldCount();
128
 
 
129
 
        /**
130
 
        * Select features based on a bounding rectangle. Features can be retrieved 
131
 
        * with calls to getFirstFeature and getNextFeature.
132
 
        * @param mbr QgsRect containing the extent to use in selecting features
133
 
        */
134
 
        void select(QgsRect *mbr, bool useIntersect=false);
135
 
 
136
 
        /** 
137
 
        * Set the data source specification. This may be a path or database
138
 
        * connection string
139
 
        * @uri data source specification
140
 
        */
141
 
        void setDataSourceUri(QString uri);
142
 
        
143
 
        /** 
144
 
        * Get the data source specification. This may be a path or database
145
 
        * connection string
146
 
        * @return data source specification
147
 
        */
148
 
        QString getDataSourceUri();
149
 
        
150
 
        /**
151
 
        * Identify features within the search radius specified by rect
152
 
        * @param rect Bounding rectangle of search radius
153
 
        * @return std::vector containing QgsFeature objects that intersect rect
154
 
        */
155
 
        virtual std::vector<QgsFeature>& identify(QgsRect *rect);
156
 
 
157
 
        /** Return endian-ness for this layer
158
 
        */      
159
 
        int endian();
160
 
 
161
 
        /** Return the extent for this data layer
162
 
        */
163
 
        virtual QgsRect *extent();
164
 
 
165
 
        /**
166
 
        * Get the field information for the layer
167
 
        */
168
 
        std::vector<QgsField>& fields();
 
103
 
 
104
  QgsGrassProvider(QString uri=0);
 
105
 
 
106
  virtual ~QgsGrassProvider();
 
107
 
 
108
  /** Used to ask the layer for its projection as a WKT string. Implements
 
109
   * virtual method of same name in QgsDataProvider. */
 
110
  QString getProjectionWKT(void);
 
111
 
 
112
  /* Following functions work only until first edit operation! (category index used) */
 
113
 
 
114
  /**
 
115
   * Get the first feature resulting from a select operation
 
116
   * @return QgsFeature
 
117
   */
 
118
  QgsFeature * getFirstFeature(bool fetchAttributes=false);
 
119
 
 
120
  /** 
 
121
   * Get the next feature resutling from a select operation
 
122
   * @return QgsFeature
 
123
   */
 
124
  QgsFeature * getNextFeature(bool fetchAttributes=false);
 
125
  bool getNextFeature(QgsFeature &feature, bool fetchAttributes=false);
 
126
  QgsFeature* getNextFeature(std::list<int> const & attlist);
 
127
        
 
128
  /** 
 
129
   * Get the feature type as defined in WKBTYPE (qgis.h). 
 
130
   * @return int representing the feature type
 
131
   */
 
132
  int geometryType() const;
 
133
 
 
134
  /** 
 
135
   * Get the number of features in the layer
 
136
   */
 
137
  long featureCount() const;
 
138
 
 
139
  /** 
 
140
   * Get the number of fields in the layer
 
141
   */
 
142
  int fieldCount() const;
 
143
 
 
144
  /**
 
145
   * Select features based on a bounding rectangle. Features can be retrieved 
 
146
   * with calls to getFirstFeature and getNextFeature.
 
147
   * @param mbr QgsRect containing the extent to use in selecting features
 
148
   */
 
149
  void select(QgsRect *mbr, bool useIntersect=false);
 
150
 
 
151
  /** 
 
152
   * Set the data source specification. This may be a path or database
 
153
   * connection string
 
154
   * @uri data source specification
 
155
   */
 
156
  void setDataSourceUri(QString uri);
 
157
        
 
158
  /** 
 
159
   * Get the data source specification. This may be a path or database
 
160
   * connection string
 
161
   * @return data source specification
 
162
   */
 
163
  QString getDataSourceUri();
 
164
        
 
165
  /**
 
166
   * Identify features within the search radius specified by rect
 
167
   * @param rect Bounding rectangle of search radius
 
168
   * @return std::vector containing QgsFeature objects that intersect rect
 
169
   */
 
170
  virtual std::vector<QgsFeature>& identify(QgsRect *rect);
 
171
 
 
172
  /** Return the extent for this data layer
 
173
   */
 
174
  virtual QgsRect *extent();
 
175
 
 
176
  /**
 
177
   * Get the field information for the layer
 
178
   */
 
179
  std::vector<QgsField> const& fields() const;
169
180
         
170
 
        /* Reset the layer - for an OGRLayer, this means clearing the
171
 
        * spatial filter and calling ResetReading
172
 
        */
173
 
        void reset();
174
 
 
175
 
        /**Returns the minimum value of an attribut
176
 
           @param position the number of the attribute*/
177
 
        QString minValue(int position);
178
 
 
179
 
        /**Returns the maximum value of an attribut
180
 
           @param position the number of the attribute*/
181
 
        QString maxValue(int position);
182
 
 
183
 
        /** Update (reload) non static members (marked !UPDATE!) from the static layer and the map.
184
 
        *   This method MUST be called whenever lastUpdate of the map is later then mapLastUpdate 
185
 
        *   of the instance.
186
 
        */
187
 
        void update();
188
 
 
189
 
        /**Returns true if this is a valid layer
190
 
        */
191
 
        bool isValid();
192
 
 
193
 
        // ----------------------------------- Edit ----------------------------------
194
 
        
195
 
        /** Is the layer editable? I.e. the layer is valid and current user is owner of the mapset
196
 
        *   @return true the layer editable
197
 
        *   @return false the is not editable
198
 
        */
199
 
        bool isGrassEditable();
200
 
 
201
 
        /** Returns true if the layer is currently edited (opened in update mode)
202
 
        *   @return true in update mode
203
 
        *   @return false not edited
204
 
        */
205
 
        bool isEdited();
206
 
 
207
 
        /** Start editing. Reopen the vector for update and set GMAP.update = true
208
 
        *   @return true success
209
 
        *   @return false failed to reopen success
210
 
        */
211
 
        bool startEdit();
212
 
 
213
 
        /** Close editing. Rebuild topology, GMAP.update = false 
214
 
        *   @return true success
215
 
        *   @return false failed to close vector or vector was not in update mode
216
 
        */
217
 
        bool closeEdit();
218
 
 
219
 
        /** Get current number of lines. 
220
 
        *   @return number of lines
221
 
        */
222
 
        int numLines ( void );
223
 
 
224
 
        /** Get current number of nodes. 
225
 
        *   @return number of nodes
226
 
        */
227
 
        int numNodes ( void );
228
 
 
229
 
        /** Read line 
230
 
        *   @param Points pointer to existing structure or NULL
231
 
        *   @param Cats pointer to existing structure or NULL
232
 
        *   @param line line number
233
 
        *   @return line type
234
 
        *   @return <0 deadline or error
235
 
        */
236
 
        int readLine ( struct line_pnts * Points, struct line_cats * Cats, int line );
237
 
 
238
 
        /** Read node coordinates 
239
 
        *   @param line line number
240
 
        *   @return true node is alive
241
 
        *   @return false node is dead
242
 
        */
243
 
        bool nodeCoor ( int node, double *x, double *y );
244
 
 
245
 
        /** Read line nodes 
246
 
        *   @param line line number
247
 
        *   @return true line is alive
248
 
        *   @return false line is dead
249
 
        */
250
 
        bool lineNodes ( int line, int *node1, int *node2 );
251
 
 
252
 
        /** Read boundary areas 
253
 
        *   @param line line number
254
 
        *   @return true line is alive
255
 
        *   @return false line is dead
256
 
        */
257
 
        bool lineAreas ( int line, int *left, int *right );
258
 
 
259
 
        /** Get centroid area 
260
 
        *   @param centroid line number
261
 
        *   @return area number (negative for island)
262
 
        */
263
 
        int centroidArea ( int centroid );
264
 
 
265
 
        /** Get number of lines at node 
266
 
        *   @param node node number
267
 
        *   @return number of lines at node (including dead lines)
268
 
        */
269
 
        int nodeNLines ( int node );
270
 
 
271
 
        /** Get line number of line at node for given line index
272
 
        *   @param node node number
273
 
        *   @param idx line index
274
 
        *   @return line number
275
 
        */
276
 
        int nodeLine ( int node, int idx );
277
 
 
278
 
        /** True if line is alive
279
 
        *   @param line line number
280
 
        *   @return true alive
281
 
        *   @return false dead
282
 
        */
283
 
        int lineAlive ( int line );
284
 
 
285
 
        /** True if node is alive
286
 
        *   @param node node number
287
 
        *   @return true alive
288
 
        *   @return false dead
289
 
        */
290
 
        int nodeAlive ( int node );
291
 
 
292
 
        /** Write a new line into vector. 
293
 
        *   @return line number
294
 
        *   @return -1 error
295
 
        */
296
 
        int writeLine ( int type, struct line_pnts *Points, struct line_cats *Cats );
297
 
 
298
 
        /** Rewrite line. 
299
 
        *   @return line number
300
 
        *   @return -1 error
301
 
        */
302
 
        int rewriteLine ( int line, int type, struct line_pnts *Points, struct line_cats *Cats );
303
 
 
304
 
        /** Delete line 
305
 
        *   @return 0 OK
306
 
        *   @return -1 error
307
 
        */
308
 
        int deleteLine ( int line );
309
 
 
310
 
        /** Number of updated lines 
311
 
        */
312
 
        int numUpdatedLines ( void );
313
 
 
314
 
        /** Number of updated nodes 
315
 
        */
316
 
        int numUpdatedNodes ( void );
317
 
 
318
 
        /** Get updated line 
319
 
        */
320
 
        int updatedLine ( int idx );
321
 
 
322
 
        /** Get updated node 
323
 
        */
324
 
        int updatedNode ( int idx );
325
 
 
326
 
        /** Find nearest line 
327
 
        *   @param threshold maximum distance
328
 
        *   @return line number
329
 
        *   @return 0 nothing found
330
 
        */
331
 
        int findLine ( double x, double y, int type, double threshold );
332
 
 
333
 
        /** Find nearest node 
334
 
        *   @param threshold maximum distance
335
 
        *   @return node number
336
 
        *   @return 0 nothing found
337
 
        */
338
 
        int findNode ( double x, double y, double threshold );
339
 
 
340
 
        /** Get columns' definitions
341
 
        *   @param field
342
 
        *   @param cat
343
 
        *   @return vector of attributes
344
 
        */
345
 
        std::vector<QgsField> *columns ( int field );
346
 
 
347
 
        /** Read attributes from DB
348
 
        *   @param field
349
 
        *   @param cat
350
 
        *   @return vector of attributes
351
 
        */
352
 
        std::vector<QgsFeatureAttribute> *attributes ( int field, int cat );
353
 
 
354
 
        /** Key (cat) column name 
355
 
        *   @param field
356
 
        *   @return Key column name or empty string 
357
 
        */
358
 
        QString *key ( int field );
359
 
 
360
 
        /** Get number of db links 
361
 
        *   @return number of links 
362
 
        */
363
 
        int numDbLinks ( void );
364
 
 
365
 
        /** Get db link field
366
 
         *  @param link
367
 
        *   @return field number or 0 
368
 
        */
369
 
        int dbLinkField ( int link );
370
 
 
371
 
        /** Update attributes 
372
 
        *   @param field
373
 
        *   @param cat
374
 
        *   @param update comma separated update string, e.g.: col1 = 5, col2 = 'Val d''Aosta'
375
 
        *   @return empty string or error message 
376
 
        */
377
 
        QString *updateAttributes ( int field, int cat, const QString &values );
378
 
 
379
 
        /** Insert new attributes to the table (it does not check if attributes already exists)
380
 
        *   @param field
381
 
        *   @param cat
382
 
        *   @return empty string or error message 
383
 
        */
384
 
        QString *insertAttributes ( int field, int cat );
385
 
 
386
 
        /** Create table and link vector to this table
387
 
        *   @param field
388
 
        *   @param columns SQL definition for columns, e.g. cat integer, label varchar(10) 
389
 
        *   @return empty string or error message
390
 
        */
391
 
        QString *createTable ( int field, const QString &key, const QString &columns );
392
 
 
393
 
        /** Add column to table
394
 
        *   @param field
395
 
        *   @param column SQL definition for columns, e.g. label varchar(10) 
396
 
        *   @return empty string or error message
397
 
        */
398
 
        QString *addColumn ( int field, const QString &column );
399
 
 
400
 
        /* Following functions work only until first edit operation! (category index used) */
401
 
        
402
 
        /** Get number of fields in category index */
403
 
        int cidxGetNumFields ( void );
404
 
 
405
 
        /** Get field number for index */
406
 
        int cidxGetFieldNumber ( int idx );
407
 
 
408
 
        /** Get maximum category for field index */
409
 
        int cidxGetMaxCat ( int idx );
 
181
  /* Reset the layer - for an OGRLayer, this means clearing the
 
182
   * spatial filter and calling ResetReading
 
183
   */
 
184
  void reset();
 
185
 
 
186
  /**Returns the minimum value of an attribut
 
187
     @param position the number of the attribute*/
 
188
  QString minValue(int position);
 
189
 
 
190
  /**Returns the maximum value of an attribut
 
191
     @param position the number of the attribute*/
 
192
  QString maxValue(int position);
 
193
 
 
194
  /** Update (reload) non static members (marked !UPDATE!) from the static layer and the map.
 
195
   *   This method MUST be called whenever lastUpdate of the map is later then mapLastUpdate 
 
196
   *   of the instance.
 
197
   */
 
198
  void update();
 
199
 
 
200
  /**Returns true if this is a valid layer
 
201
   */
 
202
  bool isValid();
 
203
 
 
204
  // ----------------------------------- Edit ----------------------------------
 
205
        
 
206
  /** Is the layer editable? I.e. the layer is valid and current user is owner of the mapset
 
207
   *   @return true the layer editable
 
208
   *   @return false the is not editable
 
209
   */
 
210
  bool isGrassEditable();
 
211
 
 
212
  /** Returns true if the layer is currently edited (opened in update mode)
 
213
   *   @return true in update mode
 
214
   *   @return false not edited
 
215
   */
 
216
  bool isEdited();
 
217
 
 
218
  /** Start editing. Reopen the vector for update and set GMAP.update = true
 
219
   *   @return true success
 
220
   *   @return false failed to reopen success
 
221
   */
 
222
  bool startEdit();
 
223
 
 
224
  /** Close editing. Rebuild topology, GMAP.update = false 
 
225
   *   @return true success
 
226
   *   @return false failed to close vector or vector was not in update mode
 
227
   */
 
228
  bool closeEdit();
 
229
 
 
230
  /** Get current number of lines. 
 
231
   *   @return number of lines
 
232
   */
 
233
  int numLines ( void );
 
234
 
 
235
  /** Get current number of nodes. 
 
236
   *   @return number of nodes
 
237
   */
 
238
  int numNodes ( void );
 
239
 
 
240
  /** Read line 
 
241
   *   @param Points pointer to existing structure or NULL
 
242
   *   @param Cats pointer to existing structure or NULL
 
243
   *   @param line line number
 
244
   *   @return line type
 
245
   *   @return <0 deadline or error
 
246
   */
 
247
  int readLine ( struct line_pnts * Points, struct line_cats * Cats, int line );
 
248
 
 
249
  /** Read node coordinates 
 
250
   *   @param line line number
 
251
   *   @return true node is alive
 
252
   *   @return false node is dead
 
253
   */
 
254
  bool nodeCoor ( int node, double *x, double *y );
 
255
 
 
256
  /** Read line nodes 
 
257
   *   @param line line number
 
258
   *   @return true line is alive
 
259
   *   @return false line is dead
 
260
   */
 
261
  bool lineNodes ( int line, int *node1, int *node2 );
 
262
 
 
263
  /** Read boundary areas 
 
264
   *   @param line line number
 
265
   *   @return true line is alive
 
266
   *   @return false line is dead
 
267
   */
 
268
  bool lineAreas ( int line, int *left, int *right );
 
269
 
 
270
  /** Get centroid area 
 
271
   *   @param centroid line number
 
272
   *   @return area number (negative for island)
 
273
   */
 
274
  int centroidArea ( int centroid );
 
275
 
 
276
  /** Get number of lines at node 
 
277
   *   @param node node number
 
278
   *   @return number of lines at node (including dead lines)
 
279
   */
 
280
  int nodeNLines ( int node );
 
281
 
 
282
  /** Get line number of line at node for given line index
 
283
   *   @param node node number
 
284
   *   @param idx line index
 
285
   *   @return line number
 
286
   */
 
287
  int nodeLine ( int node, int idx );
 
288
 
 
289
  /** True if line is alive
 
290
   *   @param line line number
 
291
   *   @return true alive
 
292
   *   @return false dead
 
293
   */
 
294
  int lineAlive ( int line );
 
295
 
 
296
  /** True if node is alive
 
297
   *   @param node node number
 
298
   *   @return true alive
 
299
   *   @return false dead
 
300
   */
 
301
  int nodeAlive ( int node );
 
302
 
 
303
  /** Write a new line into vector. 
 
304
   *   @return line number
 
305
   *   @return -1 error
 
306
   */
 
307
  int writeLine ( int type, struct line_pnts *Points, struct line_cats *Cats );
 
308
 
 
309
  /** Rewrite line. 
 
310
   *   @return line number
 
311
   *   @return -1 error
 
312
   */
 
313
  int rewriteLine ( int line, int type, struct line_pnts *Points, struct line_cats *Cats );
 
314
 
 
315
  /** Delete line 
 
316
   *   @return 0 OK
 
317
   *   @return -1 error
 
318
   */
 
319
  int deleteLine ( int line );
 
320
 
 
321
  /** Number of updated lines 
 
322
   */
 
323
  int numUpdatedLines ( void );
 
324
 
 
325
  /** Number of updated nodes 
 
326
   */
 
327
  int numUpdatedNodes ( void );
 
328
 
 
329
  /** Get updated line 
 
330
   */
 
331
  int updatedLine ( int idx );
 
332
 
 
333
  /** Get updated node 
 
334
   */
 
335
  int updatedNode ( int idx );
 
336
 
 
337
  /** Find nearest line 
 
338
   *   @param threshold maximum distance
 
339
   *   @return line number
 
340
   *   @return 0 nothing found
 
341
   */
 
342
  int findLine ( double x, double y, int type, double threshold );
 
343
 
 
344
  /** Find nearest node 
 
345
   *   @param threshold maximum distance
 
346
   *   @return node number
 
347
   *   @return 0 nothing found
 
348
   */
 
349
  int findNode ( double x, double y, double threshold );
 
350
 
 
351
  /** Get columns' definitions
 
352
   *   @param field
 
353
   *   @param cat
 
354
   *   @return vector of attributes
 
355
   */
 
356
  std::vector<QgsField> *columns ( int field );
 
357
 
 
358
  /** Read attributes from DB
 
359
   *   @param field
 
360
   *   @param cat
 
361
   *   @return vector of attributes
 
362
   */
 
363
  std::vector<QgsFeatureAttribute> *attributes ( int field, int cat );
 
364
 
 
365
  /** Key (cat) column name 
 
366
   *   @param field
 
367
   *   @return Key column name or empty string 
 
368
   */
 
369
  QString *key ( int field );
 
370
 
 
371
  /** Get number of db links 
 
372
   *   @return number of links 
 
373
   */
 
374
  int numDbLinks ( void );
 
375
 
 
376
  /** Get db link field
 
377
   *  @param link
 
378
   *   @return field number or 0 
 
379
   */
 
380
  int dbLinkField ( int link );
 
381
 
 
382
  /** Update attributes 
 
383
   *   @param field
 
384
   *   @param cat
 
385
   *   @param update comma separated update string, e.g.: col1 = 5, col2 = 'Val d''Aosta'
 
386
   *   @return empty string or error message 
 
387
   */
 
388
  QString *updateAttributes ( int field, int cat, const QString &values );
 
389
 
 
390
  /** Insert new attributes to the table (it does not check if attributes already exists)
 
391
   *   @param field
 
392
   *   @param cat
 
393
   *   @return empty string or error message 
 
394
   */
 
395
  QString *insertAttributes ( int field, int cat );
 
396
 
 
397
  /** Create table and link vector to this table
 
398
   *   @param field
 
399
   *   @param columns SQL definition for columns, e.g. cat integer, label varchar(10) 
 
400
   *   @return empty string or error message
 
401
   */
 
402
  QString *createTable ( int field, const QString &key, const QString &columns );
 
403
 
 
404
  /** Add column to table
 
405
   *   @param field
 
406
   *   @param column SQL definition for columns, e.g. label varchar(10) 
 
407
   *   @return empty string or error message
 
408
   */
 
409
  QString *addColumn ( int field, const QString &column );
 
410
 
 
411
  /* Following functions work only until first edit operation! (category index used) */
 
412
        
 
413
  /** Get number of fields in category index */
 
414
  int cidxGetNumFields ( void );
 
415
 
 
416
  /** Get field number for index */
 
417
  int cidxGetFieldNumber ( int idx );
 
418
 
 
419
  /** Get maximum category for field index */
 
420
  int cidxGetMaxCat ( int idx );
410
421
  /** get data source uri (not implemented) */
411
422
  QgsDataSourceURI * getURI(){ return 0;};
 
423
  
 
424
  /** Returns GRASS layer number */
 
425
  int grassLayer();
 
426
 
412
427
private:
413
 
        enum ENDIAN {
414
 
                NDR = 1,
415
 
                XDR = 0
416
 
        };
417
 
 
418
 
        // Layer type (layerType)
419
 
        enum    TYPE {   // layer name:
420
 
            POINT = 1,   // <field>_point
421
 
            LINE,        // <field>_line
422
 
            POLYGON,     // <field>_polygon
423
 
            BOUNDARY,    // boundary (currently not used)
424
 
            CENTROID     // centroid (currently not used)
425
 
        };
426
 
 
427
 
        QString mGisdbase;      // map gisdabase
428
 
        QString mLocation;      // map location name (not path!)
429
 
        QString mMapset;        // map mapset
430
 
        QString mMapName;       // map name
431
 
        QString mLayer;         // layer name
432
 
        int     mLayerField;    // field part of layer or -1 if no field specified
433
 
        int     mLayerType;     // layer type POINT, LINE, ...
434
 
        int     mGrassType;     // grass feature type: GV_POINT, GV_LINE | GV_BOUNDARY, GV_AREA, 
435
 
                                // ( GV_BOUNDARY, GV_CENTROID )
436
 
        int     mQgisType;      // WKBPoint, WKBLineString, ...
437
 
        int     mLayerId;       // ID used in layers
438
 
        struct  Map_info *mMap; // vector header pointer
439
 
        int     mMapVersion;    // The version of the map for which the instance was last time updated
440
 
        
441
 
        struct line_pnts *mPoints; // points structure 
442
 
        struct line_cats *mCats;   // cats structure
443
 
        struct ilist     *mList; 
444
 
        int    mCidxFieldIndex;    // !UPDATE! Index for layerField in category index or -1 if no such field
445
 
        int    mCidxFieldNumCats;  // !UPDATE! Number of records in field index
446
 
        int    mNextCidx;          // !UPDATE! Next index in cidxFieldIndex to be read, used to find nextFeature
447
 
 
448
 
        // selection: array of size nlines or nareas + 1, set to 1 - selected or 0 - not selected, 2 - read
449
 
        // Code 2 means that the line was already read in this cycle, all 2 must be reset to 1
450
 
        // if getFirstFeature() or select() is calles. 
451
 
        // Distinction between 1 and 2 is used if attribute table exists, in that case attributes are
452
 
        // read from the table and geometry is append and selection set to 2.
453
 
        // In the end the selection array is scanned for 1 (attributes missing), and the geometry 
454
 
        // is returned without attributes
455
 
        char    *mSelection;           // !UPDATE!
456
 
        int     mSelectionSize;        // !UPDATE! Size of selection array
457
 
 
458
 
        QString mDataSourceUri;
459
 
        bool    mValid;                // !UPDATE! 
460
 
        long    mNumberFeatures;       // !UPDATE!
461
 
        int     mEndian;               // endian
462
 
        
463
 
        void resetSelection(bool sel); // reset selection
464
 
        void checkEndian();            // set endian
465
 
 
466
 
        // -----------------------------------------------------------------------------------------
467
 
        /* Static variables and methods.
468
 
         * These methods opens GRASS vectors and loads some parts of vectors to the memory.
469
 
         * it maintains the list of opened layers so that sources are not duplicated in the memory. 
470
 
         * Layers are identified by layer ID. 
471
 
         * The layers have unique URI, if next layer of the same URI is requested, 
472
 
         * nUsers is increased and ID of the layer which is already opened is returned.
473
 
         * Attributes are loaded from DB and stored in the memory when layer is opened.
474
 
         */
475
 
 
476
 
        /*! Open layer. Layer for QgsGrassVector means Map+field
477
 
         *  @param gisdbase 
478
 
         *  @param location
479
 
         *  @param mapset
480
 
         *  @param mapName
481
 
         *  @param field
482
 
         *  @return layer ID
483
 
         *  @return -1 cannot open
484
 
         */
485
 
        static int openLayer(QString gisdbase, QString location, QString mapset, QString mapName, int field);
486
 
 
487
 
        /*! Load sources from the map.
488
 
         *  Must be set: layer.mapId, layer.map, layer.field
489
 
         *  Updates: layer.fieldInfo, layer.nColumns, layer.nAttributes, layer.attributes, layer.keyColumn
490
 
         *  Unchanged: layer.valid
491
 
         *
492
 
         *  Old sources are released, namely: layer.fields and layer.attributes
493
 
         *
494
 
         *  layer.attributes must be pointer to existing array or 0
495
 
         */
496
 
        static void loadLayerSourcesFromMap ( GLAYER &layer );
497
 
 
498
 
        /*! Close layer. 
499
 
         *  @param layerId 
500
 
         */
501
 
        static void closeLayer( int layerId );
502
 
 
503
 
        /*! Open map. 
504
 
         *  @param gisdbase 
505
 
         *  @param location
506
 
         *  @param mapset
507
 
         *  @param mapName
508
 
         *  @return map ID
509
 
         *  @return -1 cannot open
510
 
         */
511
 
        static int openMap(QString gisdbase, QString location, QString mapset, QString mapName);
512
 
 
513
 
        /*! Close map. 
514
 
         *  @param mapId 
515
 
         */
516
 
        static void closeMap( int mapId );
517
 
 
518
 
        /*! Update map. Close and reopen vector, all layers in mLayers using this map are also updated.
519
 
         *  Instances of QgsGrassProvider are not updated and should call update() method.
520
 
         *  @param mapId 
521
 
         */
522
 
        static void updateMap( int mapId );
523
 
 
524
 
        /*! The map is outdated. The map was for example rewritten by GRASS module outside QGIS.
525
 
         *  This function checks internal timestamp stored in QGIS.
526
 
         *  @param mapId 
527
 
         */
528
 
        static bool mapOutdated( int mapId );
529
 
 
530
 
        /*! Allocate sellection array for given map id. The array is large enough for lines or areas
531
 
         *  (bigger from num lines and num areas)
532
 
         *  Possible old selection array is not released.
533
 
         *  @param map pointer to map structure 
534
 
         *  @param selection pointer to pointer to char array
535
 
         *  @return selection size
536
 
         */
537
 
        static int allocateSelection(  struct Map_info *map, char **selection );
538
 
        
539
 
        /*! Get layer map. 
540
 
         *  @param layerId 
541
 
         *  @return pointer to Map_info structure
542
 
         */
543
 
        static struct Map_info *layerMap( int layerId );
544
 
        
545
 
        /*! Get attribute by category(key) and attribute number. 
546
 
         *  @param layerId 
547
 
         *  @param category (key) 
548
 
         *  @param column column number ( < nColumns ) 
549
 
         *  @return pointer to string representation of the value or NULL, this value must not be changed
550
 
         */
551
 
        static char *attribute( int layerId, int cat, int column );
552
 
        
553
 
        /*! Set feature attributes. 
554
 
         *  @param layerId
555
 
         *  @param feature
556
 
         *  @param cat category number
557
 
         */
558
 
        static void setFeatureAttributes ( int layerId, int cat, QgsFeature *feature);
559
 
 
560
 
        /*! Set feature attributes. 
561
 
         *  @param layerId
562
 
         *  @param feature
563
 
         *  @param cat category number
564
 
         *  @param attlist a list containing the index number of the fields to set
565
 
         */
566
 
        static void setFeatureAttributes ( int layerId, int cat, QgsFeature *feature, std::list<int>& attlist);
567
 
 
568
 
        /* Static arrays of opened layers and vectors */
569
 
        static  std::vector<GLAYER> mLayers; // Map + field/attributes
570
 
        static  std::vector<GMAP> mMaps;     // Map
 
428
  // Layer type (layerType)
 
429
  enum    TYPE {   // layer name:
 
430
      POINT = 1,   // <field>_point
 
431
      LINE,        // <field>_line
 
432
      POLYGON,     // <field>_polygon
 
433
      BOUNDARY,    // boundary (currently not used)
 
434
      CENTROID     // centroid (currently not used)
 
435
  };
 
436
 
 
437
  QString mGisdbase;      // map gisdabase
 
438
  QString mLocation;      // map location name (not path!)
 
439
  QString mMapset;        // map mapset
 
440
  QString mMapName;       // map name
 
441
  QString mLayer;         // layer name
 
442
  int     mLayerField;    // field part of layer or -1 if no field specified
 
443
  int     mLayerType;     // layer type POINT, LINE, ...
 
444
  int     mGrassType;     // grass feature type: GV_POINT, GV_LINE | GV_BOUNDARY, GV_AREA, 
 
445
  // ( GV_BOUNDARY, GV_CENTROID )
 
446
  int     mQgisType;      // WKBPoint, WKBLineString, ...
 
447
  int     mLayerId;       // ID used in layers
 
448
  struct  Map_info *mMap; // vector header pointer
 
449
  int     mMapVersion;    // The version of the map for which the instance was last time updated
 
450
        
 
451
  struct line_pnts *mPoints; // points structure 
 
452
  struct line_cats *mCats;   // cats structure
 
453
  struct ilist     *mList; 
 
454
  int    mCidxFieldIndex;    // !UPDATE! Index for layerField in category index or -1 if no such field
 
455
  int    mCidxFieldNumCats;  // !UPDATE! Number of records in field index
 
456
  int    mNextCidx;          // !UPDATE! Next index in cidxFieldIndex to be read, used to find nextFeature
 
457
 
 
458
  // selection: array of size nlines or nareas + 1, set to 1 - selected or 0 - not selected, 2 - read
 
459
  // Code 2 means that the line was already read in this cycle, all 2 must be reset to 1
 
460
  // if getFirstFeature() or select() is calles. 
 
461
  // Distinction between 1 and 2 is used if attribute table exists, in that case attributes are
 
462
  // read from the table and geometry is append and selection set to 2.
 
463
  // In the end the selection array is scanned for 1 (attributes missing), and the geometry 
 
464
  // is returned without attributes
 
465
  char    *mSelection;           // !UPDATE!
 
466
  int     mSelectionSize;        // !UPDATE! Size of selection array
 
467
 
 
468
  QString mDataSourceUri;
 
469
  bool    mValid;                // !UPDATE! 
 
470
  long    mNumberFeatures;       // !UPDATE!
 
471
        
 
472
  void resetSelection(bool sel); // reset selection
 
473
 
 
474
  // -----------------------------------------------------------------------------------------
 
475
  /* Static variables and methods.
 
476
   * These methods opens GRASS vectors and loads some parts of vectors to the memory.
 
477
   * it maintains the list of opened layers so that sources are not duplicated in the memory. 
 
478
   * Layers are identified by layer ID. 
 
479
   * The layers have unique URI, if next layer of the same URI is requested, 
 
480
   * nUsers is increased and ID of the layer which is already opened is returned.
 
481
   * Attributes are loaded from DB and stored in the memory when layer is opened.
 
482
   */
 
483
 
 
484
  /*! Open layer. Layer for QgsGrassVector means Map+field
 
485
   *  @param gisdbase 
 
486
   *  @param location
 
487
   *  @param mapset
 
488
   *  @param mapName
 
489
   *  @param field
 
490
   *  @return layer ID
 
491
   *  @return -1 cannot open
 
492
   */
 
493
  static int openLayer(QString gisdbase, QString location, QString mapset, QString mapName, int field);
 
494
 
 
495
  /*! Load sources from the map.
 
496
   *  Must be set: layer.mapId, layer.map, layer.field
 
497
   *  Updates: layer.fieldInfo, layer.nColumns, layer.nAttributes, layer.attributes, layer.keyColumn
 
498
   *  Unchanged: layer.valid
 
499
   *
 
500
   *  Old sources are released, namely: layer.fields and layer.attributes
 
501
   *
 
502
   *  layer.attributes must be pointer to existing array or 0
 
503
   */
 
504
  static void loadLayerSourcesFromMap ( GLAYER &layer );
 
505
 
 
506
  /*! Load attributes from database table.
 
507
   *  Must be set: layer.mapId, layer.map, layer.field
 
508
   *  Updates: layer.fieldInfo, layer.nColumns, layer.nAttributes, layer.attributes, layer.keyColumn
 
509
   *  Unchanged: layer.valid
 
510
   *
 
511
   *  Old sources are released, namely: layer.attributes
 
512
   *
 
513
   *  layer.attributes must be pointer to existing array or 0
 
514
   */
 
515
  static void loadAttributes ( GLAYER &layer );
 
516
 
 
517
  /*! Close layer. 
 
518
   *  @param layerId 
 
519
   */
 
520
  static void closeLayer( int layerId );
 
521
 
 
522
  /*! Open map. 
 
523
   *  @param gisdbase 
 
524
   *  @param location
 
525
   *  @param mapset
 
526
   *  @param mapName
 
527
   *  @return map ID
 
528
   *  @return -1 cannot open
 
529
   */
 
530
  static int openMap(QString gisdbase, QString location, QString mapset, QString mapName);
 
531
 
 
532
  /*! Close map. 
 
533
   *  @param mapId 
 
534
   */
 
535
  static void closeMap( int mapId );
 
536
 
 
537
  /*! Update map. Close and reopen vector, all layers in mLayers using this map are also updated.
 
538
   *  Instances of QgsGrassProvider are not updated and should call update() method.
 
539
   *  @param mapId 
 
540
   */
 
541
  static void updateMap( int mapId );
 
542
 
 
543
  /*! The map is outdated. The map was for example rewritten by GRASS module outside QGIS.
 
544
   *  This function checks internal timestamp stored in QGIS.
 
545
   *  @param mapId 
 
546
   */
 
547
  static bool mapOutdated( int mapId );
 
548
 
 
549
  /*! The attributes are outdated. The table was for example updated by GRASS module outside QGIS.
 
550
   *  This function checks internal timestamp stored in QGIS.
 
551
   *  @param mapId 
 
552
   */
 
553
  static bool attributesOutdated( int mapId );
 
554
 
 
555
  /*! Allocate sellection array for given map id. The array is large enough for lines or areas
 
556
   *  (bigger from num lines and num areas)
 
557
   *  Possible old selection array is not released.
 
558
   *  @param map pointer to map structure 
 
559
   *  @param selection pointer to pointer to char array
 
560
   *  @return selection size
 
561
   */
 
562
  static int allocateSelection(  struct Map_info *map, char **selection );
 
563
        
 
564
  /*! Get layer map. 
 
565
   *  @param layerId 
 
566
   *  @return pointer to Map_info structure
 
567
   */
 
568
  static struct Map_info *layerMap( int layerId );
 
569
        
 
570
  /*! Get attribute by category(key) and attribute number. 
 
571
   *  @param layerId 
 
572
   *  @param category (key) 
 
573
   *  @param column column number ( < nColumns ) 
 
574
   *  @return pointer to string representation of the value or NULL, this value must not be changed
 
575
   */
 
576
  static char *attribute( int layerId, int cat, int column );
 
577
        
 
578
  /*! Set feature attributes. 
 
579
   *  @param layerId
 
580
   *  @param feature
 
581
   *  @param cat category number
 
582
   */
 
583
  void setFeatureAttributes ( int layerId, int cat, QgsFeature *feature);
 
584
 
 
585
  /*! Set feature attributes. 
 
586
   *  @param layerId
 
587
   *  @param feature
 
588
   *  @param cat category number
 
589
   *  @param attlist a list containing the index number of the fields to set
 
590
   */
 
591
  void setFeatureAttributes ( int layerId, int cat, QgsFeature *feature, std::list<int> const & attlist);
 
592
 
 
593
  /* Static arrays of opened layers and vectors */
 
594
  static        std::vector<GLAYER> mLayers; // Map + field/attributes
 
595
  static        std::vector<GMAP> mMaps;     // Map
571
596
};
572
597
 
573
598
#endif // QGSGRASSPROVIDER_H