【问题标题】:Using Math::Polygon to determine if point is inside a polygon. Not working使用 Math::Polygon 确定点是否在多边形内。不工作
【发布时间】:2019-11-10 18:51:06
【问题描述】:

我正在尝试确定某个点是否在多边形内。尝试使用 Math::Polygon。我在 mySQL 表中将多边形的边界作为多边形的每个点的一条记录: 示例:
1,38.33208,-75.51919
2,38.33286,-75.52265
等等
38,38.33208,-75.51919

无论我做什么,例程都不会显示多边形内的点,尽管该点确实在多边形内。

这是相关代码

use Math::Polygon;

my @poly = ();
$sqlc = "SELECT segment,lat,lon FROM boundary WHERE xxxxxxx ";
my $stcc = $dbh->prepare(qq{$sqlc});
$stcc->execute();
while(($seg,$slat,$slon) = $stcc->fetchrow_array())
{
    $poly[$seg] = ([$slat,$slon]);
}
my $bound = Math::Polygon->new(points => @poly);
my @this_loc = ([$lat2,$lon2]);

if($bound->contains( $this_loc ))
{
    # never reaches this point. 
}

无论如何,我无法让 ->contains() 例程返回 true。

任何想法将不胜感激。谢谢。

【问题讨论】:

  • 你在使用use strict; use warnings;吗?如果没有,您应该这样做。

标签: perl polygon cpan


【解决方案1】:

似乎(至少)有两个错误:

my $bound = Math::Polygon->new(points => @poly);

构造函数引用一个数组,而不是数组。所以应该是:

my $bound = Math::Polygon->new(points => \@poly);

其次,$bound->contains( ) 引用了一个点数组,所以应该是:

if($bound->contains( \@this_loc )) { ... }

【讨论】:

  • 更改为my $bound = Math::Polygon->new(points => \@poly); ...似乎有效。谢谢 如果我将反斜杠添加到 @this_loc,我会得到:在 /usr/local/share/perl/5.18.2/Math/Polygon/Calc.pm 第 286 行的数字 eq (==) 中使用未初始化的值 $y。在 /usr/local/share/perl/5.18.2/Math/Polygon/Calc.pm 第 294 行的数字文件 () 和 gt(>) 的其他值在第 294 行。
  • @MichelleBradley 太好了!你定义了$lat2$lon2吗?
  • 是的,那些是在别处定义的。这只是相关的sn-p代码。
  • 你能再检查一下$lon2的定义吗?您也可以将您正在处理的代码发送给我,我会查看它(我的电子邮件在我的个人资料中)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-19
  • 2016-02-21
  • 2014-11-18
  • 2010-09-18
相关资源
最近更新 更多