~ubuntu-branches/ubuntu/saucy/merkaartor/saucy

« back to all changes in this revision

Viewing changes to ImportExport/ImportExportSHP.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Bernd Zeimetz
  • Date: 2009-09-13 00:52:12 UTC
  • mto: (1.2.7 upstream) (0.1.3 upstream) (3.1.7 sid)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: james.westby@ubuntu.com-20090913005212-pjecal8zxm07x0fj
ImportĀ upstreamĀ versionĀ 0.14+svnfixes~20090912

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// C++ Implementation: ImportExportSHP
3
 
//
4
 
// Description:
5
 
//
6
 
//
7
 
// Author: cbro <cbro@semperpax.com>, (C) 2008
8
 
//
9
 
// Copyright: See COPYING file that comes with this distribution
10
 
//
11
 
//
12
 
 
13
 
#include <QtGui>
14
 
 
15
 
#include "../ImportExport/ImportExportSHP.h"
16
 
 
17
 
#include "ogrsf_frmts.h"
18
 
 
19
 
#include <QDir>
20
 
 
21
 
bool parseContainer(QDomElement& e, MapLayer* aLayer);
22
 
 
23
 
ImportExportSHP::ImportExportSHP(MapDocument* doc)
24
 
 : IImportExport(doc)
25
 
{
26
 
}
27
 
 
28
 
 
29
 
ImportExportSHP::~ImportExportSHP()
30
 
{
31
 
}
32
 
 
33
 
// Specify the input as a QFile
34
 
bool ImportExportSHP::loadFile(QString filename)
35
 
{
36
 
        FileName = filename;
37
 
 
38
 
        return true;
39
 
}
40
 
 
41
 
bool ImportExportSHP::saveFile(QString filename)
42
 
{
43
 
        return false;
44
 
}
45
 
 
46
 
 
47
 
// export
48
 
bool ImportExportSHP::export_(const QVector<MapFeature *>& featList)
49
 
{
50
 
        Q_UNUSED(featList);
51
 
 
52
 
        return false;
53
 
}
54
 
 
55
 
// IMPORT
56
 
 
57
 
void parseGeometry(MapLayer* aLayer, OGRGeometry *poGeometry)
58
 
{
59
 
        if ( wkbFlatten(poGeometry->getGeometryType()) == wkbPoint )
60
 
        {
61
 
                OGRPoint *poPoint = (OGRPoint *) poGeometry;
62
 
 
63
 
                TrackPoint* N = new TrackPoint(Coord(angToInt(poPoint->getY()), angToInt(poPoint->getX())));
64
 
 
65
 
                aLayer->add(N);
66
 
        } else
67
 
        if ( wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon )
68
 
        {
69
 
                OGRPolygon  *poPoly = (OGRPolygon *) poGeometry;
70
 
                OGRLinearRing *poRing = poPoly->getExteriorRing();
71
 
                OGRPoint p;
72
 
 
73
 
                if(int numNode = poRing->getNumPoints()) {
74
 
                        Road* R = new Road();
75
 
                        for(int i=0; i<numNode; i++) {
76
 
                                poRing->getPoint(i, &p);
77
 
                                TrackPoint* N = new TrackPoint(Coord(angToInt(p.getY()), angToInt(p.getX())));
78
 
                                aLayer->add(N);
79
 
 
80
 
                                R->add(N);
81
 
                        }
82
 
                        aLayer->add(R);
83
 
                }
84
 
        } else
85
 
        if ( wkbFlatten(poGeometry->getGeometryType()) == wkbLineString )
86
 
        {
87
 
                OGRLineString  *poLS = (OGRLineString *) poGeometry;
88
 
                OGRPoint p;
89
 
 
90
 
                if(int numNode = poLS->getNumPoints()) {
91
 
                        Road* R = new Road();
92
 
                        for(int i=0; i<numNode; i++) {
93
 
                                poLS->getPoint(i, &p);
94
 
                                TrackPoint* N = new TrackPoint(Coord(angToInt(p.getY()), angToInt(p.getX())));
95
 
                                aLayer->add(N);
96
 
 
97
 
                                R->add(N);
98
 
                        }
99
 
                        aLayer->add(R);
100
 
                }
101
 
        } else
102
 
        if (
103
 
                ( wkbFlatten(poGeometry->getGeometryType()) == wkbMultiPolygon  ) ||
104
 
                ( wkbFlatten(poGeometry->getGeometryType()) == wkbMultiLineString  ) ||
105
 
                ( wkbFlatten(poGeometry->getGeometryType()) == wkbMultiPoint  )
106
 
                )
107
 
        {
108
 
                OGRGeometryCollection  *poCol = (OGRGeometryCollection *) poGeometry;
109
 
 
110
 
                if(int numCol = poCol->getNumGeometries()) {
111
 
                        for(int i=0; i<numCol; i++) {
112
 
                                parseGeometry(aLayer, poCol->getGeometryRef(i));
113
 
                        }
114
 
                }
115
 
        }
116
 
}
117
 
 
118
 
// import the  input
119
 
bool ImportExportSHP::import(MapLayer* aLayer)
120
 
{
121
 
        OGRRegisterAll();
122
 
 
123
 
        OGRDataSource       *poDS;
124
 
 
125
 
        poDS = OGRSFDriverRegistrar::Open( FileName.toUtf8().constData(), FALSE );
126
 
        if( poDS == NULL )
127
 
        {
128
 
                qDebug( "SHP Open failed.\n" );
129
 
                return false;
130
 
        }
131
 
 
132
 
        OGRLayer  *poLayer;
133
 
 
134
 
        //poLayer = poDS->GetLayerByName( "point" );
135
 
        poLayer = poDS->GetLayer( 0 );
136
 
 
137
 
        OGRFeature *poFeature;
138
 
 
139
 
        poLayer->ResetReading();
140
 
        while( (poFeature = poLayer->GetNextFeature()) != NULL )
141
 
        {
142
 
                //OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
143
 
                //int iField;
144
 
 
145
 
                //for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ )
146
 
                //{
147
 
                //      OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn( iField );
148
 
 
149
 
                //      if( poFieldDefn->GetType() == OFTInteger )
150
 
                //              qDebug( "%d,", poFeature->GetFieldAsInteger( iField ) );
151
 
                //      else if( poFieldDefn->GetType() == OFTReal )
152
 
                //              qDebug( "%.3f,", poFeature->GetFieldAsDouble(iField) );
153
 
                //      else if( poFieldDefn->GetType() == OFTString )
154
 
                //              qDebug( "%s,", poFeature->GetFieldAsString(iField) );
155
 
                //      else
156
 
                //              qDebug( "%s,", poFeature->GetFieldAsString(iField) );
157
 
                //}
158
 
 
159
 
                OGRGeometry *poGeometry;
160
 
 
161
 
                poGeometry = poFeature->GetGeometryRef();
162
 
                if( poGeometry != NULL) {
163
 
                        // qDebug( "GeometryType : %d,", poGeometry->getGeometryType() );
164
 
 
165
 
                        parseGeometry(aLayer, poGeometry);
166
 
                }
167
 
                else
168
 
                {
169
 
                        qDebug( "no geometry\n" );
170
 
                }
171
 
 
172
 
                OGRFeature::DestroyFeature( poFeature );
173
 
        }
174
 
 
175
 
        OGRDataSource::DestroyDataSource( poDS );
176
 
 
177
 
        return true;
178
 
}
179