一、添加依賴的Maven庫:
<dependency> <groupId>net.sf.geographiclib</groupId> <artifactId>GeographicLib-Java</artifactId> <version>1.48</version> </dependency>
二、工具類實現:
package com.steven.gis.util;
import net.sf.geographiclib.Geodesic;
import net.sf.geographiclib.GeodesicData;
import net.sf.geographiclib.PolygonArea;
import net.sf.geographiclib.PolygonResult;
/**
* 使用GeographicLib來封裝通用的GIS能力
*
* @author liuzhuanghong
*
*/
public class GeographicLibUtil {
/**
* 計算兩個坐標點之間的距離,單位為米
*
* @param srcLat
* 起始點的緯度
* @param srcLon
* 起始點的經度
* @param descLat
* 目標點的緯度
* @param descLon
* 目標點的經度
* @return 兩個坐標點之間的距離,單位為米
*/
public static double computeLengthWithGeographicLib(double srcLat, double srcLon, double descLat, double descLon) {
double result = 0.0f;
try {
GeodesicData g = Geodesic.WGS84.Inverse(srcLat, srcLon, descLat, descLon);
result = g.s12;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* @param coords
* [緯度,經度]格式的數組
* @return 平方米
*/
public static double computeAreaWithGeographicLib(double[][] coords) {
double result = 0.0f;
try {
PolygonArea p = new PolygonArea(Geodesic.WGS84, false);
int size = coords.length;
for (int i = 0; i < size; i++) {
p.AddPoint(coords[i][0], coords[i][1]);
}
PolygonResult r = p.Compute();
result = r.area;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
三、測試代碼:
package com.steven.gis.util;
import org.junit.Test;
import com.steven.gis.util.GeographicLibUtil;
import net.sf.geographiclib.Geodesic;
import net.sf.geographiclib.GeodesicData;
import net.sf.geographiclib.PolygonArea;
import net.sf.geographiclib.PolygonResult;
/**
* 測試使用封裝了GeographicLib庫的工具類
*
* @author liuzhuanghong
*
*/
public class TestGeographiclibArea {
/**
* 測試的數據
*/
private static double[][] coords = { { 22.656304210485004, 114.06466523520893 },
{ 22.651868529042016, 114.06466523520893 }, { 22.651868529042016, 114.06605998390202 },
{ 22.656304210485004, 114.06605998390202 } };
/**
* 沒有使用封裝的工具類實現
*/
@Test
public void computeWithGeographicLib() {
System.out.println("==========================");
System.out.println("[沒有使用封裝的工具類實現]");
GeodesicData g = null;
double distance = 0.0f;
for (int i = 0; i < coords.length - 1; i++) {
// System.out.println(coords[i][0] + "," + coords[i][1]);
g = Geodesic.WGS84.Inverse(coords[i][0], coords[i][1], coords[i + 1][0], coords[i + 1][1]);
distance = g.s12;
System.out.println("第" + (i + 1) + "和第" + (i + 2) + "個點的距離是:" + distance + "米");
}
g = Geodesic.WGS84.Inverse(coords[0][0], coords[0][1], coords[3][0], coords[3][1]);
distance = g.s12;
PolygonArea p = new PolygonArea(Geodesic.WGS84, false);
for (int i = 0; i < coords.length; i++) {
p.AddPoint(coords[i][0], coords[i][1]);
}
PolygonResult result = p.Compute();
System.out.println("節點數:" + result.num + ",總長度:" + result.perimeter + "米,面積:" + result.area + "平方米");
}
/**
* 使用封裝的工具類計算面積
*/
@Test
public void testGeographicLibUtilArea() {
System.out.println("==========================");
System.out.println("[使用封裝的工具類計算面積]");
double areaResult = GeographicLibUtil.computeAreaWithGeographicLib(coords);
System.out.println("使用工具類計算的面積是:" + areaResult + "平方米");
}
/**
* 使用封裝的工具類計算兩個點之間的距離
*/
@Test
public void testGeographicLibUtilLength() {
System.out.println("==========================");
System.out.println("[使用封裝的工具類計算長度]");
double distance = 0.0f;
for (int i = 0; i < coords.length - 1; i++) {
// System.out.println(coords[i][0] + "," + coords[i][1]);
distance = GeographicLibUtil.computeLengthWithGeographicLib(coords[i][0], coords[i][1], coords[i + 1][0],
coords[i + 1][1]);
System.out.println("第" + (i + 1) + "和第" + (i + 2) + "個點的距離是:" + distance + "米");
}
}
}
測試代碼運行效果如下:
========================== [沒有使用封裝的工具類實現] 第1和第2個點的距離是:491.2038282207398米 第2和第3個點的距離是:143.35724147447107米 第3和第4個點的距離是:491.2038282207398米 節點數:4,總長度:1269.1175338166026米,面積:70416.49471479654平方米 ========================== [使用封裝的工具類計算面積] 使用工具類計算的面積是:70416.49471479654平方米 ========================== [使用封裝的工具類計算長度] 第1和第2個點的距離是:491.2038282207398米 第2和第3個點的距離是:143.35724147447107米 第3和第4個點的距離是:491.2038282207398米
