计算JTS缓冲区面积(Java)

该博客展示了如何利用JTS库计算由多个点坐标定义的线串的缓冲区面积。通过读取JSON文件获取点坐标,转换为Coordinate对象,创建Geometry对象,并应用BufferOp进行缓冲区处理。最后计算得到的缓冲区多边形面积表示了点的路径区域。文章还提供了一个读取文件的辅助方法和一个主方法用于演示整个过程。

根据两个点坐标,加上点幅宽计算点的路径面积,此为计算多边形面积,主动去掉重复面积。

引入pom文件:

<dependency>
    <groupId>com.vividsolutions</groupId>
    <artifactId>jts</artifactId>
    <version>1.13</version>
</dependency>

处理demo:

package com.demo.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.operation.buffer.BufferOp;
import com.vividsolutions.jts.operation.buffer.BufferParameters;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.List;

/**
 * 计算JTS缓冲区面积
 * 
 * @author elinx
 */
public class GeoAreaTest {

    /**
     * TODO
     *
     * @return
     * @throws IOException
     */
    private static String testReadFile(String filename) throws IOException {
        Resource resource = new ClassPathResource(filename);
        InputStream is = resource.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String data = br.readLine();
        br.close();
        isr.close();
        is.close();
        return data;
    }

    /**
     * 计算JTS缓冲区面积
     *
     * @param coordinatesList List
     * @param degree degree
     * @return double
     */
    public static double calculatedCacheArea(List<Object> coordinatesList, double degree) {
        Coordinate[] coordinates4 = new Coordinate[coordinatesList.size()];
        for (int i = 0; i < coordinatesList.size(); i++) {
            Object positionList = coordinatesList.get(i);
            List<BigDecimal> positionArr = (List<BigDecimal>) positionList;
            double x = (new BigDecimal(String.valueOf(positionArr.get(0)))).doubleValue();
            double y = (new BigDecimal(String.valueOf(positionArr.get(1)))).doubleValue();
            coordinates4[i] = new Coordinate(x, y);
        }
        GeometryFactory gf = new GeometryFactory();
        Geometry gfLineString = gf.createLineString(coordinates4);
        //double degree = 100米 / (2 * Math.PI * 6371004) * 360; // 按米转化为度单位
        //double degree = 1.25;
        // 缓冲区建立
        BufferOp bufOp = new BufferOp(gfLineString);
        // 结束端点样式
        bufOp.setEndCapStyle(BufferParameters.CAP_FLAT);
        // 设置线段:象线段
        bufOp.setQuadrantSegments(5);
        Geometry bg = bufOp.getResultGeometry(degree);

        // 判断点是否在多边形内
        // Coordinate point = new Coordinate(116.663609, 40.387187);
        // PointLocator a   = new PointLocator();
        // boolean p1       = a.intersects(point, bg);

        return bg.getArea();
    }


    public static void main(String[] args) throws IOException {
    	// 点集合:[[x,y], [x,y] ... ]
        String rs = testReadFile("/data/xxx.json");
        JSONArray jsonArray = JSON.parseArray(rs);

        Long start = System.currentTimeMillis();

        System.out.println(start);

        double area = calculatedCacheArea(jsonArray, 1.25);
        System.out.println("=====多边形面积: " + area);

        System.out.println(System.currentTimeMillis() - start);

    }

}
在使用 Java 创建国际航线周边 50 海里缓冲区时,通常涉及空间数据处理和缓冲区分析,适用于航空、地理信息系统(GIS)等领域。可以通过 Java 结合 GIS 库实现航线缓冲区的创建。以下是实现方法的详细步骤和代码示例。 ### 空间数据处理库 Java 本身并不直接支持地理空间数据处理,但可以借助第三方库来实现,例如 JTSJava Topology Suite)。JTS 是一个强大的几何计算库,支持缓冲区分析、空间关系判断等操作。此外,还可以结合 GeoTools 库进行地理空间数据的处理和可视化。 #### 依赖库引入 如果使用 Maven 构建项目,需要在 `pom.xml` 中引入 JTS 和 GeoTools 的依赖: ```xml <dependencies> <!-- JTS Topology Suite --> <dependency> <groupId>com.github.jtst</groupId> <artifactId>jts-core</artifactId> <version>1.18.2</version> </dependency> <!-- GeoTools --> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-main</artifactId> <version>25.0</version> </dependency> </dependencies> ``` ### 缓冲区创建步骤 1. **定义航线几何对象**:将国际航线表示为线要素(LineString),并将其转换为 `Geometry` 对象。 2. **设置缓冲区距离**:根据需求将 50 海里转换为适当的单位(如千米或米),并计算出对应的缓冲区距离。 3. **创建缓冲区**:使用 JTS 提供的 `buffer` 方法对航线进行缓冲区分析。 4. **可视化或保存缓冲区**:将缓冲区结果以图形形式显示或保存为文件。 #### 代码示例 以下是一个使用 JTS 创建缓冲区的简单示例: ```java import com.vividsolutions.jts.geom.*; import com.vividsolutions.jts.util.GeometricShapeFactory; public class BufferExample { public static void main(String[] args) { // 创建几何工厂 GeometryFactory geometryFactory = new GeometryFactory(); // 定义国际航线(以线要素为例) Coordinate[] coordinates = new Coordinate[] { new Coordinate(-118.2437, 34.0522), // 起点:洛杉矶 new Coordinate(-74.0060, 40.7128) // 终点:纽约 }; LineString flightPath = geometryFactory.createLineString(coordinates); // 设置缓冲区距离(50海里 ≈ 92.6千米) double bufferDistance = 92600; // 转换为米 // 创建缓冲区 Geometry bufferZone = flightPath.buffer(bufferDistance); // 输出缓冲区的几何类型和面积 System.out.println("缓冲区几何类型: " + bufferZone.getGeometryType()); System.out.println("缓冲区面积 (平方米): " + bufferZone.getArea()); } } ``` ### 数据单位与坐标系 在进行缓冲区分析时,需要注意以下几点: - **坐标系**:数据应使用平面坐标系(如 UTM)以确保缓冲区计算的准确性。若数据为地理坐标(经纬度),需先将其转换为投影坐标。 - **单位转换**:50 海里应根据需求转换为合适的单位(如千米或米),1 海里约为 1.852 千米。 ### 可视化与输出 缓冲区创建完成后,可以通过 GeoTools 将其可视化或保存为 Shapefile 文件。GeoTools 提供了丰富的 API 来处理地理空间数据的显示和存储。 #### 示例:将缓冲区保存为 Shapefile 文件 ```java import org.geotools.data.shapefile.ShapefileDataStore; import org.geotools.data.simple.SimpleFeatureSource; import org.geotools.data.simple.SimpleFeatureStore; import org.geotools.feature.DefaultFeatureCollection; import org.geotools.feature.simple.SimpleFeatureBuilder; import org.geotools.feature.simple.SimpleFeatureTypeBuilder; import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; import java.io.File; import java.net.URL; import java.nio.file.Paths; public class SaveBufferToShapefile { public static void main(String[] args) throws Exception { // 创建几何工厂 GeometryFactory geometryFactory = new GeometryFactory(); // 定义国际航线(以线要素为例) Coordinate[] coordinates = new Coordinate[] { new Coordinate(-118.2437, 34.0522), // 起点:洛杉矶 new Coordinate(-74.0060, 40.7128) // 终点:纽约 }; LineString flightPath = geometryFactory.createLineString(coordinates); // 设置缓冲区距离(50海里 ≈ 92.6千米) double bufferDistance = 92600; // 转换为米 // 创建缓冲区 Geometry bufferZone = flightPath.buffer(bufferDistance); // 创建特征类型 SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder(); typeBuilder.setName("BufferFeature"); typeBuilder.add("geometry", Polygon.class); SimpleFeatureType featureType = typeBuilder.buildFeatureType(); // 创建特征集合 DefaultFeatureCollection featureCollection = new DefaultFeatureCollection(); SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType); SimpleFeature feature = featureBuilder.buildFeature(null); feature.setAttribute("geometry", bufferZone); featureCollection.add(feature); // 保存为 Shapefile 文件 File file = new File("buffer_zone.shp"); URL url = Paths.get(file.getAbsolutePath()).toUri().toURL(); ShapefileDataStore dataStore = new ShapefileDataStore(url); dataStore.createSchema(featureType); SimpleFeatureSource featureSource = dataStore.getFeatureSource(); if (featureSource instanceof SimpleFeatureStore) { SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource; featureStore.addFeatures(featureCollection); } dataStore.dispose(); System.out.println("缓冲区已保存为 Shapefile 文件!"); } } ``` ### 注意事项 - **数据精度**:确保输入数据的精度足够高,以避免缓冲区计算误差。 - **坐标系转换**:若数据为地理坐标,需使用 GeoTools 或其他库进行坐标转换。 - **性能优化**:对于大规模航线数据,需考虑性能优化,例如分批次处理或使用多线程。 通过上述方法,可以使用 Java 创建国际航线周边 50 海里的缓冲区,并将其保存为 Shapefile 文件或进行可视化展示。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值