summaryrefslogtreecommitdiff
blob: 4197610c17f01fc905576956f3ee8c34a43458c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
--- a/src/ImportExport/ImportExportGdal.cpp 2015-06-15 18:48:39.604575423 +0200
+++ b/src/ImportExport/ImportExportGdal.cpp 2015-06-15 18:48:39.604575423 +0200
@@ -21,10 +21,13 @@
 #include "ProjectionChooser.h"
 #include "Global.h"
 
-#include "cpl_vsi.h"
+#include <cpl_vsi.h>
+#include <gdal.h>
+#include <gdal_priv.h>
 
 #include <QDir>
 
+
 bool parseContainer(QDomElement& e, Layer* aLayer);
 
 ImportExportGdal::ImportExportGdal(Document* doc)
@@ -55,6 +58,55 @@ bool ImportExportGdal::saveFile(QString)
 bool ImportExportGdal::export_(const QList<Feature *>& featList)
 {
     const char *pszDriverName = "SQLite";
+    QString fileName(HOMEDIR + "/test.sqlite");
+#ifdef GDAL2
+    GDALDriver *poDriver;
+    GDALDriverManager *driverManager = GetGDALDriverManager();
+    GDALAllRegister();
+    driverManager->AutoLoadDrivers();
+
+    poDriver = driverManager->GetDriverByName(pszDriverName);
+    if( poDriver == NULL )
+    {
+        qDebug( "%s driver not available.", pszDriverName );
+        return false;
+    }
+
+    /* Create create options */
+    char **createOptions = NULL;
+    createOptions = CSLSetNameValue( createOptions, "SPATIALITE", "YES" );
+
+    /* Open new dataset */
+    GDALDataset *poDS;
+
+    QFile::remove(fileName);
+    poDS = poDriver->Create( fileName.toUtf8().constData(), 0, 0, 0, GDT_Unknown, createOptions );
+    if( poDS == NULL )
+    {
+        qDebug( "Creation of output file failed." );
+        return false;
+    }
+    poDS->ExecuteSQL("PRAGMA synchronous = OFF", NULL, NULL);
+
+    /* Create Spatial reference object */
+    OGRSpatialReference *poSRS;
+    poSRS = new OGRSpatialReference();
+    poSRS->importFromEPSG(4326);
+
+    /* Create layer options */
+    char **layerOptions = NULL;
+    layerOptions = CSLSetNameValue( layerOptions, "FORMAT", "SPATIALITE" );
+    layerOptions = CSLSetNameValue( layerOptions, "SPATIAL_INDEX", "YES" );
+
+    /* Create layer */
+    OGRLayer *poLayer;
+    poLayer = poDS->CreateLayer( "osm", poSRS, wkbUnknown, layerOptions);
+
+    /* Free the options */
+    CSLDestroy( createOptions );
+    CSLDestroy( layerOptions );
+#else
+    /* This is legacy code, and is not tested at all */
     OGRSFDriver *poDriver;
 
     OGRRegisterAll();
@@ -68,8 +120,8 @@ bool ImportExportGdal::export_(const QList<Feature *>& featList)
 
     OGRDataSource *poDS;
 
-    QFile::remove(QString(HOMEDIR + "/test.sqlite"));
-    poDS = poDriver->CreateDataSource( QString(HOMEDIR + "/test.sqlite").toUtf8().constData(), NULL );
+    QFile::remove(fileName);
+    poDS = poDriver->CreateDataSource( fileName.toUtf8().constData(), NULL );
     if( poDS == NULL )
     {
         qDebug( "Creation of output file failed." );
@@ -89,6 +141,8 @@ bool ImportExportGdal::export_(const QList<Feature *>& featList)
     OGRLayer *poLayer;
     poLayer = poDS->CreateLayer( "osm", poSRS, wkbUnknown, papszOptions);
     CSLDestroy( papszOptions );
+#endif
+
 
     if( poLayer == NULL )
     {
@@ -142,7 +196,11 @@ bool ImportExportGdal::export_(const QList<Feature *>& featList)
         }
         OGRFeature::DestroyFeature( poFeature );
     }
+#ifdef GDAL2
+    GDALClose( (GDALDatasetH) poDS );
+#else
     OGRDataSource::DestroyDataSource( poDS );
+#endif
     return true;
 }
 
@@ -264,7 +322,11 @@ Feature* ImportExportGdal::parseGeometry(Layer* aLayer, OGRGeometry *poGeometry)
 
 // import the  input
 
-bool ImportExportGdal::importGDALDataset(OGRDataSource* poDS, Layer* aLayer, bool confirmProjection)
+#ifndef GDAL2
+#define GDALDataset OGRDataSource
+#endif
+bool ImportExportGdal::importGDALDataset(GDALDataset* poDS, Layer* aLayer, bool confirmProjection)
+#undef GDALDataset
 {
     int ogrError;
 
@@ -422,11 +484,16 @@ bool ImportExportGdal::importGDALDataset(OGRDataSource* poDS, Layer* aLayer, boo
 
 bool ImportExportGdal::import(Layer* aLayer)
 {
+#ifdef GDAL2
+    GDALAllRegister();
+    GDALDataset *poDS;
+    poDS = (GDALDataset *) GDALOpen( FileName.toUtf8().constData(), GA_ReadOnly );
+#else
     OGRRegisterAll();
-
-    OGRDataSource       *poDS;
-
+    OGRDataSource *poDS;
     poDS = OGRSFDriverRegistrar::Open( FileName.toUtf8().constData(), FALSE );
+#endif
+
     if( poDS == NULL )
     {
         qDebug( "GDAL Open failed.\n" );
@@ -435,22 +502,26 @@ bool ImportExportGdal::import(Layer* aLayer)
 
     importGDALDataset(poDS, aLayer, M_PREFS->getGdalConfirmProjection());
 
-    OGRDataSource::DestroyDataSource( poDS );
+    GDALClose( (GDALDatasetH) poDS );
 
     return true;
 }
 
 bool ImportExportGdal::import(Layer* aLayer, const QByteArray& ba, bool confirmProjection)
 {
-    OGRRegisterAll();
-
-    OGRDataSource       *poDS;
-//    int ogrError;
-
     GByte* content = (GByte*)(ba.constData());
     /*FILE* f = */VSIFileFromMemBuffer("/vsimem/temp", content, ba.size(), FALSE);
 
+#ifdef GDAL2
+    GDALAllRegister();
+    GDALDataset *poDS;
+    poDS = (GDALDataset *) GDALOpen( "/vsimem/temp", GA_ReadOnly );
+#else
+    OGRRegisterAll();
+    OGRDataSource *poDS;
     poDS = OGRSFDriverRegistrar::Open( "/vsimem/temp", FALSE );
+#endif
+
     if( poDS == NULL )
     {
         qDebug( "GDAL Open failed.\n" );
@@ -458,7 +529,7 @@ bool ImportExportGdal::import(Layer* aLayer, const QByteArray& ba, bool confirmP
     }
     importGDALDataset(poDS, aLayer, confirmProjection);
 
-    OGRDataSource::DestroyDataSource( poDS );
+    GDALClose( (GDALDatasetH) poDS );
 
     return true;
 }
--- a/src/ImportExport/ImportExportGdal.h 2015-06-15 18:50:42.884995287 +0200
+++ b/src/ImportExport/ImportExportGdal.h 2015-06-15 18:50:42.884995287 +0200
@@ -13,7 +13,15 @@
 #define ImportExportGDAL_H
 
 #include "IImportExport.h"
-#include "ogrsf_frmts.h"
+
+#include <ogrsf_frmts.h>
+#include <gdal.h>
+#include <gdal_priv.h>
+#include <gdal_version.h>
+
+#if GDAL_VERSION_MAJOR == 2
+#define GDAL2
+#endif
 
 class Projection;
 class Layer;
@@ -52,7 +60,11 @@ class ImportExportGdal : public IImportExport
     Node *nodeFor(Layer* aLayer, OGRPoint point);
     Way *readWay(Layer* aLayer, OGRLineString *poRing);
 
-    bool importGDALDataset(OGRDataSource *poDs, Layer *aLayer, bool confirmProjection);
+#ifndef GDAL2
+#define GDALDataset OGRDataSource
+#endif
+    bool importGDALDataset(GDALDataset *poDs, Layer *aLayer, bool confirmProjection);
+#undef GDALDataset
 
 private:
     QHash<OGRPoint, Node*> pointHash;