• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

marble

  • sources
  • kde-4.12
  • kdeedu
  • marble
  • src
  • plugins
  • runner
  • shp
ShpRunner.cpp
Go to the documentation of this file.
1 //
2 // This file is part of the Marble Virtual Globe.
3 //
4 // This program is free software licensed under the GNU LGPL. You can
5 // find a copy of this license in LICENSE.txt in the top directory of
6 // the source code.
7 //
8 // Copyright 2011 Thibaut Gridel <tgridel@free.fr>
9 
10 #include "ShpRunner.h"
11 
12 #include "GeoDataDocument.h"
13 #include "GeoDataPlacemark.h"
14 #include "GeoDataPolygon.h"
15 #include "MarbleDebug.h"
16 
17 #include <QFileInfo>
18 
19 #include <shapefil.h>
20 
21 namespace Marble
22 {
23 
24 ShpRunner::ShpRunner(QObject *parent) :
25  ParsingRunner(parent)
26 {
27 }
28 
29 ShpRunner::~ShpRunner()
30 {
31 }
32 
33 void ShpRunner::parseFile( const QString &fileName, DocumentRole role = UnknownDocument )
34 {
35  QFileInfo fileinfo( fileName );
36  if( fileinfo.suffix().compare( "shp", Qt::CaseInsensitive ) != 0 ) {
37  emit parsingFinished( 0 );
38  return;
39  }
40 
41  SHPHandle handle = SHPOpen( fileName.toStdString().c_str(), "rb" );
42  if ( !handle ) {
43  emit parsingFinished( 0 );
44  return;
45  }
46  int entities;
47  int shapeType;
48  SHPGetInfo( handle, &entities, &shapeType, NULL, NULL );
49  mDebug() << " SHP info " << entities << " Entities "
50  << shapeType << " Shape Type ";
51 
52  DBFHandle dbfhandle;
53  dbfhandle = DBFOpen( fileName.toStdString().c_str(), "rb");
54  int nameField = DBFGetFieldIndex( dbfhandle, "Name" );
55  int noteField = DBFGetFieldIndex( dbfhandle, "Note" );
56 
57  GeoDataDocument *document = new GeoDataDocument;
58  document->setDocumentRole( role );
59 
60  for ( int i=0; i< entities; ++i ) {
61  GeoDataPlacemark *placemark = 0;
62  placemark = new GeoDataPlacemark;
63  document->append( placemark );
64 
65  SHPObject *shape = SHPReadObject( handle, i );
66  if( nameField ) {
67  const char* info = DBFReadStringAttribute( dbfhandle, i, nameField );
68  placemark->setName( info );
69  mDebug() << "name " << placemark->name();
70  }
71  if( noteField ) {
72  const char* note = DBFReadStringAttribute( dbfhandle, i, noteField );
73  placemark->setDescription( note );
74  mDebug() << "desc " << placemark->description();
75  }
76 
77  switch ( shapeType ) {
78  case SHPT_POINT: {
79  GeoDataPoint *point = new GeoDataPoint( *shape->padfX, *shape->padfY, 0, GeoDataCoordinates::Degree );
80  placemark->setGeometry( point );
81  mDebug() << "point " << placemark->name();
82  break;
83  }
84 
85  case SHPT_MULTIPOINT: {
86  GeoDataMultiGeometry *geom = new GeoDataMultiGeometry;
87  for( int j=0; j<shape->nVertices; ++j ) {
88  geom->append( new GeoDataPoint( GeoDataCoordinates(
89  shape->padfX[j], shape->padfY[j],
90  0, GeoDataCoordinates::Degree ) ) );
91  }
92  placemark->setGeometry( geom );
93  mDebug() << "multipoint " << placemark->name();
94  break;
95  }
96 
97  case SHPT_ARC: {
98  if ( shape->nParts != 1 ) {
99  GeoDataMultiGeometry *geom = new GeoDataMultiGeometry;
100  for( int j=0; j<shape->nParts-1; ++j ) {
101  GeoDataLineString *line = new GeoDataLineString;
102  for( int k=shape->panPartStart[j]; k<shape->panPartStart[j+1]; ++k ) {
103  line->append( GeoDataCoordinates(
104  shape->padfX[k], shape->padfY[k],
105  0, GeoDataCoordinates::Degree ) );
106  }
107  geom->append( line );
108  }
109  placemark->setGeometry( geom );
110  mDebug() << "arc " << placemark->name() << " " << shape->nParts;
111 
112  } else {
113  GeoDataLineString *line = new GeoDataLineString;
114  for( int j=0; j<shape->nVertices; ++j ) {
115  line->append( GeoDataCoordinates(
116  shape->padfX[j], shape->padfY[j],
117  0, GeoDataCoordinates::Degree ) );
118  }
119  placemark->setGeometry( line );
120  mDebug() << "arc " << placemark->name() << " " << shape->nParts;
121  }
122  break;
123  }
124 
125  case SHPT_POLYGON: {
126  if ( shape->nParts != 1 ) {
127  GeoDataPolygon *poly = new GeoDataPolygon;
128  for( int j=0; j<shape->nParts-1; ++j ) {
129  GeoDataLinearRing ring;
130  for( int k=shape->panPartStart[j]; k<shape->panPartStart[j+1]; ++k ) {
131  ring.append( GeoDataCoordinates(
132  shape->padfX[k], shape->padfY[k],
133  0, GeoDataCoordinates::Degree ) );
134  }
135  // TODO: outer boundary per SHP spec is for the clockwise ring
136  // and inner holes are anticlockwise
137  if ( j==0 ) {
138  poly->setOuterBoundary( ring );
139  } else {
140  poly->appendInnerBoundary( ring );
141  }
142  }
143  placemark->setGeometry( poly );
144  mDebug() << "donut " << placemark->name() << " " << shape->nParts;
145 
146  } else {
147  GeoDataPolygon *poly = new GeoDataPolygon;
148  GeoDataLinearRing ring;
149  for( int j=0; j<shape->nVertices; ++j ) {
150  ring.append( GeoDataCoordinates(
151  shape->padfX[j], shape->padfY[j],
152  0, GeoDataCoordinates::Degree ) );
153  }
154  poly->setOuterBoundary( ring );
155  placemark->setGeometry( poly );
156  mDebug() << "poly " << placemark->name() << " " << shape->nParts;
157  }
158  break;
159  }
160  }
161 
162  }
163 
164  SHPClose( handle );
165 
166  DBFClose( dbfhandle );
167 
168  if ( document->size() ) {
169  document->setFileName( fileName );
170  emit parsingFinished( document );
171  } else {
172  delete document;
173  emit parsingFinished( 0 );
174  }
175 }
176 
177 }
178 
179 #include "ShpRunner.moc"
GeoDataDocument.h
Marble::GeoDataPoint
A Geometry object representing a 3d point.
Definition: GeoDataPoint.h:47
Marble::GeoDataCoordinates
A 3d point representation.
Definition: GeoDataCoordinates.h:52
Marble::GeoDataDocument
A container for Features, Styles and in the future Schemas.
Definition: GeoDataDocument.h:64
Marble::GeoDataDocument::setDocumentRole
void setDocumentRole(DocumentRole role)
Definition: GeoDataDocument.cpp:62
Marble::GeoDataLinearRing
A LinearRing that allows to store a closed, contiguous set of line segments.
Definition: GeoDataLinearRing.h:68
GeoDataPolygon.h
Marble::GeoDataFeature::setDescription
void setDescription(const QString &value)
Set the description of this feature to value.
Definition: GeoDataFeature.cpp:518
ShpRunner.h
Marble::GeoDataFeature::description
QString description() const
Return the text description of the feature.
Definition: GeoDataFeature.cpp:513
QObject
MarbleDebug.h
Marble::GeoDataCoordinates::Degree
Definition: GeoDataCoordinates.h:66
Marble::UnknownDocument
Definition: GeoDataDocument.h:40
Marble::ParsingRunner::parsingFinished
void parsingFinished(GeoDataDocument *document, const QString &error=QString())
File parsing is finished, result in the given document object.
Marble::GeoDataFeature::setName
void setName(const QString &value)
Set a new name for this feature.
Definition: GeoDataFeature.cpp:485
Marble::GeoDataPolygon
A polygon that can have "holes".
Definition: GeoDataPolygon.h:81
Marble::ShpRunner::parseFile
virtual void parseFile(const QString &fileName, DocumentRole role)
Start a file parsing.
Definition: ShpRunner.cpp:33
Marble::GeoDataContainer::size
int size() const
size of the container
Definition: GeoDataContainer.cpp:179
Marble::ShpRunner::~ShpRunner
~ShpRunner()
Definition: ShpRunner.cpp:29
GeoDataPlacemark.h
Marble::GeoDataLineString::append
void append(const GeoDataCoordinates &position)
Appends a given geodesic position as a new node to the LineString.
Definition: GeoDataLineString.cpp:221
Marble::GeoDataLineString
A LineString that allows to store a contiguous set of line segments.
Definition: GeoDataLineString.h:75
Marble::ParsingRunner
Definition: ParsingRunner.h:23
Marble::GeoDataContainer::append
void append(GeoDataFeature *other)
add an element
Definition: GeoDataContainer.cpp:165
Marble::GeoDataMultiGeometry::append
void append(GeoDataGeometry *other)
add an element
Definition: GeoDataMultiGeometry.cpp:182
Marble::GeoDataPolygon::appendInnerBoundary
void appendInnerBoundary(const GeoDataLinearRing &boundary)
Appends a given LinearRing as an inner boundary of the Polygon.
Definition: GeoDataPolygon.cpp:111
Marble::GeoDataDocument::setFileName
void setFileName(const QString &value)
Set a new file name for this document.
Definition: GeoDataDocument.cpp:82
Marble::GeoDataFeature::name
QString name() const
The name of the feature.
Definition: GeoDataFeature.cpp:480
Marble::GeoDataMultiGeometry
Definition: GeoDataMultiGeometry.h:33
Marble::ShpRunner::ShpRunner
ShpRunner(QObject *parent=0)
Definition: ShpRunner.cpp:24
Marble::GeoDataPlacemark
a class representing a point of interest on the map
Definition: GeoDataPlacemark.h:54
Marble::mDebug
QDebug mDebug()
a function to replace qDebug() in Marble library code
Definition: MarbleDebug.cpp:31
Marble::DocumentRole
DocumentRole
Definition: GeoDataDocument.h:39
Marble::GeoDataPolygon::setOuterBoundary
void setOuterBoundary(const GeoDataLinearRing &boundary)
Sets the given LinearRing as an outer boundary of the Polygon.
Definition: GeoDataPolygon.cpp:95
Marble::GeoDataPlacemark::setGeometry
void setGeometry(GeoDataGeometry *entry)
Sets the current Geometry of this Placemark.
Definition: GeoDataPlacemark.cpp:136
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:38:52 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal