簡體   English   中英

查找2個線串之間的交點

[英]Find Intersection Point Between 2 LineStrings

我創建了一個公式來在Google地球上形成網格。 我想獲取經緯度之間的交點。 請告訴我如何獲得交叉點。我正在使用SharpKML lib生成KML

for (int x = 90; x >= 0; x = x - 15)
                {
                    Placemark placemark = new Placemark();
                    LineString line = new LineString();
                    CoordinateCollection co = new CoordinateCollection();
                    for (int i = 0; i <= 180; i = i + 15)
                    {
                        Vector cords = new Vector()
                            {
                                Latitude = x,
                                Longitude = i,
                                Altitude = 1000
                            };

                        co.Add(cords);
                    }
                    for (int i = -180; i <= 0; i = i + 15)
                    {
                        Vector cords = new Vector()
                        {
                            Latitude = x,
                            Longitude = i,
                            Altitude = 1000
                        };

                        co.Add(cords);
                    }
                    line.Coordinates = co;
                    placemark.Geometry = line;
                    document.AddFeature(placemark);
                }
            for (int x = -90; x <= 0; x = x + 15)
            {
                Placemark placemark = new Placemark();
                LineString line = new LineString();
                CoordinateCollection co = new CoordinateCollection();
                for (int i = 0; i <= 180; i = i + 15)
                {
                    Vector cords = new Vector()
                    {
                        Latitude = x,
                        Longitude = i,
                        Altitude = 1000
                    };

                    co.Add(cords);
                }
                for (int i = -180; i <= 0; i = i + 15)
                {
                    Vector cords = new Vector()
                    {
                        Latitude = x,
                        Longitude = i,
                        Altitude = 1000
                    };

                    co.Add(cords);
                }
                line.Coordinates = co;
                placemark.Geometry = line;
                document.AddFeature(placemark);
            }

            for (int i = 0; i <= 180; i = i + 15)
            {
                Placemark placemark = new Placemark();
                LineString line = new LineString();
                CoordinateCollection co = new CoordinateCollection();
                for (int x = 0; x <= 90; x = x + 15)
                {
                    Vector cords = new Vector()
                    {
                        Latitude = x,
                        Longitude = i,
                        Altitude = 1000
                    };

                    co.Add(cords);
                }
                for (int x = -90; x <= 0; x = x + 15)
                {
                    Vector cords = new Vector()
                    {
                        Latitude = x,
                        Longitude = i,
                        Altitude = 1000
                    };

                    co.Add(cords);
                }
                line.Coordinates = co;
                placemark.Geometry = line;
                document.AddFeature(placemark);
            }
            for (int i = -180; i <= 0; i = i + 15)
            {
                Placemark placemark = new Placemark();
                LineString line = new LineString();
                CoordinateCollection co = new CoordinateCollection();
                for (int x = 0; x <= 90; x = x + 15)
                {
                    Vector cords = new Vector()
                    {
                        Latitude = x,
                        Longitude = i,
                        Altitude = 1000
                    };

                    co.Add(cords);
                }
                for (int x = -90; x <= 0; x = x + 15)

                {
                    Vector cords = new Vector()
                    {
                        Latitude = x,
                        Longitude = i,
                        Altitude = 1000
                    };

                    co.Add(cords);
                }
                line.Coordinates = co;
                placemark.Geometry = line;
                document.AddFeature(placemark);
            }

如果問題是如何使用C#查找任意LineString對象與網格的交點,則Matthew是正確的。 在C ++中,您可以在Java中使用GEOS http://trac.osgeo.org/geos/ ,即JTS http://www.vividsolutions.com/jts/JTSHome.htm

但是,如果您自己創建網格,並且想要回答一個更簡單的問題,即如何找到剛創建的網格的水平線和垂直線之間的交點,那么答案將是使用相同的精確值。在嵌套循環中用於LineString的緯度,經度值:

Document document = new Document();
for(y = -90; y < 0; y += 15){
  for(x = -180; x < 0; x+= 15){
     Point point = new Point();
     point.Coordinate = new Vector(x, y);
     Placemark placemark = new Placemark();
     placemark.Geometry = point;
     document.AddFeature(placemark);
  }
}

    .. repeat for the other 4 quadrants

// It's conventional for the root element to be Kml,
// but you could use document instead.
Kml root = new Kml();
root.Feature = document;
XmlFile kml = KmlFile.Create(root, false);

這是一些源代碼,例如,如果您想使用DotSpatial來查找網格和Shapefile之間的交點。 在這種情況下,shapefile具有河流線並且僅產生一個交點。 請注意,拓撲交叉點代碼有點慢,因此您將需要使用范圍檢查來加快速度。 在您的情況下,您可能想通過使用KMLSharp來讀取kml源文件中的線串坐標來構建新功能,而不是打開shapefile,但是相交代碼將是相似的。

附帶說明一下,我認為看似簡單易用的FeatureSet.Intersection方法不夠聰明,無法處理直線相交將點要素作為相交的情況。 它僅適用於輸出可能與輸入具有相同要素類型的點或多邊形。

    using DotSpatial.Controls;
    using DotSpatial.Data;
    using DotSpatial.Topology;
    using DotSpatial.Symbology;


    private FeatureSet gridLines;

    private void buttonAddGrid_Click(object sender, EventArgs e)
    {
        gridLines = new FeatureSet(FeatureType.Line);
        for (int x = -180; x < 0; x += 15)
        {
            List<Coordinate> coords = new List<Coordinate>();
            coords.Add(new Coordinate(x, -90));
            coords.Add(new Coordinate(x, 90));
            LineString ls = new LineString(coords);
            gridLines.AddFeature(ls);
        }
        for (int y = -90; y < 0; y += 15)
        {
            List<Coordinate> coords = new List<Coordinate>();
            coords.Add(new Coordinate(-180, y));
            coords.Add(new Coordinate(180, y));
            LineString ls = new LineString(coords);
            gridLines.AddFeature(ls);
        }

        map1.Layers.Add(new MapLineLayer(gridLines));
    }

    private void buttonIntersect_Click(object sender, EventArgs e)
    {
        if (gridLines == null)
        {
            MessageBox.Show("First add the grid.");
        }

        IFeatureSet river = FeatureSet.Open(@"C:\Data\Rivers\River.shp");
        MapLineLayer riverLayer = new MapLineLayer(river);
        map1.Layers.Add(river);




        List<DotSpatial.Topology.Point> allResultPoints = new List<DotSpatial.Topology.Point>();
        foreach (Feature polygon in river.Features)
        {
            Geometry lineString = polygon.BasicGeometry as Geometry;
            foreach (Feature lineFeature in gridLines.Features)
            {
                // Speed up calculation with extent testing.
                if(!lineFeature.Envelope.Intersects(lineString.Envelope)){
                    continue;
                }
                IFeature intersectFeature = lineFeature.Intersection(lineString);
                if (intersectFeature == null)
                {
                    continue;
                }


                MultiPoint multi = intersectFeature.BasicGeometry as MultiPoint;
                if (multi != null)
                {
                    for(int i = 0; i < multi.NumGeometries; i++)
                    {
                        allResultPoints.Add(intersectFeature.GetBasicGeometryN(i) as DotSpatial.Topology.Point);
                    }
                }
                DotSpatial.Topology.Point single = intersectFeature.BasicGeometry as DotSpatial.Topology.Point;
                {
                    allResultPoints.Add(single);
                }
            }
        }

        FeatureSet finalPoints = new FeatureSet(FeatureType.Point);
        foreach(DotSpatial.Topology.Point pt in allResultPoints){
            finalPoints.AddFeature(pt);
        }
        map1.Layers.Add(new MapPointLayer(finalPoints));
    }

我認為DotSpatial庫應該可以滿足您的需求,我過去曾使用過此庫,但未使用過交叉功能:

http://dotspatial.codeplex.com/wikipage?title=DotSpatial.Data.FeatureSetExt.Intersection

如果您嘗試進行自己的線相交分析,則要知道,簡單的笛卡爾平面方法會引入誤差([我認為]當接近極點時,這種誤差會變得更加明顯)。

參見此處: http : //www.geog.ubc.ca/courses/klink/gis.notes/ncgia/u32.html

此處: 兩條地理線之間的交點

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM