首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

java 生成kml 文件

  • 25-04-24 21:01
  • 2127
  • 13893
blog.csdn.net

首先我们需要生成一个包含位置信息的kml文件(类似xml文件),有多种方式创建kml,这里使用的是Dom4J方式。android中就导入jar包下载地址:https://dom4j.github.io/,java中就在pom.xml文件中添加如下。

 

 
dom4j
 
dom4j
 
1.6.1
 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

直接上代码,代码中有注释自己根据需求自己添加删除

package com.yf.fruitforest.common;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Namespace;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import java.io.*;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class KmlUtils {

    //coordinates(经纬度:经度,纬度,高度)代表一个地理信息的对象集合(自定义)  生成kml的名称
    public static boolean setTravelsKml(String coordinates, String fileName) throws IOException {
        Element root = DocumentHelper.createElement("kml");

        Document document = DocumentHelper.createDocument(root);
        //根节点添加属性
        /*这里用namespace是因为当时xmlns这个属性添加不上去,所以用这个方法可行
         * Namespace namespace = Namespace.get("http://www.opengis.net/kml/2.2");
         *  root.add(namespace);
         * */
        Namespace namespace = Namespace.get("http://www.opengis.net/kml/2.2");
        root.addAttribute("xmlns", "http://www.opengis.net/kml/2.2");
        // .addAttribute("xmlns:gx", "http://www.google.com/kml/ext/2.2")
        //  .addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
        // .addAttribute("xsi:schemaLocation",
        //     "http://www.opengis.net/kml/2.2 http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd http://www.google.com/kml/ext/2.2 http://code.google.com/apis/kml/schema/kml22gx.xsd");
        root.add(namespace);
        Element documentElement = root.addElement("Document");
        // documentElement.addElement("name").addText(fileName);
        // documentElement.addElement("Snippet").addText("");
        // Element folderElement = documentElement.addElement("Document");//添加一个目录
        // folderElement.addAttribute("id", "FeatureLayer0");
        documentElement.addElement("name").addText("轨迹点位"); //名称
        //folderElement.addElement("Snippet").addText(""); //显示在Google Earth之中的对description的简短概要.


        //生成点位图标数据
        int i = 1;
        // for (String travelRecord:coordinates) {
        //  i++;
        Element placeMarkElement = documentElement.addElement("Placemark");//在文件夹中添加一个地标
        //placeMarkElement.addAttribute("id", String.valueOf(i));
        placeMarkElement.addElement("name").addText("点位" + String.valueOf(i));
        placeMarkElement.addElement("styleUrl").addText("#randomColorRegion");
        // placeMarkElement.addElement("Snippet").addText("");
        placeMarkElement.addElement("description").addText("R");//简介
        //placeMarkElement.addElement("styleUrl").addText("#MyStyle");//风格
        Element pointElement = placeMarkElement.addElement("Polygon");
        // Element pointElement = placeMarkElement.addElement("Point");
        Element outerBoundaryIs1 = pointElement.addElement("outerBoundaryIs");
        Element LinearRing1 = outerBoundaryIs1.addElement("LinearRing");
        //分解
        // String[] con = travelRecord.split(",");
        //添加点位的经纬度坐标以及高度(显示时绘制高度m)
        LinearRing1.addElement("coordinates").addText(coordinates);//可以是是任何几何形状的子元素,定义每一个点的经度、纬度和高度(按照严格的顺序). 多个点使用空格隔开,经纬度按照WGS84标准.
        //  }


        //生成轨迹线路径数据
//        Element lineElement = documentElement.addElement("Placemark");//在文件夹外添加一个地标
//        lineElement.addElement("name").addText("轨迹线");
//        lineElement.addElement("description").addText("");
//        lineElement.addElement("styleUrl").addText("#MyStyle");
//        Element pointElement1 = lineElement.addElement("LineString");
//        pointElement1.addElement("altitudeMode").addText("absolute");
//        pointElement1.addElement("extrude").addText("1");
//        pointElement1.addElement("tessellate").addText("1");
        String linedata = "";
        //每个坐标以及高度用换行符或空格分开
        /*for (TravelRecord travelRecord:travelRecords) {
            linedata =linedata+"\n"+ travelRecord.lng+","+travelRecord.lat+",30";
        }*/
        //pointElement.addElement("coordinates").addText("115.86602,25.70925");
        //生成显示风格
        //Element styleElement = documentElement.addElement("Style");//Style节点
        //styleElement.addAttribute("id", "MyStyle");
        // IconStyle 图标风格
        //Element iconStyleElement = styleElement.addElement("IconStyle");
        // Element iconElement = iconStyleElement.addElement("Icon");
//	        iconElement.addElement("href").addText("http://192.168.10.108:8080/images/mark_b.png");//在线图标
        //iconStyleElement.addElement("scale").addText("0.250000");
        // LabelStyle  标签风格
        // Element labelStyleElement = styleElement.addElement("LabelStyle");
        // labelStyleElement.addElement("color").addText("00000000");
        //labelStyleElement.addElement("scale").addText("0.000000");
        // PolyStyle 图形风格
        Element Style = placeMarkElement.addElement("Style");
        Element polyStyleElement = Style.addElement("PolyStyle");
        polyStyleElement.addElement("color").addText("96ffff00");
        //polyStyleElement.addElement("outline").addText("0");

        // LineStyle  路径线风格
     /*  Element lineStyleElement = styleElement.addElement("LineStyle");
        lineStyleElement.addElement("color").addText("7f00ffff");
        lineStyleElement.addElement("width").addText("4");*/

        //创建kml到本地
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("utf-8");
        XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("D:/web/airportData/" + fileName + "_1.kml"), format);

        xmlWriter.write(document);

        xmlWriter.close();
        //开始对文件进行压缩,一个kml文件其实是一个压缩文件,里面包含一个kml文件和一个png图标
        String[] strs = new String[2];
        strs[0] = "D:/web/Travels/" + fileName + "_1.kml";
        strs[1] = "D:/web/Travels/images/img_mark.png";//这里写图片的路径  如果使用在线图标这段代码屏蔽
        writeKml(strs, "D:/web/Travels/" + fileName);//压缩

        return true;

    }


    public static void writeKml(String[] strs, String kmlName) throws IOException {
        String[] files = strs;
        OutputStream os = new BufferedOutputStream(new FileOutputStream(kmlName + ".kml"));
        ZipOutputStream zos = new ZipOutputStream(os);
        byte[] buf = new byte[8192];
        int len;
        for (int i = 0; i < files.length; i++) {
            File file = new File(files[i]);
            if (!file.isFile())
                continue;
            ZipEntry ze = new ZipEntry(file.getName());
            zos.putNextEntry(ze);
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            while ((len = bis.read(buf)) > 0) {
                zos.write(buf, 0, len);
            }
            zos.closeEntry();
            bis.close();
        }

        zos.closeEntry();
        zos.close();
        os.close();

    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148

生成的kml文件如图:
在这里插入图片描述
将kml文件导入到http://geojson.io/效果如图
在这里插入图片描述

注:本文转载自blog.csdn.net的嘿!宝贝,此刻想我没的文章"https://blog.csdn.net/Heyyouare/article/details/108018536"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top