当前位置: 首页 > 工具软件 > jQuery Geo > 使用案例 >

GeoTools——JTS空间操作

东方飞捷
2023-12-01

目录

一、引言

二、代码操作

1、服务端

2、返回数据

3、客户端

三、总结


 

一、引言

 

使用geotools主要是对数据进行操作,这里的操作包括空间关系判断和空间关系运算。这里的空间关系判断常用的是否相交、是否包含等;空间运算主要包括取交集、并集、擦除、切除、对称差异分析等等。

 

 

二、代码操作

 

1、服务端

由于geotools中没有gts的帮助文档,这里直接参考了牛哥的博客作为示例。其中几何对象的创建和空间操作都是使用的jts库,由wkt格式创建。

    /**
     * 后台读取数据进行空间运算,geometry转为wkt返回
     * @return
     * @throws ParseException
     */
    @RequestMapping("/operation")
    @ResponseBody
    public Object GEOOperation() throws ParseException {
        String wktPoint = "POINT(103.83489981581 33.462715497945)";
        String wktLine = "LINESTRING(108.32803893589 41.306670233001,99.950999898452 25.84722546391)";
        String wktPolygon = "POLYGON((100.02715479879 32.168082192159,102.76873121104 37.194305614622,107.0334056301 34.909658604412,105.96723702534 30.949603786713,100.02715479879 32.168082192159))";
        String wktPolygon1 = "POLYGON((96.219409781775 32.777321394882,96.219409781775 40.240501628236,104.82491352023001 40.240501628236,104.82491352023001 32.777321394882,96.219409781775 32.777321394882))";

        //空间关系
        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null );
        WKTReader reader = new WKTReader( geometryFactory );
        Point point = (Point) reader.read(wktPoint);
        LineString line = (LineString) reader.read(wktLine);
        Polygon polygon = (Polygon) reader.read(wktPolygon);
        Polygon polygon1 = (Polygon) reader.read(wktPolygon1);
        System.out.println("-------空间关系判断-------");
        System.out.println(polygon.contains(point));
        System.out.println(polygon.intersects(line));
        System.out.println(polygon.overlaps(polygon1));

        //空间操作
        System.out.println("\r\n-------空间计算-------");
        WKTWriter write = new WKTWriter();
        Polygon intersection = (Polygon)polygon.intersection( polygon1 );
        Geometry union = polygon.union( polygon1 );
        Geometry difference = polygon.difference( polygon1 );
        Geometry symdifference = polygon.symDifference( polygon1 );
        System.out.println("\t+++++++++++叠加分析+++++++++++");
        System.out.println(write.write(intersection));
        System.out.println("\t+++++++++++合并分析+++++++++++");
        System.out.println(write.write(union));
        System.out.println("\t+++++++++++差异分析+++++++++++");
        System.out.println(write.write(difference));
        System.out.println("\t+++++++++++sym差异分析+++++++++++");
        System.out.println(write.write(symdifference));
        //return intersection.toString();
        //return union.toString();
        //return difference.toString();
        return symdifference.toString();
    }

 

2、返回数据

 

返回的数据直接是wkt格式

MULTIPOLYGON (((100.02715479879 32.168082192159, 100.35946709118406 32.777321394882, 104.82491352023001 32.777321394882, 104.82491352023001 36.09277937755627, 107.0334056301 34.909658604412, 105.96723702534 30.949603786713, 100.02715479879 32.168082192159)), ((104.82491352023001 36.09277937755627, 102.76873121104 37.194305614622, 100.35946709118406 32.777321394882, 96.219409781775 32.777321394882, 96.219409781775 40.240501628236, 104.82491352023001 40.240501628236, 104.82491352023001 36.09277937755627)))

 

3、客户端

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../js/jquery-3.3.1.js"></script>
    <script src="../js/ol.js"></script>
    <link rel="stylesheet" href="../js/ol.css">
</head>
<body>
<div id="map" style="width: 100%"></div>
<script>


    var wktFormat = new ol.format.WKT();
    $.ajax({
        type: 'POST',
        url: "http://localhost:8080/testmaven/shp/operation",
        data: {
            layer: "capital"
        },
        success: function (data) {
            //data = eval("(" + data + ")");
            console.log(data);

            var feature = wktFormat.readFeature(data);
            feature.getGeometry().transform("EPSG:4326","EPSG:3857")
            polygonVectorSource.addFeature(feature);

/*            var polygon_org=[[[120,40],[122,50],[111,45]]];
            var geomPolygon=new ol.geom.Polygon(polygon_org);
            geomPolygon.transform("EPSG:4326","EPSG:3857");
            var featurepolygon= new ol.Feature({
                geometry:geomPolygon
            });
            polygonVectorSource.addFeature(featurepolygon);*/
        }
    });
    //面样式
    var fill = new ol.style.Fill({
        color: '#dd942e'
    });
    var stroke = new ol.style.Stroke({
        color: '#cc1000',
        width: 1.25
    });
    var feature1=wktFormat.readFeature("POLYGON((100.02715479879 32.168082192159,102.76873121104 37.194305614622,107.0334056301 34.909658604412,105.96723702534 30.949603786713,100.02715479879 32.168082192159))");
    feature1.getGeometry().transform("EPSG:4326","EPSG:3857");
    var feature2=wktFormat.readFeature("POLYGON((96.219409781775 32.777321394882,96.219409781775 40.240501628236,104.82491352023001 40.240501628236,104.82491352023001 32.777321394882,96.219409781775 32.777321394882))");
    feature2.getGeometry().transform("EPSG:4326","EPSG:3857");
    var polygonVectorSource = new ol.source.Vector({
        //features:[feature1,feature2]
    });
    var stylePolygon = [
        new ol.style.Style({
            image: new ol.style.Circle({
                fill: new ol.style.Fill({
                    color: 'rgba(255,0,255,0.4)'
                }),
                stroke: new ol.style.Stroke({
                    color: '#cc3540',
                    width: 1.25
                }),
                radius: 5
            }),
            fill: fill,
            stroke: stroke
        })
    ];
    //面图层
    var polygonVectorLayer = new ol.layer.Vector({
        title:"面",
        source: polygonVectorSource,
        //style: styleFunction,
        style:stylePolygon,
        renderMode:'image'
    });
    //view
        var view=new ol.View({
            // 设置成都为地图中心
            center: ol.proj.transform([110,39],"EPSG:4326","EPSG:3857"),
            zoom: 4
        });
    /*视图*/
    // map
    var map = new ol.Map({
        layers: [
            getTdtLayer("vec_w"),
            getTdtLayer("cva_w"),
            polygonVectorLayer
        ],
        view:view,
        target: 'map'
    });

    function getTdtLayer(lyr) {
        var urls=[];
        for(var i=0;i<8;i++)
        {
            urls.push( "http://t"+i+".tianditu.com/DataServer?T=" + lyr + "&X={x}&Y={y}&L={z}&tk=07c3369864c03fb6a162ac560811db9b")
        }
        var layer = new ol.layer.Tile({
            source: new ol.source.XYZ({
                urls:urls
            })
        });
        return layer;
    }
</script>
</body>
</html>

 

 

三、总结

 

  • GTS空间操作判断与空间运算;

 

  • 空间操作判断与空间操作运算代码实现;

 

 

 类似资料: