Go to content Go to navigation and search


Current Oracle Spatial Blog Articles

    Using Oracle's ST_GEOMETRY type hierarchy with SDO_GEOMETRY: ST_PointN and ST_NumPoints
    Making Sdo Geometry Metadata
    SC4O Test log
    The Frontage Problem: Creating references from land parcel street frontage boundary to point in street
    Scheduling the Export of Spatial Data in Oracle to a Shapefile each night
    Oracle CENTROID package update
    Book: "Applying And Extending Oracle Spatial"
    Create Polygons and Linestrings From Bearings And Distances (COGO)
    Simple Implementation Of Travelling Salesman Problem
    ST_Densify for Object PLSQL
    PLSQL ST_AsEWKT and ST_FromWKT Functions For Oracle (any version)
    Useful Package of Wrapper Functions for Sdo_Util.AffineTransforms
    GeoRaptor 4.x Update 2 (Project has Stalled)
    Tip: Drop that Spatial Index!
    Convert Single Geometry to Multi-part Geometry in Oracle Spatial
    Optimized Rectangle to 5 Point Polygon
    Centroid Package now supports Y ordinate seeding
    Convert GeoJSON document to Sdo_Geometry objects
    Function That Returns a Compass Point From a Whole Circle Bearing
    Playing around with Centroids by using different seed values
    Simple Oracle C Sprintf or Java String.format
    Some Oriented Point Functions
    Extracting Inner Rings Changed Ordinate Ordering: A Trap For Players Who Don't Read Documentation!
    PLS-00306: wrong number or types of arguments in call to 'SDO_GEOMETRY'
    Converting Google Earth Formatted Longitude/Latitude points to decimal degrees
    Oracle Business Intelligence Warehousing and Analytics - Spatial Summit
    How far inside, is inside? Measuring actual distance.
    Noding and building a polygon from single, overlapping linestrings
    Analyzing Spatial Query Performance Improvements in Oracle Spatial and Graph 12c Through Cross-Vendor Comparison
    ST_VertexN / ST_PointN - Extracting a specific point from any geometry
    Convert Single Point stored in SDO_ORDINATES to SDO_POINT_TYPE
    Aggregate APPEND Islands and XOR polygons
    Circular Arcs in Geodetic Polygons
    Some SDO_GEOMETRY/DIMINFO handling functions
    Changing all DIMINFO sdo_tolerance values for all metadata records in one go.
    Building Polygons from Incomplete Linestrings using ST_PolygonBuilder
    Computing Cardinal Directions to nearby geometries
    Intersecting two aggregated polygon layers with SC4O
    Spatial and Oracle 12c
    Update Triggers and SDO_GEOMETRY Equality
    Duplicate Geometry data and Data Models
    CENTROID package update
    How to calculate cumulative length of a linestring
    Compute Location from known Lat/Long point using delta easting and northing in miles
    Sorting SDO_GEOMETRY data using the ORDER BY clause of a SELECT statement
    Creating linestrings from points
    Rounding Coordinates or Ordinates in SDO_GEOMETRY
    Effects of Sdo_Geometry Ordinate Precision on Performance
    Effects of Sdo_Geometry Ordinate Precision on Storage
    The Spatial filtering of geometries: The effect of tolerances on relationships
    Application of Delaunay Triangulation and Inverse Distance Weighting (IDW) in Oracle for Soils Interpolation
    Selecting all SDO_GTYPE values for all tables/sdo_geometry columns in a schema
    CENTROID package - Tips for Use
    Announcing the Spatial Companion For Oracle (SC4O)
    Filtering Rings (Oracle Spatial)
    Splitting a polygon using one or more linestrings
    isValid, isSimple, Dimension and CoordDim methods for SDO_Geometry
    Line Merging or Collecting lines together: ST_LineMerger
    ST_DeleteVertex for Oracle SDO_Geometry based on Jaspa/JTS
    3D/4D and SRID aware Conversion functions for SDO_Geometry: WKT and EWKT
    Topological vs Non-Topological Simplification/Generalization of Aggregated Area Geometies in Oracle
    Filtering very short linestrings via bitmap function index
    CENTROID For Oracle
    Gridding a sdo_geometry line/polygon object (Oracle)
    Finding centre and radius of a circular geometry
    Constraining geometry type for sdo_geometry column in a table.
    CASE Statements and SDO_GEOMETRY
    The Power of Constraints and Indexes for Spatial Constraints: stopping duplicate points
    Replacement for SDO_GEOM.RELATE - ST_Relate based on JTS
    Changing Oracle Spatial Index Parameters on existing index
    Writing Excel Spreadsheets files from within the Oracle database using Java and PL/SQL
    Writing xSV (eg csv) files from within the Oracle database using Java and PL/SQL
    A simple spike finder for Spatial/Locator
    JTS Java class compilation for 11g and above
    Random Spatial Search Procedure
    Geometry Snapping using JTS in Oracle
    Exposing JTS's MinimumBoundingCircle functionality
    Exposing JTS's Densifier functionality
    Using JTS's Comparison Functions - HausdorffSimilarityMeasure & AreaSimilarityMeasure with SDO_GEOMETRY
    Free JTS-based Area/Length Functions
    Handy way of systematically fixing polygon geometries with 13349 and other errors
    Standalone CENTROID package now available for download
    Free Union, Intersection, Xor and Difference Functions for Oracle Locator - Part 4 Processing Geodetic data
    Configurable Buffer: JTS and Oracle
    Free Union, Intersection, Xor and Difference Functions for Oracle Locator - Part 3
    Free Union, Intersection, Xor and Difference Functions for Oracle Locator - Part 2
    Free Union, Intersection, Xor and Difference Functions for Oracle Locator - Part 1
    Building Lines into Polygons in Oracle Locator / Spatial
    Finding Intersection Points between Line and Polygon
    Free version of sdo_length
    Alternative to my SQL based GetNumRings function
    External Tables and SDO_Geometry data.
    layer_gtype keyword issue when indexing linear data on 11g
    String Tokenizer for Oracle
    Free Aggregate Method for Concatenating 2D Lines in Oracle Locator 10g
    Reducing 5 Vertex Polygon to Optimized Rectangle
    Square Buffer
    Converting decimal seconds to string
    Valid conversion unit values for Oracle sdo_geom.sdo_length()
    Removing Steps in Gridded Vector Data - SmoothGrid for Oracle
    Oracle Spatial DISJOINT search/filtering
    Creating SDO_Geometry from geometric data recorded in the columns of a table
    Concave Hull Geometries in Oracle 11gR2
    Projecting SDO_GEOM_METADATA DIMINFO XY ordinates
    Instantiating MDSYS.VERTEX_TYPE
    New PL/SQL Packages - Rotate oriented point
    GeoRaptor Development Team
    Fast Refreshing Materialized View Containing SDO_GEOMETRY and SDO_GEOM.SDO_AREA function
    Performance of PL/SQL Functions using SQL vs Pure Code
    Implementing the BEST VicGrid Projection in Oracle 10gR2
    ORA-13011 errors when using SDO_GEOM.VALIDATE_LAYER_WITH_CONTEXT()
    Extract Polygons from Compound Polygon
    Detecting sdo_geometries with compound (3-point Arcs) segments
    GEOMETRY_COLUMNS for Oracle Spatial
    Convert GML to SDO_Geometry in Oracle 10gR2
    Spatial Sorting of Data via Morton Key
    Swapping Ordinates in an SDO_GEOMETRY object
    New To_3D Function
    Extend (Reduce/Contract/Skrink) Function for Oracle
    Loading and Processing GPX 1.1 files using Oracle XMLDB
    Loading Spatial Data from an external CSV file in Oracle
    Calling the Oracle Spatial shapefile loader from within the Oracle database itself
    Implementing SDO_VertexUpdate/ST_VertexUpdate for Oracle
    Implementing SDO_RemovePoint/ST_RemovePoint for Oracle
    Implementing SDO_AddPoint/ST_AddPoint for Oracle
    ESRI ArcSDE Exverted and Inverted Polygons and Oracle Spatial
    Funky Fix Ordinates By Formula
    Implementing a SetPoint/ST_SetPoint function in Oracle
    Implementing an ST_SnapToGrid (PostGIS) function for Oracle Spatial
    Generating random point data
    Implementing an Affine/ST_Affine function for Oracle Spatial
    Implementing a Scale/ST_Scale function for Oracle Spatial
    Implementing a Parallel/ST_Parallel function for linestring data for Oracle Spatial
    Implementing a Rotate/ST_Rotate function for Oracle Spatial
    Limiting table list returned when connecting to Oracle Database using ODBC
    ST_Azimuth for Oracle: AKA Cogo.Bearing
    Implementing a Translate/ST_Translate/Move function for Oracle Spatial
    Elem_Info_Array Processing: An alternative to SDO_UTIL.GetNumRings and querying SDO_ELEM_INFO itself
    Minumum Bounding Rectangle (MBR) Object Type for Oracle
    How to extract elements from the result of an sdo_intersection of two polygons.
    How to restart a database after failed parameter change
    Fixing failed spatial indexes after import using data pump
    generate_series: an Oracle implementation in light of SQL Design Patterns
    Multi-Centroid Shootout
    Oracle Spatial Centroid Shootout
    On the use of ROLLUP in Oracle SELECT statements
    Surrounding Parcels
    Spatial Pipelining
    Using Oracle's SDO_NN Operator - Some examples
    Converting distances and units of measure in Oracle Locator
    Split Sdo_Geometry Linestring at a known point
    Forcing an Sdo_Geometry object to contain only points, lines or areas
    Unpacking USER_SDO_GEOM_METADATA's DIMINFO structure using SQL
    Generating multi-points from single point records in Oracle Spatial
    Object Tables of Sdo_Geometry
    Oracle Locator vs Oracle Spatial: A Reflection on Oracle Licensing of the SDO_GEOM Package
    FAST REFRESHing of Oracle Materialized Views containing Sdo_Geometry columns
    Australian MGA/AMG Zone Calculation from geographic (longitude/latitude) data
    Loading Shapefiles (SHP) into Oracle Spatial
    Oracle Spatial Mapping and Map Rendering Performance Tips
    The significance of sdo_lb/sdo_ub in USER_SDO_GEOM_METDATA: Do I need it?
    Oracle Spatial Forum - Melbourne April 2007
    Layer_GTypes for spatial indexes
    Oracle's SQL/MM Compliant Types
    Tips and Tricks



RSS / Atom

Email me


Creative Commons License
All Blog Articles, Data Models and Free Source Code by Simon Greener, The SpatialDB Advisor is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

Generating multi-points from single point records in Oracle Spatial

Friday November 16 2007 at 10:31

The background to this blog article comes from some work I did for GeoScience Australia in May-June 2007. This work involved the creation of a “distribution” database from a “production” database using Oracle replication with presentation by UMN MapServer.

The “production” database included a single table (containing around 70 million individual point records that described around 13,000 individual hydrographic surveys. Dynamic searching and visualising individual surveys in the base table was considered to be too slow to be used in the interactive mapping that was required for the data discovery web application so “surrogate” vector linestrings existed against which searching and visualisation would occur.

Surrogate Survey Line vs Actual Survey Points

No one had yet been able to implement search and visualisation against the base point data: until I came along.

I have not included a description of the original tables in this blog; what I have done is create a set of tables and scripts to generate random data which are included in a download for you to run yourself. Also, to simplify the material for this article, I will not discuss how the original code handles surveys with more 3D points than Oracle’s Sdo_Geometry Sdo_Ordinate_Array can hold (which is ( 1048576 – 1 ) / 3 = 349,525 coordinates): all generated data will not break this limit.

The rest of this article describes the testing that was done testing different methods for the creation of single multi-point records from the base single point data. The article also shows how Oracle’s PL/SQL profiling package was used in improving the performance of the actual code.

Note that all timings in this article are based on Oracle’s DBMS_PROFILER package whose use will be described in another blog article.
Creating and Populating the base table.
The base table holding the individual survey points is declared as follows :

CREATE TABLE singlebeam_bathymetry ( 
 POINTNO     Integer,
 SB_ENO      Integer,
 DATETIME    Date,
 Geom        MDSYS.SDO_Geometry

Note, sb_eno is the enterprise identifier of a single singlebeam survey; the pointno is the survey point within a singlebeam survey – normally, sb_eno,pointno is the primary key but, for simplicity, the generated pointno will actually be unique across all surveys.

To populate this table we can do this:

Prompt Define some constants and variables ...
Prompt Create the data ...
   v_SRID number := 8311; -- GDA94
   v_MINX number := 105; 
   v_MAXX number := 179;
   v_MINY number := -70; 
   v_MAXY number := -5;
   v_MINZ number := -1000; 
   v_MAXZ number := 1000; 
    INTO singlebeam_bathymetry
  SELECT rownum,
         TRUNC(dbms_random.value(1,&&NUM_SB_ENOS. + 1)),
ALTER TABLE singlebeam_bathymetry LOGGING;

This data does not need to be indexed so this is not covered.

Target Tables

The target table that will hold our multipoint sdo_geometry objects is:

DROP TABLE singlebeam_mpoint;
CREATE TABLE singlebeam_mpoint (
  SB_ENO          NUMBER(8),

Generating the MultiPoint objects
Method One: Simple is Best

“Forget about the maneuvers. Go straight at them.” Admiral Horatio Nelson

Of course, in a perfect world, the generation of the multipoint sdo_geometries should be as simple as (the APPEND hint provides a more efficient “direct path” insert into the table):

INSERT /*+APPEND */ INTO singlebeam_mpoint
  FROM singlebeam_bathymetry a

However, except for small datasets, this method is slow (some numbers justifying this assertion will be provided later).

Method Two: “Appendix D”

The Oracle Spatial Team’s recommended approach for the most efficient use of the SDO_AGGR_UNION function is documented in Section D.4 of Appendix D of the Oracle® Spatial User’s Guide and Reference 10g Release 2 (10.2) documentation.

INSERT /*+APPEND */ INTO singlebeam_mpoint 
  FROM (SELECT sb_eno,
               SDO_AGGR_UNION(MDSYS.SDOAGGRTYPE(ageom,0.05)) ageom
          FROM (SELECT sb_eno,
                       SDO_AGGR_UNION(MDSYS.SDOAGGRTYPE(ageom,0.05)) ageom
                  FROM (SELECT sb_eno,
                               SDO_AGGR_UNION(MDSYS.SDOAGGRTYPE(ageom,0.05)) ageom
                          FROM (SELECT a.SB_ENO,
                                       SDO_AGGR_UNION(MDSYS.SDOAGGRTYPE(a.geom,0.05)) ageom
                                  FROM mv_singlebeam_bathymetry a
                                  GROUP BY a.SB_ENO, mod(rownum,16)
                          GROUP BY SB_ENO, mod (rownum, 8)
                  GROUP BY SB_ENO, mod (rownum, 4)
          GROUP BY SB_ENO, mod (rownum, 2)

The general idea here is to avoid expensive “context swapping” in the Oracle aggregation engine by implementing a form of “parallelisation”. The numbers presented later will show that this is effective but that it still does not perform fast enough compared to other methods.

Method Three: Pipelined Function

During my testing at GeoScience Australia I had the idea of using PL/SQL to generate the multi-points using a well-known, scalable, Oracle collection aggregation technique. The best way to describe this is by example in the following PL/SQL (the code includes a number of standard optimisations familiar to PL/SQL developers i.e., the use of CURSORs, BULK COLLECTion, FORALL “array” insertion, ROWTYPEs etc). It is the SQL statement that is of most interest and has been highlighted in the code.

This first method included the use of a pipelined function (pipelined function use will be covered in another blog) which turned an Sdo_Point object into an Sdo_Ordinate_Array object which could then be BULK COLLECTED in the highlighted SQL:

    PIPE ROW (p_point_geom.sdo_point.x);
    PIPE ROW (p_point_geom.sdo_point.y);
    PIPE ROW (p_point_geom.sdo_point.z);
  TYPE t_recs        IS TABLE OF singlebeam_mpoint%ROWTYPE INDEX BY PLS_INTEGER;
  v_mpoint_recs      t_recs;
  v_3D_ordinates     mdsys.sdo_ordinate_array;
  v_mpoint_rec       singlebeam_mpoint%ROWTYPE;
  v_rec_count        PLS_INTEGER := 0;
  v_commit           PLS_INTEGER := 50;
  CURSOR c_eno_list
     FROM singlebeam_bathymetry sbl
    GROUP BY sbl.SB_ENO;
  FOR rec IN c_eno_list LOOP
     SELECT b.*
       BULK COLLECT INTO v_3D_ordinates
       FROM singlebeam_bathymetry a,
            TABLE(asOrdinates(a.geom)) b
      WHERE a.sb_eno = rec.sb_eno
      ORDER BY a.pointno;
     v_mpoint_rec.sb_eno      := rec.sb_eno;
     v_mpoint_rec.pointcount  := rec.pointcount;
     v_mpoint_rec.geom_mpoint := MDSYS.SDO_GEOMETRY(3005,8311,NULL,
                                              1,1,v_mpoint_rec.pointcount ),
     v_rec_count := v_rec_count + 1;
     v_mpoint_recs(v_rec_count) := v_mpoint_rec;
     IF ( v_rec_count > v_commit ) THEN
        FORALL i IN 1..v_rec_count
          INSERT INTO singlebeam_mpoint VALUES v_mpoint_recs(i);
        v_rec_count := 0;
     END IF;
  IF ( v_rec_count > 0 ) THEN -- Write out any left over ...
     FORALL i IN 1..v_rec_count
       INSERT INTO singlebeam_mpoint VALUES v_mpoint_recs(i);
     v_rec_count := 0;

The BULK COLLECT aggregates the individual Sdo_Ordinate_Arrays returned by the function into a single array for the survey. What I discovered was this approach was substantially faster than the previous two methods.

After getting this to work, I then realised I didn’t need the Function as is described in the next method.

Method Four: PL/SQL SDO_Ordinate_Array aggregation

The next improvement to the algorithm was to get rid of the custom “asOrdinates” function in favour of using the Oracle Spatial type library directly.

     SELECT ords.*
       BULK COLLECT INTO v_3D_ordinates
       FROM singlebeam_bathymetry a,
                                           a.geom.sdo_point.z) ) ords
      WHERE a.sb_eno = rec.sb_eno
      ORDER BY a.pointno;

All the heavy lifting in this approach is done by directly referencing the individual ordinates coded in the geometry’s 3D Sdo_Point object and converting them into an Sdo_Ordinate_Array which can then be BULK COLLECTED into a resultant Sdo_Ordinate_Array.

This method was the fastest of all the methods used. However, because some surveys where larger than can be represented in an Sdo_Ordinate_Array, I did look at creating “custom” point arrays that did not have the million ordinate restriction that is in the MDSYS.SDO_ORDINATE_ARRAY object.

Method Five: Custom array of SDO_Point_Type

Another method using aggregation involved the use of a custom array of SDO_Point_Type as in the following PL/SQL. (One of the advantages of this approach is the fact that the aggregation SELECT statement can be used for all multi-point aggregation regardless as to whether the resulting object breaks the SDO_Ordinate_Array limit; a negative is the need to turn the array of SDO_Point_Type back into array bound “safe” SDO_Ordinate_Arrays on insert. But the method is illustrative of what can be done if one “thinks outside the square”.)

  TYPE t_recs           IS TABLE OF singlebeam_mpoint%ROWTYPE INDEX BY PLS_INTEGER;
  /* Create array of SDO_POINT_TYPE than is bigger than the ordinary SDO_ORDINATE_ARRAY and larger than max number points per survey */
  TYPE t_3D_Point_Array IS VARRAY(4194304) OF MDSYS.SDO_POINT_TYPE; 
  v_3D_Points           t_3D_Point_Array;
  v_mpoint_recs      t_recs;
  v_3D_ordinates     mdsys.sdo_ordinate_array;
  v_mpoint_rec       singlebeam_mpoint%ROWTYPE;
  v_index            PLS_INTEGER := 0;
  v_rec_count        PLS_INTEGER := 0;
  v_commit           PLS_INTEGER := 50;
  CURSOR c_eno_list
     FROM singlebeam_bathymetry sbl;
  FOR rec IN c_eno_list LOOP
     SELECT a.geom.sdo_point
       BULK COLLECT INTO v_3D_Points
       FROM singlebeam_bathymetry a
      ORDER BY a.pointno;
     /* To insert we need the data to be held in the SDO_Ordinate_Array */
     v_index := 0;
     FOR v_point IN v_3D_Points.FIRST..v_3D_Points.LAST LOOP
         v_index := v_index + 1;
         v_3D_ordinates(v_index) := v_3D_Points(v_point).X;
         v_index := v_index + 1;
         v_3D_ordinates(v_index) := v_3D_Points(v_point).Y;
         v_index := v_index + 1;
         v_3D_ordinates(v_index) := v_3D_Points(v_point).Z;
     END LOOP;
     v_mpoint_rec.sb_eno      := rec.sb_eno;
     v_mpoint_rec.pointcount  := rec.pointcount;
     v_mpoint_rec.geom_mpoint := MDSYS.SDO_GEOMETRY(3005,8311,NULL,
                                              1,1,v_mpoint_rec.pointcount ),
     v_rec_count := v_rec_count + 1;
     v_mpoint_recs(v_rec_count) := v_mpoint_rec;
     IF ( v_rec_count > v_commit ) THEN
        FORALL i IN 1..v_rec_count
          INSERT INTO singlebeam_mpoint VALUES v_mpoint_recs(i);
        v_rec_count := 0;
     END IF;
  IF ( v_rec_count > 0 ) THEN -- Write out any left over ...
     FORALL i IN 1..v_rec_count
       INSERT INTO singlebeam_mpoint VALUES v_mpoint_recs(i);
     v_rec_count := 0;


To ensure that the performance of all four methods can be compared, the first two methods were “wrapped” by PL/SQL such that the SELECT statement was executed once for each sb_eno. Also, the final methods went through a few different interations in the handling of those surveys whose points broke the SDO_Ordinate_Array limit (see the actual SQL for how this was handled).

The first chart shows how substantially different the “native” versus “custom” methods can be: one cannot see the actual timings for the “custom” methods:

Performance of Oracle Aggregate vs PL/SQL Methods

When one removes the “native” methods, one can see more clearly the relative performance of the “custom” methods.

Performance of Individual PL/SQL Methods


Replication of the full, 67 million row raw point data, to a 13,000 row multi-point table was achieved in less than 50 minutes of processing. This was a substantial improvement and as a result of the work GeoScience Australia moved to a database replication strategy that included the efficient generation of multi-point survey data from the raw observations. This also gives the end-user the ability to work with the actual points in a web application instead of the surrogate averaged linework resulting in more accurate analysis and data discovery.

The “take home message” for users of Oracle Spatial/Locator should look to the power of the whole database to solve data warehousing and analysis problems and not just the Spatial packages themselves.

A fully worked example showing the fastest method for you to try youself is available here

Creative Commons License

post this at del.icio.uspost this at Diggpost this at Technoratipost this at Redditpost this at Farkpost this at Yahoo! my webpost this at Windows Livepost this at Google Bookmarkspost this to Twitter

Comment [2]

I modified you sample for lat and long. When I do method 4 I get lat and long sometimes with lat first and sometimes with long first. I am I doing something wrong? Thanks Bill

Bill King · 19 May 2010, 18:39 · #

The code just aggregates what is in the sdo_point.

It is likely that some of your data has Lat in X and Long in Y and others Lat in Y and Long in X.

Check that this is the case (or not).


Simon · 24 May 2010, 07:59 · #