• 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
  • tools
  • poly2kml
tools/poly2kml/main.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 2010 Dennis Nienhüser <earthwings@gentoo.org>
9 //
10 
11 #include <QCoreApplication>
12 #include <QFile>
13 #include <QFileInfo>
14 #include <QTextStream>
15 #include <QTime>
16 #include <QDebug>
17 
18 #include "geodata/parser/GeoDataParser.h"
19 #include "geodata/data/GeoDataLineString.h"
20 #include "geodata/data/GeoDataDocument.h"
21 #include "geodata/data/GeoDataFolder.h"
22 #include "geodata/data/GeoDataPlacemark.h"
23 #include "geodata/data/GeoDataMultiGeometry.h"
24 #include "geodata/data/GeoDataStyle.h"
25 #include "geodata/data/GeoDataStyleMap.h"
26 #include "geodata/data/GeoDataLineStyle.h"
27 #include "geodata/data/GeoDataData.h"
28 #include "geodata/data/GeoDataExtendedData.h"
29 #include "geodata/writer/GeoWriter.h"
30 
31 using namespace Marble;
32 
33 int usage()
34 {
35  qDebug() << "Usage: [options] poly2kml input.poly output.kml";
36  qDebug() << "\tOptions store additional metadata in output.kml:";
37  qDebug() << "\t--version aVersion";
38  qDebug() << "\t--name aName";
39  qDebug() << "\t--date aDate";
40  qDebug() << "\t--payload aFilename";
41  return 1;
42 }
43 
44 QColor randomColor()
45 {
46  QVector<QColor> colors = QVector<QColor>() << Oxygen::aluminumGray4 << Oxygen::brickRed4;
47  colors << Oxygen::hotOrange4 << Oxygen::forestGreen4 << Oxygen::hotOrange4;
48  colors << Oxygen::seaBlue2 << Oxygen::skyBlue4 << Oxygen::sunYellow6;
49  return colors.at( qrand() % colors.size() );
50 }
51 
52 void parseBoundingBox( const QFileInfo &file, const QString &name,
53  const QString &version, const QString &date,
54  const QString &transport, const QString &payload,
55  GeoDataDocument* document )
56 {
57  GeoDataPlacemark* placemark = new GeoDataPlacemark;
58  GeoDataMultiGeometry *geometry = new GeoDataMultiGeometry;
59  QFile input( file.absoluteFilePath() );
60  QString country = "Unknown";
61  if ( input.open( QFile::ReadOnly ) ) {
62  QTextStream stream( &input );
63  country = stream.readLine();
64  float lat( 0.0 ), lon( 0.0 );
65  GeoDataLinearRing *box = new GeoDataLinearRing;
66  while ( !stream.atEnd() ) {
67  bool inside = true;
68  bool skip = false;
69  QString line = stream.readLine().trimmed();
70  QStringList entries = line.split( QLatin1Char( ' ' ), QString::SkipEmptyParts );
71  if ( entries.size() == 1 ) {
72  if ( entries.first() == "END" && inside ) {
73  inside = false;
74  if (!box->isEmpty()) {
75  geometry->append(box);
76  box = new GeoDataLinearRing;
77  }
78  } else if ( entries.first() == "END" && !inside ) {
79  qDebug() << "END not expected here";
80  } else if ( entries.first().startsWith( QLatin1String( "!" ) ) ) {
81  skip = true;
82  qDebug() << "Warning: Negative polygons not supported, skipping";
83  } else {
84  //int number = entries.first().toInt();
85  inside = true;
86  }
87  } else if ( entries.size() == 2 ) {
88  lon = entries.first().toDouble();
89  lat = entries.last().toDouble();
90  GeoDataCoordinates point( lon, lat, 0.0, GeoDataCoordinates::Degree );
91  *box << point;
92  } else {
93  qDebug() << "Warning: Ignoring line in" << file.absoluteFilePath()
94  << "with" << entries.size() << "fields:" << line;
95  }
96  }
97  }
98 
99  GeoDataStyle style;
100  GeoDataLineStyle lineStyle;
101  QColor color = randomColor();
102  color.setAlpha( 200 );
103  lineStyle.setColor( color );
104  lineStyle.setWidth( 4 );
105  style.setLineStyle(lineStyle);
106  style.setStyleId("border");
107 
108  GeoDataStyleMap styleMap;
109  styleMap.setStyleId("map-border");
110  styleMap.insert("normal", QString("#").append(style.styleId()));
111  document->addStyleMap(styleMap);
112  document->addStyle(style);
113 
114  placemark->setStyleUrl(QString("#").append(styleMap.styleId()));
115 
116  placemark->setName( name );
117  if ( !version.isEmpty() ) {
118  placemark->extendedData().addValue( GeoDataData( "version", version ) );
119  }
120  if ( !date.isEmpty() ) {
121  placemark->extendedData().addValue( GeoDataData( "date", date ) );
122  }
123  if ( !transport.isEmpty() ) {
124  placemark->extendedData().addValue( GeoDataData( "transport", transport ) );
125  }
126  if ( !payload.isEmpty() ) {
127  placemark->extendedData().addValue( GeoDataData( "payload", payload ) );
128  }
129  placemark->setGeometry(geometry);
130  document->append(placemark);
131 }
132 
133 int save( GeoDataDocument* document, const QFileInfo &filename )
134 {
135  GeoWriter writer;
136  writer.setDocumentType( "http://earth.google.com/kml/2.2" );
137 
138  QFile file( filename.absoluteFilePath() );
139  if ( !file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
140  {
141  qDebug() << "Cannot write to " << file.fileName();
142  return usage();
143  }
144 
145  if ( !writer.write( &file, document ) ) {
146  qDebug() << "Can not write to " << file.fileName();
147  }
148  file.close();
149  return 0;
150 }
151 
152 int main( int argc, char* argv[] )
153 {
154  QCoreApplication app( argc, argv );
155  if ( argc < 3 ) {
156  usage();
157  return 0;
158  }
159 
160  QString inputFile = argv[argc-2];
161  QString outputFile = argv[argc-1];
162  QString name;
163  QString version;
164  QString date;
165  QString transport;
166  QString payload;
167  for ( int i=1; i<argc-2; ++i ) {
168  QString arg( argv[i] );
169  if ( arg == "--name" ) {
170  name = argv[++i];
171  } else if ( arg == "--version" ) {
172  version = argv[++i];
173  } else if ( arg == "--date" ) {
174  date = argv[++i];
175  } else if ( arg == "--transport" ) {
176  transport = argv[++i];
177  } else if ( arg == "--payload" ) {
178  payload = argv[++i];
179  } else {
180  usage();
181  return 1;
182  }
183  }
184 
185  qsrand( QTime::currentTime().msec() );
186  QFileInfo input( inputFile );
187  if ( !input.exists() || !input.isFile() ) {
188  qDebug() << "Invalid input file";
189  return usage();
190  }
191 
192  GeoDataDocument document;
193  parseBoundingBox( input, name, version, date, transport, payload, &document );
194  return save( &document, QFileInfo( outputFile ) );
195 }
GeoDataDocument.h
Marble::GeoDataLineStyle
specifies the style how lines are drawn
Definition: GeoDataLineStyle.h:36
Marble::GeoDataCoordinates
A 3d point representation.
Definition: GeoDataCoordinates.h:52
Marble::GeoDataDocument::addStyleMap
void addStyleMap(const GeoDataStyleMap &map)
Add a stylemap to the stylemap storage.
Definition: GeoDataDocument.cpp:137
Marble::GeoDataStyleSelector::setStyleId
void setStyleId(const QString &value)
Set a new style id.
Definition: GeoDataStyleSelector.cpp:60
Marble::GeoDataDocument
A container for Features, Styles and in the future Schemas.
Definition: GeoDataDocument.h:64
Marble::GeoDataLinearRing
A LinearRing that allows to store a closed, contiguous set of line segments.
Definition: GeoDataLinearRing.h:68
Marble::GeoDataColorStyle::setColor
void setColor(const QColor &value)
Set a new color.
Definition: GeoDataColorStyle.cpp:73
GeoDataStyle.h
usage
int usage()
Definition: tools/poly2kml/main.cpp:33
GeoDataParser.h
Marble::GeoDataStyleMap
a class to map different styles to one style
Definition: GeoDataStyleMap.h:38
GeoDataExtendedData.h
GeoDataLineStyle.h
Marble::GeoDataCoordinates::Degree
Definition: GeoDataCoordinates.h:66
Marble::Oxygen::hotOrange4
QColor const hotOrange4
Definition: MarbleColors.h:86
GeoWriter.h
GeoDataMultiGeometry.h
Marble::GeoDataStyle
an addressable style group
Definition: GeoDataStyle.h:55
Marble::GeoDataFeature::setName
void setName(const QString &value)
Set a new name for this feature.
Definition: GeoDataFeature.cpp:485
Marble::Oxygen::aluminumGray4
QColor const aluminumGray4
Definition: MarbleColors.h:92
Marble::GeoDataLineStyle::setWidth
void setWidth(const float &width)
Set the width of the line.
Definition: GeoDataLineStyle.cpp:76
Marble::GeoWriter
Standard Marble way of writing XML This class is intended to be a standardised way of writing XML for...
Definition: GeoWriter.h:28
randomColor
QColor randomColor()
Definition: tools/poly2kml/main.cpp:44
GeoDataLineString.h
Marble::Oxygen::sunYellow6
QColor const sunYellow6
Definition: MarbleColors.h:78
Marble::GeoDataLineString::first
GeoDataCoordinates & first()
Returns a reference to the first node in the LineString. This method detaches the returned coordinate...
Definition: GeoDataLineString.cpp:173
Marble::GeoDataDocument::addStyle
void addStyle(const GeoDataStyle &style)
Add a style to the style storage.
Definition: GeoDataDocument.cpp:110
Marble::Oxygen::seaBlue2
QColor const seaBlue2
Definition: MarbleColors.h:64
Marble::Oxygen::skyBlue4
QColor const skyBlue4
Definition: MarbleColors.h:56
GeoDataPlacemark.h
GeoDataStyleMap.h
Marble::GeoDataContainer::append
void append(GeoDataFeature *other)
add an element
Definition: GeoDataContainer.cpp:165
Marble::GeoDataFeature::setStyleUrl
void setStyleUrl(const QString &value)
Set the styleUrl of this feature to value.
Definition: GeoDataFeature.cpp:556
Marble::GeoDataMultiGeometry::append
void append(GeoDataGeometry *other)
add an element
Definition: GeoDataMultiGeometry.cpp:182
parseBoundingBox
void parseBoundingBox(const QFileInfo &file, const QString &name, const QString &version, const QString &date, const QString &transport, const QString &payload, GeoDataDocument *document)
Definition: tools/poly2kml/main.cpp:52
Marble::GeoWriter::setDocumentType
void setDocumentType(const QString &documentType)
Set the current document type.
Definition: GeoWriter.cpp:79
QCoreApplication
GeoDataFolder.h
Marble::GeoDataStyle::setLineStyle
void setLineStyle(const GeoDataLineStyle &style)
set the line style
Definition: GeoDataStyle.cpp:88
Marble::Oxygen::brickRed4
QColor const brickRed4
Definition: MarbleColors.h:32
Marble::GeoDataData
Definition: GeoDataData.h:26
GeoDataData.h
Marble::GeoDataLineString::isEmpty
bool isEmpty() const
Returns whether the LineString has no nodes at all.
Definition: GeoDataLineString.cpp:129
Marble::GeoDataExtendedData::addValue
void addValue(const GeoDataData &data)
add a data object to the GeoDataExtendedData with the key
Definition: GeoDataExtendedData.cpp:59
Marble::Oxygen::forestGreen4
QColor const forestGreen4
Definition: MarbleColors.h:74
Marble::GeoDataMultiGeometry
Definition: GeoDataMultiGeometry.h:33
Marble::GeoDataStyleSelector::styleId
QString styleId() const
Return the style id.
Definition: GeoDataStyleSelector.cpp:65
Marble::GeoWriter::write
bool write(QIODevice *device, const GeoNode *feature)
The main API call to use the XML writer.
Definition: GeoWriter.cpp:28
main
int main(int argc, char *argv[])
Definition: tools/poly2kml/main.cpp:152
Marble::GeoDataFeature::extendedData
GeoDataExtendedData & extendedData() const
Return the ExtendedData assigned to the feature.
Definition: GeoDataFeature.cpp:653
save
int save(GeoDataDocument *document, const QFileInfo &filename)
Definition: tools/poly2kml/main.cpp:133
Marble::GeoDataPlacemark
a class representing a point of interest on the map
Definition: GeoDataPlacemark.h:54
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:51 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