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

marble

  • sources
  • kde-4.14
  • kdeedu
  • marble
  • src
  • lib
  • marble
  • geodata
  • handlers
  • kml
KmlCoordinatesTagHandler.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 Patrick Spendrin <ps_ml@gmx.de>
3 
4  This file is part of the KDE project
5 
6  This library is free software you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  aint with this library see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include "KmlCoordinatesTagHandler.h"
23 
24 #include <QStringList>
25 #include <QRegExp>
26 
27 #include "MarbleDebug.h"
28 #include "KmlElementDictionary.h"
29 #include "GeoDataTrack.h"
30 #include "GeoDataPlacemark.h"
31 #include "GeoDataPoint.h"
32 #include "GeoDataModel.h"
33 #include "GeoDataLineString.h"
34 #include "GeoDataLinearRing.h"
35 #include "GeoDataMultiGeometry.h"
36 #include "GeoDataPhotoOverlay.h"
37 #include "GeoDataLatLonQuad.h"
38 #include "GeoParser.h"
39 #include "MarbleGlobal.h"
40 
41 namespace Marble
42 {
43 namespace kml
44 {
45 KML_DEFINE_TAG_HANDLER( coordinates )
46 
47 static const bool kmlStrictSpecs = false;
48 
49 // We can't use KML_DEFINE_TAG_HANDLER_GX22 because the name of the tag ("coord")
50 // and the TagHandler ("KmlcoordinatesTagHandler") don't match
51 static GeoTagHandlerRegistrar s_handlercoordkmlTag_nameSpaceGx22(GeoParser::QualifiedName(kmlTag_coord, kmlTag_nameSpaceGx22 ),
52  new KmlcoordinatesTagHandler());
53 
54 GeoNode* KmlcoordinatesTagHandler::parse( GeoParser& parser ) const
55 {
56  Q_ASSERT( parser.isStartElement()
57  && ( parser.isValidElement( kmlTag_coordinates )
58  || parser.isValidElement( kmlTag_coord ) ) );
59 
60  GeoStackItem parentItem = parser.parentElement();
61 
62  if( parentItem.represents( kmlTag_Point )
63  || parentItem.represents( kmlTag_LineString )
64  || parentItem.represents( kmlTag_MultiGeometry )
65  || parentItem.represents( kmlTag_LinearRing )
66  || parentItem.represents( kmlTag_LatLonQuad ) ) {
67  QStringList coordinatesLines;// = parser.readElementText().trimmed().split( QRegExp("\\s"), QString::SkipEmptyParts );
68  // Splitting using the "\\s" regexp is slow, split manually instead.
69  QString text = parser.readElementText().trimmed();
70 
71  if ( !kmlStrictSpecs ) {
72  // Removing spaces before and after commas
73  for ( int i = 1; i < text.size() - 1; ++i ) {
74  if ( text[i] == ',' ) {
75  // Before
76  int l = i - 1;
77  while ( l > 0 && text[l].isSpace() ) {
78  --l;
79  }
80 
81  // After
82  int r = i + 1;
83  while ( r < text.size() && text[r].isSpace() ) {
84  ++r;
85  }
86 
87  text.remove( l + 1, r - l - 1 ).insert( l + 1, ',' );
88  }
89  }
90  }
91 
92  int index = 0;
93  bool inside = true;
94  int const size = text.size();
95  for ( int i=0; i<size; ++i ) {
96  if ( text[i].isSpace() ) {
97  if ( inside ) {
98  coordinatesLines.append( text.mid( index, i-index ) );
99  inside = false;
100  }
101  index = i+1;
102  } else {
103  inside = true;
104  }
105  }
106  coordinatesLines.append( text.mid( index ) );
107  int coordinatesIndex = 0;
108  Q_FOREACH( const QString& line, coordinatesLines ) {
109  QStringList coordinates = line.trimmed().split( ',' );
110  if ( parentItem.represents( kmlTag_Point ) && parentItem.is<GeoDataFeature>() ) {
111  GeoDataCoordinates coord;
112  if ( coordinates.size() == 2 ) {
113  coord.set( coordinates.at( 0 ).toDouble(),
114  coordinates.at( 1 ).toDouble(), 0.0, GeoDataCoordinates::Degree );
115  } else if( coordinates.size() == 3 ) {
116  coord.set( coordinates.at( 0 ).toDouble(),
117  coordinates.at( 1 ).toDouble(),
118  coordinates.at( 2 ).toDouble(),
119  GeoDataCoordinates::Degree );
120  }
121  parentItem.nodeAs<GeoDataPlacemark>()->setCoordinate( coord );
122  } else {
123  GeoDataCoordinates coord;
124  if ( coordinates.size() == 2 ) {
125  coord.set( DEG2RAD * coordinates.at( 0 ).toDouble(),
126  DEG2RAD * coordinates.at( 1 ).toDouble() );
127  } else if( coordinates.size() == 3 ) {
128  coord.set( DEG2RAD * coordinates.at( 0 ).toDouble(),
129  DEG2RAD * coordinates.at( 1 ).toDouble(),
130  coordinates.at( 2 ).toDouble() );
131  }
132 
133  if ( parentItem.represents( kmlTag_LineString ) ) {
134  parentItem.nodeAs<GeoDataLineString>()->append( coord );
135  } else if ( parentItem.represents( kmlTag_LinearRing ) ) {
136  parentItem.nodeAs<GeoDataLinearRing>()->append( coord );
137  } else if ( parentItem.represents( kmlTag_MultiGeometry ) ) {
138  GeoDataPoint *point = new GeoDataPoint( coord );
139  parentItem.nodeAs<GeoDataMultiGeometry>()->append( point );
140  } else if ( parentItem.represents( kmlTag_Model) ) {
141  parentItem.nodeAs<GeoDataModel>()->setCoordinates( coord);
142  } else if ( parentItem.represents( kmlTag_Point ) ) {
143  // photo overlay
144  parentItem.nodeAs<GeoDataPoint>()->setCoordinates( coord );
145  } else if ( parentItem.represents( kmlTag_LatLonQuad ) ) {
146  switch ( coordinatesIndex ) {
147  case 0:
148  parentItem.nodeAs<GeoDataLatLonQuad>()->setBottomLeft( coord );
149  break;
150  case 1:
151  parentItem.nodeAs<GeoDataLatLonQuad>()->setBottomRight( coord );
152  break;
153  case 2:
154  parentItem.nodeAs<GeoDataLatLonQuad>()->setTopRight( coord );
155  break;
156  case 3:
157  parentItem.nodeAs<GeoDataLatLonQuad>()->setTopLeft( coord );
158  break;
159  case 4:
160  mDebug() << "Ignoring excessive coordinates in LatLonQuad (must not have more than 4 pairs)";
161  break;
162  default:
163  // Silently ignore any more coordinates
164  break;
165  }
166  } else {
167  // raise warning as coordinates out of valid parents found
168  }
169  }
170 
171  ++coordinatesIndex;
172  }
173  }
174 
175  if( parentItem.represents( kmlTag_Track ) ) {
176  QString input = parser.readElementText().trimmed();
177  if ( !kmlStrictSpecs ) {
178  input = input.replace( QRegExp( "\\s*,\\s*" ), "," );
179  }
180  QStringList coordinates = input.split( ' ' );
181 
182  GeoDataCoordinates coord;
183  if ( coordinates.size() == 2 ) {
184  coord.set( DEG2RAD * coordinates.at( 0 ).toDouble(),
185  DEG2RAD * coordinates.at( 1 ).toDouble() );
186  } else if( coordinates.size() == 3 ) {
187  coord.set( DEG2RAD * coordinates.at( 0 ).toDouble(),
188  DEG2RAD * coordinates.at( 1 ).toDouble(),
189  coordinates.at( 2 ).toDouble() );
190  }
191  parentItem.nodeAs<GeoDataTrack>()->appendCoordinates( coord );
192  }
193 
194  return 0;
195 }
196 
197 }
198 }
Marble::GeoDataPoint
A Geometry object representing a 3d point.
Definition: GeoDataPoint.h:47
Marble::kml::kmlTag_LineString
const char * kmlTag_LineString
Definition: KmlElementDictionary.cpp:100
Marble::GeoDataCoordinates
A 3d point representation.
Definition: GeoDataCoordinates.h:52
Marble::kml::kmlTag_nameSpaceGx22
const char * kmlTag_nameSpaceGx22
Definition: KmlElementDictionary.cpp:35
Marble::GeoDataTrack
A geometry for tracking objects made of (time, coordinates) pairs.
Definition: GeoDataTrack.h:54
Marble::GeoStackItem::nodeAs
T * nodeAs()
Definition: GeoParser.h:120
Marble::GeoDataLinearRing
A LinearRing that allows to store a closed, contiguous set of line segments.
Definition: GeoDataLinearRing.h:68
Marble::kml::KmlcoordinatesTagHandler
Definition: KmlCoordinatesTagHandler.h:32
GeoDataLatLonQuad.h
Marble::GeoNode
A shared base class for all classes that are mapped to a specific tag (ie.
Definition: GeoDocument.h:60
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
GeoParser.h
QList::at
const T & at(int i) const
QString::size
int size() const
GeoDataModel.h
Marble::kml::s_handlercoordkmlTag_nameSpaceGx22
static GeoTagHandlerRegistrar s_handlercoordkmlTag_nameSpaceGx22(GeoParser::QualifiedName(kmlTag_coord, kmlTag_nameSpaceGx22), new KmlcoordinatesTagHandler())
QString::remove
QString & remove(int position, int n)
Marble::GeoDataModel
Definition: GeoDataModel.h:29
MarbleDebug.h
GeoDataTrack.h
Marble::GeoDataCoordinates::Degree
Definition: GeoDataCoordinates.h:66
Marble::kml::kmlStrictSpecs
static const bool kmlStrictSpecs
Definition: KmlCoordinatesTagHandler.cpp:47
QList::size
int size() const
Marble::GeoParser
Definition: GeoParser.h:40
Marble::kml::kmlTag_coordinates
const char * kmlTag_coordinates
Definition: KmlElementDictionary.cpp:54
GeoDataMultiGeometry.h
QRegExp
Marble::GeoDataLatLonQuad
Definition: GeoDataLatLonQuad.h:22
Marble::GeoStackItem::represents
bool represents(const char *tagName) const
Definition: GeoParser.h:113
QList::append
void append(const T &value)
QString::insert
QString & insert(int position, QChar ch)
KmlElementDictionary.h
Marble::kml::kmlTag_coord
const char * kmlTag_coord
Definition: KmlElementDictionary.cpp:210
QString::trimmed
QString trimmed() const
GeoDataLineString.h
Marble::GeoStackItem
Definition: GeoParser.h:97
KML_DEFINE_TAG_HANDLER
#define KML_DEFINE_TAG_HANDLER(Name)
Definition: KmlElementDictionary.h:242
QString
MarbleGlobal.h
Marble::GeoDataCoordinates::set
void set(qreal lon, qreal lat, qreal alt=0, GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian)
(re)set the coordinates in a GeoDataCoordinates object
Definition: GeoDataCoordinates.cpp:657
GeoDataPlacemark.h
Marble::DEG2RAD
const qreal DEG2RAD
Definition: MarbleGlobal.h:219
Marble::kml::kmlTag_LatLonQuad
const char * kmlTag_LatLonQuad
Definition: KmlElementDictionary.cpp:97
Marble::GeoDataLineString
A LineString that allows to store a contiguous set of line segments.
Definition: GeoDataLineString.h:75
QStringList
GeoDataLinearRing.h
QString::replace
QString & replace(int position, int n, QChar after)
Marble::kml::kmlTag_LinearRing
const char * kmlTag_LinearRing
Definition: KmlElementDictionary.cpp:99
QString::mid
QString mid(int position, int n) const
GeoDataPoint.h
Marble::kml::kmlTag_MultiGeometry
const char * kmlTag_MultiGeometry
Definition: KmlElementDictionary.cpp:125
Marble::GeoDataFeature
A base class for all geodata features.
Definition: GeoDataFeature.h:57
KmlCoordinatesTagHandler.h
Marble::GeoTagHandlerRegistrar
Definition: GeoTagHandler.h:74
Marble::GeoDataMultiGeometry
Definition: GeoDataMultiGeometry.h:33
Marble::kml::kmlTag_Point
const char * kmlTag_Point
Definition: KmlElementDictionary.cpp:144
Marble::kml::kmlTag_Track
const char * kmlTag_Track
Definition: KmlElementDictionary.cpp:209
GeoDataPhotoOverlay.h
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:36
Marble::GeoStackItem::is
bool is() const
Definition: GeoParser.h:127
Marble::kml::kmlTag_Model
const char * kmlTag_Model
Definition: KmlElementDictionary.cpp:124
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:13:40 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
  • 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