7 #include "KmlCoordinatesTagHandler.h"
12 #include "MarbleDebug.h"
13 #include "KmlElementDictionary.h"
14 #include "GeoDataTrack.h"
15 #include "GeoDataPlacemark.h"
16 #include "GeoDataPoint.h"
17 #include "GeoDataModel.h"
18 #include "GeoDataLineString.h"
19 #include "GeoDataLinearRing.h"
20 #include "GeoDataMultiGeometry.h"
21 #include "GeoDataLatLonQuad.h"
22 #include "GeoParser.h"
23 #include "MarbleGlobal.h"
29 KML_DEFINE_TAG_HANDLER( coordinates )
31 static const bool kmlStrictSpecs =
false;
35 static GeoTagHandlerRegistrar s_handlercoordkmlTag_nameSpaceGx22(GeoParser::QualifiedName(
QLatin1String(kmlTag_coord),
QLatin1String(kmlTag_nameSpaceGx22)),
36 new KmlcoordinatesTagHandler());
38 GeoNode* KmlcoordinatesTagHandler::parse( GeoParser& parser )
const
40 Q_ASSERT(parser.isStartElement()
44 GeoStackItem parentItem = parser.parentElement();
46 if( parentItem.represents( kmlTag_Point )
47 || parentItem.represents( kmlTag_LineString )
48 || parentItem.represents( kmlTag_MultiGeometry )
49 || parentItem.represents( kmlTag_LinearRing )
50 || parentItem.represents( kmlTag_LatLonQuad ) ) {
55 if ( !kmlStrictSpecs ) {
57 for (
int i = 1; i < text.
size() - 1; ++i ) {
61 while ( l > 0 && text[l].isSpace() ) {
67 while ( r < text.
size() && text[r].isSpace() ) {
78 int const size = text.
size();
79 for (
int i=0; i<size; ++i ) {
80 if ( text[i].isSpace() ) {
82 coordinatesLines.
append( text.
mid( index, i-index ) );
90 coordinatesLines.
append( text.
mid( index ) );
91 int coordinatesIndex = 0;
92 for(
const QString& line: coordinatesLines ) {
94 if ( parentItem.represents( kmlTag_Point ) && parentItem.is<GeoDataFeature>() ) {
95 GeoDataCoordinates coord;
96 if ( coordinates.
size() == 2 ) {
97 coord.set( coordinates.
at( 0 ).toDouble(),
98 coordinates.
at( 1 ).toDouble(), 0.0, GeoDataCoordinates::Degree );
99 }
else if( coordinates.
size() == 3 ) {
100 coord.set( coordinates.
at( 0 ).toDouble(),
101 coordinates.
at( 1 ).toDouble(),
102 coordinates.
at( 2 ).toDouble(),
103 GeoDataCoordinates::Degree );
105 parentItem.nodeAs<GeoDataPlacemark>()->setCoordinate( coord );
107 GeoDataCoordinates coord;
108 if ( coordinates.
size() == 2 ) {
109 coord.set( DEG2RAD * coordinates.
at( 0 ).toDouble(),
110 DEG2RAD * coordinates.
at( 1 ).toDouble() );
111 }
else if( coordinates.
size() == 3 ) {
112 coord.set( DEG2RAD * coordinates.
at( 0 ).toDouble(),
113 DEG2RAD * coordinates.
at( 1 ).toDouble(),
114 coordinates.
at( 2 ).toDouble() );
117 if ( parentItem.represents( kmlTag_LineString ) ) {
118 parentItem.nodeAs<GeoDataLineString>()->append( coord );
119 }
else if ( parentItem.represents( kmlTag_LinearRing ) ) {
120 parentItem.nodeAs<GeoDataLinearRing>()->append( coord );
121 }
else if ( parentItem.represents( kmlTag_MultiGeometry ) ) {
122 GeoDataPoint *point =
new GeoDataPoint( coord );
123 parentItem.nodeAs<GeoDataMultiGeometry>()->append( point );
124 }
else if ( parentItem.represents( kmlTag_Model) ) {
125 parentItem.nodeAs<GeoDataModel>()->setCoordinates( coord);
126 }
else if ( parentItem.represents( kmlTag_Point ) ) {
128 parentItem.nodeAs<GeoDataPoint>()->setCoordinates( coord );
129 }
else if ( parentItem.represents( kmlTag_LatLonQuad ) ) {
130 switch ( coordinatesIndex ) {
132 parentItem.nodeAs<GeoDataLatLonQuad>()->setBottomLeft( coord );
135 parentItem.nodeAs<GeoDataLatLonQuad>()->setBottomRight( coord );
138 parentItem.nodeAs<GeoDataLatLonQuad>()->setTopRight( coord );
141 parentItem.nodeAs<GeoDataLatLonQuad>()->setTopLeft( coord );
144 mDebug() <<
"Ignoring excessive coordinates in LatLonQuad (must not have more than 4 pairs)";
159 if( parentItem.represents( kmlTag_Track ) ) {
161 if ( !kmlStrictSpecs ) {
162 input.
replace(
QRegExp(QStringLiteral(
"\\s*,\\s*")), QStringLiteral(
","));
166 GeoDataCoordinates coord;
167 if ( coordinates.
size() == 2 ) {
168 coord.set( DEG2RAD * coordinates.
at( 0 ).toDouble(),
169 DEG2RAD * coordinates.
at( 1 ).toDouble() );
170 }
else if( coordinates.
size() == 3 ) {
171 coord.set( DEG2RAD * coordinates.
at( 0 ).toDouble(),
172 DEG2RAD * coordinates.
at( 1 ).toDouble(),
173 coordinates.
at( 2 ).toDouble() );
175 parentItem.nodeAs<GeoDataTrack>()->appendCoordinates( coord );