⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tokml.java

📁 基于Java的地图数据管理软件。使用MySQL数据库管理系统。
💻 JAVA
字号:
package net.aetherial.gis.our;

import net.aetherial.gis.dataType.filter.WptTrkFilter;
import javax.swing.*;
import java.io.File;
import net.aetherial.gis.excel.ExcelFilter;
import net.aetherial.gis.surface.ItemValue;
import org.w3c.dom.Node;
import net.aetherial.gis.dataType.ElementToWpt;
import net.aetherial.gis.surface.CreateFile;
import net.aetherial.gis.dataType.ElementToTrk;
import org.w3c.dom.NodeList;
import net.launchtech.util.file.arcgis.shape.*;
import net.launchtech.util.jdbf.*;
import java.util.Date;
import java.io.*;
import java.text.DateFormat;
import java.text.*;
import java.util.*;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2004</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class ToKML {
  String header = "";
    String header1 = "" +
      "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
      "<kml xmlns=\"http://earth.google.com/kml/2.0\">\n" +
      "  <Document>\n" ;

      String header2 = "" +
      "    <Style id=\"default\">\n" +
      "      <IconStyle id=\"jzc\">\n" +
      "        <scale>0.3</scale>\n" +
      "        <Icon>\n" +
      "          <href>root://icons/palette-4.png</href>\n" +
      "          <x>32</x>\n" +
      "          <y>128</y>\n" +
      "          <w>32</w>\n" +
      "          <h>32</h>\n" +
      "        </Icon>\n" +
      "      </IconStyle>\n" +
      "      <LabelStyle id=\"khLabelStyle1355\">\n" +
      "        <scale>0.5</scale>\n" +
      "      </LabelStyle>\n" +
      "      <LineStyle id=\"guodao\">\n" +
      "        <color>ff7fffff</color>\n" +
      "        <width>2</width>\n" +
      "      </LineStyle>\n" +
      "    </Style>" +
      "    <Style id=\"jzc\">\n" +
      "      <IconStyle id=\"jzc\">\n" +
      "        <scale>0.5</scale>\n" +
      "        <Icon>\n" +
      "          <href>root://icons/palette-3.png</href>\n" +
      "          <x>160</x>\n" +
      "          <y>160</y>\n" +
      "          <w>32</w>\n" +
      "          <h>32</h>\n" +
      "        </Icon>\n" +
      "      </IconStyle>\n" +
      "      <LabelStyle id=\"khLabelStyle1355\">\n" +
      "        <scale>0.699999988079071</scale>\n" +
      "      </LabelStyle>\n" +
      "    </Style>" +
      "    <Style id=\"xuexiao\">\n" +
      "      <IconStyle id=\"xuexiao\">\n" +
      "        <scale>0.5</scale>\n" +
      "        <Icon>\n" +
      "          <href>root://icons/palette-2.png</href>\n" +
      "          <x>64</x>\n" +
      "          <y>224</y>\n" +
      "          <w>32</w>\n" +
      "          <h>32</h>\n" +
      "        </Icon>\n" +
      "      </IconStyle>\n" +
      "      <LabelStyle id=\"khLabelStyle1355\">\n" +
      "        <scale>0.699999988079071</scale>\n" +
      "      </LabelStyle>\n" +
      "    </Style>" +
      "    <Style id=\"guodao\">\n" +
      "      <LineStyle id=\"guodao\">\n" +
      "        <color>ff0000ff</color>\n" +
      "        <width>4</width>\n" +
      "      </LineStyle>\n" +
      "    </Style>" +
      "    <Style id=\"shendao\">\n" +
      "      <LineStyle id=\"shendao\">\n" +
      "        <color>ff7f55ff</color>\n" +
      "        <width>4</width>\n" +
      "      </LineStyle>\n" +
      "    </Style>" +
      "    <Style id=\"xiandao\">\n" +
      "      <LineStyle id=\"xiandao\">\n" +
      "        <color>ff00aaff</color>\n" +
      "        <width>4</width>\n" +
      "      </LineStyle>\n" +
      "    </Style>" +
      "    <Style id=\"xiangdao\">\n" +
      "      <LineStyle id=\"xiangdao\">\n" +
      "        <color>ff7faaff</color>\n" +
      "        <width>2</width>\n" +
      "      </LineStyle>\n" +
      "    </Style>" +
      "    <Style id=\"cundao\">\n" +
      "      <LineStyle id=\"cundao\">\n" +
      "        <color>ff7fffff</color>\n" +
      "        <width>2</width>\n" +
      "      </LineStyle>\n" +
      "    </Style>" +
      "    <Style id=\"zhuanyong\">\n" +
      "      <LineStyle id=\"zhuanyong\">\n" +
      "        <color>ff0055aa</color>\n" +
      "        <width>3</width>\n" +
      "      </LineStyle>\n" +

      "    </Style>" +
      "";
  final static String footer = "" +
      "  </Document>\n" +
      "</kml>" +
      "";
  public ToKML() {
    super();
  }

  Vector tracks = new Vector();
  Vector wpts = new Vector();

  public void addWpt(Node wpt) {
    wpts.add(buildWptKML(wpt));
  }

  public void addWpts(Node[] wpts) {
    for (int i = 0; wpts != null && i < wpts.length; i++) {
      this.wpts.add(buildWptKML(wpts[i]));
    }
  }

  public void setName(String name) {
    header = header1+
        "    <name>"+name+"</name>\n" +
        header2;
//    header.replaceAll("ZZxinjiang.kml", name);
  }

  private String getStyleID(String key) {
    if ("国道".equals(key)) {
      return "#guodao";
    }
    ;
    if ("省道".equals(key)) {
      return "#shendao";
    }
    ;
    if ("县道".equals(key)) {
      return "#xiandao";
    }
    ;
    if ("乡道".equals(key)) {
      return "#xiangdao";
    }
    ;
    if ("村道".equals(key)) {
      return "#cundao";
    }
    ;
    if ("专用道路".equals(key)) {
      return "#zhuanyong";
    }
    ;
    if ("行政村".equals(key)) {
      return "#jzc";
    }
    ;
    if ("学校".equals(key)) {
      return "#xuexiao";
    }
    ;

    return "#default";
  }

  private String buildWptKML(Node wpt) {
    String name = ItemValue.getWaypointName(wpt);
    String x = ItemValue.getWaypointX(wpt);
    String y = ItemValue.getWaypointY(wpt);
    String z = ItemValue.getWaypointZ(wpt);
    String kp = ItemValue.getWaypointKP(wpt);
    String desc = kp;

    String result = "<Placemark>\n" +
        "<name>" + name + "</name>\n" +
        "<visibility>1</visibility>\n" +
        "<description>" + desc + "</description>\n" +
        "<LookAt>\n" +
        "  <longitude>" + x + "</longitude>\n" +
        "  <latitude>" + y + "</latitude>\n" +
        "  <range>5000</range>\n" +
        "  <tilt>0</tilt>\n" +
        "  <heading>0</heading>\n" +
        "</LookAt>\n" +
        "<styleUrl>" + getStyleID(kp) + "</styleUrl>\n" +
        "<Point>\n" +
        "  <coordinates>" + x + "," + y + "," + z + "</coordinates>\n" +
        "</Point>\n" +
        "</Placemark>\n";
    return result;
  }

  public void addTrk(Node trk) {
    tracks.add(buildTrkKML(trk));
  }

  public void addTrks(Node[] trks) {
    for (int i = 0; trks != null && i < trks.length; i++) {
      this.tracks.add(buildTrkKML(trks[i]));
    }
  }

  private String buildLineStr(NodeList nl) {
    String result = "";
    String x, y, z;
    for (int i = 0; i < nl.getLength(); i++) {
      x = ItemValue.getTracksPointX(nl.item(i));
      y = ItemValue.getTracksPointY(nl.item(i));
      z = ItemValue.getTracksPointZ(nl.item(i));
      result += " " + x + "," + y + "," + z;
    }
    return result;
  }

  private String buildTrkKML(Node trk) {
    String name = ItemValue.getTracksName(trk);
    String lineStr = buildLineStr(ItemValue.getTracksPoint(trk));
    String grade = ItemValue.getTracksGrade(trk);
    String dis = ItemValue.getTracksStringDistance(trk);
     SimpleDateFormat formatter = new SimpleDateFormat(
      "EEE MMM dd",
      Locale.US);

    String desc = "<![CDATA["+
              "编码:"+ItemValue.getTracksNumber(trk)+"<br>"+ //new JDBField("RID", 'C', 100, 0),
              "名称:"+ItemValue.getTracksName(trk)+"<br>"+ //new JDBField("NAME", 'C', 100, 0),
              "所在市:"+ItemValue.getTracksT1(trk)+"<br>"+ //new JDBField("CITY", 'C', 100, 0), //第三个参数值一定不大于20
              "所在县:"+ItemValue.getTracksT2(trk)+"<br>"+ //new JDBField("COUNTY", 'C', 100, 6), //F类型与N类型同,且第四个参数值有小数位数,否则会截短
              "所在乡镇:"+ItemValue.getTracksT3(trk)+"<br>"+ //new JDBField("TOWN", 'C', 100, 0),
              "所在村:"+ItemValue.getTracksT4(trk)+"<br>"+ //new JDBField("VILLAGE", 'C', 100, 0),
              "行政等级:"+ItemValue.getTracksGrade(trk)+"<br>"+ //new JDBField("LEVEL", 'C', 100, 0),
              "技术等级:"+ItemValue.getTracksT5(trk)+"<br>"+ //new JDBField("STANDARD", 'C', 100, 0)
              "路面类型:"+ItemValue.getTracksType(trk)+"<br>"+ //new JDBField("TYPE", 'C', 100, 0),
              "路面结构:"+ItemValue.getTracksStructure(trk)+"<br>"+ //new JDBField("STRUCTURE", 'C', 100, 0),
              "路面宽度:"+(ItemValue.getTracksWidth(trk))+" 米<br>"+ //new JDBField("WEIGHT", 'N', 20, 6),
              "路基宽度:"+(ItemValue.getTracks_jitongbu_luji_With(trk))+" 米<br>"+ //new JDBField("WEIGHT", 'N', 20, 6),
              "道路现状:"+ItemValue.getTracksStatus(trk)+"<br>"+ //new JDBField("STATUS", 'C', 100, 0),
              "里程:"+ItemValue.getTracksStringDistance(trk)+" 公里<br>" //new JDBField("LENGTH", 'C', 100, 0),

+"]]>";
    String result = "<Placemark>\n" +
        "<name>" + name + "</name>\n" +
        "<visibility>1</visibility>\n" +
        "<description>" + desc + "</description>\n" +
        "<styleUrl>" + getStyleID(grade) + "</styleUrl>\n" +
        "<LineString>\n" +
        "  <coordinates>" + lineStr + "</coordinates>\n" +
        "</LineString>\n" +
        "</Placemark>\n";
    return result;
  }

  private String buildTrksKML(Vector trks) {
    String result = "";
    for (int i = 0; i < trks.size(); i++) {
      result += (String) trks.get(i);
    }
    return "<Folder xmlns=\"\">\n        <name>航迹</name>\n        <visibility>1</visibility>\n        <open>1</open>"
        + result +
        "      </Folder>";
  }

  private String buildWptsKML(Vector wpts) {
    String result = "";
    for (int i = 0; i < wpts.size(); i++) {
      result += (String) wpts.get(i);
    }

    return "<Folder xmlns=\"\">\n        <name>航点</name>\n        <visibility>1</visibility>\n        <open>0</open>"
        + result +
        "      </Folder>";

  }

  public String buildKML(Vector trks, Vector wpts) {

    return (header + buildWptsKML(wpts) + buildTrksKML(trks) + footer);
  }

  public String buildKML() {

    return buildKML(tracks, wpts);
  }

  public void saveToFile(String path, String fileName) {
    try {
      ToKML kml = new ToKML();
      kml.setName(fileName);
      kml.addWpts(ItemValue.getWaypoint());
      kml.addTrks(ItemValue.getTracks());

      BufferedOutputStream kmlFile = new BufferedOutputStream(new
          FileOutputStream(path + fileName));
      kmlFile.write(kml.buildKML().getBytes("UTF-8"));
      kmlFile.close();
    }
    catch (UnsupportedEncodingException ex2) {
      ex2.printStackTrace();
    }
    catch (IOException ex2) {
      ex2.printStackTrace();
    }

  }

  public static void main(String[] args) {
    ToKML tokml = new ToKML();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -