一、背景
記錄自身在項目實踐過程中編寫的一些GIS常用工具,源碼已上傳GitHub:
- 幾何基本屬性工具:提供幾何的基本空間屬性擷取和操作
- 坐标轉換工具:提供百度,高德坐标系與标準坐标系的互相轉化功能
- 幾何空間關系判斷:提供幾何關系(相交,包含等)判斷
二、工具類
1、GeometryRelatedUtil
1.1、空間位置關系判斷
- disjoint 相離
兩個幾何對象不相交,即不存在交點。
- equal 相等
如果給定的幾何圖形“空間相等”,則傳回TRUE 如果兩個幾何對象equal 為 TRUE
LINESTRING(0 0, 10 10) equal LINESTRING(0 0, 5 5, 10 10) = TRUE
- touch 相接
幾何是否至少有一個共同點,但它們的内部不相交。
- intersects 相交
如果兩個幾何對象在2D中空間相交(共享空間的任何部分)則傳回,如果它們不共享則傳回
TRUE
(它們是disjoint)
FALSE
- within 内含
如果幾何體A完全位于幾何體B内,則傳回 TRUE
6、contain 包含
幾何A包含幾何B,當且僅當B的外部沒有位于A的外部時,并且B的内部的至少一個點位于A的内部。
contain是within的反轉。是以contain(A,B)意味着within(B,A)
- True
- FALSE
- overlap 交疊
如果幾何圖形“空間重疊”,則傳回TRUE。意思是它們相交,但一個并不完全包含另一個。
- cover 覆寫
如果幾何B中沒有點在幾何A之外,則傳回 TRUE
- coveredBy 被覆寫
如果幾何A中沒有點在幾何B之外,則傳回 TRUE
- cross 交叉
提供的幾何是否有一些共同的内部點,但不是全部,則傳回 TRUE
。
1.2、空間疊加分析
- difference 差異分析
AB形狀的差異分析:A裡有B裡沒有的所有點的集合。
- union 聯合分析
AB的聯合操作:AB所有點的集合。
- intersection 交叉分析
AB的交叉操作:多邊形AB中所有共同點的集合。
- symDifference 對稱差異分析
AB形狀的對稱差異分析:位于A中或者B中但不同時在AB中的所有點的集合
2、GeometryBaseAnalysisUtil
1、getBuffer 擷取緩沖區
給定緩沖距離,生成緩沖區(當幾何對象坐标系為地理坐标系,機關為度;當幾何對象坐标系為投影坐标系,機關為米)
2、getEnvelope 外包矩形
擷取目前幾何對象的包絡矩形
3、convexHull 最小凸包多邊形
擷取目前幾何對象的最小外包多邊形
4、getCentroid 擷取質心點
擷取目前幾何對象的幾何中心點
5、getLength 擷取長度
擷取目前幾何對象的長度 線幾何傳回長度 面幾何傳回周長,其他傳回0.0
6、getArea 擷取面積
擷取目前幾何對象的面積 面幾何傳回面積,其他幾何傳回0.0
3、CoordinateTransformUtil
坐标轉換類 提供了BD09,GCJ02,WGS84等EPSG标準的坐标系互相轉換功能
- transform(transformGeomBean)
根據傳入的 TransformGeomBean 進行坐标轉換
- transformFormOther2EPSG(geometry,source,target)
給Geometry 從自定義坐标系轉EPSG标準坐标系
- transformFormEPSG2Other(geometry,source,target)
給Geometry 從EPSG标準坐标系轉自定義坐标系
- transformForEPSG(geometry,source,target)
Geometry EPSG标準坐标系互相轉化
- transformForOther(geometry,source,target)
Geometry 自定義坐标系互相轉化
三、基礎類
- BaseGeomBean:基本幾何類型類
名稱 類型 說明 geomType GeomTypeEnum 幾何類型 dataType DataTypeEnum 資料類型 coordinates String 坐标串 - TransformGeomBean:坐标轉換基礎類,繼承自
BaseGeomBean
名稱 類型 說明 fromCrsCode String 幾何對象源坐标系 toCrsCode String 幾何目标坐标系 - RelatedGeomBean:空間關系判斷基礎類,繼承自
BaseGeomBean
名稱 類型 說明 srid int 坐标參考系EPSG代碼
四、示例
1、分析
import bean.RelatedGeomBean;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryCollection;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTWriter;
import enums.DataTypeEnum;
import enums.GeomTypeEnum;
import factory.GeoFactory;
import org.junit.Test;
import utils.CoordinateTransformUtil;
import utils.GeometryBaseAnalysisUtil;
import utils.GeometryRelatedUtil;
import utils.GeometryUtil;
import java.io.IOException;
import java.util.List;
/**
* @author gisShield
* @title: GeometryRelatedUtilTest
* @projectName gis-tools
* @description: TODO
* @date 2021/5/4 12:04
*/
public class GeometryRelatedUtilTest {
GeoFactory myGeometryFactory = new GeoFactory();
@Test
public void disjointGeoTest() throws IOException, ParseException {
// 測試點線相離
RelatedGeomBean point1 = new RelatedGeomBean();
point1.setGeomType(GeomTypeEnum.POINT);
point1.setDataType(DataTypeEnum.BASE);
point1.setCoordinates("119.440,30.324");
RelatedGeomBean point2 = new RelatedGeomBean();
point2.setGeomType(GeomTypeEnum.LINE);
point2.setDataType(DataTypeEnum.WKT);
point2.setCoordinates("LINESTRING (119.440 30.539, 119.440 30.156)");
Geometry geom1 = myGeometryFactory.getGeometry(point1);
Geometry geom2 = myGeometryFactory.getGeometry(point2);
System.out.println("點相離判斷:" + GeometryRelatedUtil.disjointGeo(geom1, geom2));
}
@Test
public void equalsGeoTest() throws IOException, ParseException {
// 測試線線相等
RelatedGeomBean relatedGeomBean1 = new RelatedGeomBean();
relatedGeomBean1.setGeomType(GeomTypeEnum.LINE);
relatedGeomBean1.setDataType(DataTypeEnum.BASE);
relatedGeomBean1.setCoordinates("119.4401,30.539,119.440,30.156");
RelatedGeomBean relatedGeomBean2 = new RelatedGeomBean();
relatedGeomBean2.setGeomType(GeomTypeEnum.LINE);
relatedGeomBean2.setDataType(DataTypeEnum.WKT);
relatedGeomBean2.setCoordinates("LINESTRING (119.440 30.539, 119.440 30.156)");
Geometry geom1 = myGeometryFactory.getGeometry(relatedGeomBean1);
Geometry geom2 = myGeometryFactory.getGeometry(relatedGeomBean2);
System.out.println("線線相等判斷:" + GeometryRelatedUtil.equalsGeo(geom1, geom2));
}
@Test
public void touchesGeoTest() throws IOException, ParseException {
//測試線面相接
RelatedGeomBean point1 = new RelatedGeomBean();
point1.setGeomType(GeomTypeEnum.LINE);
point1.setDataType(DataTypeEnum.BASE);
// true 121.151931,30.284941,120.152074,30.295875
point1.setCoordinates("120.151931,30.284941,120.152074,30.295875");
RelatedGeomBean point2 = new RelatedGeomBean();
point2.setGeomType(GeomTypeEnum.POLYGON);
point2.setDataType(DataTypeEnum.WKT);
point2.setCoordinates("POLYGON ((120.152074 30.295875, 120.151816 30.276156, 120.176707 30.275637, 120.152074 30.295875))");
Geometry geom1 = myGeometryFactory.getGeometry(point1);
Geometry geom2 = myGeometryFactory.getGeometry(point2);
System.out.println("線面相接判斷:" + GeometryRelatedUtil.touchesGeo(geom1, geom2));
}
@Test
public void crossesTest() throws IOException, ParseException {
//測試線面交叉
RelatedGeomBean point1 = new RelatedGeomBean();
point1.setGeomType(GeomTypeEnum.LINE);
point1.setDataType(DataTypeEnum.BASE);
// false 120.151931,30.284941,120.152074,30.295875
point1.setCoordinates("120.1815,30.2847,120.1448,30.2847");
RelatedGeomBean point2 = new RelatedGeomBean();
point2.setGeomType(GeomTypeEnum.POLYGON);
point2.setDataType(DataTypeEnum.WKT);
point2.setCoordinates("POLYGON ((120.152074 30.295875, 120.151816 30.276156, 120.176707 30.275637, 120.152074 30.295875))");
Geometry geom1 = myGeometryFactory.getGeometry(point1);
Geometry geom2 = myGeometryFactory.getGeometry(point2);
System.out.println("線面相接判斷:" + GeometryRelatedUtil.crossesGeo(geom1, geom2));
}
@Test
public void withinGeoTest() throws IOException, ParseException {
//測試點在面内
RelatedGeomBean point1 = new RelatedGeomBean();
point1.setGeomType(GeomTypeEnum.POINT);
point1.setDataType(DataTypeEnum.BASE);
// false 120.151931,30.284941
point1.setCoordinates("120.1595,30.2818");
RelatedGeomBean point2 = new RelatedGeomBean();
point2.setGeomType(GeomTypeEnum.POLYGON);
point2.setDataType(DataTypeEnum.WKT);
point2.setCoordinates("POLYGON ((120.152074 30.295875, 120.151816 30.276156, 120.176707 30.275637, 120.152074 30.295875))");
Geometry geom1 = myGeometryFactory.getGeometry(point1);
Geometry geom2 = myGeometryFactory.getGeometry(point2);
System.out.println("點在面内判斷:" + GeometryRelatedUtil.withinGeo(geom1, geom2));
}
@Test
public void containsGeoTest() throws IOException, ParseException {
//測試面包含點
RelatedGeomBean point1 = new RelatedGeomBean();
point1.setGeomType(GeomTypeEnum.POINT);
point1.setDataType(DataTypeEnum.BASE);
// false 120.151931,30.284941
point1.setCoordinates("120.1595,30.2818");
RelatedGeomBean point2 = new RelatedGeomBean();
point2.setGeomType(GeomTypeEnum.POLYGON);
point2.setDataType(DataTypeEnum.WKT);
point2.setCoordinates("POLYGON ((120.152074 30.295875, 120.151816 30.276156, 120.176707 30.275637, 120.152074 30.295875))");
Geometry geom1 = myGeometryFactory.getGeometry(point1);
Geometry geom2 = myGeometryFactory.getGeometry(point2);
System.out.println("面包含點判斷:" + GeometryRelatedUtil.containsGeo(geom2, geom1));
}
@Test
public void overlapsGeoTest() throws IOException, ParseException {
//測試面面交疊
RelatedGeomBean point1 = new RelatedGeomBean();
point1.setGeomType(GeomTypeEnum.POLYGON);
point1.setDataType(DataTypeEnum.BASE);
// false 120.151931,30.284941
point1.setCoordinates("120.1554692,30.2872386,120.154711,30.280239,120.159814,30.279412,120.16834,30.28976,120.15899,30.29470,120.1554692,30.2872386");
RelatedGeomBean point2 = new RelatedGeomBean();
point2.setGeomType(GeomTypeEnum.POLYGON);
point2.setDataType(DataTypeEnum.WKT);
point2.setCoordinates("POLYGON ((120.152074 30.295875, 120.151816 30.276156, 120.176707 30.275637, 120.152074 30.295875))");
Geometry geom1 = myGeometryFactory.getGeometry(point1);
Geometry geom2 = myGeometryFactory.getGeometry(point2);
System.out.println("面面交疊判斷:" + GeometryRelatedUtil.overlapsGeo(geom1, geom2));
}
@Test
public void coversGeoTest() throws IOException, ParseException {
//測試面面覆寫
RelatedGeomBean point1 = new RelatedGeomBean();
point1.setGeomType(GeomTypeEnum.MULTIPOLYGON);
point1.setDataType(DataTypeEnum.BASE);
// false 120.151931,30.284941
point1.setCoordinates("120.15547,30.28724,120.15471,30.28024,120.15981,30.27941,120.16398,30.28149,120.15905,30.28646,120.15547,30.28724;120.16425,30.28087,120.16036,30.27856,120.16539,30.27774,120.16425,30.28087");
RelatedGeomBean point2 = new RelatedGeomBean();
point2.setGeomType(GeomTypeEnum.POLYGON);
point2.setDataType(DataTypeEnum.WKT);
point2.setCoordinates("POLYGON ((120.152074 30.295875, 120.151816 30.276156, 120.176707 30.275637, 120.152074 30.295875))");
Geometry geom1 = myGeometryFactory.getGeometry(point1);
Geometry geom2 = myGeometryFactory.getGeometry(point2);
System.out.println("面面覆寫判斷:" + GeometryRelatedUtil.coversGeo(geom2, geom1));
}
@Test
public void coveredByGeoTest() throws IOException, ParseException {
//測試線被面覆寫
RelatedGeomBean point1 = new RelatedGeomBean();
point1.setGeomType(GeomTypeEnum.MULTILINESTRING);
point1.setDataType(DataTypeEnum.BASE);
// false 120.151931,30.284941
point1.setCoordinates("120.15418,30.29101,120.15339,30.28739,120.15574,30.28856;120.15663,30.29011,120.17015,30.27718,120.15574,30.27698");
RelatedGeomBean point2 = new RelatedGeomBean();
point2.setGeomType(GeomTypeEnum.POLYGON);
point2.setDataType(DataTypeEnum.WKT);
point2.setCoordinates("POLYGON ((120.152074 30.295875, 120.151816 30.276156, 120.176707 30.275637, 120.152074 30.295875))");
Geometry geom1 = myGeometryFactory.getGeometry(point1);
Geometry geom2 = myGeometryFactory.getGeometry(point2);
System.out.println("線被面覆寫判斷:" + GeometryRelatedUtil.coveredByGeo(geom1, geom2));
}
@Test
public void intersectGeoTest() throws IOException, ParseException {
// 測試線面相交
RelatedGeomBean point1 = new RelatedGeomBean();
point1.setGeomType(GeomTypeEnum.MULTILINESTRING);
point1.setDataType(DataTypeEnum.BASE);
// false 120.151931,30.284941
point1.setCoordinates("120.15418,30.29101,120.15339,30.28739,120.15574,30.28856;120.15663,30.29011,120.17015,30.27718,120.15574,30.27698");
RelatedGeomBean point2 = new RelatedGeomBean();
point2.setGeomType(GeomTypeEnum.POLYGON);
point2.setDataType(DataTypeEnum.WKT);
point2.setCoordinates("POLYGON ((120.152074 30.295875, 120.151816 30.276156, 120.176707 30.275637, 120.152074 30.295875))");
Geometry geom1 = myGeometryFactory.getGeometry(point1);
Geometry geom2 = myGeometryFactory.getGeometry(point2);
System.out.println("線面相交判斷:" + GeometryRelatedUtil.intersectGeo(geom1, geom2));
}
@Test
public void isInBufferTest() throws IOException, ParseException {
RelatedGeomBean line = new RelatedGeomBean();
line.setGeomType(GeomTypeEnum.LINE);
line.setDataType(DataTypeEnum.BASE);
// true 121.151931,30.284941,120.152074,30.295875
line.setCoordinates("120.151931,30.284941,120.152074,30.295875");
RelatedGeomBean point1 = new RelatedGeomBean();
point1.setGeomType(GeomTypeEnum.POINT);
point1.setDataType(DataTypeEnum.BASE);
// false 120.151931,30.284941
point1.setCoordinates("121.1595,30.2818");
// double angular = 1000.0/(Math.PI/180) / 6378137;
Geometry geom1 = CoordinateTransformUtil.transformForEPSG(myGeometryFactory.getGeometry(line), "EPSG:4326", "EPSG:3857");
Geometry geom2 = CoordinateTransformUtil.transformForEPSG(myGeometryFactory.getGeometry(point1), "EPSG:4326", "EPSG:3857");
// buffer 以坐标系為機關 坐标系以米為機關 則buffer 為米 ,坐标系以度為機關 則buffer 為度
Geometry geometry = GeometryBaseAnalysisUtil.getBuffer(geom1, 0.000001);
System.out.println("緩沖區:" + GeometryRelatedUtil.isInBuffer(geometry, geom2));
}
@Test
public void isWithinDistanceTest() throws IOException, ParseException {
RelatedGeomBean line = new RelatedGeomBean();
line.setGeomType(GeomTypeEnum.LINE);
line.setDataType(DataTypeEnum.BASE);
line.setSrid(4326);
// true 121.151931,30.284941,120.152074,30.295875
line.setCoordinates("120.151931,30.284941,120.152074,30.295875");
RelatedGeomBean point1 = new RelatedGeomBean();
point1.setGeomType(GeomTypeEnum.POINT);
point1.setDataType(DataTypeEnum.BASE);
line.setSrid(4326);
// false 120.151931,30.284941
point1.setCoordinates("121.1595,30.2818");
// 進行米轉度操作
double angular = 0.000001;
System.out.println(angular);
Geometry geom1 = CoordinateTransformUtil.transformForEPSG(myGeometryFactory.getGeometry(line), "EPSG:4326", "EPSG:3857");
Geometry geom2 = CoordinateTransformUtil.transformForEPSG(myGeometryFactory.getGeometry(point1), "EPSG:4326", "EPSG:3857");
System.out.println("緩沖區:" + GeometryRelatedUtil.isWithinDistance(geom1, geom2, angular));
}
@Test
public void geometryDifferenceTest() throws IOException, ParseException {
//測試面面交疊
RelatedGeomBean point1 = new RelatedGeomBean();
point1.setGeomType(GeomTypeEnum.POLYGON);
point1.setDataType(DataTypeEnum.BASE);
// false 120.151931,30.284941
point1.setCoordinates("120.1554692,30.2872386,120.154711,30.280239,120.159814,30.279412,120.16834,30.28976,120.15899,30.29470,120.1554692,30.2872386");
RelatedGeomBean point2 = new RelatedGeomBean();
point2.setGeomType(GeomTypeEnum.POLYGON);
point2.setDataType(DataTypeEnum.WKT);
point2.setCoordinates("POLYGON ((120.152074 30.295875, 120.151816 30.276156, 120.176707 30.275637, 120.152074 30.295875))");
Geometry geom1 = myGeometryFactory.getGeometry(point1);
Geometry geom2 = myGeometryFactory.getGeometry(point2);
Geometry geometry = GeometryRelatedUtil.geometryDifference(geom1, geom2);
System.out.println("差異分析:" + geometry.getGeometryType());
List<Coordinate> coordinateList = GeometryUtil.getGeomCoordinates(geometry);
StringBuffer sb = new StringBuffer();
for (Coordinate coord :
coordinateList) {
sb.append(coord.x + " " + coord.y + ",");
}
System.out.println(sb.toString());
}
@Test
public void geometryUnionTest() throws IOException, ParseException {
//測試面面交疊
RelatedGeomBean point1 = new RelatedGeomBean();
point1.setGeomType(GeomTypeEnum.POLYGON);
point1.setDataType(DataTypeEnum.BASE);
// false 120.151931,30.284941
point1.setCoordinates("120.1554692,30.2872386,120.154711,30.280239,120.159814,30.279412,120.16834,30.28976,120.15899,30.29470,120.1554692,30.2872386");
RelatedGeomBean point2 = new RelatedGeomBean();
point2.setGeomType(GeomTypeEnum.POLYGON);
point2.setDataType(DataTypeEnum.WKT);
point2.setCoordinates("POLYGON ((120.152074 30.295875, 120.151816 30.276156, 120.176707 30.275637, 120.152074 30.295875))");
Geometry geom1 = myGeometryFactory.getGeometry(point1);
Geometry geom2 = myGeometryFactory.getGeometry(point2);
Geometry geometry = GeometryRelatedUtil.geometryUnion(geom1, geom2);
System.out.println("聯合分析:" + geometry.getGeometryType());
List<Coordinate> coordinateList = GeometryUtil.getGeomCoordinates(geometry);
StringBuffer sb = new StringBuffer();
for (Coordinate coord :
coordinateList) {
sb.append(coord.x + " " + coord.y + ",");
}
System.out.println(sb.toString());
}
@Test
public void geometryIntersectionTest() throws IOException, ParseException {
RelatedGeomBean point1 = new RelatedGeomBean();
point1.setGeomType(GeomTypeEnum.POLYGON);
point1.setDataType(DataTypeEnum.BASE);
// false 120.151931,30.284941
point1.setCoordinates("120.1554692,30.2872386,120.154711,30.280239,120.159814,30.279412,120.16834,30.28976,120.15899,30.29470,120.1554692,30.2872386");
RelatedGeomBean point2 = new RelatedGeomBean();
point2.setGeomType(GeomTypeEnum.POLYGON);
point2.setDataType(DataTypeEnum.WKT);
point2.setCoordinates("POLYGON ((120.152074 30.295875, 120.151816 30.276156, 120.176707 30.275637, 120.152074 30.295875))");
Geometry geom1 = myGeometryFactory.getGeometry(point1);
Geometry geom2 = myGeometryFactory.getGeometry(point2);
Geometry geometry = GeometryRelatedUtil.geometryIntersection(geom1, geom2);
System.out.println("交叉分析:" + geometry.getGeometryType());
List<Coordinate> coordinateList = GeometryUtil.getGeomCoordinates(geometry);
StringBuffer sb = new StringBuffer();
for (Coordinate coord :
coordinateList) {
sb.append(coord.x + " " + coord.y + ",");
}
System.out.println(sb.toString());
}
@Test
public void geometrySymdifferenceTest() throws IOException, ParseException {
RelatedGeomBean point1 = new RelatedGeomBean();
point1.setGeomType(GeomTypeEnum.POLYGON);
point1.setDataType(DataTypeEnum.BASE);
// false 120.151931,30.284941
point1.setCoordinates("120.1554692,30.2872386,120.154711,30.280239,120.159814,30.279412,120.16834,30.28976,120.15899,30.29470,120.1554692,30.2872386");
RelatedGeomBean point2 = new RelatedGeomBean();
point2.setGeomType(GeomTypeEnum.POLYGON);
point2.setDataType(DataTypeEnum.WKT);
point2.setCoordinates("POLYGON ((120.152074 30.295875, 120.151816 30.276156, 120.176707 30.275637, 120.152074 30.295875))");
Geometry geom1 = myGeometryFactory.getGeometry(point1);
Geometry geom2 = myGeometryFactory.getGeometry(point2);
Geometry geometry = GeometryRelatedUtil.geometrySymdifference(geom1, geom2);
System.out.println("對稱差異分析:" + geometry.getGeometryType());
List<List<Coordinate>> coordinateList = GeometryUtil.getGeomCoordinates((GeometryCollection) geometry);
for (List<Coordinate> coordList :
coordinateList) {
StringBuffer sb = new StringBuffer();
for (Coordinate coord :
coordList) {
sb.append(coord.x + " " + coord.y + ",");
}
System.out.println(sb.toString());
}
}
@Test
public void getEnvelope() throws IOException, ParseException {
RelatedGeomBean point2 = new RelatedGeomBean();
point2.setGeomType(GeomTypeEnum.POLYGON);
point2.setDataType(DataTypeEnum.WKT);
point2.setCoordinates("POLYGON ((120.152074 30.295875, 120.151816 30.276156, 120.176707 30.275637, 120.152074 30.295875))");
Geometry geom1 = myGeometryFactory.getGeometry(point2);
Geometry geometry = GeometryBaseAnalysisUtil.getEnvelope(geom1);
WKTWriter wktWriter = new WKTWriter();
System.out.println(wktWriter.write(geometry));
}
@Test
public void convexHull() throws IOException, ParseException {
RelatedGeomBean point2 = new RelatedGeomBean();
point2.setGeomType(GeomTypeEnum.POLYGON);
point2.setDataType(DataTypeEnum.WKT);
point2.setCoordinates("POLYGON ((120.152074 30.295875, 120.151816 30.276156, 120.176707 30.275637, 120.152074 30.295875))");
Geometry geom1 = myGeometryFactory.getGeometry(point2);
Geometry geometry = GeometryBaseAnalysisUtil.convexHull(geom1);
WKTWriter wktWriter = new WKTWriter();
System.out.println(wktWriter.write(geometry));
}
@Test
public void getCentroid() throws IOException, ParseException {
RelatedGeomBean point2 = new RelatedGeomBean();
point2.setGeomType(GeomTypeEnum.POLYGON);
point2.setDataType(DataTypeEnum.WKT);
point2.setCoordinates("POLYGON ((120.152074 30.295875, 120.151816 30.276156, 120.176707 30.275637, 120.152074 30.295875))");
Geometry geom1 = myGeometryFactory.getGeometry(point2);
Geometry geometry = GeometryBaseAnalysisUtil.getCentroid(geom1);
WKTWriter wktWriter = new WKTWriter();
System.out.println(wktWriter.write(geometry));
}
@Test
public void getLength() throws IOException, ParseException {
RelatedGeomBean point2 = new RelatedGeomBean();
point2.setGeomType(GeomTypeEnum.POLYGON);
point2.setDataType(DataTypeEnum.WKT);
point2.setCoordinates("POLYGON ((120.152074 30.295875, 120.151816 30.276156, 120.176707 30.275637, 120.152074 30.295875))");
Geometry geom1 = CoordinateTransformUtil.transformForEPSG(myGeometryFactory.getGeometry(point2), "EPSG:4326", "EPSG:3857");
double length = GeometryBaseAnalysisUtil.getLength(geom1);
System.out.println(length);
}
@Test
public void getArea() throws IOException, ParseException {
RelatedGeomBean point2 = new RelatedGeomBean();
point2.setGeomType(GeomTypeEnum.POLYGON);
point2.setDataType(DataTypeEnum.WKT);
point2.setCoordinates("POLYGON ((120.152074 30.295875, 120.151816 30.276156, 120.176707 30.275637, 120.152074 30.295875))");
Geometry geom1 = CoordinateTransformUtil.transformForEPSG(myGeometryFactory.getGeometry(point2), "EPSG:4326", "EPSG:3857");
double area = GeometryBaseAnalysisUtil.getArea(geom1);
System.out.println(String.valueOf(area));
}
@Test
public void geojson2Geom() throws IOException, ParseException {
RelatedGeomBean relatedGeomBean = new RelatedGeomBean();
relatedGeomBean.setGeomType(GeomTypeEnum.POINT);
relatedGeomBean.setDataType(DataTypeEnum.GEOJSON);
relatedGeomBean.setCoordinates("{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[102,0.5]},\"properties\":{\"prop0\":\"value0\"}}");
Geometry geom1 = myGeometryFactory.getGeometry(relatedGeomBean);
System.out.println(GeometryUtil.getGeomCoordinatesString(geom1));
}
}
2、坐标轉換
import bean.TransformGeomBean;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryCollection;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTWriter;
import enums.DataTypeEnum;
import enums.GeomTypeEnum;
import org.junit.Test;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.operation.TransformException;
import utils.CoordinateTransformUtil;
import utils.GeometryUtil;
import java.io.IOException;
/**
* @author gisShield
* @title: CoordinateTransformUtilTest
* @projectName gis-tools
* @description: TODO
* @date 2021/5/4 12:06
*/
public class CoordinateTransformUtilTest {
/**
* 坐标轉換
*
* @throws FactoryException
* @throws TransformException
* @throws ParseException
* @throws IOException
*/
@Test
public void transform() throws FactoryException, TransformException, ParseException, IOException {
TransformGeomBean transformGeomBean = new TransformGeomBean();
transformGeomBean.setFromCrsCode("gcj02");
transformGeomBean.setToCrsCode("EPSG:4326");
transformGeomBean.setCoordinates("119.695947,30.202053,119.697097,30.20237,119.698332,30.202705,119.700422,30.203293,119.704157,30.204344,119.705496,30.204728,119.705912,30.204865,119.706183,30.204981,119.706381,30.205087,119.706614,30.205224,119.706955,30.205442,119.707277,30.205704,119.707634,30.20601,119.708076,30.206392,119.708519,30.206779,119.708811,30.20706,119.709262,30.207581,119.709664,30.208112,119.709927,30.208511,119.710211,30.209005,119.710412,30.209445,119.710576,30.209832,119.71071,30.210208,119.710895,30.211047,119.711008,30.211733,119.711064,30.212178,119.711078,30.212482,119.711072,30.21276,119.71104,30.213008,119.711002,30.213253,119.710863,30.213995,119.710589,30.215003,119.709191,30.219181,119.708362,30.221611,119.707681,30.223528,119.707176,30.224967,119.706829,30.225965,119.706768,30.226162,119.706728,30.226326,119.706696,30.226499,119.706676,30.22662,119.706659,30.226703,119.706441,30.227561,119.706218,30.228427,119.706099,30.228898,119.705829,30.229964,119.705705,30.230543,119.705622,30.230981,119.705561,30.231484,119.705539,30.231924,119.705526,30.232274,119.705547,30.232615,119.70559,30.2329,119.705689,30.233201,119.705845,30.233488,119.706062,30.233774,119.706309,30.234045,119.706561,30.23426,119.706797,30.234443,119.707173,30.234668,119.707674,30.234911,119.708368,30.235188,119.709023,30.235401,119.709635,30.235588,119.710333,30.235801,119.71095,30.236007,119.711454,30.236213,119.712342,30.236616,119.714697,30.237752,119.721277,30.240471,119.7271,30.242904,119.729519,30.243933,119.730468,30.244311,119.73082,30.244449,119.731046,30.244524,119.731256,30.24458,119.731434,30.244613,119.731622,30.244625,119.731838,30.244627,119.732145,30.244621,119.732915,30.244619,119.736877,30.244544,119.73742,30.244554,119.738323,30.244543,119.739236,30.244527,119.739916,30.244526,119.740268,30.244531,119.740643,30.244568,119.741115,30.244628,119.741657,30.244737,119.742153,30.244876,119.742639,30.24505,119.742929,30.245168,119.743178,30.245286,119.743374,30.245395,119.743556,30.245513,119.743733,30.245647,119.743961,30.24584,119.744194,30.246045,119.744554,30.246365,119.745793,30.247447,119.747059,30.248555,119.747948,30.249365,119.74821,30.249586,119.748455,30.249803,119.748637,30.249957,119.748937,30.250207,119.753115,30.253721,119.754242,30.25461,119.755013,30.255264,119.755663,30.255795,119.75632,30.256319,119.756687,30.256583,119.756982,30.256792,119.757401,30.257072,119.757822,30.257304,119.758356,30.257517,119.758962,30.257688,119.75982,30.25782,119.760972,30.257876,119.762155,30.257873,119.763383,30.257883,119.764346,30.257922,119.765068,30.25798,119.765729,30.258065,119.766231,30.258155,119.76677,30.258264,119.767269,30.258382,119.767747,30.258509,119.768511,30.258736,119.773253,30.26024,119.77384,30.260407,119.774747,30.26062,119.775369,30.260767,119.77605,30.260916,119.776595,30.260992,119.777116,30.261065,119.778044,30.261183,119.779904,30.261439,119.780778,30.261583,119.781116,30.261629,119.781391,30.261651,119.781679,30.261661,119.782346,30.261668,119.782984,30.261663,119.783653,30.261634,119.78464,30.261563,119.785138,30.261535,119.78646,30.261491,119.787123,30.261454,119.787659,30.261408,119.78844,30.261353,119.788943,30.261331,119.789456,30.261312,119.789843,30.261302,119.790197,30.261279,119.790877,30.261235,119.791227,30.261208,119.791646,30.261189,119.792113,30.261159,119.792631,30.26112,119.794931,30.260942,119.797001,30.260685,119.805287,30.259621,119.806355,30.259498,119.806945,30.259433,119.813779,30.258921,119.815312,30.258774,119.816241,30.258649,119.816711,30.258581,119.817058,30.258518,119.81768,30.258383,119.820153,30.25794,119.822294,30.257532,119.823101,30.25736,119.823483,30.257278,119.823789,30.257208,119.824622,30.257069,119.825131,30.257022,119.825641,30.256969,119.826467,30.256918,119.826999,30.256895,119.827557,30.256869,119.828001,30.256857,119.828472,30.256856,119.828928,30.256867,119.830969,30.256897,119.831957,30.256923,119.83318,30.256965,119.834083,30.256999,119.836057,30.257015,119.838423,30.257075,119.84133,30.257128,119.844047,30.257186,119.846875,30.257184,119.851783,30.257172,119.854063,30.257202,119.858164,30.257184,119.85991,30.257168,119.86098,30.257149,119.861586,30.257133,119.862144,30.257084,119.862831,30.257006,119.863402,30.256913,119.864169,30.256762,119.864759,30.256637,119.865425,30.256468,119.866079,30.25628,119.866734,30.256037,119.867587,30.255703,119.868442,30.255314,119.869412,30.254818,119.870017,30.254466,119.870607,30.254086,119.872204,30.253034,119.873371,30.252256,119.874281,30.251604,119.875312,30.250889,119.87562,30.2507,119.876344,30.250331,119.876573,30.250238,119.876761,30.250181,119.877014,30.250115,119.877308,30.250042,119.877828,30.249918,119.878316,30.249779,119.878796,30.249646,119.88211,30.248744,119.883698,30.248341,119.884808,30.248112,119.885602,30.247977,119.886525,30.247848,119.887338,30.247727,119.887914,30.247646,119.888539,30.247588,119.88906,30.247551,119.892506,30.247373,119.894695,30.247261,119.896792,30.247183,119.897348,30.24717,119.897686,30.247149,119.898002,30.24711,119.898584,30.247017,119.899019,30.246927,119.899166,30.24689,119.899603,30.246777,119.900051,30.246663,119.900311,30.246594,119.900574,30.246515,119.900825,30.246435,119.901036,30.246355,119.901431,30.246194,119.901746,30.246074,119.902599,30.245765,119.912384,30.242387,119.91295,30.242236,119.913578,30.2421,119.914508,30.241928,119.915005,30.241861,119.915415,30.241819,119.91729,30.241662,119.920666,30.241449,119.921361,30.241402,119.92171,30.241381,119.923086,30.241307,119.923907,30.241282,119.924341,30.241293,119.924682,30.241316,119.925012,30.241358,119.925339,30.241416,119.925741,30.241509,119.926179,30.241632,119.926589,30.241764,119.927329,30.242026,119.927904,30.242286,119.929447,30.243033,119.930981,30.243771,119.931512,30.244046,119.9396,30.248071,119.943264,30.249953,119.944326,30.250458,119.945072,30.25075,119.945436,30.250866,119.945823,30.250947,119.946434,30.251049,119.947024,30.251118,119.947475,30.251139,119.948003,30.251134,119.948714,30.251065,119.949285,30.250961,119.950036,30.250803,119.950712,30.250629,119.95131,30.250432,119.951892,30.250198,119.952635,30.24989,119.954486,30.249163,119.955111,30.248923,119.955554,30.248755,119.955998,30.248584,119.95648,30.248387,119.958687,30.247567,119.959838,30.247226,119.960213,30.247122,119.960476,30.247057,119.96071,30.247029,119.960876,30.247013,119.961045,30.247004,119.961222,30.247015,119.961418,30.247045,119.961597,30.247094,119.961774,30.247168,119.96197,30.247268,119.96211,30.247358,119.96223,30.247446,119.962327,30.247551,119.962426,30.247671,119.962566,30.247884,119.962807,30.248355,119.966707,30.255987,119.968489,30.259422,119.968843,30.260134,119.969202,30.260846,119.969543,30.261423,119.969699,30.261627,119.969841,30.261764,119.970004,30.261868,119.970133,30.261918,119.970254,30.261948,119.970405,30.261968,119.970554,30.261973,119.970734,30.261968,119.971013,30.261954,119.971346,30.261931,119.971898,30.26188,119.972842,30.261786,119.97382,30.261695,119.988704,30.260347,119.990975,30.260213,119.992117,30.260136");
transformGeomBean.setGeomType(GeomTypeEnum.LINE);
transformGeomBean.setDataType(DataTypeEnum.BASE);
Geometry geometry = CoordinateTransformUtil.transform(transformGeomBean);
if (GeometryUtil.isGeometryCollection(geometry)) {
System.out.println(GeometryUtil.getGeomCoordinatesString((GeometryCollection) geometry));
} else {
WKTWriter wktWriter = new WKTWriter();
System.out.println(wktWriter.write(geometry));
}
}
}