2014-07-18 21 views
0

我有這樣的.txt文件:JDOM - TXT轉換爲KML - 一些細節

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

訂單號;經度;緯度;優先;服務訂單

現在,我已經在KML改造它,我努力去適應一個教程中,我看到的和我做了這樣的:

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(); 
     } 

    } 

} 

的事情是:我不知道爲什麼訂單爲信息做的工作並不奏效,如果可能的話,我需要將優先級和服務訂單放在一起,或者至少放在同一個信息氣球中。

除了我只有它出現:

<?xml version="1.0" encoding="UTF-8"?> 
<kml xmlns="http://www.opengis.net/kml/2.2"> 

,我需要的是:

<?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"> 

提示?想法?因爲我在做什麼來解決它真的很困惑:/

回答

0

我做了這個代碼,這將有助於您:

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")); 

這只是爲基本結構:

<?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>