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

« back to all changes in this revision

Viewing changes to plugins/spit/qgsshapefile.cpp

  • 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:
14
14
 *   (at your option) any later version.                                   *
15
15
 *                                                                         *
16
16
 ***************************************************************************/
17
 
/* $Id: qgsshapefile.cpp,v 1.9.2.2 2004/12/14 19:20:15 gsherman Exp $ */
 
17
/* $Id: qgsshapefile.cpp,v 1.11.2.3 2005/09/16 23:35:21 timlinux Exp $ */
18
18
 
19
19
#include <qapplication.h>
20
20
#include <ogrsf_frmts.h>
30
30
#include "qgsscangeometries.h"
31
31
#include "../../src/qgis.h"
32
32
 
 
33
// for htonl
 
34
#ifdef WIN32
 
35
#include <winsock.h>
 
36
#else
 
37
#include <netinet/in.h>
 
38
#endif    
 
39
 
33
40
 
34
41
QgsShapeFile::QgsShapeFile(QString name){
35
42
  filename = name;
50
57
}
51
58
 
52
59
QgsShapeFile::~QgsShapeFile(){
53
 
  delete ogrLayer;
 
60
  if(ogrDataSource != 0)
 
61
  {
 
62
    // don't delete the layer if the datasource is bad -- (causes crash)
 
63
    delete ogrLayer;
 
64
  }
54
65
  delete ogrDataSource;
55
66
  delete filename;
56
67
  delete geom_type;
123
134
  // type. 
124
135
  qApp->processEvents();
125
136
  isMulti = scanGeometries();
126
 
  OGRFeature *feat = ogrLayer->GetNextFeature();
 
137
  OGRFeature *feat;
 
138
  // skip features without geometry
 
139
  while ((feat = ogrLayer->GetNextFeature()) != NULL) {
 
140
    if (feat->GetGeometryRef())
 
141
      break;
 
142
  }
127
143
  if(feat){
128
144
    OGRGeometry *geom = feat->GetGeometryRef();
129
145
    if(geom){
152
168
      // read header
153
169
      DbaseHeader dbh;
154
170
      dbf.read((char *)&dbh, sizeof(dbh));
 
171
      // Check byte order
 
172
      if(htonl(1) == 1) 
 
173
      {
 
174
        /* DbaseHeader is stored in little-endian format.
 
175
         * The num_recs, size_hdr and size_rec fields must be byte-swapped when read
 
176
         * on a big-endian processor. Currently only size_hdr is used.
 
177
         */
 
178
        unsigned char *byte = reinterpret_cast<unsigned char *>(&dbh.size_hdr);
 
179
        unsigned char t = *byte; *byte = *(byte+1); *(byte+1) = t;
 
180
      }
155
181
 
156
182
      Fda fda;
157
183
      QString str_type = "varchar(";
228
254
  connect(pro, SIGNAL(cancelled()), this, SLOT(cancelImport()));
229
255
  import_cancelled = false;
230
256
  bool result = true;
231
 
 
 
257
  // Mangle the table name to make it PG compliant by replacing spaces with 
 
258
  // underscores
 
259
  table_name = table_name.replace(" ","_");
232
260
  QString query = "CREATE TABLE "+schema+"."+table_name+"(gid int4 PRIMARY KEY, ";
233
261
  for(int n=0; n<column_names.size() && result; n++){
234
262
    if(!column_names[n][0].isLetter())
235
263
      result = false;
236
264
    char * esc_str = new char[column_names[n].length()*2+1];
 
265
    std::cerr << "Escaping " << column_names[n] << " to ";
237
266
    PQescapeString(esc_str, (const char *)column_names[n].lower(), column_names[n].length());
 
267
    std::cerr << esc_str << std::endl; 
238
268
    query += esc_str;
 
269
    std::cerr << query << std::endl; 
239
270
    query += " ";
 
271
    std::cerr << query << std::endl; 
240
272
    query += column_types[n];
 
273
    std::cerr << query << std::endl; 
241
274
    if(n<column_names.size()-1)
 
275
    {
242
276
      query += ", ";
 
277
      std::cerr << query << std::endl; 
 
278
    }
243
279
    delete[] esc_str;
244
280
  }
245
281
  query += " )";
 
282
      std::cerr << query << std::endl; 
246
283
 
247
284
  PGresult *res = PQexec(conn, (const char *)query);
248
285
  qWarning(query);