Java地理信息開發-圓形、橢圓、矩形、扇形的表示方法及相關判斷
寫在前面
因爲項目需要,做了些地理信息的開發工作,主要包括三部分:
1、將長度(米)轉換成經緯度的度數;
2、將圓形、橢圓、矩形、扇形用Java對象表示:用到com.vividsolutions.jts.util.GeometricShapeFactory工廠類和com.vividsolutions.jts.geom.Geometry對象;
3、判斷線段與上述圖形在地圖上是否相交。
一、長度(米)轉換爲經緯度
長度和經緯度轉換中,緯度轉換容易,因爲所有緯線等長,可是不同位置經線長度不同,導致不同位置經度轉換不同。下面我整理經度和緯度轉換兩種方式:
- 定義地球半徑
/**
* 定義地球半徑(米)
*/
private static final double R_EARTH = 6371000;
- 定義地球赤道周長
/**
* 定義地球赤道周長(米)
*/
private static final double P_EARTH = 2 * Math.PI * R_EARTH;
- 將長度轉換爲緯度
/**
* 將Y軸的長度(米)轉換成緯度
* @param length
* @return
*/
public static double parseYLengthToDegree(double length){
//將length長度轉換爲度數
double yDegree = length / P_EARTH * 360;
return yDegree;
}
- 將長度轉換爲經度
根據線段所在緯度,首先計算當前緯度的地球經線長,除以360得出每度經線的長度,即可將長度轉換爲經度。
/**
* 根據所在緯度,將X軸的長度(米)轉換成經度
* (因爲不同緯度下,1°經度代表的長度不同)
* @param y 所在緯度
* @param length 線段長度
* @return
*/
public static double parseXLengthToDegree(double y,double length){
//將角度(緯度)轉換爲弧度
double latRadian = Math.toRadians(y);
//計算當前緯度地球周長
double latPEarth = P_EARTH * Math.cos(latRadian);
//將length長度轉換爲度數
double xDegree = length / latPEarth * 360;
return xDegree;
}
二、將圓形、橢圓、矩形、扇形用Geometry對象表示
表示圓形
輸入參數爲中心點的經緯度、半徑(米),先將半徑轉換爲經緯度單位,使用com.vividsolutions.jts.util包,生成GeometricShapeFactory工廠類對象,設置相應參數,生成Geometry圓形對象。
import com.vividsolutions.jts.geom.*;
import com.vividsolutions.jts.util.GeometricShapeFactory;
/**
* 根據圓形中心點經緯度、半徑生成圓形(類圓形,32邊多邊形)
* @param x 中心點經度
* @param y 中心點緯度
* @param radius 半徑(米)
* @return
*/
public static Polygon createCircle(double x, double y, final double radius) {
//將半徑轉換爲度數
double radiusDegree = parseYLengthToDegree(radius);
//生成工廠類
private static GeometricShapeFactory shapeFactory = new GeometricShapeFactory();
//設置生成的類圓形邊數
shapeFactory.setNumPoints(32);
//設置圓形中心點經緯度
shapeFactory.setCentre(new Coordinate(x, y));
//設置圓形直徑
shapeFactory.setSize(radiusDegree * 2);
//使用工廠類生成圓形
Polygon circle = shapeFactory.createCircle();
return circle;
}
表示橢圓
輸入參數爲:中心點經緯度、長軸長度(米)、短軸長度(米)、長軸和X軸夾角(度)。
首先將長軸和短軸轉換爲經緯度,
然後將夾角轉換爲弧度,
爲GeometricShapeFactory工廠類設置相關屬性,生成Geometry橢圓對象。
/**
* 根據中心點經緯度、長軸、短軸、角度生成橢圓
* @param x
* @param y
* @param macroaxis
* @param brachyaxis
* @param direction
* @return
*/
public static Polygon createEllipse(double x,double y,double macroaxis,double brachyaxis,double direction){
//將長短軸轉換爲度數
double macroaxisDegree = parseYLengthToDegree(macroaxis);
double brachyaxisDegree = parseYLengthToDegree(brachyaxis);
//將夾角轉換爲弧度
double radians = Math.toRadians(direction);
//設置中心點
shapeFactory.setCentre(new Coordinate(x,y));
//設置長軸長度
shapeFactory.setWidth(macroaxisDegree);
//設置短軸長度
shapeFactory.setHeight(brachyaxisDegree);
//設置長軸和X軸夾角
shapeFactory.setRotation(radians);
//生成橢圓對象
Polygon ellipse = shapeFactory.createEllipse();
return ellipse;
}
表示矩形
待完善。。。
表示扇形
輸入參數:中心點經緯度、扇形半徑(米)、起始角度、終止角度;
首先將半徑轉換爲經緯度度數,
然後將起始角度和終止角度轉換爲弧度,
爲GeometricShapeFactory工廠類設置相關屬性,生成Geometry扇形對象。
/**
* 根據中心點經緯度、半徑、起止角度生成扇形
* @param x 經度
* @param y 緯度
* @param radius 半徑(米)
* @param bAngle 起始角度
* @param eAngle 終止角度
* @param pointsNum 點數
* @return
*/
public static Polygon createSector(double x,double y,double radius,double bAngle,double eAngle,int pointsNum){
//將半徑轉換爲度數
double radiusDegree = parseYLengthToDegree(radius);
//將起始角度轉換爲弧度
double bAngleRadian = Math.toRadians(bAngle);
//將終止角度-起始角度計算扇形夾角
double angleRadian = Math.toRadians((eAngle - bAngle + 360) % 360);
//設置點數
shapeFactory.setNumPoints(pointsNum);
//設置中心點經緯度
shapeFactory.setCentre(new Coordinate(x, y));
//設置直徑
shapeFactory.setSize(radiusDegree * 2);
//傳入起始角度和扇形夾角,生成扇形
Polygon sector = shapeFactory.createArcPolygon(bAngleRadian,angleRadian);
return sector;
}
效果展示
三、判斷線段與圖形是否相交
先創建一個線段對象:
String lineStr = "LINESTRING(123.22 35.22,127.32 38.23)";
WKTReader wktReader = new WKTReader();
LineString line = (LineString) wktReader.read(strLine);
然後通過線段對象的方法來判斷:
//判斷線段是否包含在橢圓中
line.within(ellipse)
//判斷線段與橢圓是否相交
line.crosses(ellipse)
其他形狀同理。
總結
上面是我項目用到技術的簡單整理,希望可以幫助到大家。