需求:根据前端传递的笛卡尔坐标转换为kml或者kmz格式的文件
前端给的笛卡尔坐标数据格式为:
[{"x":-2385687.468728053,"y":5388713.58735659,"z":2431266.178004133},{"x":-2386487.889323949,"y":5388376.532927499,"z":2431227.9203221975},{"x":-2385720.8091358775,"y":5388914.668443492,"z":2430790.9295658045},{"x":-2386030.8053309857,"y":5388422.52348345,"z":2431572.274479368},{"x":-2386549.913805695,"y":5388583.616999787,"z":2430711.4885351057},{"x":-2385867.777372268,"y":5388687.89246894,"z":2431146.993799864}]
解:
需要用到的依赖:
<!--JavaAPIforKml依赖-->
<dependency>
<groupId>de.micromata.jak</groupId>
<artifactId>JavaAPIforKml</artifactId>
<version>2.2.0</version>
</dependency>
因为这里是笛卡尔坐标,在kml文件内需要用到的是经纬度和海报高度,所以得先去把笛卡尔坐标转换成经纬度;
//笛卡尔坐标转经纬度
public static String ECEFtoWGS84(double x, double y, double z) {
double a, b, c, d;
double Longitude;//经度
double Latitude;//纬度
double Altitude;//海拔高度
double p, q;
double N;
a = 6378137.0;
b = 6356752.31424518;
c = Math.sqrt(((a * a) - (b * b)) / (a * a));
d = Math.sqrt(((a * a) - (b * b)) / (b * b));
p = Math.sqrt((x * x) + (y * y));
q = Math.atan2((z * a), (p * b));
Longitude = Math.atan2(y, x);
Latitude = Math.atan2((z + (d * d) * b * Math.pow(Math.sin(q), 3)), (p - (c * c) * a * Math.pow(Math.cos(q), 3)));
N = a / Math.sqrt(1 - ((c * c) * Math.pow(Math.sin(Latitude), 2)));
Altitude = (p / Math.cos(Latitude)) - N;
Longitude = Longitude * 180.0 / Math.PI;
Latitude = Latitude * 180.0 / Math.PI;
//默认海拔高度为0
return Longitude + "," + Latitude + "," + Altitude + " "; //这里最后加了一个空,是为了方便数据一次性返回
}
public Void getMarkerId(Integer id,Integer type,HttpServletResponse response) {
Marker marker = this.getById(id);
JSONArray jsonlist= JSON.parseArray(String.valueOf(marker.getGeom()));
List<Coordinate> coordinates = new ArrayList<>();
StringBuffer sb = new StringBuffer();
for (Object list : jsonlist) {
double x = Double.parseDouble(JSON.parseObject(String.valueOf(list)).getString("x"));
double y= Double.parseDouble(JSON.parseObject(String.valueOf(list)).getString("y"));
double z= Double.parseDouble(JSON.parseObject(String.valueOf(list)).getString("z"));
String s = ECEFtoWGS84(x, y, z);
sb.append(s);
Coordinate coordinate = new Coordinate(s);
coordinates.add(coordinate);
}
// 创建KML文档
Kml kml = new Kml();
Document document = kml.createAndSetDocument();
// 创建Placemark
Placemark placemark = document.createAndAddPlacemark();
placemark.withName(marker.getTitle());
//设置颜色
Style andAddStyle = placemark.createAndAddStyle();
LineStyle lineStyle = new LineStyle();
lineStyle.setColor("80d94800");
PolyStyle polyStyle = new PolyStyle();
polyStyle.setColor("cc0050f0");
andAddStyle.setLineStyle(lineStyle);
andAddStyle.setPolyStyle(polyStyle);
// 创建Polygon
Polygon andSetPolygon = placemark.createAndSetPolygon();
Boundary andSetOuterBoundaryIs = andSetPolygon.createAndSetOuterBoundaryIs();
LinearRing linearRing = new LinearRing();
linearRing.setCoordinates(coordinates);
andSetOuterBoundaryIs.setLinearRing(linearRing);
String today = DateUtil.today();
String fileName = today + "_kml_file.kml";
String fileKmzName = today + "_kml_file.kmz";
try {
if (type == 1){
response.setHeader("Content-Disposition", "attachment;filename*=\""+fileName+"\"");
response.setContentType("application/vnd.google-earth.kml+xml");
OutputStream out = response.getOutputStream();
kml.marshal(out);
out.close();
}else {
response.setHeader("Content-Disposition", "attachment;filename=\""+fileKmzName+"\"");
response.setContentType("application/vnd.google-earth.kmz");
ByteArrayOutputStream kmlStream = new ByteArrayOutputStream();
kml.marshal(kmlStream);
ZipOutputStream zipStream = new ZipOutputStream(response.getOutputStream());
ZipEntry zipEntry = new ZipEntry(fileName);
zipStream.putNextEntry(zipEntry);
zipStream.write(kmlStream.toByteArray());
zipStream.closeEntry();
zipStream.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return null;
}
散会。