|
@@ -1,26 +1,26 @@
|
|
package com.onemap.analyse.utils;
|
|
package com.onemap.analyse.utils;
|
|
|
|
|
|
-import org.geotools.data.Query;
|
|
+
|
|
-import org.geotools.data.shapefile.ShapefileDataStore;
|
|
+
|
|
-import org.geotools.data.shapefile.ShapefileDataStoreFactory;
|
|
+
|
|
-import org.geotools.data.simple.SimpleFeatureSource;
|
|
+
|
|
-import org.geotools.feature.FeatureCollection;
|
|
+
|
|
-import org.geotools.feature.FeatureIterator;
|
|
+
|
|
-import org.geotools.geometry.jts.JTS;
|
|
+
|
|
-import org.geotools.referencing.CRS;
|
|
+
|
|
-import org.locationtech.jts.geom.Geometry;
|
|
+
|
|
-import org.opengis.feature.simple.SimpleFeature;
|
|
+
|
|
-import org.opengis.feature.simple.SimpleFeatureType;
|
|
+
|
|
-import org.opengis.referencing.FactoryException;
|
|
+
|
|
-import org.opengis.referencing.crs.CoordinateReferenceSystem;
|
|
+
|
|
-import org.opengis.referencing.operation.MathTransform;
|
|
+
|
|
-import org.opengis.referencing.operation.TransformException;
|
|
+
|
|
-
|
|
+
|
|
-import java.io.File;
|
|
+
|
|
-import java.io.IOException;
|
|
+
|
|
-import java.net.MalformedURLException;
|
|
+
|
|
-import java.nio.charset.Charset;
|
|
+
|
|
-import java.text.DecimalFormat;
|
|
+
|
|
|
|
|
|
|
|
|
|
* @Author: siwei
|
|
* @Author: siwei
|
|
@@ -28,109 +28,109 @@ import java.text.DecimalFormat;
|
|
* @Date: 2023年07月19日
|
|
* @Date: 2023年07月19日
|
|
* @Description: 利用geoTools计算shp面积
|
|
* @Description: 利用geoTools计算shp面积
|
|
*/
|
|
*/
|
|
-public class CalculateShpArea {
|
|
+
|
|
-
|
|
+
|
|
- static DecimalFormat df = new DecimalFormat("#.00");
|
|
+
|
|
- private final static String EPSG_CODE = "EPSG:3857";
|
|
+
|
|
-
|
|
+
|
|
-
|
|
+
|
|
- * 利用geoTools计算shp面积
|
|
+
|
|
- *
|
|
+
|
|
- * @param shpFilePath
|
|
+
|
|
- * @return
|
|
+
|
|
- */
|
|
+
|
|
- public static double getShpArea(String shpFilePath) {
|
|
+
|
|
-
|
|
+
|
|
- double shpArea = getArea(shpFilePath);
|
|
+
|
|
-
|
|
+
|
|
- shpArea = shpArea / 666.7;
|
|
+
|
|
- return Double.valueOf(df.format(shpArea));
|
|
+
|
|
- }
|
|
+
|
|
-
|
|
+
|
|
-
|
|
+
|
|
- * 计算shp面积
|
|
+
|
|
- *
|
|
+
|
|
- * @param shpFilePath
|
|
+
|
|
- * @return
|
|
+
|
|
- */
|
|
+
|
|
- public static double getArea(String shpFilePath) {
|
|
+
|
|
-
|
|
+
|
|
- double shpTotalArea = 0;
|
|
+
|
|
-
|
|
+
|
|
- ShapefileDataStore shpStore = buildDataStore(shpFilePath);
|
|
+
|
|
- try {
|
|
+
|
|
-
|
|
+
|
|
- CoordinateReferenceSystem srcCRS = CRS.parseWKT(getCRSWkt(shpFilePath));
|
|
+
|
|
- SimpleFeatureSource source = shpStore.getFeatureSource();
|
|
+
|
|
- SimpleFeatureType schema = source.getSchema();
|
|
+
|
|
- Query query = new Query(schema.getTypeName());
|
|
+
|
|
- FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures(query);
|
|
+
|
|
- try (FeatureIterator<SimpleFeature> features = collection.features()) {
|
|
+
|
|
- while (features.hasNext()) {
|
|
+
|
|
- SimpleFeature feature = features.next();
|
|
+
|
|
- Geometry geometry = (Geometry) feature.getDefaultGeometry();
|
|
+
|
|
- geometry.setSRID(4326);
|
|
+
|
|
- CoordinateReferenceSystem crsTarget = CRS.decode(EPSG_CODE);
|
|
+
|
|
-
|
|
+
|
|
- MathTransform transform = CRS.findMathTransform(srcCRS, crsTarget);
|
|
+
|
|
- Geometry res = JTS.transform(geometry, transform);
|
|
+
|
|
-
|
|
+
|
|
- shpTotalArea = shpTotalArea + res.getArea();
|
|
+
|
|
- }
|
|
+
|
|
- }
|
|
+
|
|
- } catch (IOException e) {
|
|
+
|
|
- e.printStackTrace();
|
|
+
|
|
- } catch (FactoryException e) {
|
|
+
|
|
- e.printStackTrace();
|
|
+
|
|
- } catch (TransformException e) {
|
|
+
|
|
- e.printStackTrace();
|
|
+
|
|
- }
|
|
+
|
|
- return shpTotalArea;
|
|
+
|
|
- }
|
|
+
|
|
-
|
|
+
|
|
-
|
|
+
|
|
- * 构建ShapeDataStore对象。
|
|
+
|
|
- *
|
|
+
|
|
- * @param shpFilePath shape文件路径。
|
|
+
|
|
- * @return
|
|
+
|
|
- */
|
|
+
|
|
- public static ShapefileDataStore buildDataStore(String shpFilePath) {
|
|
+
|
|
- ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
|
|
+
|
|
- try {
|
|
+
|
|
- ShapefileDataStore dataStore = (ShapefileDataStore) factory
|
|
+
|
|
- .createDataStore(new File(shpFilePath).toURI().toURL());
|
|
+
|
|
- if (dataStore != null) {
|
|
+
|
|
-
|
|
+
|
|
- dataStore.setCharset(Charset.forName("UTF-8"));
|
|
+
|
|
-
|
|
+
|
|
- }
|
|
+
|
|
- return dataStore;
|
|
+
|
|
- } catch (MalformedURLException e) {
|
|
+
|
|
- e.printStackTrace();
|
|
+
|
|
- } catch (IOException e) {
|
|
+
|
|
- e.printStackTrace();
|
|
+
|
|
- }
|
|
+
|
|
- return null;
|
|
+
|
|
- }
|
|
+
|
|
-
|
|
+
|
|
-
|
|
+
|
|
- * 读取WKT格式的坐标系信息
|
|
+
|
|
- * <p>
|
|
+
|
|
- * shape文件并不是一个文件而是一堆文件,坐标系信息存储在*.prj文件,*.prj文件中存储的直接就是WKT格式的坐标系信息。
|
|
+
|
|
- * <p>
|
|
+
|
|
- * 也可以直接读取*.prj来获取对应的WKT格式数据
|
|
+
|
|
- *
|
|
+
|
|
- * @param shpFilePath
|
|
+
|
|
- * @return
|
|
+
|
|
- */
|
|
+
|
|
- public static String getCRSWkt(String shpFilePath) {
|
|
+
|
|
- ShapefileDataStore dataStore = buildDataStore(shpFilePath);
|
|
+
|
|
- String wkt = null;
|
|
+
|
|
- try {
|
|
+
|
|
- wkt = dataStore.getSchema().getCoordinateReferenceSystem().toWKT();
|
|
+
|
|
- } catch (IOException e) {
|
|
+
|
|
- e.printStackTrace();
|
|
+
|
|
- }
|
|
+
|
|
- return wkt;
|
|
+
|
|
- }
|
|
+
|
|
-}
|
|
+
|