简体   繁体   English

以经度-纬度表示多边形

[英]Point in polygon in terms of Longitude - Latitude

We are working on one map solution. 我们正在开发一种地图解决方案。 Here user has drawn one polygon on the map and for a given point we need to find if point is residing in or out of the polygon. 在这里,用户在地图上绘制了一个多边形,对于给定的点,我们需要确定点是位于多边形内还是位于多边形外。

We are using one SQL technique as per http://www.sql-statements.com/point-in-polygon.html to do this task till now but during the time we found out that its not worked for some areas. 到目前为止,我们正在按照http://www.sql-statements.com/point-in-polygon.html使用一种SQL技术来执行此任务,但是在这段时间内,我们发现它不适用于某些领域。

Does anyone has the proper solution on this? 有人对此有适当的解决方案吗? We can also try the solution in C# if its working fine 如果工作正常,我们也可以在C#中尝试解决方案

Thank you. 谢谢。

If you are using SQL Server 2008 then you can use STIntersection (geography Data Type) - SQL Server will do everything for you. 如果您使用的是SQL Server 2008,则可以使用STIntersection(地理数据类型) -SQL Server将为您做所有事情。

I'd recommend SQL Server 2008 in this case because it provides native support for geographical data. 在这种情况下,我建议使用SQL Server 2008,因为它为地理数据提供了本机支持。 Before you use it " STIntersect giving incorrect result for geography Datatype " might be worth of reading. 在使用它之前,“ STIntersect为地理数据类型提供不正确的结果 ”可能值得一读。 Example from that site: 该站点的示例:

declare @point geometry
declare @polygon geometry
set @point =  geometry::STGeomFromText('POINT (-88.22 41.50000001)', 4326)
set @polygon = geometry::STGeomFromText('POLYGON ((-88.2 41.5, -88.2 41.6, -88.3 41.6, -88.3 41.5, -88.2 41.5))', 4326)--124
Select @point.STIntersection(@polygon).ToString()

Paraphrased from http://en.wikipedia.org/wiki/Point_in_polygon 释义自http://en.wikipedia.org/wiki/Point_in_polygon

The easiest way to do this is draw an imaginary line from your point in one direction and count the number of lines it crosses. 最简单的方法是在一个方向上从您的点绘制一条假想的线,并计算其交叉的线数。 If it's odd, the point is inside, even the point is outside. 如果很奇怪,则该点位于内部,甚至该点位于外部。

Basically iterate through each point pair, find where it crosses the horizontal line at your point, if it crosses to the right, increment the counter, if it crosses to the left or doesn't cross at all, ignore it. 基本上遍历每个点对,找到它与您的水平线的交叉点,如果它与右边交叉,则增加计数器,如果它与左边交叉或根本不交叉,则忽略它。 Horizonal lines right at your point should not be counted either (boundary condition). 您所在位置的水平线也不应计算在内(边界条件)。

IMHO the basic explanation of the PIP solutions are missing half of the important stuff, ie how to determine what lines of the polygon to actually test for having been crossed. 恕我直言,PIP解决方案的基本解释缺少了重要内容的一半,即如何确定要实际测试的多边形的哪些线已经过。 Just in case of Michał Powaga's solution not working for you. 万一MichałPowaga的解决方案对您不起作用。

Point P(x, y) is your point. 点P(x,y)是您的点。 Points P0(x0, y0) and P1(x1, y1) form a line. 点P0(x0,y0)和P1(x1,y1)形成一条线。 The imaginary line we draw to see how many poly-lines we cross is horizontal. 我们绘制的假想线是要看到多少条折线是水平的。

1) First determine what lines are actually crossable (lines that are parallel to the imaginary line you draw or lines that are above or below the line would obviously not be crossed): 1)首先确定哪些线实际上是可交叉的(与您绘制的假想线平行的线或该线之上或之下的线显然不会被交叉):

For each line of the polygon, compute weather P would be able to cross it.
If ((x0 < x < x1) OR (x0 > x > x1)) add line to some list.

2) Determine which of the remaining lines (those in the list) are actually crossed: 2)确定剩余的哪些行(列表中的哪些行)实际被交叉:

For each line in list, compute
    result = (y - y0) * (x1- x0) - (x - x0) * (y1 - y0)
If (result < 0) the line was crossed, increment a counter.
If (result == 0) the point is ON the line, increment a counter if thats supposed
    to count as the point having crossed the line, else don't ...
If (result > 0) the line was not crossed, so just continue with the loop.
[Note: double check weather I got the sides right ...]

3) Now, if the counter is an odd number your point is inside, if it is even or zero it is outside of the polygon. 3)现在,如果计数器是一个奇数,则您的点在内部,如果它是偶数或零,则它在多边形的外部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM