geotools实现多边形的合并

package com.github.pig.admin.utils;

import java.util.ArrayList;
import java.util.List;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.MultiPolygon;

import static org.geotools.geometry.jts.Geometries.MULTIPOLYGON;
//import com.vividsolutions.jts.geom.Coordinate;
//import com.vividsolutions.jts.geom.Geometry;
//import com.vividsolutions.jts.geom.GeometryFactory;
//import com.vividsolutions.jts.io.WKTReader;
//import com.vividsolutions.jts.algorithm.PointLocator;

public class UnionPolygon {
    public static void main(String[] args) {

        //创建4个多边形
        Coordinate[] coordinates1 = new Coordinate[] {
                new Coordinate(0, 0),
                new Coordinate(0, 1),
                new Coordinate(1, 1),
                new Coordinate(1, 0),
                new Coordinate(0, 0)};

        Coordinate[] coordinates2 = new Coordinate[] {
                new Coordinate(1, 0),
                new Coordinate(1, 1),
                new Coordinate(2, 1),
                new Coordinate(2, 0),
                new Coordinate(1, 0)};


        Coordinate[] coordinates3 = new Coordinate[] {
                new Coordinate(7, 0),
                new Coordinate(7, 1),
                new Coordinate(8, 1),
                new Coordinate(8, 0),
                new Coordinate(7, 0)};

//        GeometryFactory gf=new GeometryFactory();
        GeometryFactory gf = JTSFactoryFinder.getGeometryFactory( null );
        Geometry g1 = gf.createPolygon(coordinates1);
        Geometry g2 = gf.createPolygon(coordinates2);
        Geometry g3 = gf.createPolygon(coordinates3);
        //多个合并,设置多边形数组,再通过循环依次叠加各个多边形
        Geometry[] geos=new Geometry[] {g1,g2,g3};
        Geometry allunion = geos[0];
        for(int i=1; i<geos.length; i++) {
            allunion=allunion.union(geos[i]);
        }
//        System.out.println(union);
//        System.out.println(union2);
//        System.out.println(allunion);

//        MultiPolygon multiPolygon= (MultiPolygon) allunion;
//        Geometry geometry=allunion.getGeometryN(1);
        int num=allunion.getNumGeometries();
        for(int i=0;i<num;i++){
            Geometry geometry=allunion.getGeometryN(i);
            Coordinate[] coordinates=geometry.getCoordinates();
            for (Coordinate coordinate:coordinates){
                System.out.println(coordinate.toString());
            }
//            System.out.println(allunion.getGeometryN(i).toString());
        }

//        for(Coordinate coordinate:coordinates){
//
//        }




//        //创建4个多边形
//        Coordinate[] coordinates1 = new Coordinate[] {
//                new Coordinate(0, 0),
//                new Coordinate(0, 1),
//                new Coordinate(1, 2),
//                new Coordinate(2, 1), //该点在union后会被消去
//                new Coordinate(2, 0),
//                new Coordinate(0, 0), };
//        Coordinate[] coordinates2 = new Coordinate[] {
//                new Coordinate(1, 0),
//                new Coordinate(1, 1), //该点在union后会被消去
//                new Coordinate(2, 2),
//                new Coordinate(3, 1),
//                new Coordinate(3, 0),
//                new Coordinate(1, 0)};
//        Coordinate[] coordinates3 = new Coordinate[] {
//                new Coordinate(0, 0),
//                new Coordinate(1, 0),
//                new Coordinate(2, 0),
//                new Coordinate(2, -1),
//                new Coordinate(0, -1),
//                new Coordinate(0, 0), };
//        Coordinate[] coordinates4 = new Coordinate[] {
//                new Coordinate(1, 0),
//                new Coordinate(2, 0),
//                new Coordinate(3, 0),
//                new Coordinate(3, -1),
//                new Coordinate(1, -1),
//                new Coordinate(1, 0)};
//
//        GeometryFactory gf=new GeometryFactory();
//        Geometry g1 = gf.createPolygon(coordinates1);
//        Geometry g2 = gf.createPolygon(coordinates2);
//        Geometry g3 = gf.createPolygon(coordinates3);
//        Geometry g4 = gf.createPolygon(coordinates4);
//        //两个合并
        Geometry union = g1.union(g2);
        Geometry union2 = union.union(g3);
//        //多个合并,设置多边形数组,再通过循环依次叠加各个多边形
//        Geometry[] geos=new Geometry[] {g1,g2,g3,g4};
//        Geometry allunion = geos[0];
//        for(int i=1; i<geos.length; i++) {
//            allunion=allunion.union(geos[i]);
//        }
        System.out.println(union);
        System.out.println(union2);
//        System.out.println(allunion);

//        //缓冲区建立
//        Geometry g3buffer=g3.buffer(1);                 //对第三个多边形加缓冲区
//        Geometry allunionbuffer=allunion.buffer(1);     //对全部合并后的多边形加缓冲区
//        System.out.println(g3buffer);
//        System.out.println(allunionbuffer);

//        //点是否在多边形内判断
//        Coordinate point1 = new Coordinate(1, 1);
//        PointLocator a=new PointLocator();
//        boolean p1=a.intersects(point1, allunion);
//        if(p1)
//            System.out.println("point1:"+"该点在多边形内");
//        else
//            System.out.println("point1:"+"该点不在多边形内");
//
//        Coordinate point2 = new Coordinate(5, 5);
//        PointLocator b=new PointLocator();
//        boolean p2=b.intersects(point2, allunion);
//        if(p2)
//            System.out.println("point2:"+"该点在多边形内");
//        else
//            System.out.println("point2:"+"该点不在多边形内");
    }
}

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用Geotools库来实现地块重叠判断。Geotools是一个用于地理空间数据处理的开源Java库,它提供了许多功能来处理和分析地理空间数据。 要判断两个地块是否重叠,你可以使用Geotools中的几何操作。首先,你需要将地块数据转换为Geotools中的几何对象。通常情况下,地块可以表示为多边形。 下面是一个示例代码片段,展示了如何使用Geotools来判断两个地块是否重叠: ```java import org.geotools.geometry.jts.JTS; import org.geotools.geometry.jts.JTSFactoryFinder; import org.locationtech.jts.geom.Geometry; import org.locationtech.jts.geom.Polygon; public class GeotoolsExample { public static void main(String[] args) { // 创建地块1的多边形 Polygon polygon1 = createPolygon(/* 地块1的坐标数据 */); // 创建地块2的多边形 Polygon polygon2 = createPolygon(/* 地块2的坐标数据 */); // 执行地块重叠判断 boolean isOverlap = polygon1.intersects(polygon2); // 输出结果 if (isOverlap) { System.out.println("地块重叠"); } else { System.out.println("地块不重叠"); } } private static Polygon createPolygon(/* 地块的坐标数据 */) { // 使用JTS库创建几何工厂 final org.locationtech.jts.geom.GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); // 创建地块的坐标数组(示例中使用简单的矩形) Coordinate[] coordinates = new Coordinate[] { new Coordinate(0, 0), new Coordinate(0, 1), new Coordinate(1, 1), new Coordinate(1, 0), new Coordinate(0, 0) }; // 创建地块的多边形 Polygon polygon = geometryFactory.createPolygon(coordinates); return polygon; } } ``` 请根据你的实际需求替换示例代码中的地块坐标数据,然后运行代码即可判断两个地块是否重叠。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

huayang183

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值