发布我自己问题的答案以供将来参考。我能够想出一个创建空域横截面的解决方案。它基于坐标几何。
1) 输入数据是横截面的起点和终点坐标。
2) 遍历所有区域并检查开始和/或结束坐标是否落在任何空域区域(多边形)内。 PHP 代码从另一个 SO 答案略微修改,以检查点是否在多边形内:
// Point class, storage of lat/long-pairs
class Point {
var $lat;
var $long;
function Point($lat, $long) {
$this->lat = $lat;
$this->long = $long;
}
}
function pointInPolygon($p, $polygon) {
$c = 0;
$p1 = $polygon[0];
$n = count($polygon);
for ($i=1; $i<=$n; $i++) {
$p2 = $polygon[$i % $n];
if ($p->long > min($p1->long, $p2->long)
&& $p->long <= max($p1->long, $p2->long)
&& $p->lat <= max($p1->lat, $p2->lat)
&& $p1->long != $p2->long) {
$xinters = ($p->long - $p1->long) * ($p2->lat - $p1->lat) / ($p2->long - $p1->long)
if ($p1->lat == $p2->lat || $p->lat <= $xinters) {
$c++;
}
}
$p1 = $p2;
}
// if the number of edges we passed through is even, then it's not in the poly.
return $c%2!=0;
}
3) 循环遍历包含空域数据的每个区域(多边形)的所有线段。 PHP 代码从另一个 SO 答案稍作修改以返回两条线段的交点:
function Det2($x1, $x2, $y1, $y2) {
return ($x1 * $y2 - $y1 * $x2);
}
function lineIntersection ($v1Y, $v1X, $v2Y, $v2X, $v3Y, $v3X, $v4Y, $v4X) {
$tolerance = 0.000001;
$a = Det2($v1X - $v2X, $v1Y - $v2Y, $v3X - $v4X, $v3Y - $v4Y);
if (abs($a) < $tolerance) return null; // Lines are parallel
$d1 = Det2($v1X, $v1Y, $v2X, $v2Y);
$d2 = Det2($v3X, $v3Y, $v4X, $v4Y);
$x = Det2($d1, $v1X - $v2X, $d2, $v3X - $v4X) / $a;
$y = Det2($d1, $v1Y - $v2Y, $d2, $v3Y - $v4Y) / $a;
if ($x < min($v1X, $v2X) - $tolerance || $x > max($v1X, $v2X) + $tolerance) return null;
if ($y < min($v1Y, $v2Y) - $tolerance || $y > max($v1Y, $v2Y) + $tolerance) return null;
if ($x < min($v3X, $v4X) - $tolerance || $x > max($v3X, $v4X) + $tolerance) return null;
if ($y < min($v3Y, $v4Y) - $tolerance || $y > max($v3Y, $v4Y) + $tolerance) return null;
return array($x, $y);
}
4) 如果有相交的线段,计算出相交的距离(作为上一步的结果)到横截面的起始坐标。将其除以横截面的总长度,得到空域区域边界在横截面上的相对位置。
5) 根据第 2 点和第 4 点的结果,绘制 SVG 多边形。相对交点位置被转换为多边形的 X 坐标,高度数据(下限和上限)成为多边形的 Y 坐标。