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.

Creating linestrings from points

Saturday May 12 2012 at 00:00

Keywordscast collect points linestrings group

There are many methods for creating linestrings from points. Here are two of many.

UPDATED: Fix in Order By and addition of second example.

Here are some examples of how to construct linestrings from points stored in tables in two different formats.

XYZ Values in SDO_POINT of SDO_GEOMETRY object

The following example creates SDO_GEOMETRY point objects (2D) and stores them in a table linepoints. These points described individual features (identified by the column feature_id). The points are correctly ordered by the point_id column.

  1. DROP   TABLE linepoints;
  2. -- Result
  3. TABLE LINEPOINTS dropped.
  4. CREATE TABLE linepoints AS
  5. SELECT CASE WHEN trunc(level / 7) = 0 THEN 398639 ELSE 2215595 END AS feature_id,
  6.        ROW_NUMBER() OVER (partition BY 1 ORDER BY rownum) AS point_id,
  7.        sdo_geometry(2001,NULL,
  8.                     sdo_point_type(round(dbms_random.VALUE(10000,19999),3),
  9.                                    round(dbms_random.VALUE(20000,29999),3),
  10.                                    NULL),NULL,NULL) AS pointGeom
  11.   FROM dual
  12. CONNECT BY LEVEL < 13;
  13. -- Result
  14. TABLE LINEPOINTS created.
  15. -- Select one feature's worth...
  16. SELECT a.feature_id, a.point_id, a.pointGeom FROM linepoints a WHERE a.feature_id = 398639;
  17. -- Results
  19. ---------- -------- --------------------------------------------------------------------------
  20. 398639     1        SDO_GEOMETRY(2001,NULL,SDO_POINT_TYPE(19674.833,22929.276,NULL),NULL,NULL)
  21. 398639     2        SDO_GEOMETRY(2001,NULL,SDO_POINT_TYPE(15015.917,26297.82,NULL),NULL,NULL)
  22. 398639     3        SDO_GEOMETRY(2001,NULL,SDO_POINT_TYPE(15917.343,28141.968,NULL),NULL,NULL)
  23. 398639     4        SDO_GEOMETRY(2001,NULL,SDO_POINT_TYPE(16322.858,20066.442,NULL),NULL,NULL)
  24. 398639     5        SDO_GEOMETRY(2001,NULL,SDO_POINT_TYPE(14752.7,26373.809,NULL),NULL,NULL)
  25. 398639     6        SDO_GEOMETRY(2001,NULL,SDO_POINT_TYPE(16154.323,25280.283,NULL),NULL,NULL)
  26. .
  27.  6 ROWS selected

Secondly, given this table linepoints here is how I would create linear (2002) sdo_geometry objects:

  1. SELECT feature_id,
  2.        substr(sdo_geom.validate_geometry(linestring,0.005),1,5) AS vLine,
  3.        linestring
  4.   FROM (SELECT c.feature_id,
  5.                mdsys.sdo_geometry(2002,NULL,NULL,
  6.                                   mdsys.sdo_elem_info_array(1,2,1),
  7.                                   CAST(MULTISET(SELECT b.COLUMN_VALUE
  8.                                                   FROM linepoints a,
  9.                                                        TABLE(mdsys.sdo_ordinate_array(a.pointGeom.sdo_point.x,
  10.                                                                                       a.pointGeom.sdo_point.y)) b
  11.                                                  WHERE a.feature_id = c.feature_id
  12.                                                  ORDER BY a.point_id, rownum)
  13.                                   AS mdsys.sdo_ordinate_array)) AS linestring
  14.           FROM linepoints c
  15.          GROUP BY c.feature_id
  16.          ORDER BY c.feature_id
  17.   ) f;
  18. -- Results
  20. ---------- ----- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  21.     398639 TRUE  SDO_GEOMETRY(2002,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,2,1),SDO_ORDINATE_ARRAY(11381.777,29179.921,15533.654,22883.293,14257.442,22215.899,18098.61,29694.596,19875.69,29429.811,16936.329,23639.846))
  22.    2215595 TRUE  SDO_GEOMETRY(2002,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,2,1),SDO_ORDINATE_ARRAY(19300.099,21279.124,14874.566,25400.551,11572.547,23158.262,15915.272,21011.269,19665.059,22354.97,16725.616,28165.866))

A good result.

Note that the ORDER BY a.point_id, rownum clause in the CAST(MULTISET(SELECT are needed to ensure correct ordering of the points and their X,Y values when being collected into the SDO_ORDINATE_ARRAY of the resultant linestring.

XYZ Values in Individual Columns

The following example assumes the ordinate data are in separate columns in a table rather than in the SDO_POINT_TYPE SDO_GEOMETRY structure.

Firstly let’s create some test data.

  1. DROP TABLE line_points;
  2. -- Result
  3. TABLE LINE_POINTS dropped.
  4. CREATE TABLE line_points AS
  5. SELECT trunc(level/5)+1 AS feature_id,
  6.        ROW_NUMBER() OVER (partition BY 1 ORDER BY rownum) AS point_id,
  7.        round(dbms_random.VALUE(10000,20000),3) AS x,
  8.        round(dbms_random.VALUE(30000,40000),3) AS y,
  9.        round(dbms_random.VALUE(-100,100),1)    AS z
  10.   FROM dual
  11. CONNECT BY LEVEL < 100;
  12. -- Result
  13. TABLE LINE_POINTS created.
  14. -- Select one feature's worth...
  15. SELECT a.feature_id, a.point_id, a.x, a.y, a.z FROM line_points a WHERE a.feature_id = 1;
  16. -- Result
  17. FEATURE_ID POINT_ID X         Y         Z
  18. ---------- -------- --------- --------- -----
  19. 1          1        14245.355 39103.198 -56.4
  20. 1          2        13611.439 39075.327 57.2
  21. 1          3        19677.775 32157.407 91.2
  22. 1          4        19148.487 39934.174 -7.8

Now let’s build the 3D linestring.

  1. SELECT feature_id,
  2.        substr(sdo_geom.validate_geometry(linestring,0.005),1,10) AS vLine,
  3.        linestring
  4.   FROM (SELECT a.feature_id,
  5.                mdsys.sdo_geometry(3002,NULL,NULL,
  6.                                   mdsys.sdo_elem_info_array(1,2,1),
  7.                                   CAST(MULTISET(SELECT b.COLUMN_VALUE
  8.                                                   FROM line_points b,
  9.                                                        TABLE(mdsys.sdo_ordinate_array(b.x,b.y,b.z)) b
  10.                                                  WHERE b.feature_id = a.feature_id
  11.                                                  ORDER BY b.point_id, rownum)
  12.                                       AS mdsys.sdo_ordinate_array)) AS LineString
  13.           FROM line_points a
  14.          GROUP BY a.feature_id
  15.          ORDER BY a.feature_id
  16.       ) f;
  17. -- Result
  19. ---------- ----- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  20.          1 TRUE  SDO_GEOMETRY(3002,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,2,1),SDO_ORDINATE_ARRAY(10834.967,30240.895,-15.2,11277.951,39088.68,-43.2,12186.848,38194.666,-83.7,16531.394,34999.423,38.7))
  21.          2 TRUE  SDO_GEOMETRY(3002,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,2,1),SDO_ORDINATE_ARRAY(12762.782,36113.949,84.6,10840.308,36369.348,64.1,18131.782,30854.663,-62.3,16727.793,39623.237,83.7,17867.121,34401.128,-86.3))
  22.          3 TRUE  SDO_GEOMETRY(3002,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,2,1),SDO_ORDINATE_ARRAY(17002.407,39531.838,-27.1,15454.69,33234.52,84.9,14253.969,33251.42,-52.4,19832.06,35341.975,-66.8,15774.957,35213.841,-55.5))
  23.          4 TRUE  SDO_GEOMETRY(3002,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,2,1),SDO_ORDINATE_ARRAY(12469.867,36990.592,-40.8,15568.675,33472.239,-32.8,10684.001,33537.21,0.2,19862.667,37460.655,90,12262.616,38702.088,-7.2))
  24.          5 TRUE  SDO_GEOMETRY(3002,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,2,1),SDO_ORDINATE_ARRAY(14928.194,38280.358,72.4,16483.747,37641.538,-71.4,10060.866,30785.363,65,19311.863,32594.842,-70.9,10887.833,36615.265,-68.3))
  25.          6 TRUE  SDO_GEOMETRY(3002,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,2,1),SDO_ORDINATE_ARRAY(10430.269,30601.649,56.9,14843.597,32296.468,-38.1,12545.397,38551.122,58.8,15699.728,36865.074,39.8,13140.555,37717.306,-61.3))
  26.          7 TRUE  SDO_GEOMETRY(3002,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,2,1),SDO_ORDINATE_ARRAY(13887.14,39182.163,-69.4,10998.301,36315.807,-40.3,13088.562,35835.823,20,10476.982,31781.73,91.7,14414.514,36456.508,-98.2))
  27.          8 TRUE  SDO_GEOMETRY(3002,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,2,1),SDO_ORDINATE_ARRAY(14945.188,31114.27,-17.2,12857.532,34706.264,95.1,12593.085,34808.013,45.1,12580.36,30627.921,2.9,15613.011,39624.302,7.2))
  28.          9 TRUE  SDO_GEOMETRY(3002,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,2,1),SDO_ORDINATE_ARRAY(13365.794,33948.006,99.4,19782.622,30310.164,78.2,14439.438,33976.395,-51,17582.394,30907.25,-52.3,17430.688,39258.111,75.1))
  29.         10 TRUE  SDO_GEOMETRY(3002,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,2,1),SDO_ORDINATE_ARRAY(13182.98,37490.586,93.3,12077.195,38557.26,-68.6,16749.617,35733.641,-49.5,19189.818,36467.5,96.2,16677.211,36611.312,18.8))
  30.         11 TRUE  SDO_GEOMETRY(3002,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,2,1),SDO_ORDINATE_ARRAY(18342.358,36454.356,95.7,15966.729,30259.604,-58.2,18496.083,38853.952,-91.3,12538.593,32527.582,-59.6,16522.237,30288.221,-20.7))
  31.         12 TRUE  SDO_GEOMETRY(3002,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,2,1),SDO_ORDINATE_ARRAY(18510.1,35776.065,-54,16918.603,34657.555,83.7,16716.77,32362.628,7.2,17884.163,32555.611,-91.7,19777.609,36459.833,38.4))
  32.         13 TRUE  SDO_GEOMETRY(3002,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,2,1),SDO_ORDINATE_ARRAY(14850.759,32781.795,-91.4,12234.918,33549.123,-76.7,14475.272,35926.771,62.2,19185.88,35721.572,-99.6,11686.912,38599.432,76.9))
  33.         14 TRUE  SDO_GEOMETRY(3002,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,2,1),SDO_ORDINATE_ARRAY(15535.712,35259.717,-69.8,14823.918,36108.42,-77.5,18995.265,33393.98,79.5,11298.053,36226.521,80.2,10199.336,35744.338,34.4))
  34.         15 TRUE  SDO_GEOMETRY(3002,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,2,1),SDO_ORDINATE_ARRAY(19013.37,33346.841,-67.3,10863.654,34773.51,21.5,13188.374,30997.509,24.3,12076.438,37695.98,-70,12316.767,38605.515,-34.9))
  35.         16 TRUE  SDO_GEOMETRY(3002,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,2,1),SDO_ORDINATE_ARRAY(18030.662,32252.482,52.4,16867.649,32708.082,73.3,11540.562,38772.874,97.1,15895.416,36148.811,80.2,19440.718,32434.254,85.9))
  36.         17 TRUE  SDO_GEOMETRY(3002,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,2,1),SDO_ORDINATE_ARRAY(17886.291,33488.643,85.5,19747.81,30049.534,-90.1,16094.872,34875.287,91.9,15060.472,37612.15,-40.9,13007.343,37140.685,-5.7))
  37.         18 TRUE  SDO_GEOMETRY(3002,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,2,1),SDO_ORDINATE_ARRAY(14382.043,37025.927,12.9,16596.284,38165.701,78.7,17673.999,31739.898,-9.7,16573.251,34908.787,89.9,10644.063,30304.372,44.2))
  38.         19 TRUE  SDO_GEOMETRY(3002,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,2,1),SDO_ORDINATE_ARRAY(15370.166,31074.666,-10.3,15488.603,31824.248,54.9,11992.719,38411.639,-30.4,12853.929,33091.134,97.3,10578.041,39874.991,97))
  39.         20 TRUE  SDO_GEOMETRY(3002,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,2,1),SDO_ORDINATE_ARRAY(13377.967,35922.605,16,15500.276,32491.701,-13.7,17942.919,37114.717,-16.5,13810.673,34459.542,67.9,18769.264,30391.873,-92.9))
  40. --
  41.  20 ROWS selected

Again, note that the ORDER BY a.point_id, rownum clause in the CAST(MULTISET(SELECT are needed to ensure correct ordering of the points and their X,Y values when being collected into the SDO_ORDINATE_ARRAY of the resultant linestring.

Both these methods are simple, direct and effective. And, in my view, superior to writing PL/SQL.

I hope this is of use to someone.

José Sánchez

The following is my solution to José’s example.

  1. WITH line_points AS (
  2.   SELECT 1 AS feature_id, 1 AS point_id,470532.39 AS X,145290.99 AS Y FROM dual UNION ALL
  3.   SELECT 1 AS feature_id, 2 AS point_id,470532.39 AS X,145290.99 AS Y FROM dual UNION ALL
  4.   SELECT 1 AS feature_id, 3 AS point_id,470532.38 AS X,145291.02 AS Y FROM dual UNION ALL
  5.   SELECT 1 AS feature_id, 4 AS point_id,470532.38 AS X,145291.06 AS Y FROM dual UNION ALL
  6.   SELECT 1 AS feature_id, 5 AS point_id,470532.43 AS X,145291.14 AS Y FROM dual UNION ALL
  7.   SELECT 1 AS feature_id, 6 AS point_id,470532.51 AS X,145291.20 AS Y FROM dual UNION ALL
  8.   SELECT 1 AS feature_id, 7 AS point_id,470532.59 AS X,145291.18 AS Y FROM dual UNION ALL
  9.   SELECT 1 AS feature_id, 8 AS point_id,470532.67 AS X,145291.14 AS Y FROM dual UNION ALL
  10.   SELECT 1 AS feature_id, 9 AS point_id,470532.68 AS X,145291.10 AS Y FROM dual UNION ALL
  11.   SELECT 1 AS feature_id, 10 AS point_id,470532.68 AS X,145291.07 AS Y FROM dual UNION ALL
  12.   SELECT 1 AS feature_id, 11 AS point_id,470532.56 AS X,145291.04 AS Y FROM dual UNION ALL
  13.   SELECT 1 AS feature_id, 12 AS point_id,470531.41 AS X,145291.25 AS Y FROM dual UNION ALL
  14.   SELECT 1 AS feature_id, 13 AS point_id,470238.55 AS X,145400.22 AS Y FROM dual UNION ALL
  15.   SELECT 1 AS feature_id, 14 AS point_id,470014.12 AS X,145887.29 AS Y FROM dual
  16. )
  17. SELECT feature_id,
  18.        substr(sdo_geom.validate_geometry_with_context(linestring,0.005),1,10) AS vLine,
  19.        linestring
  20.   FROM (SELECT a.feature_id,
  21.                sdo_util.Remove_Duplicate_Vertices(
  22.                  mdsys.sdo_geometry(2002,27700,NULL,
  23.                                     mdsys.sdo_elem_info_array(1,2,1),
  24.                                     CAST(MULTISET(SELECT b.COLUMN_VALUE
  25.                                                     FROM line_points b,
  26.                                                          TABLE(mdsys.sdo_ordinate_array(b.x,b.y)) b
  27.                                                    WHERE b.feature_id = a.feature_id
  28.                                                    ORDER BY b.point_id)
  29.                                         AS mdsys.sdo_ordinate_array)),
  30.                  0.005)
  31.                    AS LineString
  32.           FROM line_points a
  33.          GROUP BY a.feature_id
  34.          ORDER BY a.feature_id
  35.       ) f;
  36. -- Result
  38. ---------- ---------- -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  39.          1 TRUE       SDO_GEOMETRY(2002,27700,NULL,SDO_ELEM_INFO_ARRAY(1,2,1),SDO_ORDINATE_ARRAY(470532.39,145290.99,470532.38,145291.02,470532.38,145291.06,470532.43,145291.14,470532.51,145291.2,470532.59,145291.18,470532.67,145291.14,470532.68,145291.1,470532.68,145291.07,470532.56,145291.04,470531.41,145291.25,470238.55,145400.22,470014.12,145887.29))

Hopefully, that solves your problem.

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 [7]

Hello Simon,

using your above construction of linestrings from individual points I found faulty. I do convert some 10 to hundreds of points into a single linestring, depending on the sort the x and y values get intermingled, i.e. some points in the linestring have lat and long exchanged. Any ideas ?


— Kai Jancke · 11 May 2012, 13:27 · #

Hello !, achieve create the table with geometries , the issue is that does not recognize and then ask questions, something wrong geometry of its structure is wrong , and I think it is MDSYS . sdo_elem_info_array (1, 2, 1 ) failed to see them in autocad Maps and does not jump no mistake, this is proven to work ?

— Javier · 6 April 2016, 01:17 · #


The example presented by this blog article works.

You have obviously modified the final SQL statement to suite your data. You must have done this incorrectly. Is your data 2D and you are using the 3D sdo_gtype ie 3002.

Please send me the sql so that I can see what you are doing wrong.


— Simon Greener · 6 April 2016, 11:03 · #

I’ve tried this and it works, thanks.

My problem now is that some of the points I took for the experiment inevitably create self-intersecting lines and then Oracle tries closing the final point with the first one, but this is obviously a different issue than above.

— JOSE SANCHEZ · 30 March 2017, 20:11 · #

These are the points (a simplification of the whole array) I want to convert into a line:

1 470532.39 145290.99
2 470532.39 145290.99
3 470532.38 145291.02
4 470532.38 145291.06
5 470532.43 145291.14
6 470532.51 145291.20
7 470532.59 145291.18
8 470532.67 145291.14
9 470532.68 145291.10
10 470532.68 145291.07
11 470532.56 145291.04
12 470531.41 145291.25
13 470238.55 145400.22
14 470014.12 145887.29

The points are in British National Grid (27700) and the ID is the right order in which the subsequent line should be generated from. If you plot the points you will see how soon after starting building up the segments, the line self-intersects (i think this might be the issue behind but not completely sure, and either way don’t know how to control that by using SQL language). Then, Oracle creates a line, yes, but it joins up the last point with the starting one, so it closes itself.

Any ideas?

— JOSE SANCHEZ · 30 March 2017, 20:42 · #

Dear Sanchez,

I am stuck on how to implement a trajectory function which take a Moving_Point and returns an SDO_GEOMETRY of that object (Moving_Point) representing its projection to a 2D space.

I have already defined the ADT Moving_Point as follow:

  1. Xs, Ys and Xe, Ye represent the start and end positions for the segment at times ts, te respectively.


object(units UPOINT_V);

Here is a working function I implemented on a MovingGraphPoint which returns a Gline_V:

public static GLine_V trajectory (MGPoint_V mgp) { if (mgp == null) return null; try { int counter = 0; Section sects[] = new Section[mgp.getUnits().length()]; SortedSet sortedMGPoint = UtilityFunctions.getSpatialOrderedMGPoint(mgp); BigDecimal s, e; Integer rid, side;

Iterator it = sortedMGPoint.iterator(); UGPoint ugp = (UGPoint) it.next(); rid = ugp.getRid(); side = ugp.getSide(); s = ugp.getPos1(); e = ugp.getPos2(); if (! it.hasNext()) sects[counter++] = new Section(ugp.getRid(), ugp.getSide(), ugp.getPos1(), ugp.getPos2()); else { while (it.hasNext()) { ugp = (UGPoint) it.next(); if (rid.equals(ugp.getRid()) && side.equals(ugp.getSide()) && (s.compareTo(ugp.getPos1()) <= 0) && (e.compareTo(ugp.getPos1()) >= 0)) { if (e.compareTo(ugp.getPos2()) < 0) e = ugp.getPos2(); } else { sects[counter++] = new Section(rid, side, s, e); rid = ugp.getRid(); side = ugp.getSide(); s = ugp.getPos1(); e = ugp.getPos2(); } } sects[counter++] = new Section(rid, side, s, e); } Section finalSects[] = new Section[counter]; for (int i=0; i<counter; i++) finalSects[i] = sects[i]; return new GLine_V(mgp.getUnits().getElement(0).getNid(), new Sections_V(finalSects)); } catch (SQLException e) { e.printStackTrace(); } return null; }

— Yassine BAKHTI · 8 February 2020, 06:17 · #


I think I understand what you are trying to do.

My expertise is with PL/SQL (and Java), not C++ or

I can’t see where in you C++ code you use a database connection to create a point or point array.

I would suggest you create a connection and try to insert a upoint into a table of upoint. Then construct the line using pure SQL. Or write the components of the upoint into a table of four columns, then create the upoint and line.

Once you have this working you can evolve the C++ code to have it write the line more directly.


— Simon Greener · 8 February 2020, 11:31 · #