File: importshp.cpp

package info (click to toggle)
librecad 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 106,400 kB
  • sloc: cpp: 188,363; ansic: 3,069; sh: 336; xml: 43; makefile: 24
file content (455 lines) | stat: -rw-r--r-- 14,625 bytes parent folder | download
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*****************************************************************************/
/*  importshp.cpp - shape file importer                                      */
/*                                                                           */
/*  Copyright (C) 2011 Rallaz, rallazz@gmail.com                             */
/*                                                                           */
/*  This library is free software, licensed under the terms of the GNU       */
/*  General Public License as published by the Free Software Foundation,     */
/*  either version 2 of the License, or (at your option) any later version.  */
/*  You should have received a copy of the GNU General Public License        */
/*  along with this program.  If not, see <http://www.gnu.org/licenses/>.    */
/*****************************************************************************/

#include <QtPlugin>
#include <QVBoxLayout>
#include <QPushButton>
#include <QFileDialog>
#include <QSettings>
#include <QMessageBox>

#include "importshp.h"

/*TODO
    complete readAttributes
    void readPolyline(DBFHandle dh, int i);
    void readPolylineC(DBFHandle dh, int i);
    void readMultiPolyline(DBFHandle dh, int i);
    SHPT_MULTIPOINT
*/

PluginCapabilities ImportShp::getCapabilities() const
{
    PluginCapabilities pluginCapabilities;
    pluginCapabilities.menuEntryPoints
            << PluginMenuLocation("plugins_menu", "ESRI Shapefile");
    return pluginCapabilities;
}

QString ImportShp::name() const
{
    return (tr("Import ESRI Shapefile"));
}

void ImportShp::execComm(Document_Interface *doc,
                             QWidget *parent, QString /*cmd*/)
{
    dibSHP pdt(parent);
    int result = pdt.exec();
    if (result == QDialog::Accepted)
        pdt.procesFile(doc);
}


/*****************************/
dibSHP::dibSHP(QWidget *parent) :  QDialog(parent)
{
    setWindowTitle(tr("Import ESRI Shapefile"));
    QVBoxLayout *mainLayout = new QVBoxLayout;

    QPushButton *filebut = new QPushButton(tr("File..."));
    fileedit = new QLineEdit();
    QHBoxLayout *lofile = new QHBoxLayout;
    lofile->addWidget(filebut);
    lofile->addWidget(fileedit);
    lofile->setSizeConstraint(QLayout::SetFixedSize);//ni caso
    mainLayout->addLayout(lofile);

    QLabel *formatlabel = new QLabel(tr("File type:"));
    formattype = new QLabel(tr("Unknown"));
    QHBoxLayout *loformat = new QHBoxLayout;
    loformat->addWidget(formatlabel);
    loformat->addWidget(formattype);
    loformat->addStretch();
    mainLayout->addLayout(loformat);

    QGroupBox *laybox = new QGroupBox(tr("Layer"));
    radiolay1 = new QRadioButton(tr("Current"));
    QRadioButton *radiolay2 = new QRadioButton(tr("From data:"));
    layerdata = new QComboBox();
    radiolay1->setChecked(true);
    QHBoxLayout *laylayout = new QHBoxLayout;
    laylayout->addWidget(radiolay1);
    laylayout->addWidget(radiolay2);
    laylayout->addWidget(layerdata);
    laylayout->addStretch(0);
    laybox->setLayout(laylayout);
    mainLayout->addWidget(laybox);

    QGroupBox *colbox = new QGroupBox(tr("Color"));
    radiocol1 = new QRadioButton(tr("Current"));
    QRadioButton *radiocol2 = new QRadioButton(tr("From data:"));
    colordata = new QComboBox();
    radiocol1->setChecked(true);
    QHBoxLayout *collayout = new QHBoxLayout;
    collayout->addWidget(radiocol1);
    collayout->addWidget(radiocol2);
    collayout->addWidget(colordata);
    collayout->addStretch(1);
    colbox->setLayout(collayout);
    mainLayout->addWidget(colbox);

    QGroupBox *ltypebox = new QGroupBox(tr("Line type"));
    radioltype1 = new QRadioButton(tr("Current"));
    QRadioButton *radioltype2 = new QRadioButton(tr("From data:"));
    ltypedata = new QComboBox();
    radioltype1->setChecked(true);
    QHBoxLayout *ltypelayout = new QHBoxLayout;
    ltypelayout->addWidget(radioltype1);
    ltypelayout->addWidget(radioltype2);
    ltypelayout->addWidget(ltypedata);
    ltypelayout->addStretch(1);
    ltypebox->setLayout(ltypelayout);
    mainLayout->addWidget(ltypebox);

    QGroupBox *lwidthbox = new QGroupBox(tr("Width"));
    radiolwidth1 = new QRadioButton(tr("Current"));
    QRadioButton *radiolwidth2 = new QRadioButton(tr("From data:"));
    lwidthdata = new QComboBox();
    radiolwidth1->setChecked(true);
    QHBoxLayout *lwidthlayout = new QHBoxLayout;
    lwidthlayout->addWidget(radiolwidth1);
    lwidthlayout->addWidget(radiolwidth2);
    lwidthlayout->addWidget(lwidthdata);
    lwidthlayout->addStretch(1);
    lwidthbox->setLayout(lwidthlayout);
    mainLayout->addWidget(lwidthbox);

    pointbox = new QGroupBox(tr("Point"));
    radiopoint1 = new QRadioButton(tr("as Point"));
    QRadioButton *radiopoint2 = new QRadioButton(tr("as Label:"));
    pointdata = new QComboBox();
    radiopoint1->setChecked(true);
    QHBoxLayout *pointlayout = new QHBoxLayout;
    pointlayout->addWidget(radiopoint1);
    pointlayout->addWidget(radiopoint2);
    pointlayout->addWidget(pointdata);
    pointlayout->addStretch(1);
    pointbox->setLayout(pointlayout);
    mainLayout->addWidget(pointbox);

    QHBoxLayout *loaccept = new QHBoxLayout;
    QPushButton *acceptbut = new QPushButton(tr("Accept"));
    QPushButton *cancelbut = new QPushButton(tr("Cancel"));
    loaccept->addStretch();
    loaccept->addWidget(acceptbut);
    loaccept->addWidget(cancelbut);
    loaccept->addStretch();
    mainLayout->addLayout(loaccept);

    setLayout(mainLayout);
    readSettings();
    updateFile();

    connect(cancelbut, SIGNAL(clicked()), this, SLOT(reject()));
    connect(acceptbut, SIGNAL(clicked()), this, SLOT(checkAccept()));
    connect(filebut, SIGNAL(clicked()), this, SLOT(getFile()));
    connect(fileedit, SIGNAL(editingFinished()), this, SLOT(updateFile()));
}

void dibSHP::checkAccept()
{
    writeSettings();
    accept();
}

void dibSHP::getFile()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Select file"),
                                fileedit->text(), "ESRI Shapefiles (*.shp)");
    fileedit->setText(fileName);
    updateFile();
}

void dibSHP::updateFile()
{
    QString fileName = fileedit->text();
    int num_ent, st, num_field;
    double min_bound[4], max_bound[4];
    QStringList txtformats;
    char field_name[12];

    QFileInfo fi = QFileInfo(fileName);
    if (fi.suffix().toLower() != "shp") return;
    QString file = fi.canonicalFilePath ();
    if (file.isEmpty()) return;

    SHPHandle sh = SHPOpen( file.toLocal8Bit(), "rb" );
    SHPGetInfo( sh, &num_ent, &st, min_bound, max_bound );
    SHPClose( sh );
    DBFHandle dh = DBFOpen( file.toLocal8Bit(), "rb" );
    num_field = DBFGetFieldCount( dh );

    for( int i = 0; i < num_field; i++ ) {
        DBFGetFieldInfo( dh, i, field_name,NULL, NULL);

        txtformats << field_name;
    }
    DBFClose( dh );

    txtformats.sort();
    layerdata->clear();
    layerdata->addItems(txtformats);
    colordata->clear();
    colordata->addItems(txtformats);
    ltypedata->clear();
    ltypedata->addItems(txtformats);
    lwidthdata->clear();
    lwidthdata->addItems(txtformats);
    pointdata->clear();
    pointdata->addItems(txtformats);

    switch (st) {
    case SHPT_POINT:
        formattype->setText(tr("Point"));
        pointbox->setDisabled(false);
        break;
    case SHPT_POINTM:
        formattype->setText(tr("Point+Measure"));
        pointbox->setDisabled(false);
        break;
    case SHPT_POINTZ: //3d point
        formattype->setText(tr("3D Point"));
        pointbox->setDisabled(false);
        break;
    case SHPT_MULTIPOINT:
        formattype->setText(tr("Multi Point"));
        pointbox->setDisabled(false);
        break;
    case SHPT_MULTIPOINTM:
        formattype->setText(tr("Multi Point+Measure"));
        pointbox->setDisabled(false);
        break;
    case SHPT_MULTIPOINTZ:
        formattype->setText(tr("3D Multi Point"));
        pointbox->setDisabled(false);
        break;
    case SHPT_ARC:
        formattype->setText(tr("Arc"));
        pointbox->setDisabled(true);
        break;
    case SHPT_ARCM:
        formattype->setText(tr("Arc+Measure"));
        pointbox->setDisabled(true);
        break;
    case SHPT_ARCZ:
        formattype->setText(tr("3D Arc"));
        pointbox->setDisabled(true);
        break;
    case SHPT_POLYGON:
        formattype->setText(tr("Polygon"));
        pointbox->setDisabled(true);
        break;
    case SHPT_POLYGONM:
        formattype->setText(tr("Polygon+Measure"));
        pointbox->setDisabled(true);
        break;
    case SHPT_POLYGONZ:
        formattype->setText(tr("3D Polygon"));
        pointbox->setDisabled(true);
        break;
    case SHPT_MULTIPATCH:
        formattype->setText(tr("Multipatch"));
        pointbox->setDisabled(true);
        break;
    case SHPT_NULL:
    default:
        formattype->setText(tr("Unknown"));
        pointbox->setDisabled(true);
        break;
    }
}

void dibSHP::procesFile(Document_Interface *doc)
{
    int num_ent, st;
    double min_bound[4], max_bound[4];

    currDoc = doc;

    QFileInfo fi = QFileInfo(fileedit->text());
    if (fi.suffix().toLower() != "shp") {
        QMessageBox::critical ( this, "Shapefile", QString(tr("The file %1 not have extension .shp")).arg(fileedit->text()) );
        return;
    }

    if (!fi.exists() ) {
        QMessageBox::critical ( this, "Shapefile", QString(tr("The file %1 not exist")).arg(fileedit->text()) );
        return;
    }

    QString file = fi.canonicalFilePath ();

    SHPHandle sh = SHPOpen( file.toLocal8Bit(), "rb" );
    SHPGetInfo( sh, &num_ent, &st, min_bound, max_bound );
    DBFHandle dh = DBFOpen( file.toLocal8Bit(), "rb" );

    if (radiolay1->isChecked()) {
        layerF = -1;
        attdata.layer = currDoc->getCurrentLayer();
    } else {
        layerF = DBFGetFieldIndex( dh, (layerdata->currentText()).toLatin1().data() );
        layerT = DBFGetFieldInfo( dh, layerF, NULL, NULL, NULL );
    }
    if (radiocol1->isChecked())
        colorF = -1;
    else {
        colorF = DBFGetFieldIndex( dh, (colordata->currentText()).toLatin1().data() );
        colorT = DBFGetFieldInfo( dh, colorF, NULL, NULL, NULL );
    }
    if (radioltype1->isChecked())
        ltypeF = -1;
    else {
        ltypeF = DBFGetFieldIndex( dh, (ltypedata->currentText()).toLatin1().data() );
        ltypeT = DBFGetFieldInfo( dh, ltypeF, NULL, NULL, NULL );
    }
    if (radiolwidth1->isChecked())
        lwidthF = -1;
    else {
        lwidthF = DBFGetFieldIndex( dh, (lwidthdata->currentText()).toLatin1().data() );
        lwidthT = DBFGetFieldInfo( dh, lwidthF, NULL, NULL, NULL );
    }
    if (radiopoint1->isChecked())
        pointF = -1;
    else {
        pointF = DBFGetFieldIndex( dh, (pointdata->currentText()).toLatin1().data() );
        pointT = DBFGetFieldInfo( dh, pointF, NULL, NULL, NULL );
    }

    currlayer =currDoc->getCurrentLayer();
    for( int i = 0; i < num_ent; i++ ) {
        sobject= NULL;
        sobject = SHPReadObject( sh, i );
        if (sobject) {
            switch (sobject->nSHPType) {
            case SHPT_NULL:
                break;
            case SHPT_POINT:
            case SHPT_POINTM: //2d point with measure
            case SHPT_POINTZ: //3d point
                readPoint(dh, i);
                break;
            case SHPT_MULTIPOINT:
            case SHPT_MULTIPOINTM:
            case SHPT_MULTIPOINTZ:
                break;
            case SHPT_ARC:
            case SHPT_ARCM:
            case SHPT_ARCZ:
            case SHPT_POLYGON:
                readPolyline(dh, i);
                break;
            case SHPT_POLYGONM:
            case SHPT_POLYGONZ:
                readPolylineC(dh, i);
                // fall-through
            case SHPT_MULTIPATCH:
                readMultiPolyline(dh, i);
            default:
                break;
            }
            SHPDestroyObject(sobject);
        }
    }

    SHPClose( sh );
    DBFClose( dh );
    currDoc->setLayer(currlayer);
}

void dibSHP::readPoint(DBFHandle dh, int i){
    Plug_Entity *ent =NULL;
    QHash<int, QVariant> data;
    if (pointF < 0) {
        ent = currDoc->newEntity(DPI::POINT);
        ent->getData(&data);
    } else {
        ent = currDoc->newEntity(DPI::MTEXT);
        ent->getData(&data);
        data.insert(DPI::TEXTCONTENT, DBFReadStringAttribute( dh, i, pointF ) );
    }
    data.insert(DPI::STARTX, *(sobject->padfX));
    data.insert(DPI::STARTY, *(sobject->padfY));
    readAttributes(dh, i);
    data.insert(DPI::LAYER, attdata.layer);
    ent->updateData(&data);
    currDoc->addEntity(ent);
}

void dibSHP::readPolyline(DBFHandle dh, int i){
    int maxPoints;
    Plug_Entity *ent =NULL;
    QHash<int, QVariant> data;
    QList<Plug_VertexData> vl;

    readAttributes(dh, i);
    data.insert(DPI::LAYER, attdata.layer);
    for( int i = 0; i < sobject->nParts; i++ ) {
        if ( (i+1) < sobject->nParts) maxPoints = sobject->panPartStart[i+1];
        else maxPoints = sobject->nVertices;
        vl.clear();
        for( int j = sobject->panPartStart[i]; j < maxPoints; j++ ) {
            vl.append( Plug_VertexData( QPointF(sobject->padfX[j],sobject->padfY[j]), 0.0) );
        }
        if (vl.size() > 2 ) {
            ent = currDoc->newEntity(DPI::POLYLINE);
            ent->updateData(&data);
            currDoc->addEntity(ent);
            ent->updatePolylineData(&vl);
        }
    }
}

void dibSHP::readPolylineC(DBFHandle /*dh*/, int /*i*/){
}

void dibSHP::readMultiPolyline(DBFHandle /*dh*/, int /*i*/){
}

void dibSHP::readAttributes(DBFHandle dh, int i){
    if (layerF >= 0) {
        attdata.layer = DBFReadStringAttribute( dh, i, layerF );
        currDoc->setLayer(attdata.layer);
    }
/*    if (colorF >= 0)
        readColor(dh, i);
    if (ltypeF >= 0)
        readLType(dh, i);
    if (lwidthF >= 0)
        readWidth(dh, i);*/
}

dibSHP::~dibSHP()
{
/*    while (!dataList.isEmpty())
         delete dataList.takeFirst();*/
}

void dibSHP::readSettings()
 {
    QString str;
    QSettings settings(QSettings::IniFormat, QSettings::UserScope, "LibreCAD", "importshp");
    QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
    QSize size = settings.value("size", QSize(325,425)).toSize();
    str = settings.value("lastfile").toString();
    fileedit->setText(str);
    resize(size);
    move(pos);
 }

void dibSHP::writeSettings()
 {
    QSettings settings(QSettings::IniFormat, QSettings::UserScope, "LibreCAD", "importshp");
    settings.setValue("pos", pos());
    settings.setValue("size", size());
    settings.setValue("lastfile", fileedit->text());
 }