|
@@ -0,0 +1,122 @@
|
|
|
+package com.zjugis.ysgzybz.utils;
|
|
|
+
|
|
|
+import com.vividsolutions.jts.geom.Geometry;
|
|
|
+import com.vividsolutions.jts.geom.GeometryCollection;
|
|
|
+import com.vividsolutions.jts.operation.buffer.BufferOp;
|
|
|
+import com.vividsolutions.jts.operation.buffer.BufferParameters;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.geotools.data.simple.SimpleFeatureCollection;
|
|
|
+import org.geotools.data.simple.SimpleFeatureIterator;
|
|
|
+import org.geotools.feature.FeatureCollection;
|
|
|
+import org.geotools.feature.FeatureIterator;
|
|
|
+import org.geotools.geojson.feature.FeatureJSON;
|
|
|
+import org.geotools.geojson.geom.GeometryJSON;
|
|
|
+import org.geotools.geometry.jts.JTS;
|
|
|
+import org.geotools.referencing.CRS;
|
|
|
+import org.opengis.feature.Feature;
|
|
|
+import org.opengis.feature.simple.SimpleFeature;
|
|
|
+import org.opengis.referencing.FactoryException;
|
|
|
+import org.opengis.referencing.crs.CRSAuthorityFactory;
|
|
|
+import org.opengis.referencing.crs.CoordinateReferenceSystem;
|
|
|
+import org.opengis.referencing.operation.MathTransform;
|
|
|
+import org.opengis.referencing.operation.TransformException;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.StringReader;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+public class GisUtils {
|
|
|
+
|
|
|
+ public static Geometry parseGeojson(String geojson) throws IOException {
|
|
|
+ GeometryJSON geometryJSON = new GeometryJSON(6);
|
|
|
+ Geometry geometry = geometryJSON.read(new StringReader(geojson));
|
|
|
+ return geometry;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<Geometry> parseFeatureCollection(String geojson) throws IOException {
|
|
|
+ List<Geometry> resultList = new ArrayList<>();
|
|
|
+ FeatureJSON featureJSON = new FeatureJSON();
|
|
|
+ FeatureCollection featureCollection = featureJSON.readFeatureCollection(geojson);
|
|
|
+ SimpleFeatureIterator features = (SimpleFeatureIterator) featureCollection.features();
|
|
|
+ while (features.hasNext()) {
|
|
|
+ SimpleFeature next = features.next();
|
|
|
+ Geometry defaultGeometry = (Geometry) next.getDefaultGeometry();
|
|
|
+ if (defaultGeometry == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ resultList.add(defaultGeometry);
|
|
|
+ }
|
|
|
+ return resultList;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断点是否位于polygon的缓冲区内
|
|
|
+ *
|
|
|
+ * @param point
|
|
|
+ * @param polygon wkid:4490坐标系
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static boolean isPointLocate(String point, String polygon) throws IOException {
|
|
|
+ if (StringUtils.isEmpty(point)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(polygon)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ Geometry geometry = parseGeojson(polygon);
|
|
|
+ Geometry transformGeoemtry = crsTransform(geometry, 4490, 4326);
|
|
|
+ double degree = 500 / (2 * Math.PI * 6371004) * 360;//将meter转换为度
|
|
|
+ Geometry buffer = transformGeoemtry.buffer(degree);
|
|
|
+ System.out.println("buffer500:" + buffer.toText());
|
|
|
+ Geometry pointGeo = parseGeojson(point);
|
|
|
+ return buffer.contains(pointGeo);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 坐标系转换
|
|
|
+ *
|
|
|
+ * @param geometry
|
|
|
+ * @param sourceEPSG
|
|
|
+ * @param targetEPSG
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Geometry crsTransform(Geometry geometry, int sourceEPSG, int targetEPSG) {
|
|
|
+ if (sourceEPSG == targetEPSG) {
|
|
|
+ return geometry;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ /*
|
|
|
+ 强调经度在前。有些投影如高斯投影,XY与一般坐标系是相反的,
|
|
|
+ X轴是我们通常意义上的Y轴,Y轴是X轴,刚好换过来。
|
|
|
+ 应对这种情况,就要使用这个函数。否则死活转换不成功,报什么:
|
|
|
+ 错误:The transform result may be 482.279 meters away from the expected position.
|
|
|
+ Are you sure that the input coordinates are inside this map projection area of validity?
|
|
|
+ The point is located 32°36.2'E away from the central meridian and 0°19.8'N away from the latitude of origin.
|
|
|
+ The projection is "Transverse_Mercator".
|
|
|
+ */
|
|
|
+ CRSAuthorityFactory factory = CRS.getAuthorityFactory(true);
|
|
|
+ CoordinateReferenceSystem sourceCRS = factory.createCoordinateReferenceSystem(String.format("EPSG:%d", sourceEPSG));
|
|
|
+ CoordinateReferenceSystem targetCRS = factory.createCoordinateReferenceSystem(String.format("EPSG:%d", targetEPSG));
|
|
|
+ boolean lenient = true;//允许错误
|
|
|
+ MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, lenient);
|
|
|
+ Geometry transformGeom = JTS.transform(geometry, transform);
|
|
|
+ return transformGeom;
|
|
|
+ } catch (FactoryException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (TransformException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws IOException {
|
|
|
+// String geojson = "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"id\":\"bjwkfly2000.65\",\"geometry\":{\"type\":\"MultiPolygon\",\"coordinates\":[[[[120.17376215,30.1550689],[120.17371998,30.15505164],[120.17351625,30.1552117],[120.17387465,30.15554496],[120.17380729,30.15560406],[120.17392119,30.15571815],[120.17400582,30.15586233],[120.17405761,30.15579803],[120.17412072,30.1557161],[120.17416208,30.15566551],[120.17416494,30.15561077],[120.17415718,30.15557068],[120.17407017,30.15544573],[120.17392918,30.15523398],[120.17376215,30.1550689]]]]},\"geometry_name\":\"shape\",\"properties\":{\"id\":65,\"objectid\":71,\"OBJECTID_1\":65,\"dkbh\":\"CH-86\",\"dkmj\":\"2230.52957561753\",\"dkwz\":\"长河街道\",\"mj\":3.35,\"dkxz\":\"土方;空地\",\"gddw\":\"城建发展公司\",\"lxr\":\"蒋鹏飞\",\"lxdh\":\"13867121627\",\"bz\":\" \",\"szfw\":\"东至长桥线,南至空地,西至仓库,北至长桥线\",\"st_area_sh\":2.1E-7,\"st_length_\":0.00220052,\"shape_leng\":0.00220052,\"dksyh\":\"480a668a9e4c11ee94c4074096d72126\",\"st_area_shape_\":2.1E-7,\"st_length_shape_\":0.00220052,\"st_area_shape1\":2.1E-7,\"st_length_shape1\":0.00220052,\"st_area_shape1_1\":2.1E-7,\"st_length_shape1_1\":0.00220052,\"st_area_shape1_12\":2.1E-7,\"st_length_shape1_12\":0.00220052,\"shape_Length\":0.0022005175620851926,\"shape_Area\":2.088635342349575E-7}}],\"totalFeatures\":1,\"numberMatched\":1,\"numberReturned\":1,\"timeStamp\":\"2024-06-18T08:00:11.960Z\"}";
|
|
|
+ String geojson = "{\"type\":\"MultiPolygon\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4490\"}},\"coordinates\":[[[[120.185892363,30.150623136],[120.185830634,30.150590162],[120.185758439,30.150699298],[120.185532579,30.150868505],[120.185306728,30.151037711],[120.1855314,30.1512482],[120.185756096,30.15145869],[120.185883776,30.151650994],[120.186193588,30.151911057],[120.186503391,30.152171119],[120.186645222,30.152280673],[120.186819674,30.152415447],[120.187039065,30.152178015],[120.187121981,30.152062935],[120.187205372,30.151947179],[120.187241939,30.151896439],[120.187371691,30.151716362],[120.187537998,30.151485526],[120.187456279,30.151442713],[120.187195629,30.151306113],[120.186934971,30.151169522],[120.186760665,30.151078174],[120.186674322,30.151032931],[120.186413665,30.15089633],[120.186153019,30.150759738],[120.185892363,30.150623136]]]]}";
|
|
|
+ String point = "{\"type\":\"Point\",\"coordinates\":[120.17396,30.15565]}";
|
|
|
+ boolean pointLocate = GisUtils.isPointLocate(point, geojson);
|
|
|
+ System.out.println(pointLocate);
|
|
|
+ }
|
|
|
+}
|