Skip to content

Commit

Permalink
Started reading KML GIS file (code is unstable, do not use it yet!)
Browse files Browse the repository at this point in the history
  • Loading branch information
denizdiktas committed Jun 23, 2023
1 parent e815709 commit 76c9873
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Arrangement_on_surface_2/demo/earth/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ endif()

set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/opengl/earth")

find_package(Qt6 REQUIRED COMPONENTS Core Gui OpenGL OpenGLWidgets Widgets)
find_package(Qt6 REQUIRED COMPONENTS Core Gui OpenGL OpenGLWidgets Widgets Xml)
add_definitions(-DQT_NO_VERSION_TAGGING)

# CGAL and its components
Expand Down Expand Up @@ -73,6 +73,7 @@ target_link_libraries(earth PRIVATE
Qt6::OpenGL
Qt6::OpenGLWidgets
Qt6::Widgets
Qt6::Xml
CGAL::CGAL
)

Expand Down
165 changes: 165 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Main_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,174 @@ void Main_widget::timerEvent(QTimerEvent*)
update();
}

#include <qfile.h>
#include <qiodevice.h>
#include <QJsonArray.h>
#include <qjsonobject.h>
#include <QJsonParseError>
#include <qstring.h>


#include <QtXml/qdom.h>
#include <qxmlstream.h>

namespace {
struct Node
{
double lon, lat;
};

struct LinearRing
{
std::vector<Node> nodes;
QString str;
};

struct MultiGeometry
{
std::vector<LinearRing> polygons;
};

struct Placemark
{
MultiGeometry geometry;
QString name;
};
}


void Main_widget::initializeGL()
{
QString file_name("C:/work/gsoc2023/data/world_countries.kml");

if(0)
{
QDomDocument doc("mydoc");
QFile file(file_name);
if (!file.open(QFile::ReadOnly | QFile::Text))
{
qDebug() << "could not open file!";
return;
}
if (!doc.setContent(&file)) {
file.close();
return;
}
file.close();

//QDomElement docElem = doc.documentElement();
//QDomNode n = docElem.firstChild();
//qDebug() << n.nodeName();
//qDebug() << n.isElement();
//auto doc_elem = n.toElement();

auto placemarks = doc.elementsByTagName("Placemark");
qDebug() << placemarks.count();

}
else
{
std::vector<Placemark> placemarks;

Placemark placemark;
MultiGeometry mgeometry;
LinearRing lring;


QFile file(file_name);
if (file.open(QIODevice::ReadOnly))
{
QXmlStreamReader xmlReader;
xmlReader.setDevice(&file);

xmlReader.readNext();

// Reading from the file
while (!xmlReader.isEndDocument())
{
QString name = xmlReader.name().toString();
// qDebug() << "----------------------";
// qDebug() << name;
// qDebug() << xmlReader.text();


if (xmlReader.isStartElement())
{
// qDebug() << "START ELEMENT";
if (name == "Placemark")
{
// qDebug() << "Placemark - Start";
placemark = Placemark{};
}
else if (name == "MultiGeometry")
{
// qDebug() << "MultiGeometry - Start";
mgeometry = MultiGeometry{};
}
else if (name == "LinearRing")
{
// qDebug() << "LinearRing - Start";
lring = LinearRing{};
}
else if (name == "coordinates")
{
// qDebug() << "coordinates - Start";
xmlReader.readNext();
lring.str = xmlReader.text().toString();
// qDebug() << lring.str;
}
else if (name == "SimpleData")
{
// qDebug() << "SimpleData - Start";
auto attributes = xmlReader.attributes();
auto attr_name = attributes[0].name().toString();
auto attr_value = attributes[0].value().toString();
if ((attr_name == "name") && (attr_value == "name"))
{
xmlReader.readNext();
placemark.name = xmlReader.text().toString();
// qDebug() << "country name = " << placemark.name;
}
//qDebug() << "num attribues = " << attributes.size();
//qDebug() << "attribute[0].name = " << attributes[0].name();
//qDebug() << "attribute[0].value = " << attributes[0].value();
}
}
else if (xmlReader.isEndElement())
{
// qDebug() << "END ELEMENT";
if (name == "Placemark")
{
// qDebug() << "Placemark - End";
placemarks.push_back(placemark); // move?
}
else if (name == "MultiGeometry")
{
// qDebug() << "MultiGeometry - End";
placemark.geometry = mgeometry; // move?
}
else if (name == "LinearRing")
{
// qDebug() << "LinearRing - End";
mgeometry.polygons.push_back(lring); // move?
}
else if (name == "coordinates")
{
// qDebug() << "coordinates - End";
// no need to do anything here: the coordinates are read above!
}
}

xmlReader.readNext();
}

if (xmlReader.hasError())
{
std::cout << "XML error: " << xmlReader.errorString().data() << std::endl;
}
}
}

initializeOpenGLFunctions();

init_camera();
Expand Down

0 comments on commit 76c9873

Please sign in to comment.