|
@@ -1,18 +1,27 @@
|
|
|
package com.onemap.sanya.utils;
|
|
|
|
|
|
+import com.onemap.common.core.annotation.Excel;
|
|
|
import com.onemap.sanya.domain.spatial.CustomXY;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.lang.reflect.Field;
|
|
|
import java.text.ParseException;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
+
|
|
|
+import com.onemap.sanya.domain.spatial.FldInfo;
|
|
|
+import org.geotools.geojson.GeoJSON;
|
|
|
import org.geotools.geometry.jts.JTS;
|
|
|
+import org.geotools.referencing.crs.DefaultGeographicCRS;
|
|
|
import org.geotools.util.factory.Hints;
|
|
|
|
|
|
import org.locationtech.jts.geom.*;
|
|
|
|
|
|
-import org.locationtech.jts.geom.Polygon;
|
|
|
+import org.locationtech.jts.geom.Polygon;
|
|
|
import org.locationtech.jts.geom.GeometryFactory;
|
|
|
import org.locationtech.jts.io.WKTReader;
|
|
|
import org.locationtech.jts.io.WKTWriter;
|
|
|
-import org.geotools.referencing.CRS;
|
|
|
+import org.geotools.referencing.CRS;
|
|
|
import org.opengis.referencing.crs.CoordinateReferenceSystem;
|
|
|
import org.opengis.referencing.cs.CoordinateSystem;
|
|
|
import org.opengis.referencing.operation.MathTransform;
|
|
@@ -22,14 +31,78 @@ public class SpatialOperate {
|
|
|
// static GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(hints);
|
|
|
|
|
|
|
|
|
- static PrecisionModel precisionModel = new PrecisionModel(PrecisionModel.FIXED);
|
|
|
+ static PrecisionModel precisionModel = new PrecisionModel(PrecisionModel.FLOATING);
|
|
|
|
|
|
// 定义一个坐标参考系统,这里使用EPSG:4326,即WGS84
|
|
|
static final int srid = 4490;
|
|
|
+ static String srid4326 = "GEOGCS[\"WGS 84\",\n" +
|
|
|
+ " DATUM[\"WGS_1984\",\n" +
|
|
|
+ " SPHEROID[\"WGS 84\",6378137,298.257223563,\n" +
|
|
|
+ " AUTHORITY[\"EPSG\",\"7030\"]],\n" +
|
|
|
+ " AUTHORITY[\"EPSG\",\"6326\"]],\n" +
|
|
|
+ " PRIMEM[\"Greenwich\",0,\n" +
|
|
|
+ " AUTHORITY[\"EPSG\",\"8901\"]],\n" +
|
|
|
+ " UNIT[\"degree\",0.0174532925199433,\n" +
|
|
|
+ " AUTHORITY[\"EPSG\",\"9122\"]],\n" +
|
|
|
+ " AUTHORITY[\"EPSG\",\"4326\"]]";
|
|
|
+ static String srid3857 = "PROJCS[\"WGS 84 / Pseudo-Mercator\",\n" +
|
|
|
+ " GEOGCS[\"WGS 84\",\n" +
|
|
|
+ " DATUM[\"WGS_1984\",\n" +
|
|
|
+ " SPHEROID[\"WGS 84\",6378137,298.257223563,\n" +
|
|
|
+ " AUTHORITY[\"EPSG\",\"7030\"]],\n" +
|
|
|
+ " AUTHORITY[\"EPSG\",\"6326\"]],\n" +
|
|
|
+ " PRIMEM[\"Greenwich\",0,\n" +
|
|
|
+ " AUTHORITY[\"EPSG\",\"8901\"]],\n" +
|
|
|
+ " UNIT[\"degree\",0.0174532925199433,\n" +
|
|
|
+ " AUTHORITY[\"EPSG\",\"9122\"]],\n" +
|
|
|
+ " AUTHORITY[\"EPSG\",\"4326\"]],\n" +
|
|
|
+ " PROJECTION[\"Mercator_1SP\"],\n" +
|
|
|
+ " PARAMETER[\"central_meridian\",0],\n" +
|
|
|
+ " PARAMETER[\"scale_factor\",1],\n" +
|
|
|
+ " PARAMETER[\"false_easting\",0],\n" +
|
|
|
+ " PARAMETER[\"false_northing\",0],\n" +
|
|
|
+ " UNIT[\"metre\",1,\n" +
|
|
|
+ " AUTHORITY[\"EPSG\",\"9001\"]],\n" +
|
|
|
+ " AXIS[\"Easting\",EAST],\n" +
|
|
|
+ " AXIS[\"Northing\",NORTH],\n" +
|
|
|
+ " EXTENSION[\"PROJ4\",\"+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs\"],\n" +
|
|
|
+ " AUTHORITY[\"EPSG\",\"3857\"]]";
|
|
|
+ static String srid4490 = "GEOGCS[\"China Geodetic Coordinate System 2000\",\n" +
|
|
|
+ " DATUM[\"China_2000\",\n" +
|
|
|
+ " SPHEROID[\"CGCS2000\",6378137,298.257222101,\n" +
|
|
|
+ " AUTHORITY[\"EPSG\",\"1024\"]],\n" +
|
|
|
+ " AUTHORITY[\"EPSG\",\"1043\"]],\n" +
|
|
|
+ " PRIMEM[\"Greenwich\",0,\n" +
|
|
|
+ " AUTHORITY[\"EPSG\",\"8901\"]],\n" +
|
|
|
+ " UNIT[\"degree\",0.0174532925199433,\n" +
|
|
|
+ " AUTHORITY[\"EPSG\",\"9122\"]],\n" +
|
|
|
+ " AUTHORITY[\"EPSG\",\"4490\"]]";
|
|
|
+ static String srid4546 = "PROJCS[\"CGCS2000 / 3-degree Gauss-Kruger CM 111E\",\n" +
|
|
|
+ " GEOGCS[\"China Geodetic Coordinate System 2000\",\n" +
|
|
|
+ " DATUM[\"China_2000\",\n" +
|
|
|
+ " SPHEROID[\"CGCS2000\",6378137,298.257222101,\n" +
|
|
|
+ " AUTHORITY[\"EPSG\",\"1024\"]],\n" +
|
|
|
+ " AUTHORITY[\"EPSG\",\"1043\"]],\n" +
|
|
|
+ " PRIMEM[\"Greenwich\",0,\n" +
|
|
|
+ " AUTHORITY[\"EPSG\",\"8901\"]],\n" +
|
|
|
+ " UNIT[\"degree\",0.0174532925199433,\n" +
|
|
|
+ " AUTHORITY[\"EPSG\",\"9122\"]],\n" +
|
|
|
+ " AUTHORITY[\"EPSG\",\"4490\"]],\n" +
|
|
|
+ " PROJECTION[\"Transverse_Mercator\"],\n" +
|
|
|
+ " PARAMETER[\"latitude_of_origin\",0],\n" +
|
|
|
+ " PARAMETER[\"central_meridian\",111],\n" +
|
|
|
+ " PARAMETER[\"scale_factor\",1],\n" +
|
|
|
+ " PARAMETER[\"false_easting\",500000],\n" +
|
|
|
+ " PARAMETER[\"false_northing\",0],\n" +
|
|
|
+ " UNIT[\"metre\",1,\n" +
|
|
|
+ " AUTHORITY[\"EPSG\",\"9001\"]],\n" +
|
|
|
+ " AUTHORITY[\"EPSG\",\"4546\"]]";
|
|
|
+
|
|
|
|
|
|
// 创建GeometryFactory
|
|
|
static GeometryFactory geometryFactory = new GeometryFactory(precisionModel, srid);
|
|
|
|
|
|
+
|
|
|
public static Polygon createPolygonByXYArray(List<CustomXY> coors) {
|
|
|
|
|
|
Polygon pPolygon = null;
|
|
@@ -37,21 +110,29 @@ public class SpatialOperate {
|
|
|
return pPolygon;
|
|
|
Coordinate[] coordinates = new Coordinate[]{new Coordinate(1, 2), new Coordinate(1, 2), new Coordinate(1, 2)};
|
|
|
pPolygon = geometryFactory.createPolygon();
|
|
|
- geometryFactory.getSRID();
|
|
|
|
|
|
return pPolygon;
|
|
|
}
|
|
|
- public static String geometry2Wkt(Geometry geometry)
|
|
|
- {
|
|
|
- String wkt="";
|
|
|
- WKTWriter wktWriter=new WKTWriter();
|
|
|
- wkt= wktWriter.write(geometry);
|
|
|
- return wkt;
|
|
|
+
|
|
|
+ public static String geometry2Wkt(Geometry geometry) {
|
|
|
+ String wkt = "SRID=" + srid + ";";
|
|
|
+ WKTWriter wktWriter = new WKTWriter();
|
|
|
+ wkt += wktWriter.write(geometry);
|
|
|
+ return wkt;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String geometry2GeoJson(Geometry geometry) throws IOException {
|
|
|
+ String wkt = "";
|
|
|
+ GeoJSON.write(geometry, wkt);
|
|
|
+ return wkt;
|
|
|
|
|
|
}
|
|
|
+
|
|
|
public static Point createPoint(Double x, Double y) {
|
|
|
return geometryFactory.createPoint(new Coordinate(x, y));
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 根据坐标点创建
|
|
|
*
|
|
@@ -62,28 +143,33 @@ public class SpatialOperate {
|
|
|
Polygon polygon = null;
|
|
|
if (points == null)
|
|
|
return polygon;
|
|
|
+
|
|
|
Coordinate[] coordinates = points.toArray(new Coordinate[points.size()]);
|
|
|
- polygon = geometryFactory.createPolygon(coordinates);
|
|
|
+ LinearRing ring = geometryFactory.createLinearRing(coordinates);
|
|
|
+ polygon = geometryFactory.createPolygon(ring);
|
|
|
+ polygon.setSRID(srid);
|
|
|
return polygon;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 经纬度转墨卡托 便于面积转换
|
|
|
+ *
|
|
|
* @param geom
|
|
|
* @return
|
|
|
*/
|
|
|
public static Geometry lonlat2WebMactor(Geometry geom) {
|
|
|
try {
|
|
|
//这里是以OGC WKT形式定义的是World Mercator投影,网页地图一般使用该投影
|
|
|
- Integer crid = geom.getSRID();
|
|
|
- if (crid == 0)
|
|
|
- crid = srid;
|
|
|
- CoordinateReferenceSystem sourceTarget = CRS.decode("EPSG:" + crid.toString());
|
|
|
- CoordinateReferenceSystem crsTarget = CRS.decode("EPSG:4546");
|
|
|
|
|
|
+ CoordinateReferenceSystem sourceTarget = CRS.parseWKT(srid4490);
|
|
|
+
|
|
|
+// MathTransform transform1 = CRS.findMathTransform(DefaultGeographicCRS.WGS84, sourceTarget);
|
|
|
+// Geometry geometry1= JTS.transform(geom, transform1);
|
|
|
// 投影转换
|
|
|
- MathTransform transform = CRS.findMathTransform(sourceTarget, crsTarget);
|
|
|
- return JTS.transform(geom, transform);
|
|
|
+ CoordinateReferenceSystem crsTarget = CRS.parseWKT(srid4546);
|
|
|
+
|
|
|
+ MathTransform transform2 = CRS.findMathTransform(sourceTarget, crsTarget, false);
|
|
|
+ return JTS.transform(geom, transform2);
|
|
|
} catch (Exception e) {
|
|
|
// TODO Auto-generated catch block
|
|
|
e.printStackTrace();
|
|
@@ -93,10 +179,11 @@ public class SpatialOperate {
|
|
|
|
|
|
/**
|
|
|
* 获取面积(平方米)
|
|
|
+ *
|
|
|
* @param geometry
|
|
|
* @return
|
|
|
*/
|
|
|
- public static Double getGeometryArea(Geometry geometry) {
|
|
|
+ public static Double getGeometryArea(Geometry geometry) {
|
|
|
if (geometry == null)
|
|
|
return 0.00;
|
|
|
Geometry geometry1 = lonlat2WebMactor(geometry);
|
|
@@ -105,19 +192,39 @@ public class SpatialOperate {
|
|
|
return geometry1.getArea();
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 根据wkt 生成多边形
|
|
|
- *
|
|
|
- * @param wkt
|
|
|
- * @return
|
|
|
- * @throws ParseException
|
|
|
- */
|
|
|
+ /**
|
|
|
+ * 根据wkt 生成多边形
|
|
|
+ *
|
|
|
+ * @param wkt
|
|
|
+ * @return
|
|
|
+ * @throws ParseException
|
|
|
+ */
|
|
|
public static Polygon createPolygonByWkt(String wkt) throws ParseException, org.locationtech.jts.io.ParseException {
|
|
|
WKTReader reader = new WKTReader();
|
|
|
+ Integer index = wkt.indexOf(";");
|
|
|
+ if (index > -1)
|
|
|
+ wkt = wkt.substring(index + 1);
|
|
|
Polygon geometry = (Polygon) reader.read(wkt);
|
|
|
return geometry;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据wkt 生成多边形
|
|
|
+ *
|
|
|
+ * @param wkt
|
|
|
+ * @return
|
|
|
+ * @throws ParseException
|
|
|
+ */
|
|
|
+ public static Geometry createGeometryByWkt(String wkt) throws ParseException, org.locationtech.jts.io.ParseException {
|
|
|
+ WKTReader reader = new WKTReader();
|
|
|
+ Integer index = wkt.indexOf(";");
|
|
|
+ if (index > -1)
|
|
|
+ wkt = wkt.substring(index + 1);
|
|
|
+ Geometry geometry = reader.read(wkt);
|
|
|
+ geometry.setSRID(srid);
|
|
|
+ return geometry;
|
|
|
+ }
|
|
|
+
|
|
|
public static Double getIntersectsArea(Geometry polygon1, Geometry other) {
|
|
|
Geometry intersetGeom = (Geometry) polygon1.intersection(other);
|
|
|
if (intersetGeom == null) {
|
|
@@ -126,8 +233,69 @@ public class SpatialOperate {
|
|
|
Polygon pPolgon = (Polygon) intersetGeom;
|
|
|
if (pPolgon == null)
|
|
|
return 0.00;
|
|
|
- pPolgon= (Polygon) lonlat2WebMactor(pPolgon);
|
|
|
- return pPolgon.getArea()/666.66;
|
|
|
+ pPolgon = (Polygon) lonlat2WebMactor(pPolgon);
|
|
|
+ return pPolgon.getArea() / 666.66;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算多个面交集
|
|
|
+ *
|
|
|
+ * @param polygons
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Double getIntersectByGeometrys(List<Geometry> polygons) {
|
|
|
+
|
|
|
+ Geometry intersetGeom = null;
|
|
|
+ if (polygons.size() >= 2) {
|
|
|
+ for (int i = 1; i < polygons.size(); i++) {
|
|
|
+// 第一次用前两个比较
|
|
|
+ if (i == 1) {
|
|
|
+ intersetGeom = polygons.get(0).intersection(polygons.get(i));
|
|
|
+ }
|
|
|
+// 后续用上次的结果比较
|
|
|
+ else if (i == polygons.size() - 1) {
|
|
|
+ intersetGeom = intersetGeom.intersection(polygons.get(i));
|
|
|
+ }
|
|
|
+ // 检查交集是否为空,如果为空则提前结束循环
|
|
|
+ if (intersetGeom.isEmpty()) {
|
|
|
+ return 0.00;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!intersetGeom.isEmpty()) {
|
|
|
+ return lonlat2WebMactor(intersetGeom).getArea()/666.66;
|
|
|
+ } else {
|
|
|
+ return 0.00;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据类生成字段列表
|
|
|
+ *
|
|
|
+ * @param object
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static List<FldInfo> getFieldAnnotation(Object object) {
|
|
|
+ Field[] fields = object.getClass().getDeclaredFields();
|
|
|
+ List<FldInfo> fldInfos = new ArrayList<>();
|
|
|
+ for (Field field : fields) {
|
|
|
+ // 是否引用ApiModelProperty注解
|
|
|
+ String name = field.getName();
|
|
|
+ String typeName = field.getType().getName();
|
|
|
+
|
|
|
+ boolean bool = field.isAnnotationPresent(Excel.class);
|
|
|
+ String cnName = "";
|
|
|
+ if (bool) {
|
|
|
+ cnName = field.getAnnotation(Excel.class).name();
|
|
|
+// fldInfos.add(new FldInfo(value);
|
|
|
+ System.out.print(cnName);
|
|
|
+
|
|
|
+ }
|
|
|
+ if (cnName == "" || cnName == null)
|
|
|
+ cnName = name;
|
|
|
+ fldInfos.add(new FldInfo(name, cnName, typeName));
|
|
|
+ }
|
|
|
+ return fldInfos;
|
|
|
}
|
|
|
//
|
|
|
// private GeometryFactory geometryFactory = new GeometryFactory();
|