Go to content Go to navigation and search

Current Oracle Spatial Blogs

Search

RSS / Atom

Email me

textpattern

Tip #3: Generating sample (test) point data · Nov 9, 06:54 pm by Simon Greener

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.

Possible point spread from algorithm.

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,
  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): '

Prompt Generate &InternalWindows. random Windows within our area each having a random window size
DECLARE
  v_round_factor NUMBER;
BEGIN
  v_round_factor := ROUND(log(10,1/&sdotolerance.))+1;
  <<window_generation_loop>>
  -- First let's create and process a list of windows defined from randomly created centre points with random extents
  -- We'll include a sprinkling of points across the whole area and not just within particular windows
  For rec In ( SELECT rownum As WindowID, X, Y, WinHeight, WinWidth, PointCount
                 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. 
                      ) 
              ) LOOP
    dbms_output.put_line('Generating ' || rec.PointCount || ' Points for Window ' || rec.WindowId || ' of size ('|| rec.WinWidth||','||rec.WinHeight||')');
    -- Now for each window create a random number of points within its extent ...
    INSERT INTO my_points 
      SELECT my_points_seq.nextval,
             rec.WindowId,
             mdsys.sdo_geometry(2001,NULL,
                   MDSYS.SDO_POINT_TYPE(
                         ROUND(dbms_random.value(rec.x - ( rec.WinWidth  / 2 ),  
                                           rec.x + ( rec.WinWidth  / 2 )),v_round_factor),
                         ROUND(dbms_random.value(rec.y - ( rec.WinHeight / 2 ), 
                                           rec.y + ( rec.WinHeight / 2 ) ),v_round_factor),
                         NULL),
                   NULL,NULL)
       FROM DUAL
     CONNECT BY LEVEL <= rec.PointCount; 
    COMMIT;
  End Loop window_generation_loop;
END;
/
SHOW ERRORS

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 SELECT statements inside the BEGIN/END PL/SQL anonymous block. The clever use of the CONNECT BY LEVEL clause is down to Tom Kyte.

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!

Download the source code for this tip here
  Textile Help

<<Oracle's SQL/MM Compliant Types >>Tip #2: layer_gtypes for spatial indexes