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.

Optimized Rectangle to 5 Point Polygon

Wednesday November 30 2016 at 19:06

Keywordsconvert polygon optimized rectangle 5 vertex

Article showing how to convert an polygon described by a single optimized rectangle to its 5 vertex equivalent.

Here is a function that takes a any polygon whose elements are defined by an optimized rectangle and converts all elements to 5 vertex polygons. Polygon may be multi; may have non-optimized rectangle components. Does NOT process geometries with compound elements.

  2. FUNCTION Rectangle2Polygon(p_geometry IN mdsys.sdo_geometry)
  3.   RETURN mdsys.sdo_geometry deterministic
  4. AS
  5.   v_dims             pls_integer;
  6.   v_vertices         mdsys.vertex_set_type;
  7.   v_ordinates        mdsys.sdo_ordinate_array  := NEW mdsys.sdo_ordinate_array(NULL);
  8.   v_elem_info        mdsys.sdo_elem_info_array := NEW mdsys.sdo_elem_info_array(NULL);
  9.   v_ord_count        pls_integer;
  10.   v_num_elements     pls_integer;
  11.   v_num_sub_elements pls_integer;
  12.   v_element          sdo_geometry;
  13.   -- A common utility function that allows for a single vertex
  14.   -- to be added to the end of an sdo_ordinate_array
  15.   -- All inserts are before existing vertex in same position
  16.   --
  17.   PROCEDURE appendVertex(p_ordinates IN OUT nocopy mdsys.sdo_ordinate_array,
  18.                          p_dims      IN INTEGER,
  19.                          p_x         IN NUMBER,
  20.                          p_y         IN NUMBER,
  21.                          p_z         IN NUMBER,
  22.                          p_w         IN NUMBER)
  23.   IS
  24.     v_ord pls_integer := 0;
  25.   BEGIN
  26.     IF ( p_ordinates IS NULL ) THEN
  27.       p_ordinates := NEW sdo_ordinate_array(0);
  28.       p_ordinates.DELETE;
  29.     END IF;
  30.     v_ord := p_ordinates.COUNT + 1;
  31.     p_ordinates.EXTEND(p_dims);
  32.     -- Insert first vertex
  33.     p_ordinates(v_ord)   := p_X;
  34.     p_ordinates(v_ord+1) := p_Y;
  35.     IF (p_dims>=3) THEN
  36.        p_ordinates(v_ord+2) := p_z;
  37.        IF ( p_dims > 3 ) THEN
  38.            p_ordinates(v_ord+3) := p_w;
  39.        END IF;
  40.     END IF;
  41.   END appendVertex;
  42.   FUNCTION hasOptimizedRectangles(p_geometry IN sdo_geometry)
  44.   IS
  45.     v_etype           pls_integer;
  46.     v_interpretation  pls_integer;
  47.     v_elements        pls_integer := 0;
  48.     v_rectangle_count NUMBER := 0;
  49.   BEGIN
  50.     IF ( p_geometry.get_gtype() IN (1,2,5,6) ) THEN
  51.       RETURN 0;
  52.     END IF;
  53.     v_elements := ( ( p_geometry.sdo_elem_info.COUNT / 3 ) - 1 );
  54.     <<element_extraction>>
  55.     FOR v_i IN 0 .. v_elements LOOP
  56.       v_etype          := p_geometry.sdo_elem_info(v_i * 3 + 2);
  57.       v_interpretation := p_geometry.sdo_elem_info(v_i * 3 + 3);
  58.       IF ( ( v_etype IN (1003,2003) AND
  59.              v_interpretation = 3 ) ) THEN
  60.          v_rectangle_count := v_rectangle_count + 1;
  61.       END IF;
  62.     END LOOP element_extraction;
  63.     RETURN v_rectangle_count;
  64.   END hasOptimizedRectangles;
  65.   FUNCTION hasCompoundElements(p_geometry IN sdo_geometry)
  67.   IS
  68.     v_etype           pls_integer;
  69.     v_elements        pls_integer := 0;
  70.   BEGIN
  71.     IF ( p_geometry.get_gtype() IN (1,2,5,6) ) THEN
  72.       RETURN 0;
  73.     END IF;
  74.     v_elements := ( ( p_geometry.sdo_elem_info.COUNT / 3 ) - 1 );
  75.     <<element_extraction>>
  76.     FOR v_i IN 0 .. v_elements LOOP
  77.       v_etype          := p_geometry.sdo_elem_info(v_i * 3 + 2);
  78.       IF ( v_etype IN (1005,2005) ) THEN
  79.          RETURN 1;
  80.       END IF;
  81.     END LOOP element_extraction;
  82.     RETURN 0;
  83.   END hasCompoundElements;
  84.   PROCEDURE processElement(p_geometry  IN sdo_geometry,
  85.                            p_ordinates IN OUT nocopy mdsys.sdo_ordinate_array,
  86.                            p_elem_info IN OUT nocopy mdsys.sdo_elem_info_array)
  87.   AS
  88.     v_vertices         mdsys.vertex_set_type;
  89.     v_interpretation   pls_integer;
  90.     v_etype            pls_integer;
  91.     v_start_coord      mdsys.vertex_type;
  92.     v_end_coord        mdsys.vertex_type;
  93.     v_temp_coord       mdsys.vertex_type;
  94.     v_num_sub_elements pls_integer;
  95.     v_sub_element      sdo_geometry;
  96.     v_dims             pls_integer;
  97.   BEGIN
  98.     -- Can only be called if p_geometry contains at least one optimized rectangle.
  99.     -- Count sub elements
  100.     v_dims := p_geometry.get_dims();
  101.     v_num_sub_elements := (p_geometry.sdo_elem_info.COUNT / 3);
  102.     FOR i IN 1..v_num_sub_elements loop
  103.       dbms_output.put_line('...... Processing Sub Element ' || i || ' of ' || v_num_sub_elements);
  104.       v_sub_element    := sdo_util.EXTRACT(p_geometry,1,i);
  105.       v_vertices       := sdo_util.getVertices(v_sub_element);
  106.       v_interpretation := v_sub_element.sdo_elem_info(3);
  107.       v_etype          := p_geometry.sdo_elem_info((i-1)*3+2);  /* Use Original etype 2003 for holes as extract turns holes into 1003 */
  108.       -- Update elem_info_array
  109.       p_elem_info.EXTEND(3);
  110.       p_elem_info(p_elem_info.COUNT-2) := v_ordinates.COUNT + 1;
  111.       -- If sub element not an optimized rectangle copy it through
  112.       IF ( v_interpretation <> 3 ) THEN
  113.         dbms_output.put_line('........ Skipping ' || i || ' sub element as is not optimized rectangle.');
  114.         -- Copy through elem_info and ordinates
  115.         p_elem_info(p_elem_info.COUNT-1) := v_sub_element.sdo_elem_info(2);
  116.         p_elem_info(p_elem_info.COUNT)   := v_sub_element.sdo_elem_info(3);
  117.         -- Now copy all ordinates
  118.         FOR i IN 1..v_vertices.COUNT Loop
  119.           appendVertex( p_ordinates, v_dims, v_vertices(i).x, v_vertices(i).y, v_vertices(i).z, v_vertices(i).w );
  120.         END Loop;
  121.         Continue;
  122.       END IF;
  123.       dbms_output.put_line('........ Converting Processing Found Optimized Rectangle.');
  124.       -- Change to optimized rectangle in to a 5 vertex polygon
  125.       p_elem_info(p_elem_info.COUNT-1) := CASE WHEN i=1 THEN v_sub_element.sdo_elem_info(2) ELSE 2003 END;
  126.       p_elem_info(p_elem_info.COUNT)   := 1;
  127.       v_start_coord := v_vertices(1);
  128.       v_end_coord   := v_vertices(2);
  129.       -- Check inverted only if first element
  130.       IF ( i = 1 ) THEN
  131.         v_temp_coord    := v_vertices(1);
  132.         v_temp_coord.x  := least(v_start_coord.x,v_end_coord.x);    
  133.         v_temp_coord.y  := least(v_start_coord.y,v_end_coord.y);    
  134.         v_end_coord.x   := greatest(v_start_coord.x,v_end_coord.x);    
  135.         v_end_coord.y   := greatest(v_start_coord.y,v_end_coord.y);    
  136.         v_start_coord.x := v_temp_coord.x;
  137.         v_start_coord.y := v_temp_coord.y;
  138.       END IF;
  139.       -- First coordinate
  140.       appendVertex( p_ordinates, v_dims, v_start_coord.x, v_start_coord.y, v_start_coord.z, v_start_coord.w );
  141.       -- Second coordinate
  142.       IF ( v_etype = 1003 ) THEN
  143.         appendVertex(p_ordinates,v_dims,v_end_coord.x,v_start_coord.y,(v_start_coord.z + v_end_coord.z) /2, v_start_coord.w);
  144.       ELSE
  145.         appendVertex(p_ordinates,v_dims,
  146.                      v_start_coord.x,
  147.                      v_end_coord.y,
  148.                      (v_start_coord.z + v_end_coord.z) /2,
  149.                      (v_end_coord.w - v_start_coord.w) * ((v_end_coord.x - v_start_coord.x) /
  150.                      ((v_end_coord.x - v_start_coord.x) + (v_end_coord.y - v_start_coord.y)) ));
  151.       END IF;
  152.       -- 3rd or middle coordinate
  153.       appendVertex(p_ordinates,v_dims,v_end_coord.x,v_end_coord.y,v_end_coord.z,v_end_coord.w);
  154.       -- 4th coordinate
  155.       IF ( v_etype = 1003 ) THEN
  156.         appendVertex(p_ordinates,v_dims,v_start_coord.x,v_end_coord.y,(v_start_coord.z + v_end_coord.z) /2,v_start_coord.w);
  157.       ELSE
  158.         appendVertex(p_ordinates,v_dims,v_end_coord.x,v_start_coord.y,(v_start_coord.z + v_end_coord.z) /2,
  159.             (v_end_coord.w - v_start_coord.w) * ((v_end_coord.x - v_start_coord.x) /
  160.            ((v_end_coord.x - v_start_coord.x) + (v_end_coord.y - v_start_coord.y)) ));
  161.       END IF;
  162.       -- Last coordinate
  163.       appendVertex(p_ordinates,v_dims,v_start_coord.x,v_start_coord.y,v_start_coord.z,v_start_coord.w);
  164.       dbms_output.put_line('........ Finishing Converting Optimized Rectangle.');
  165.     END Loop;
  166.   END processElement;    
  167. BEGIN
  168.     IF ( p_geometry IS NULL ) THEN
  169.       RETURN p_geometry;
  170.     END IF;
  171.     IF ( p_geometry.get_gtype() NOT IN (3,7) ) THEN
  172.       RETURN p_geometry;
  173.     END IF;
  174.     IF ( hasOptimizedRectangles(p_geometry) = 0 ) THEN
  175.       dbms_output.put_line('p_geometry does not have optimized rectangles.');
  176.       RETURN p_geometry;
  177.     END IF;
  178.     IF ( hasCompoundElements(p_geometry) <> 0 ) THEN
  179.       dbms_output.put_line('p_geometry has compound elements.');
  180.       RETURN p_geometry;
  181.     END IF;
  182.     v_ordinates.DELETE;
  183.     v_elem_info.DELETE;
  184.     v_dims         := p_geometry.get_dims();
  185.     v_num_elements := mdsys.sdo_util.getNumElem(p_geometry);
  186.     dbms_output.put_line('p_geometry has ' || v_num_elements || ' parts/elements.');
  187.     FOR elem IN 1..v_num_elements loop
  188.        v_element := mdsys.sdo_util.EXTRACT(p_geometry,elem,0);
  189.        dbms_output.put_line('... Extracted part/element ' || elem || ' has ' || hasOptimizedRectangles(v_element) || ' optimized rectangles.');
  190.        IF ( hasOptimizedRectangles(v_element) <> 0 ) THEN
  191.          processElement(v_element,v_ordinates,v_elem_info);
  192.        ELSE
  193.          -- Append
  194.         dbms_output.put_line('........ Appending non-optimized rectangle geometry to output geometry ');
  195.         v_num_sub_elements := (v_element.sdo_elem_info.COUNT / 3);    
  196.         v_ord_count := v_ordinates.COUNT;
  197.         FOR i IN 1..v_num_sub_elements loop
  198.           v_elem_info.EXTEND(3);
  199.           v_elem_info(v_elem_info.COUNT-2) := CASE WHEN i=1 THEN v_ord_count+1 ELSE v_element.sdo_elem_info(i) + v_ord_count END;
  200.           v_ord_count := v_elem_info(v_elem_info.COUNT-2);
  201.           v_elem_info(v_elem_info.COUNT-1) := v_element.sdo_elem_info(i+1);
  202.           v_elem_info(v_elem_info.COUNT  ) := v_element.sdo_elem_info(i+2);
  203.         END Loop;
  204.         v_vertices       := sdo_util.getVertices(v_element);
  205.         -- Now copy all ordinates
  206.         FOR i IN 1..v_vertices.COUNT Loop
  207.           appendVertex( v_ordinates, v_dims, v_vertices(i).x, v_vertices(i).y, v_vertices(i).z, v_vertices(i).w );
  208.         END Loop;
  209.        END IF;
  210.     END LOOP;
  211.     RETURN mdsys.sdo_geometry(p_geometry.sdo_gtype,p_geometry.sdo_srid,NULL,v_elem_info,v_ordinates);
  212. END Rectangle2Polygon;
  213. /
  214. SHOW errors

Now let’s test the function:

  1. -- Test
  2. SET serveroutput ON SIZE unlimited
  3. WITH tGeoms AS (
  4.   SELECT 'Single rectangle no hole'      AS description, sdo_geometry(2003,NULL,NULL,sdo_elem_info_array(1,1003,3),sdo_ordinate_array(0,0,1,1)) AS geom FROM dual UNION ALL
  5.   SELECT 'Inverted single rectangle'     AS description, sdo_geometry(2003,NULL,NULL,sdo_elem_info_array(1,1003,3),sdo_ordinate_array(1,1,0,0)) AS geom FROM dual UNION ALL
  6.   SELECT 'Rectangle with rectangle hole' AS description, SDO_GEOMETRY(2003,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,1003,3, 5,2003,3),SDO_ORDINATE_ARRAY(0,0, 100,100, 40,40, 60,60)) AS geom FROM dual UNION ALL
  7.   SELECT 'Compound polygon'              AS description, SDO_GEOMETRY(2003,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,1005,2, 1,2,1, 7,2,2),SDO_ORDINATE_ARRAY(10.0,128.0, 10.0,125.0, 20.0,125.0, 20.0,128.0, 15.0,130.0, 10.0,128.0)) AS geom FROM dual UNION ALL
  8.   SELECT 'Single Circle element'         AS description, SDO_GEOMETRY(2003,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,1003,4),SDO_ORDINATE_ARRAY(15.0,145.0, 10.0,150.0, 20.0,150.0)) AS geom FROM dual UNION ALL
  9.   SELECT 'No rectangle elements'         AS description, SDO_GEOMETRY(2007,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,1003,1, 11,2003,1, 31,1003,1),SDO_ORDINATE_ARRAY(50.0,168.0, 50.0,160.0, 55.0,160.0, 55.0,168.0, 50.0,168.0, 51.0,167.0, 54.0,167.0, 54.0,161.0, 51.0,161.0, 51.0,162.0, 52.0,163.0, 51.0,164.0, 51.0,165.0, 51.0,166.0, 51.0,167.0, 52.0,166.0, 52.0,162.0, 53.0,162.0, 53.0,166.0, 52.0,166.0)) AS geom FROM dual UNION ALL
  10.   SELECT 'Multi-Polygon one rectangle'   AS description, SDO_GEOMETRY(2007,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,1003,1, 11,1003,3),SDO_ORDINATE_ARRAY(50.0,105.0, 55.0,105.0, 60.0,110.0, 50.0,110.0, 50.0,105.0, 62.0,108.0, 65.0,112.0)) AS geom FROM dual UNION ALL
  11.   SELECT 'MultiPolygon All Rectangles'   AS description, SDO_GEOMETRY(2007,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,1003,3, 5,2003,3, 9,1003,3),SDO_ORDINATE_ARRAY(0,0,100,100, 40,40,60,60, 200,200,300,300)) AS geom FROM dual
  12. )
  13. SELECT f.description,
  14.        f.validOriginalGeom,
  15.        sdo_geom.validate_geometry(f.poly,0.005) AS vGeom,
  16.        f.poly
  17.   FROM (SELECT a.description,
  18.                sdo_geom.validate_geometry(a.geom,0.005) AS validOriginalGeom,
  19.                rectangle2polygon(a.geom) AS poly
  20.           FROM tGeoms a
  21.        ) f;

Results are as follows:

Single rectangle no hole TRUE TRUE SDO_GEOMETRY(2003,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,1003,1),SDO_ORDINATE_ARRAY(0,0,1,0,1,1,0,1,0,0))
Inverted single rectangle TRUE TRUE SDO_GEOMETRY(2003,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,1003,1),SDO_ORDINATE_ARRAY(0,0,1,0,1,1,0,1,0,0))
Rectangle with rectangle hole TRUE TRUE SDO_GEOMETRY(2003,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,1003,1,11,2003,1),SDO_ORDINATE_ARRAY(0,0,100,0,100,100,0,100,0,0,40,40,40,60,60,60,60,40,40,40))
Compound polygon TRUE TRUE SDO_GEOMETRY(2003,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,1005,2,1,2,1,7,2,2),SDO_ORDINATE_ARRAY(10,128,10,125,20,125,20,128,15,130,10,128))
Single Circle element TRUE TRUE SDO_GEOMETRY(2003,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,1003,4),SDO_ORDINATE_ARRAY(15,145,10,150,20,150))
No rectangle elements TRUE TRUE SDO_GEOMETRY(2007,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,1003,1,11,2003,1,31,1003,1),SDO_ORDINATE_ARRAY(50,168,50,160,55,160,55,168,50,168,51,167,54,167,54,161,51,161,51,162,52,163,51,164,51,165,51,166,51,167,52,166,52,162,53,162,53,166,52,166))
Multi-Polygon one rectangle TRUE TRUE SDO_GEOMETRY(2007,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,1003,1,11,1003,1),SDO_ORDINATE_ARRAY(50,105,55,105,60,110,50,110,50,105,62,108,65,108,65,112,62,112,62,108))
MultiPolygon All Rectangles TRUE TRUE SDO_GEOMETRY(2007,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,1003,1,11,2003,1,21,1003,1),SDO_ORDINATE_ARRAY(0,0,100,0,100,100,0,100,0,0,40,40,40,60,60,60,60,40,40,40,200,200,300,200,300,300,200,300,200,200))

I hope this is of use to someone.

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