Wednesday February 04 2009 at 00:42
This article, written November 9th 2006, has been updated to reflect the fact that Luc Van Linden emailed me with a potential solution to my original question:
I tried to combine the two SQL statements (the one generating the windows) with the one creating the points for inserting but I failed. If anyone can show me how to do this I would be greatly appreciative!
His solution, combined with some improvements of my own, has now been integrated into this, updated article.
Article
There are many times when we need to generate some point data for use with Oracle Spatial.
Certainly I have had to do this many, many times.
I have often taken different approaches but I thought I would share one approach (that uses the hierarchical “CONNECT BY” clause) to doing this.
The following code first asks for the complete extent of the area for which points are needed to be added. The properties of a number of internal windows are asked for as well: minimum width/height and maximum width/height; maximum number of points for each window etc. A width/height for each window is generated as a random number between the minimums and maximums for each window. Similarly for the number of points.
When finished the sorts of possible spread of points is as per this image.
Be aware that this approach has been created for projected data. It could be used for generating geodetic data with some simple modifications.
set timing on
set linesize 120
set pagesize 1000
set serveroutput on size 1000000
DROP SEQUENCE my_points_seq;
CREATE SEQUENCE my_points_seq;
DROP TABLE my_points;
Prompt Create a table to hold our point data maximising its use of database blocks so it uses as little space as possible
CREATE TABLE my_points (
point_id Integer primary key,
window_id Integer,
sub_point_id Integer,
geometry mdsys.sdo_geometry
) PCTUSED 99;
Accept minx number default 100000 prompt 'Enter minimum X coordinate of whole area for which points are needed (100000): '
Accept miny number default 5000000 prompt 'Enter minimum Y coordinate of whole area for which points are needed (5000000): '
Accept maxx number default 400000 prompt 'Enter Maximum X coordinate of whole area for which points are needed (4000000): '
Accept maxy number default 6000000 prompt 'Enter Maximum Y coordinate of whole area for which points are needed (6000000): '
Accept InternalWindows number default 5 prompt 'Enter number of internal windows (5): '
Accept PointsPerWindow number default 5000 prompt 'Enter possible maximum number of points per window (5000): '
Accept minWidth number default 500 prompt 'Enter Minimum width of an internal window (500): '
Accept maxWidth number default 100000 prompt 'Enter Maximum width of an internal window (100000): '
Accept minHeight number default 500 prompt 'Enter Minimum Height of an internal window (500): '
Accept maxHeight number default 100000 prompt 'Enter Maximum Height of an internal window (100000): '
Accept sdoTolerance number default 0.05 prompt 'Enter SDO_TOLERANCE metadata (0.05): '
INSERT /*+APPEND*/ INTO my_points (point_id,window_id,sub_point_id,geometry)
SELECT rownum as point_id,
w.Window_Id,
s.COLUMN_VALUE as PointN,
mdsys.sdo_geometry(2001,NULL,
MDSYS.SDO_POINT_TYPE(
ROUND(dbms_random.value(w.x  ( w.WinWidth / 2 ),
w.x + ( w.WinWidth / 2 )),ROUND(log(10,1/&sdotolerance.))+1),
ROUND(dbms_random.value(w.y  ( w.WinHeight / 2 ),
w.y + ( w.WinHeight / 2 )),ROUND(log(10,1/&sdotolerance.))+1),
NULL),
NULL,NULL) as geometry
FROM ( SELECT rownum As Window_ID,
p.PointCount,
p.X,
p.Y,
p.WinHeight,
p.WinWidth
FROM ( SELECT ( &minx. + &maxx. ) / 2 as X,
( &miny. + &maxy. ) / 2 as y,
( &maxy.  &miny. ) as WinHeight,
( &maxx.  &minx. ) as WinWidth,
trunc(dbms_random.value(1,&PointsPerWindow.),0) as PointCount
FROM DUAL
UNION ALL
SELECT dbms_random.value(&minx.,&maxx.) as X,
dbms_random.value(&miny.,&maxy.) as Y,
dbms_random.value(&minHeight.,&maxHeight.) as WinHeight,
dbms_random.value(&minWidth.,&maxWidth.) as WinWidth,
trunc(dbms_random.value(1,&PointsPerWindow.),0) as PointCount
FROM dual
CONNECT BY LEVEL <= &InternalWindows.
) p
) w,
TABLE(CAST(MULTISET(select level from dual connect by level <= w.PointCount) as SYS.UTL_NLA_ARRAY_INT)) s;
Prompt Let's sample some of the created data...
SELECT myp.geometry.sdo_point.x,
myp.geometry.sdo_point.y,
window_id
FROM my_points sample (2) myp;
Prompt Let's summary what was inserted into the table...
SELECT 'For Window '  window_id  ' '  count(*)  ' points were stored' As Result
FROM my_points
GROUP BY window_id;
Prompt Create Oracle Metadata Entry...
DELETE FROM user_sdo_geom_metadata WHERE table_name = 'MY_POINTS';
COMMIT;
INSERT INTO user_sdo_geom_metadata
SELECT 'MY_POINTS','GEOMETRY',
MDSYS.SDO_DIM_ARRAY(
MDSYS.SDO_DIM_ELEMENT('X', minx, maxx, &sdoTolerance.),
MDSYS.SDO_DIM_ELEMENT('Y', miny, maxy, &sdoTolerance.)), NULL
FROM ( SELECT TRUNC( MIN( a.geometry.sdo_point.x )  1,0) as minx,
ROUND( MAX( a.geometry.sdo_point.x ) + 1,0) as maxx,
TRUNC( MIN( a.geometry.sdo_point.y )  1,0) as miny,
ROUND( MAX( a.geometry.sdo_point.y ) + 1,0) as maxy
FROM my_points a);
Prompt Create RTree index on point data ...
DROP INDEX my_points_geometry;
CREATE INDEX my_points_geometry ON my_points(geometry)
INDEXTYPE IS MDSYS.SPATIAL_INDEX
PARAMETERS('sdo_indx_dims=2, layer_gtype=point');
All the “heavy lifting” for creating the points is done by the clever use of the CONNECT BY LEVEL clause is down to Tom Kyte.
Download the source code for this tip here
I hope this is of use to someone.

Comment [2]
I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.
Susan
http://dclottery.info
— Susan · 10 September 2009, 14:57 · #
Susan,
Thank you for your kind comments. I am pleased that you find my blog articles useful. If you can think of a topic that might be of use don’t hesitate to contact me.
regards
Simon
— Simon · 11 September 2009, 06:48 · #