我已经成功使用了DotSpatial.Contains函数来测试我的每个点(180万)是否在我的shapefile中。但是,算法非常慢,因为我正在测试大量的点和非常复杂的多边形。这是一个例子: http://picload.org/image/igararl/pointshapesel.png
图像中的德国边界是我的shapefile,用于选择点(简化但仍然是14.000个顶点),红色矩形是我的180万个点所在的区域。
为了寻找更快的空间纬度/经度坐标的多边形点测试,我遇到了光线投射算法: http://alienryderflex.com/polygon/
我将代码转换为VB.Net,它运行时没有错误,但它没有找到任何point / shapefile的交集。我知道lat / long坐标存在困难 - 但在德国,lat / long坐标与标准笛卡尔坐标系匹配。
这是我的(代码)。我首先出于速度原因声明全局变量:
Public polyCorners As Integer
Public polyX() As Double
Public polyY() As Double
Public xP, yP As Double
Public constant() As Double
Public multiple() As Double
然后我将我的Shapefile顶点添加到polyCorners列表中(这可以):
Dim ShapefilePoly As Shapefile = Shapefile.OpenFile(TextBox4.Text)
Dim x As Long = 1
For Each MyShapeRange As ShapeRange In ShapefilePoly.ShapeIndices
For Each MyPartRange As PartRange In MyShapeRange.Parts
For Each MyVertex As Vertex In MyPartRange
If MyVertex.X > 0 AndAlso MyVertex.Y > 0 Then
pointsShape.Add(New PointLatLng(MyVertex.Y, MyVertex.X))
ReDim Preserve polyY(x)
ReDim Preserve polyX(x)
polyY(x) = MyVertex.Y
polyX(x) = MyVertex.X
x = x + 1
End If
Next
Next
Next
ReDim constant(x)
ReDim multiple(x)
在实际搜索之前,我按照作者的建议调用了precalc_values():
Private Sub precalc_values()
Dim i As Integer, j As Integer = polyCorners - 1
For i = 0 To polyCorners - 1
If polyY(j) = polyY(i) Then
constant(i) = polyX(i)
multiple(i) = 0
Else
constant(i) = polyX(i) - (polyY(i) * polyX(j)) / (polyY(j) - polyY(i)) + (polyY(i) * polyX(i)) / (polyY(j) - polyY(i))
multiple(i) = (polyX(j) - polyX(i)) / (polyY(j) - polyY(i))
End If
j = i
Next
End Sub
最后,我为每个lat / lng点调用pointInPolygon():
Function LiesWithin(latP As Double, lngP As Double) As Boolean
LiesWithin = False
xP = lngP
yP = latP
If pointInPolygon() = True Then LiesWithin = True
End Function
Private Function pointInPolygon() As Boolean
Dim i As Integer, j As Integer = polyCorners - 1
Dim oddNodes As Boolean = False
For i = 0 To polyCorners - 1
If (polyY(i) < yP AndAlso polyY(j) >= yP OrElse polyY(j) < yP AndAlso polyY(i) >= yP) Then
oddNodes = oddNodes Xor (yP * multiple(i) + constant(i) < xP)
End If
j = i
Next
Return oddNodes
End Function
所有变量似乎都被正确填充,数组包含我的多边形角,并且从第一个点到最后一个点精确地检查点列表。它在20秒内完成了180万点的完整列表(与使用DotSpatial.Contains功能的1小时30分钟相比)。任何人都知道为什么没有找到任何交叉点?
答案 0 :(得分:1)
好吧,我发现问题比预期的要快: 我忘了将Shapefile顶点的数量分配给 polyCorners 。 在上面的代码中,只需在
之后添加 polyCorners = x ReDim constant(x)
ReDim multiple(x)
polyCorners = x
也许有人发现这段代码很有用。我真的很惊讶它是多么超快!