• 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
  • 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/data/GeoDataLineString.h"
19 #include "geodata/data/GeoDataDocument.h"
20 #include "geodata/data/GeoDataFolder.h"
21 #include "geodata/data/GeoDataPlacemark.h"
22 #include "geodata/data/GeoDataMultiGeometry.h"
23 #include "geodata/data/GeoDataStyle.h"
24 #include "geodata/data/GeoDataStyleMap.h"
25 #include "geodata/data/GeoDataLineStyle.h"
26 #include "geodata/data/GeoDataData.h"
27 #include "geodata/data/GeoDataExtendedData.h"
28 #include "geodata/writer/GeoWriter.h"
29 #include "geodata/handlers/kml/KmlElementDictionary.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  QString line = stream.readLine().trimmed();
69  QStringList entries = line.split( QLatin1Char( ' ' ), QString::SkipEmptyParts );
70  if ( entries.size() == 1 ) {
71  if ( entries.first() == "END" && inside ) {
72  inside = false;
73  if (!box->isEmpty()) {
74  geometry->append(box);
75  box = new GeoDataLinearRing;
76  }
77  } else if ( entries.first() == "END" && !inside ) {
78  qDebug() << "END not expected here";
79  } else if ( entries.first().startsWith( QLatin1String( "!" ) ) ) {
80  qDebug() << "Warning: Negative polygons not supported, skipping";
81  } else {
82  //int number = entries.first().toInt();
83  inside = true;
84  }
85  } else if ( entries.size() == 2 ) {
86  lon = entries.first().toDouble();
87  lat = entries.last().toDouble();
88  GeoDataCoordinates point( lon, lat, 0.0, GeoDataCoordinates::Degree );
89  *box << point;
90  } else {
91  qDebug() << "Warning: Ignoring line in" << file.absoluteFilePath()
92  << "with" << entries.size() << "fields:" << line;
93  }
94  }
95  }
96 
97  GeoDataStyle style;
98  GeoDataLineStyle lineStyle;
99  QColor color = randomColor();
100  color.setAlpha( 200 );
101  lineStyle.setColor( color );
102  lineStyle.setWidth( 4 );
103  style.setLineStyle(lineStyle);
104  style.setId("border");
105 
106  GeoDataStyleMap styleMap;
107  styleMap.setId("map-border");
108  styleMap.insert("normal", QString("#").append(style.id()));
109  document->addStyleMap(styleMap);
110  document->addStyle(style);
111 
112  placemark->setStyleUrl(QString("#").append(styleMap.id()));
113 
114  placemark->setName( name );
115  if ( !version.isEmpty() ) {
116  placemark->extendedData().addValue( GeoDataData( "version", version ) );
117  }
118  if ( !date.isEmpty() ) {
119  placemark->extendedData().addValue( GeoDataData( "date", date ) );
120  }
121  if ( !transport.isEmpty() ) {
122  placemark->extendedData().addValue( GeoDataData( "transport", transport ) );
123  }
124  if ( !payload.isEmpty() ) {
125  placemark->extendedData().addValue( GeoDataData( "payload", payload ) );
126  }
127  placemark->setGeometry(geometry);
128  document->append(placemark);
129 }
130 
131 int save( GeoDataDocument* document, const QFileInfo &filename )
132 {
133  GeoWriter writer;
134  writer.setDocumentType( kml::kmlTag_nameSpaceOgc22 );
135 
136  QFile file( filename.absoluteFilePath() );
137  if ( !file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
138  {
139  qDebug() << "Cannot write to " << file.fileName();
140  return usage();
141  }
142 
143  if ( !writer.write( &file, document ) ) {
144  qDebug() << "Can not write to " << file.fileName();
145  }
146  file.close();
147  return 0;
148 }
149 
150 int main( int argc, char* argv[] )
151 {
152  QCoreApplication app( argc, argv );
153  if ( argc < 3 ) {
154  usage();
155  return 0;
156  }
157 
158  QString inputFile = argv[argc-2];
159  QString outputFile = argv[argc-1];
160  QString name;
161  QString version;
162  QString date;
163  QString transport;
164  QString payload;
165  for ( int i=1; i<argc-2; ++i ) {
166  QString arg( argv[i] );
167  if ( arg == "--name" ) {
168  name = argv[++i];
169  } else if ( arg == "--version" ) {
170  version = argv[++i];
171  } else if ( arg == "--date" ) {
172  date = argv[++i];
173  } else if ( arg == "--transport" ) {
174  transport = argv[++i];
175  } else if ( arg == "--payload" ) {
176  payload = argv[++i];
177  } else {
178  usage();
179  return 1;
180  }
181  }
182 
183  qsrand( QTime::currentTime().msec() );
184  QFileInfo input( inputFile );
185  if ( !input.exists() || !input.isFile() ) {
186  qDebug() << "Invalid input file";
187  return usage();
188  }
189 
190  GeoDataDocument document;
191  parseBoundingBox( input, name, version, date, transport, payload, &document );
192  return save( &document, QFileInfo( outputFile ) );
193 }
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:166
Marble::GeoDataDocument
A container for Features, Styles and in the future Schemas.
Definition: GeoDataDocument.h:65
Marble::GeoDataLinearRing
A LinearRing that allows to store a closed, contiguous set of line segments.
Definition: GeoDataLinearRing.h:68
QTextStream::readLine
QString readLine(qint64 maxlen)
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
QCoreApplication
QFile::fileName
QString fileName() const
Marble::GeoDataColorStyle::setColor
void setColor(const QColor &value)
Set a new color.
Definition: GeoDataColorStyle.cpp:84
GeoDataStyle.h
QColor::setAlpha
void setAlpha(int alpha)
usage
int usage()
Definition: tools/poly2kml/main.cpp:33
Marble::GeoDataStyleMap
a class to map different styles to one style
Definition: GeoDataStyleMap.h:38
GeoDataExtendedData.h
GeoDataLineStyle.h
QFile
Marble::GeoDataCoordinates::Degree
Definition: GeoDataCoordinates.h:66
QTextStream
Marble::Oxygen::hotOrange4
QColor const hotOrange4
Definition: MarbleColors.h:86
QList::size
int size() const
GeoWriter.h
QFileInfo::isFile
bool isFile() const
GeoDataMultiGeometry.h
Marble::GeoDataStyle
an addressable style group
Definition: GeoDataStyle.h:55
QTextStream::atEnd
bool atEnd() const
Marble::GeoDataFeature::setName
void setName(const QString &value)
Set a new name for this feature.
Definition: GeoDataFeature.cpp:549
Marble::Oxygen::aluminumGray4
QColor const aluminumGray4
Definition: MarbleColors.h:92
KmlElementDictionary.h
QFileInfo::absoluteFilePath
QString absoluteFilePath() const
QString::isEmpty
bool isEmpty() const
Marble::GeoDataLineStyle::setWidth
void setWidth(const float &width)
Set the width of the line.
Definition: GeoDataLineStyle.cpp:95
QString::trimmed
QString trimmed() const
Marble::GeoWriter
Standard Marble way of writing XML This class is intended to be a standardised way of writing XML for...
Definition: GeoWriter.h:29
randomColor
QColor randomColor()
Definition: tools/poly2kml/main.cpp:44
GeoDataLineString.h
Marble::Oxygen::sunYellow6
QColor const sunYellow6
Definition: MarbleColors.h:78
Marble::GeoDataDocument::addStyle
void addStyle(const GeoDataStyle &style)
Add a style to the style storage.
Definition: GeoDataDocument.cpp:134
Marble::Oxygen::seaBlue2
QColor const seaBlue2
Definition: MarbleColors.h:64
Marble::Oxygen::skyBlue4
QColor const skyBlue4
Definition: MarbleColors.h:56
QList::first
T & first()
QString
QColor
GeoDataPlacemark.h
QStringList
GeoDataStyleMap.h
Marble::GeoDataContainer::append
void append(GeoDataFeature *other)
add an element
Definition: GeoDataContainer.cpp:272
Marble::GeoDataFeature::setStyleUrl
void setStyleUrl(const QString &value)
Set the styleUrl of this feature to value.
Definition: GeoDataFeature.cpp:631
Marble::GeoDataMultiGeometry::append
void append(GeoDataGeometry *other)
add an element
Definition: GeoDataMultiGeometry.cpp:187
QFileInfo
Marble::kml::kmlTag_nameSpaceOgc22
const char * kmlTag_nameSpaceOgc22
Definition: KmlElementDictionary.cpp:34
QFileInfo::exists
bool exists() const
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
QLatin1Char
Marble::GeoWriter::setDocumentType
void setDocumentType(const QString &documentType)
Set the current document type.
Definition: GeoWriter.cpp:79
QTime::currentTime
QTime currentTime()
GeoDataFolder.h
Marble::GeoDataStyle::setLineStyle
void setLineStyle(const GeoDataLineStyle &style)
set the line style
Definition: GeoDataStyle.cpp:107
Marble::Oxygen::brickRed4
QColor const brickRed4
Definition: MarbleColors.h:32
QVector::at
const T & at(int i) const
Marble::GeoDataObject::id
QString id() const
Get the id of the object.
Definition: GeoDataObject.cpp:75
Marble::GeoDataData
Definition: GeoDataData.h:26
GeoDataData.h
QVector< QColor >
Marble::GeoDataObject::setId
void setId(const QString &value)
Set the id of the object.
Definition: GeoDataObject.cpp:80
Marble::GeoDataLineString::isEmpty
bool isEmpty() const
Returns whether the LineString has no nodes at all.
Definition: GeoDataLineString.cpp:133
Marble::GeoDataExtendedData::addValue
void addValue(const GeoDataData &data)
add a data object to the GeoDataExtendedData with the key
Definition: GeoDataExtendedData.cpp:71
QLatin1String
QList::last
T & last()
Marble::Oxygen::forestGreen4
QColor const forestGreen4
Definition: MarbleColors.h:74
Marble::GeoDataMultiGeometry
Definition: GeoDataMultiGeometry.h:33
QMap::insert
iterator insert(const Key &key, const T &value)
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:150
Marble::GeoDataFeature::extendedData
GeoDataExtendedData & extendedData() const
Return the ExtendedData assigned to the feature.
Definition: GeoDataFeature.cpp:743
save
int save(GeoDataDocument *document, const QFileInfo &filename)
Definition: tools/poly2kml/main.cpp:131
QVector::size
int size() const
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:230
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