Created
July 26, 2016 20:28
-
-
Save OsBlaineOra/cae33df964e46a9be034b4ddb7efcb3d to your computer and use it in GitHub Desktop.
generate_random_location
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create or replace function generate_random_location(p_island in sdo_geometry) | |
return sdo_geometry | |
is | |
v_center sdo_geometry; | |
v_radius number; | |
new_radian number; | |
new_point sdo_geometry; | |
v_lon number; | |
v_lat number; | |
v_x number; | |
v_y number; | |
new_line sdo_geometry; | |
final_point sdo_geometry; | |
v_length number; | |
begin | |
v_center := sdo_geom.sdo_mbc_center(p_island, 0.005); | |
v_radius := sdo_geom.sdo_mbc_radius(p_island, 0.005) * 1.1; --add a little to make sure it's outside | |
/* get the island center's x y */ | |
select t.x, t.y into v_lon, v_lat | |
from table(sdo_util.getvertices(v_center)) t; | |
/* random radian from 0 to 2pi */ | |
new_radian := dbms_random.value(0,asin(1)*4); | |
/* generate a point on the new radian outside of the polygon */ | |
new_point := sdo_util.point_at_bearing( | |
sdo_geometry(2001, | |
4326, | |
sdo_point_type(v_lon, v_lat, null), null, null), | |
new_radian, | |
v_radius | |
); | |
/* get the new point's x y */ | |
select t.x, t.y into v_x, v_y | |
from table(sdo_util.getvertices(new_point)) t; | |
/* create a line from the center to the new point | |
get the segment that is contained inside the polygon | |
*/ | |
new_line := sdo_geom.sdo_intersection( | |
sdo_geometry (2002, 4326, null, | |
sdo_elem_info_array (1,2,1), | |
sdo_ordinate_array (v_lon, v_lat,v_x, v_y) | |
), | |
p_island, | |
0.005 | |
); | |
/* get the length of the new line */ | |
v_length := round(sdo_geom.sdo_length(new_line, 0.05)); | |
/* | |
using the length of the new line, | |
generate a point on the current radian with a random distance from 0 to the lenght of the line | |
*/ | |
final_point := sdo_util.point_at_bearing( | |
sdo_geometry(2001, 4326, | |
sdo_point_type(v_lon, v_lat, null), null, null), | |
new_radian, | |
dbms_random.value(0,v_length) | |
); | |
return final_point; | |
end; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment