I have this .txt file:
20480418; -29.761660000, -51.123630000; 0; 1 20479111; -29.761410000, -51.121220000; 0; 2 20476493; -29.764020460, -51.124986440; 0; 3 20472881; -29.762670000, -51.126950000; 1; 4 20479672; -29.767868465, -51.127055623; 1; 5 20479692; -29.744090000, -51.156210000; 1; 1 20476184; -29.746494407, -51.158303478; 1; 2 20479622; -29.746680000, -51.158340000; 1; 3 20479969; -29.739370000, -51.166900000; 1; 4 20474498; -29.737870000, -51.171300000; 1; 5 20479287; -29.748470000, -51.150200000; 1; 6 20480145; -29.746500000, -51.158300000; 2; 7
Order number; longitude; latitude; priority; service order
Now, i have to transform it in kml and i tried to adapt a tutorial i saw and i did it like that:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.Namespace;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
public class SistemaRotas {
static String inputFile = "solucao.txt";
static String outputFile = "output.kml";
public static void main(String[] args) {
/*
* Step 1: gerar XML stub
*/
Namespace ns = Namespace.getNamespace("", "http://www.opengis.net/kml/2.2");
// kml
Element kml = new Element("kml", ns);
Document kmlDocument = new Document(kml);
// Documento
Element document = new Element("Document", ns);
kml.addContent(document);
// nome + descrição
Element name = new Element("name", ns);
Element description = new Element("description", ns);
name.setText("Visualizacao da solucao");
description.setText("Visualizacao gerada para acompanhamento da solucao.");
document.addContent(name);
document.addContent(description);
/*
* Passo 2: colocar estilo dos elementos
*/
// estilo
Element style = new Element("Style", ns);
style.setAttribute("id", "pontoCaramelo");
document.addContent(style);
// estiloIcone
Element iconStyle = new Element("IconStyle", ns);
iconStyle.addContent(iconStyle);
// cor
Element color = new Element("color", ns);
color.setText("ffAAD9CB");
iconStyle.addContent(color);
// icone
Element icon = new Element("Icon", ns);
iconStyle.addContent(icon);
// href
Element href = new Element("href", ns);
href.setText("http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png");
icon.addContent(href);
/*
* Passo 3: ler informações do arquivo
* adicionar num Placemark para cada informação do elemento
*/
File file = new File(inputFile);
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(file));
try {
String line = reader.readLine();
while (line != null) {
String[] lineParts = line.split(";");
if (lineParts.length == 4) {
// add in the Placemark
// Placemark
Element placemark = new Element("Placemark", ns);
document.addContent(placemark);
// número da ordem
Element pmOrdem = new Element("name", ns);
pmOrdem.setText(lineParts[0].trim());
placemark.addContent(pmOrdem);
// styleUrl
Element pmStyleUrl = new Element("styleUrl", ns);
pmStyleUrl.setText("#pontoCaramelo");
placemark.addContent(pmStyleUrl);
// ponto
Element pmPonto = new Element("Point", ns);
placemark.addContent(pmPonto);
// coordenadas
Element pmCoordenadas = new Element("coordinates", ns);
pmCoordenadas.setText(lineParts[1].trim());
pmPonto.addContent(pmCoordenadas);
// prioridade
Element pmPrioridade = new Element("description", ns);
pmPrioridade.setText(lineParts[2].trim());
placemark.addContent(pmPrioridade);
// ordem de atendimento
Element pmAtendimento = new Element("description", ns);
pmAtendimento.setText(lineParts[3].trim());
placemark.addContent(pmAtendimento);
}
// ler a próxima linha
line = reader.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
/*
* Passo 4: escrever arquivo KML
*/
try {
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
FileOutputStream writer = new FileOutputStream(outputFile);
outputter.output(kmlDocument, writer);
//outputter.output(kmlDocument, System.out);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
The thing is: i don't know why the order done for the informations isn't working, and i would need the priority and service order to be together in a CDATA type if possible, or at least together in the same information balloon.
Besides i only have it appearing:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
And i need that:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
Tips? Ideas? Because i'm really confused in what to do to fix it :/
I've made this code that would help you:
Namespace ns = Namespace.getNamespace("", "http://www.opengis.net/kml/2.2");
Namespace ns2 = Namespace.getNamespace("gx", "http://www.google.com/kml/ext/2.2");
Element kml = new Element("kml", ns);
kml.addNamespaceDeclaration(ns2);
Document kmlDocument = new Document(kml);
Element folder = new Element("folder");
Element placemark = new Element("placemark");
folder.addContent(placemark);
kmlDocument.getRootElement().addContent(folder);
XMLOutputter xmlOutput = new XMLOutputter();
xmlOutput.setFormat(Format.getPrettyFormat());
xmlOutput.output(kmlDocument, new FileWriter("./prueba.kml"));
It's only for the basic structure:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
<folder xmlns="">
<placemark />
</folder>
</kml>