[NetTopologySuite](2)任意多边形求交

任意多边形求交:

 private void btnPolygon_Click(object sender, EventArgs e)
        {

            double[] Xs = new double[] { 30, 260, 130, 350, 250, 330, 320, 180, 250, 50, 70, 60, 30, 260 };
            double[] Ys = new double[] { 140, 260, 110, 150, 230, 210, 140, 260 };
            int m = Xs.Length / 2;
            var coords = new Coordinate[m];
            var coordscheck = new Coordinate[m];
            for (var i = 0; i < Xs.Length / 2; i++)
            {
                var x = Xs[i * 2];
                var y = Xs[i * 2 + 1];
                coords[i] = new Coordinate(x, y);
                coordscheck[i] = new Coordinate(x, y);
            }
            // Shell Rings
            var ring = new LinearRing(coords);
            var gf = new GeometryFactory();
            var ringCheck = gf.CreateLinearRing(coordscheck);
            // Hole Coordinates
            int n = Ys.Length / 2;
            var coordsholecheck = new Coordinate[n];
            var coordshole = new Coordinate[n];
            for (var i = 0; i < Ys.Length / 2; i++)
            {
                var x = Ys[i * 2];
                var y = Ys[i * 2 + 1];
                coordshole[i] = new Coordinate(x, y);
                coordsholecheck[i] = new Coordinate(x, y);
            }
            // Hole LinearRing Arrays
            var hole = new LinearRing(coordshole);
            var holes = new ILinearRing[1];
            var holeCheck = gf.CreateLinearRing(coordsholecheck);
            var holescheck = new ILinearRing[1];
            holes[0] = hole;
            holescheck[0] = holeCheck;
            Polygon pg = new Polygon(ring, holes);//
            var polygonCheck = gf.CreatePolygon(ringCheck, holescheck);
            var areaCheck = polygonCheck.Area;
            var area = pg.Area;
            //
            inputGeometry = pg;


            double[] XsAnalysis = new double[] { 210, 390, 180, 290, 170, 210, 190, 140, 360, 60, 470, 220, 440, 390 };
            double[] YsAnalysis = new double[] { 340, 310, 250, 260, 270, 140, 360, 130, 390, 230, 340, 310 };
            m = XsAnalysis.Length / 2;
            var coordsAnalysis = new Coordinate[m];
            var coordscheckAnalysis = new Coordinate[m];
            for (var i = 0; i < XsAnalysis.Length / 2; i++)
            {
                var x = XsAnalysis[i * 2];
                var y = XsAnalysis[i * 2 + 1];
                coordsAnalysis[i] = new Coordinate(x, y);
                coordscheckAnalysis[i] = new Coordinate(x, y);
            }
            // Shell Rings
            var ringAnalysis = new LinearRing(coordsAnalysis);
            var gfAnalysis = new GeometryFactory();
            var ringCheckAnalysis = gfAnalysis.CreateLinearRing(coordscheckAnalysis);
            // Hole Coordinates
            n = YsAnalysis.Length / 2;
            var coordsholecheckAnalysis = new Coordinate[n];
            var coordsholeAnalysis = new Coordinate[n];
            for (var i = 0; i < YsAnalysis.Length / 2; i++)
            {
                var x = YsAnalysis[i * 2];
                var y = YsAnalysis[i * 2 + 1];
                coordsholeAnalysis[i] = new Coordinate(x, y);
                coordsholecheckAnalysis[i] = new Coordinate(x, y);
            }
            // Hole LinearRing Arrays
            var holeAnalysis = new LinearRing(coordsholeAnalysis);
            var holesAnalysis = new ILinearRing[1];
            var holeCheckAnalysis = gf.CreateLinearRing(coordsholecheckAnalysis);
            var holescheckAnalysis = new ILinearRing[1];
            holesAnalysis[0] = holeAnalysis;
            holescheckAnalysis[0] = holeCheckAnalysis;
            Polygon pgAnalysis = new Polygon(ringAnalysis, holesAnalysis);//
            var polygonCheckAnalysis = gf.CreatePolygon(ringCheckAnalysis, holescheckAnalysis);
            var areaCheckAnalysis = polygonCheckAnalysis.Area;
            var areaAnalysis = pgAnalysis.Area;
            //
            AnalysisPolygonGeometry = pgAnalysis;


            intersectionOutput = AnalysisPolygonGeometry.Intersection(inputGeometry);
            this.Invalidate();
        }
View Code

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Python中计算两个多边形的交集,您可以使用geopandas库和shapely库。以下是一个示例代码: ``` import geopandas as gpd from shapely.geometry import Polygon p1 = Polygon([[-122.5, 45.48565], [-122.801742, 45.5], [-122.82, 45.6], [-122.584762, 45.691], [-122.4, 45.48]]) p2 = Polygon([[-122.520217, 45.535693], [-122.64038, 45.553967], [-122.720031, 45.526554], [-122.669906, 45.507309], [-122.723464, 45.446643], [-122.532577, 45.408574], [-122.487258, 45.477466]]) intersection = p1.intersection(p2) intersection_area = intersection.area print(intersection_area) ``` 在上述代码中,使用Polygon函数创建了两个多边形对象p1和p2。然后,使用intersection方法计算两个多边形的交集,并将结果赋给intersection变量。最后,可以使用intersection的area属性获取交集的面积并打印出来。 请注意,为了运行上述代码,您需要安装geopandas和shapely库。如果您的计算机上没有安装这些库,可以使用pip install命令进行安装。 另外,引用中提供了将多边形坐标转换为GeoJSON格式的代码示例。引用则提供了手动安装Python扩展包的方法。 希望这个答案对您有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [python计算两个多边形交集的面积](https://blog.csdn.net/u013239656/article/details/114944480)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值