import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.alibaba.fastjson.JSON;
public class ReadKml {
public static boolean addSampleSuccess = false; // 判断读取KML是否成功
private static Coordinate coordinate = null; // 存储从KML文件中读取出来的坐标值和name
private static List<Placemark> placemarkList = new ArrayList();// 存储每次实例化的Coordinate对象,每个Coordinate都保存着不同的x,y,name
private static String aaaaa;
private static String hh;
public static void main(String[] args) throws Exception {
String
// filePath =
// "E://eclipseworkspace//lw_tracing//WebRoot//kml//test.kml";
filePath = "C://Users//888//Desktop//王总GIS//地图文件//JZSG-8标文件.kml";
// String
// filePath =
// "C://Users//888//Desktop//王总GIS//地图文件//路基隧道线路中心线点文件.kml";// mark
// String
// filePath ="C://Users//888//Desktop//王总GIS//地图文件//桥梁点文件.kml";// mark
// String
// filePath="C://Users//888//Desktop//王总GIS//地图文件//涵洞点文件.kml";//error
// String
// filePath="C://Users//888//Desktop//王总GIS//地图文件//宣1变更.kml";
// String
//
//
// String
// filePath="C://Users//888//Desktop//王总GIS//地图文件//京张中桩线文件.kml";
// String
// filePath="C://Users//888//Desktop//王总GIS//地图文件//一分部重点拆迁区域20160625.kml";
// String
//filePath = "C://Users//888//Desktop//王总GIS//地图文件//1号线.kml";
System.out.println("main:");
System.out.println(filePath);
parseXmlWithDom4j(filePath);
System.out.println("placemarkList Value :" + placemarkList.toString());
System.out.println("------------------------------------------------");
String jsonString = JSON.toJSONString(placemarkList);
System.out.println(jsonString);
try { // 防止文件建立或读取失败,用catch捕捉错误并打印,也可以throw
/* 读入TXT文件 */
String pathname = "D:\\KMLjson.txt"; //
// 绝对路径或相对路径都可以,这里是绝对路径,写入文件时演示相对路径
// /* 写入Txt文件 */
// File writename = new File(pathname); // 相对路径,如果没有则要建立一个新的。txt文件
// writename.createNewFile(); // 创建新文件
// BufferedWriter out = new BufferedWriter(new
// FileWriter(writename));
// out.write(jsonString); // \r\n即为换行
// out.flush(); // 把缓存区内容压入文件
// out.close(); // 最后记得关闭文件
FileOutputStream writerStream = new FileOutputStream(pathname);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(writerStream, "utf-8"));
writer.write(jsonString);
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static Boolean parseXmlWithDom4j(String input) throws Exception {
Document document = null;
document = loadFile(input);
Element root = document.getRootElement();// 获取doc.kml文件的根结点
listNodes(root);
addSampleSuccess = true;
// 选择sd卡中的kml文件,解析成功后即调用MainActivity中的添加marker的方法向地图上添加样点marker
return addSampleSuccess;
}
/**
* 载入一个KML文件
*
* @param fileName
* @return
*/
private static Document loadFile(String fileName) {
Document document = null;
SAXReader reader = null;
try {
reader = new SAXReader();
FileInputStream in = new FileInputStream(new File(fileName));
InputStreamReader inReader = new InputStreamReader(in, "UTF-8");
BufferedReader bufReader = new BufferedReader(inReader);
document = reader.read(bufReader);
} catch (Exception ex) {
ex.printStackTrace();
}
return document;
}
// 遍历当前节点下的所有节点
public static void listNodes(Element node) {
String name = "";// Placemark节点中的name属性
String href = "";// Placemark节点中的name属性
String x = "";// 坐标x
String y = "";// 坐标y
String alt = "";// 高程alt
double d_x = 0.0;// 对x作string to double
double d_y = 0.0;
double d_alt = 0.0;
List<Coordinate> coordinateList = new ArrayList();
try {
// if ("IconStyle".equals(node.getName())) {// 如果当前节点是Placemark就解析其子节点
// List<Element> placemarkSons = node.elements();// 得到Placemark节点所有的子节点
// for (Element element : placemarkSons) { // 遍历所有的子节点
// if ("href".equals(element.getName())) {
// href = element.getText();
// }
// }
// }
if ("Placemark".equals(node.getName())) {// 如果当前节点是Placemark就解析其子节点
List<Element> placemarkSons = node.elements();// 得到Placemark节点所有的子节点
for (Element element : placemarkSons) { // 遍历所有的子节点
if ("name".equals(element.getName())) {
href = element.getText();
System.out.println("href Value :" + "href=" + href );
}
}
Element pointSon;// Point节点的子节点
Iterator i = node.elementIterator("Point");// 遍历Point节点的所有子节点
while (i.hasNext()) {
pointSon = (Element) i.next();
String nodeContent = "";
nodeContent = pointSon.elementText("coordinates");// 得到coordinates节点的节点内容
coordinate = makeCoordinate(nodeContent);
coordinateList.add(coordinate);// 将每一个实例化的对象存储在list中
}
if (coordinateList.size() > 0)
placemarkList.add(new Placemark(coordinateList, name, "point"));
coordinateList.clear();
// try {
Element LineSon;// 线节点的子节点
Iterator j = node.elementIterator("LineString");// 遍历线节点的所有子节点
while (j.hasNext()) {
LineSon = (Element) j.next();
String nodeContent = "";
nodeContent = LineSon.elementText("coordinates");// 得到coordinates节点的节点内容
System.out.println("beforeRepace :" + nodeContent);
nodeContent = nodeContent.replaceAll("\t|\n", " ");
System.out.println("afterRepace :" + nodeContent);
String nodeContentSplit[] = null;
nodeContentSplit = nodeContent.split(" ");
for (int it = 0; it < nodeContentSplit.length; it++) {
String coordinateStr = nodeContentSplit[it];
coordinate = makeCoordinate(coordinateStr);
coordinateList.add(coordinate);// 将每一个实例化的对象存储在list中
}
}
Placemark heh = new Placemark(coordinateList, name, "LineString");
System.out.println("Placemark Value :" + "name=" + heh.getName() + "type=" + heh.getType() + "points="
+ heh.getPoints());
if (coordinateList.size() > 0)
placemarkList.add(new Placemark(coordinateList, name, "LineString"));
// } catch (Exception e) {
// System.out.println("LLLLLLLLLLLLLLLLLLLLLL:" + "\n" + e);
// System.out.println("LLLL");
// coordinateList.clear();
// Element LineSon;// 线节点的子节点
// Iterator j = node.elementIterator("LineString");//
// 遍历线节点的所有子节点
// while (j.hasNext()) {
// LineSon = (Element) j.next();
// String nodeContent = "";
// nodeContent = LineSon.elementText("coordinates");//
// 得到coordinates节点的节点内容
// System.out.println("#########catchCoordinates :" +
// nodeContent);
//
// String nodeContentSplit[] = null;
// nodeContentSplit = nodeContent.split("\\r?\\n");
// for (int it = 0; it < nodeContentSplit.length; it++) {
// String coordinateStr = nodeContentSplit[it];
// System.out.println("NNNNNN :" + coordinateStr);
// if(!coordinateStr.isEmpty())
// {
// coordinate = makeCoordinate(coordinateStr);
// System.out.println("CCCCCCC :"+coordinate);
// coordinateList.add(coordinate);// 将每一个实例化的对象存储在list中
// }
//
// }
// }
Placemark heh0 = new Placemark(coordinateList, name, "LineString");
Sys
没有合适的资源?快使用搜索试试~ 我知道了~
java读取kml文件数据
共18个文件
jar:6个
java:5个
class:4个
4星 · 超过85%的资源 需积分: 47 90 下载量 7 浏览量
2018-01-11
16:03:12
上传
评论 4
收藏 1.59MB ZIP 举报
温馨提示
自己做的用java读取kml文件数据,并保存为json文件。方便其它地方解析。
资源推荐
资源详情
资源评论
收起资源包目录
ReadKml.zip (18个子文件)
ReadKml
.project 383B
src
ReadKml.java 10KB
Coordinate.java 504B
ReadKml1.java 8KB
Placemark.java 740B
ReadKml2.java 8KB
lib
javax.servlet.jsp.jar 50KB
dom4j-1.6.1.jar 307KB
javax.servlet.jar 96KB
fastjson-1.2.4.jar 402KB
JavaAPIforKml.jar 605KB
fastjson-1.2.5-sources.jar 264KB
.settings
org.eclipse.jdt.core.prefs 598B
.classpath 673B
bin
ReadKml.class 8KB
Placemark.class 1KB
Coordinate.class 868B
ReadKml1.class 7KB
共 18 条
- 1
资源评论
- AppByte2020-06-05不怎么样,几个文件是重复的,也没整理。而且主要功能不全逝水逆流年2020-11-03您说的文件重复其实是里面的方法都不一样的,文件名称确实没有整理,不过不影响使用的。我这个最主要的功能是读取kml文件格式文件,转换为json文件,到了这一步,下面要做的方向就比较多了。您说的功能不全是指什么,缺少什么功能呢?
- Cansluck2019-09-30案例写的很好。使用到了。逝水逆流年2020-04-20谢谢!
逝水逆流年
- 粉丝: 7
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功