引入geographiclib类来计算距离,周长和面积的方法
2023-12-14 15:35:54
步骤一、引入geographiclib包
<dependency>
<groupId>net.sf.geographiclib</groupId>
<artifactId>GeographicLib-Java</artifactId>
<version>2.0</version>
</dependency>
步骤二、分别计算距离,周长和面积
package com.opengis.tools.util;
import net.sf.geographiclib.Geodesic;
import net.sf.geographiclib.GeodesicData;
import net.sf.geographiclib.PolygonArea;
import net.sf.geographiclib.PolygonResult;
public class ComputePolygonArea {
private static Geodesic geod = Geodesic.WGS84; //定义坐标系
private static PolygonResult polygonResult;
/**
* 构建polygonResult对象
* @param polygon_wkt
*/
private static void getPolygonResult (String polygon_wkt){
PolygonArea polygonarea = new PolygonArea(geod,false);
for (String i: polygon_wkt.split(",")){
String[] point = i.split(" ");
Double lat = Double.valueOf(point[1]).doubleValue();
Double lon = Double.valueOf(point[0]).doubleValue();
polygonarea.AddPoint(lat, lon);
};
PolygonResult res = polygonarea.Compute();
polygonResult = res;
polygonarea.Clear();
};
/**
* 计算长度,单位米
* @param srcLat 起点纬度
* @param srcLon 起点经度
* @param descLat 终点纬度
* @param descLon 终点经度
* @return
*/
private static double getDistance(double srcLat, double srcLon, double descLat, double descLon) {
double result = 0.0f;
try {
GeodesicData g = geod.Inverse(srcLat, srcLon, descLat, descLon);
result = g.s12;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
*计算周长,单位米
* @param polygon_wkt
* @return
*/
private static double getPerimeter (String polygon_wkt){
getPolygonResult(polygon_wkt);
double perimeter = polygonResult.perimeter;
return perimeter;
}
/**
*计算面积,单位平方公里
* @param polygon_wkt
* @return
*/
private static double getArea (String polygon_wkt){
getPolygonResult(polygon_wkt);
double area = Math.abs(polygonResult.area);
return area;
}
public static void main(String[] args){
String polygon_wkt = new String("106.484265 29.931534,106.484909 29.932492,106.487054 29.931367,106.486293 29.930446,106.484265 29.931534");
Double per = getPerimeter(polygon_wkt);
System.out.println("周长: "+per);
Double aa = getArea(polygon_wkt);
System.out.println("面积: "+aa);
System.out.println("两点之间距离: "+getDistance(29.931534,106.484265,29.931367,106.487054));
}
}
输出结果:
周长: 720.5701485233899
面积: 29300.28374773264
两点之间距离: 269.92055435200126
文章来源:https://blog.csdn.net/cdl2008sky/article/details/134995444
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!