概述:基于PostGis生成Kml坐标文本,自定义样式
Kml文件结构说明
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:gx="http://www.google.com/kml/ext/2.2"
>
<Document>
<name>小班kml</name>
<Folder>
<name>小班kml</name>
<Placemark>
<name>吾古村-吾古村-00073001</name>
<Style>
<LineStyle><color>ff00ff00</color><width>1</width></LineStyle>
<PolyStyle><color>4D80ff00</color></PolyStyle>
</Style>
<MultiGeometry><Polygon><outerBoundaryIs><LinearRing><coordinates>119.839503088405806,28.52349172528816 119.83935538293224,28.523463182087585 119.839117657947071,28.523367408901901 119.838750213745683,28.523252379716112 119.83844752877728,28.52321382391656 119.837960832848708,28.523308738368584 119.837605123227988,28.523429082395744 119.837592766430291,28.5235473680601 119.837585853618194,28.523666223064112 119.837572132516428,28.52383855792672 119.837598866124551,28.523957451497868 119.837726701857392,28.523969490966056 119.837901669576723,28.523963757042466 119.838063205691796,28.523940176265207 119.838231407017389,28.523958206173443 119.838338993601155,28.524011822241068 119.838513834565077,28.524089292158337 119.838634932712864,28.52410726556861 119.838769677779482,28.524000448998653 119.838944790022936,28.523899622959494 119.839059349410292,28.52379278216566 119.839167123175216,28.52372159227631 119.83932207467349,28.523602912356711 119.839423110918162,28.523537658750456 119.839503088405806,28.52349172528816</coordinates></LinearRing></outerBoundaryIs></Polygon></MultiGeometry>
</Placemark>
</Folder>
</Document>
</kml>
其中文档主体结构可拆分为 Document->Folder->Placemark,各层级之间为包含关系
其中Placemark为最小矢量单元,其中name为该要素名称,Style为样式,MultiGeometry为矢量图形坐标
使用PostGis导出矢量坐标
SELECT
t1.cunname || '-' || t1.lb_name || '-' || t1.xiao_ban AS name,
ST_ASKML(t1.geom) AS kml
FROM filter_sxc t1
样式说明
<Style>
<LineStyle><color>ff00ff00</color><width>1</width></LineStyle>
<PolyStyle><color>4D80ff00</color></PolyStyle>
</Style>
color中样式为十六进制(abgr)
导出工具类
package com.dinnovate.ldysf.util;
import java.util.List;
/**
* kml导出工具类
* @author yangkun
* @createTime 2022年04月19日 21:19:00
*/
public class KmlUtil {
public static Integer COLOR_GREEN = 1;
public static Integer COLOR_RED = 2;
public static Integer COLOR_BLUE = 3;
/**
* 构造style
* @param color
* @return
*/
public static String getStyle(Integer color){
String style = "";
if(color.equals(COLOR_RED)){
style = "\t\t\t\t<Style>\n"
+ "\t\t\t\t\t<LineStyle><color>ffffc080</color><width>1</width></LineStyle>\n"
+ "\t\t\t\t\t<PolyStyle><color>4D0000ff</color></PolyStyle>\n"
+ "\t\t\t\t</Style>";
}else if(color.equals(COLOR_GREEN)){
style = "\t\t\t\t<Style>\n"
+ "\t\t\t\t\t<LineStyle><color>ff00ff00</color><width>1</width></LineStyle>\n"
+ "\t\t\t\t\t<PolyStyle><color>4D80ff00</color></PolyStyle>\n"
+ "\t\t\t\t</Style>";
}else if(color.equals(COLOR_BLUE)){
style = "\t\t\t\t<Style>\n"
+ "\t\t\t\t\t<LineStyle><color>ffffc080</color><width>1</width></LineStyle>\n"
+ "\t\t\t\t\t<PolyStyle><color>4Dff8000</color></PolyStyle>\n"
+ "\t\t\t\t</Style>";
}
return style;
}
/**
* 构造placemark
* @author yangkun
* @date 2022/4/22 11:00
* @param name:
* @param kml:
* @param color:
* @return: java.lang.String
**/
public static String getPlacemark(String name,String kml,Integer color){
String style = getStyle(color);
String placemark = "\t\t\t<Placemark>\n"
+ "\t\t\t\t<name>%s</name>\n"
+ "%s\n"
+ "\t\t\t\t%s\n"
+ "\t\t\t</Placemark>\n";
placemark = String.format(placemark, name,style,kml);
return placemark;
}
/**
* 构造完整kml文件
* @param name
* @param placemarkList
* @return
*/
public static String getKml(String name,List<String> placemarkList){
String kml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<kml xmlns=\"http://www.opengis.net/kml/2.2\" \n"
+ "\txmlns:atom=\"http://www.w3.org/2005/Atom\" \n"
+ "\txmlns:gx=\"http://www.google.com/kml/ext/2.2\" \n"
+ "\t>\n"
+ "\t<Document>\n"
+ "\t\t<name>"+name+"</name>\n"
+ "\t\t<Folder>\n"
+ "\t\t\t<name>"+name+"</name>\n"
+ "%s\n"
+ "\t\t</Folder>\n"
+ "\t</Document>\n"
+ "</kml>";
StringBuilder sb = new StringBuilder();
for(String placemark:placemarkList){
sb.append(placemark);
}
kml = String.format(kml, sb.toString());
return kml;
}
}
工具类中内置三种样式,如有其他需要,可自行扩展
使用案例
public void listKml(KmlExportCondition condition,HttpServletResponse response) {
try{
String year = systemConfigService.getCurrentYear();
condition.setYear(year);
// 获取小班kml数据
List<KmlExportBO> list = acceptanceMapper.listKml(condition);
// 组装kml文件
List<String> placemarkList = new ArrayList<>();
for(KmlExportBO kmlExportBO:list){
String placemark = KmlUtil
.getPlacemark(kmlExportBO.getName(), kmlExportBO.getKml(), condition.getResult());
placemarkList.add(placemark);
}
String kml = KmlUtil.getKml("小班kml",placemarkList);
// 文件下载
OutputStream outputStream = response.getOutputStream();
response.setContentType("application/vnd.google-earth.kml+xml");
response.setHeader("Content-Disposition", "attachment;filename=class.kml");
response.setContentType("application/octet-stream;charset=UTF-8");
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
IoUtil.write(outputStream,true,kml.getBytes(Charset.defaultCharset()));
}catch (Exception e){
log.error("获取小班kml失败",e);
}
}
- 根据条件从数据库中查询矢量数据,并转换为placemark
- 循环遍历矢量数列表,构造完整kml文档
- 将kml文档以文件流的形式供浏览器下载