我寫了一個使用光線投射算法來計算,如果該點在多邊形或不是一個PL/SQL程序。對多邊形的頂點數沒有限制。在這裏,頂點的經度和緯度以及要檢查的點以十進制格式提交,並且是分離的。
create or replace function PIP(verLong in varchar2,
verLat in varchar2,
xLong in number,
yLat in number) return boolean
is
i number := 0;
j number := 0;
c boolean := false;
tempLong number;
tempLat number;
latDegree number;
minLength number;
pi NUMBER := 3.1415926;
x number := xLong;
y number := yLat;
TYPE coodArray IS TABLE OF VARCHAR2(1000) INDEX BY BINARY_INTEGER;
longArray coodArray;
latArray coodArray;
begin
--Convert reference point in meters
--Round number to 4 places
x := round(x, 4);
y := round(y, 4);
--convert latitude to degrees
latDegree := y/60;
--convert latitude from degrees to radians
latDegree := (latDegree * pi)/180;
--compute minute or arc in meters given the latitude value
minLength := 1852.3 - (9.4 * Cos(2 * latDegree));
--Multiply the latitude and longitude pair with the length factor to get co-ordinates in meters
x := x * minLength;
y := y * minLength;
loop
--Retrieve the longitudes and latitudes from the string of latitudes or longitudes
tempLong := to_number(get_token(verLong, i + 1, '|'));
tempLat := to_number(get_token(verLat, i + 1, '|'));
i := i + 1;
if (tempLong is null) then
exit;
end if;
--Round the numbers upto four decimals
tempLong := round(tempLong, 4);
tempLat := round(tempLat, 4);
--convert latitude to degrees
latDegree := tempLat/60;
--convert latitude from degrees to radians
latDegree := (latDegree * pi)/180;
--compute the minute of an arc in meters given the latitude value
minLength := 1852.3 - (9.4 * Cos(2 * latDegree));
--Multiply the latitude and longitude pair with the length factor to get co-ordinates in meters
tempLong := tempLong * minLength;
tempLat := tempLat * minLength;
longArray(i) := tempLong;
latArray(i) := tempLat;
end Loop;
j := latArray.COUNT;
--Check whether the point lies inside the polygon formed by the vertices (Ray casting algorithm)
for i in 1 .. j loop
if (((latArray(i) > y) != (latArray(j) > y)) and
(x < (Longarray(j) - Longarray(i)) * (y - Latarray(i))/
(latarray(j) - Latarray(i)) + longarray(i))) then
c := not c;
end if;
j := i;
end loop;
return c;
end PIP;
感謝, Noorul