Go to content Go to navigation and search


Current Oracle Spatial Blog Articles

    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
    Implementation Of Travelling Salesman Problem
    Create Polygons and Linestrings From Bearings And Distances (COGO)
    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
    Applying And Extending Oracle Spatial - Book Released
    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
    Making Sdo Geometry Metadata Update Generic Code
    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.

Create Polygons and Linestrings From Bearings And Distances (COGO)

Saturday May 14 2016 at 17:39

Keywordspolygon cogo bearing distance create generate

Generate polygon from a set of bearings and distances ( Cogo )

My Coordinate Geometry (COGO) package has been around for a long time.

What I had not done was use the power of this package to construct polygons (2003) or linestrings (2002) from a starting point and a set of whole circle bearings (0-360) and distances.

The following article creates a data type to hold bearing and distances (COGO), and two functions, ST_PolygonFromCogo, and ST_LineFromCogo that take a staring point and a set of bearings and distances and construct a single polygon or linestring.


  1. --
  2. -- Create a suitable type for the bearings and distances
  3. --
  4. CREATE TYPE t_bearing_distance AS object (
  5.   sDegMinSec varchar2(4000),
  6.   nBearing   NUMBER,
  7.   distance   NUMBER,
  8.   Z          NUMBER
  9. );
  10. /
  11. SHOW errors
  12. --
  13. -- We need a collection of bearings and distances to create a polygon
  14. --
  15. CREATE TYPE tbl_bearing_distances AS TABLE OF t_bearing_distance;
  16. /
  17. SHOW errors
  18. -- Create a function that takes as input a starting point and a set of bearings and distances
  20. FUNCTION ST_PolygonFromCogo(p_start_point            IN sdo_geometry,
  21.                             p_bearings_and_distances IN tbl_bearing_distances,
  22.                             p_round_xy               IN INTEGER DEFAULT 3 )
  23.     RETURN sdo_geometry
  24. AS
  25.   v_geometry   sdo_geometry;
  26.   v_point      sdo_point_type;
  27.   v_next_point sdo_point_type;
  28.   v_round_xy   INTEGER := NVL(p_round_xy,3);
  29. BEGIN
  30.   IF ( p_start_point IS NULL OR p_start_point.sdo_gtype NOT IN (2001,3001) ) THEN
  31.     RETURN NULL;
  32.   END IF;
  33.   IF (p_bearings_and_distances IS NULL OR p_bearings_and_distances.COUNT = 0 ) THEN
  34.     RETURN NULL;
  35.   END IF;
  36.   v_point    := p_start_point.sdo_point;
  37.   v_geometry := sdo_geometry(2003,p_start_point.sdo_srid,NULL,sdo_elem_info_array(1,1003,1),sdo_ordinate_array(v_point.x,v_point.y));
  38.   FOR rec IN (SELECT CASE WHEN sDegMinSec IS NULL OR LENGTH(sDegMinSec)=0
  39.                           THEN nBearing
  40.                           ELSE COGO.DMS2DD(strDegMinSec=>sDegMinSec)
  41.                       END AS bearing,
  42.                      distance
  43.                 FROM TABLE(p_bearings_and_distances) t
  44.   )
  45.   Loop
  46.      v_next_point := COGO.PointFromBearingAndDistance(v_point.x, v_point.y, rec.bearing, rec.distance).sdo_point;
  47.      v_geometry.sdo_ordinates.extend(2);
  48.      v_geometry.sdo_ordinates(v_geometry.sdo_ordinates.count-1) := round(v_next_point.x,v_round_xy);
  49.      v_geometry.sdo_ordinates(v_geometry.sdo_ordinates.COUNT  ) := round(v_next_point.y,v_round_xy);
  50.      v_point := v_next_point;
  51.   END Loop;
  52.   -- Close polygon (if needed)
  53.   IF ( NOT (    v_next_point.X = p_start_Point.sdo_point.X
  54.             AND v_next_point.Y = p_start_Point.sdo_point.Y )
  55.       )
  56.   THEN
  57.       v_geometry.sdo_ordinates.extend(2);
  58.       v_geometry.sdo_ordinates(v_geometry.sdo_ordinates.count-1) := round(p_start_point.sdo_point.x,v_round_xy);
  59.       v_geometry.sdo_ordinates(v_geometry.sdo_ordinates.COUNT  ) := round(p_start_point.sdo_point.y,v_round_xy);
  60.   END IF;
  61.   RETURN v_geometry;
  62. END ST_PolygonFromCOGO;
  63. /
  64. SHOW errors
  65. --
  66. -- Now let's test it
  67. --
  68. SELECT sdo_geom.validate_geometry(poly,0.005) AS vPoly,
  69.        poly,
  70.        round(sdo_geom.sdo_area(poly,0.005),2) AS sqM,
  71.        SDO_GEOMETRY(2001,NULL,SDO_POINT_TYPE(2.004,1.685,NULL),NULL,NULL) AS point
  72.   FROM (SELECT ST_PolygonFromCogo(
  73.                       sdo_geometry(2001,NULL,sdo_point_type(0,3.5,NULL),NULL,NULL),
  74.                       tbl_bearing_distances(
  75.                           t_bearing_distance(NULL,180,3.5,NULL),
  76.                           t_bearing_distance(NULL,90,3.5,NULL),
  77.                           t_bearing_distance(NULL,0,3.5,NULL),
  78.                           t_bearing_distance(NULL,43.02,5.43,NULL),
  79.                           t_bearing_distance(NULL,270,9.5,NULL)))  AS poly
  80.           FROM dual) f;
  81. --
  82. -- Here is the result.
  83. --
  84. VPOLY POLY                                                                                                                                SQM
  85. ----- ----------------------------------------------------------------------------------------------------------------------------------- -----
  86. TRUE  SDO_GEOMETRY(2003,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,1003,1),SDO_ORDINATE_ARRAY(0,3.5,0,0,3.5,0,3.5,3.5,7.205,7.47,-2.295,7.47,0,3.5)) 38.06
  87. --
  88. -- Or another way to call....
  89. --
  90. WITH DATA AS (
  92.          SELECT bd
  93.            FROM (SELECT 1 AS rin, t_bearing_distance(NULL,180,3.5,NULL) AS bd                  FROM dual UNION ALL
  94.                  SELECT 2,        t_bearing_distance(NULL,90, 3.5,NULL)                        FROM dual UNION ALL
  95.                  SELECT 3,        t_bearing_distance(NULL, 0, 3.5,NULL)                        FROM dual UNION ALL
  96.                  SELECT 4,        t_bearing_distance(NULL,43.02,round(SQRT(4.5*4.5+2.2*4.2),2),NULL) FROM dual UNION ALL
  97.                  SELECT 5,        t_bearing_distance(NULL,270,(4.5+3.4+1.6),NULL)              FROM dual )
  98.                  ORDER BY rin
  99.        ) AS tbl_bearing_distances ) AS directions
  100.   FROM dual
  101. )
  102. SELECT sdo_geom.validate_geometry(poly,0.005) AS vPoly,
  103.        poly,
  104.        round(sdo_geom.sdo_area(poly,0.005),2) AS sqM
  105.   FROM (SELECT ST_PolygonFromCogo(
  106.                    p_start_point=>sdo_geometry(2001,NULL,sdo_point_type(0,3.5,NULL),NULL,NULL),
  107.                    p_bearings_and_distances=>a.directions,
  108.                    p_round_xy=>2
  109.                ) AS poly
  110.           FROM DATA a
  111.         );
  112. --
  113. -- Here is the result.
  114. --
  115. VPOLY POLY                                                                                                                            SQM
  116. ----- ------------------------------------------------------------------------------------------------------------------------------- ------
  117. TRUE  SDO_GEOMETRY(2003,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,1003,1),SDO_ORDINATE_ARRAY(0,3.5,0,0,3.5,0,3.5,3.5,7.2,7.47,-2.3,7.47,0,3.5)) 38.055
  118. --
  119. -- Finally, we can mix text descriptions of bearings with decimal degree values
  120. --
  121. SELECT sdo_geom.validate_geometry(poly,0.005) AS vPoly,
  122.        poly,
  123.        round(sdo_geom.sdo_area(poly,0.005),2) AS sqM
  124.   FROM (SELECT ST_PolygonFromCogo(
  125.                       sdo_geometry(2001,NULL,sdo_point_type(0,3.5,NULL),NULL,NULL),
  126.                       tbl_bearing_distances(
  127.                           t_bearing_distance(NULL,      180,3.5,NULL),
  128.                           t_bearing_distance('90^0''0"',NULL,3.5,NULL),
  129.                           t_bearing_distance(NULL,0,3.5,NULL),
  130.                           t_bearing_distance('43^01''12"',NULL,5.43,NULL),
  131.                           t_bearing_distance(NULL,270,9.5,NULL)))  AS poly
  132.           FROM dual) f;
  133. --
  134. -- Resulting in
  135. --
  136. VPOLY POLY                                                                                                                                SQM
  137. ----- ----------------------------------------------------------------------------------------------------------------------------------- -----
  138. TRUE  SDO_GEOMETRY(2003,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,1003,1),SDO_ORDINATE_ARRAY(0.0,3.5, 0.0,0.0, 3.5,0.0, 3.5,3.5, 7.205,7.47, -2.295,7.47, 0.0,3.5)) 38.06

I am considering added traverse adjustment to the creation of polygons.


Uses same types as ST_PolygonFromCogo.

  2. FUNCTION ST_LineFromCogo(p_start_point            IN sdo_geometry,
  3.                          p_bearings_and_distances IN tbl_bearing_distances,
  4.                          p_round_xy               IN INTEGER DEFAULT 3,
  5.                          p_round_z                IN INTEGER DEFAULT 2
  6.                         )
  7.     RETURN sdo_geometry
  8. AS
  9.   v_line       sdo_geometry;
  10.   v_point      sdo_point_type;
  11.   v_next_point sdo_point_type;
  12.   v_dims       pls_integer;
  13. BEGIN
  14.   IF ( p_start_point IS NULL OR p_start_point.sdo_gtype NOT IN (2001,3001) ) THEN
  15.     RETURN NULL;
  16.   END IF;
  17.   IF (p_bearings_and_distances IS NULL OR p_bearings_and_distances.COUNT = 0 ) THEN
  18.     RETURN NULL;
  19.   END IF;
  20.   v_point    := p_start_point.sdo_point;
  21.   v_dims     := p_start_point.get_dims();
  22.   IF ( v_dims NOT IN (2,3) ) THEN
  23.     RETURN NULL;
  24.   END IF;
  25.   -- Create basic line
  26.   v_line := sdo_geometry((v_dims*1000)+2,
  27.                          p_start_point.sdo_srid,
  28.                          NULL,
  29.                          sdo_elem_info_array(1,2,1),
  30.                          CASE v_dims
  31.                          WHEN 2 THEN sdo_ordinate_array(v_point.x,v_point.y)
  32.                          WHEN 3 THEN sdo_ordinate_array(v_point.x,v_point.y,v_point.z)
  33.                           END
  34.                         );
  35.   FOR rec IN (SELECT CASE WHEN sDegMinSec IS NULL OR LENGTH(sDegMinSec)=0
  36.                           THEN nBearing
  37.                           ELSE COGO.DMS2DD(strDegMinSec=>sDegMinSec)
  38.                       END AS bearing,
  39.                      distance,
  40.                      Z
  41.                 FROM TABLE(p_bearings_and_distances) t
  42.   )
  43.   Loop
  44.     v_next_point := COGO.PointFromBearingAndDistance(v_point.x, v_point.y, rec.bearing, rec.distance).sdo_point;
  45.     v_line.sdo_ordinates.extend(v_dims);
  46.     v_line.sdo_ordinates(v_line.sdo_ordinates.count-(v_dims-1)) := round(v_next_point.x,NVL(p_round_xy,3));
  47.     v_line.sdo_ordinates(v_line.sdo_ordinates.count-(v_dims-2)) := round(v_next_point.y,NVL(p_round_xy,3));
  48.     IF ( v_dims = 3 ) THEN
  49.       v_line.sdo_ordinates(v_line.sdo_ordinates.COUNT) := round(rec.z,NVL(p_round_z,2));
  50.     END IF;
  51.     v_point := v_next_point;
  52.   END Loop;
  53.   RETURN v_line;
  54. END ST_LineFromCOGO;
  55. /
  56. SHOW errors
  57. --
  58. -- One example will suffice
  59. --
  60. SELECT sdo_geom.validate_geometry(line,0.005) AS vLine,
  61.        line,
  62.        round(sdo_geom.sdo_length(line,0.005),2) AS distM
  63.   FROM (SELECT ST_LineFromCOGO(
  64.                  p_start_point =>sdo_geometry(3001,NULL,sdo_point_type(0,3.5,0),NULL,NULL),
  65.                  p_bearings_and_distances=>
  66.                       tbl_bearing_distances(
  67.                           t_bearing_distance(sDegMinSec=>NULL,          nBearing=>180.0,distance=>3.5,  z=>0.1),
  68.                           t_bearing_distance(sDegMinSec=>'90',          nBearing=> NULL,distance=>3.5,  z=>0.5),
  69.                           t_bearing_distance(sDegMinSec=>NULL,          nBearing=>  0.0,distance=>3.5,  z=>1.6),
  70.                           t_bearing_distance(sDegMinSec=>'43^01''21"',  nBearing=> NULL,distance=>5.43, z=>2.0),
  71.                           t_bearing_distance(sDegMinSec=>'270',         nBearing=> NULL,distance=>9.5,  z=>0.5),
  72.                           t_bearing_distance(sDegMinSec=>'149^58''6.3"',nBearing=> NULL,distance=>4.613,z=>0.1)),
  73.                  p_round_xy=>3,
  74.                  p_round_z=>2) AS line
  75.           FROM dual) f;
  76. --
  77. -- Result is
  78. --
  79. VLINE LINE                                                                                                                                                           DISTM
  80. ----- -------------------------------------------------------------------------------------------------------------------------------------------------------------- ------
  81. TRUE  SDO_GEOMETRY(3002,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,2,1),SDO_ORDINATE_ARRAY(0,3.5,0,0,0,0.1,3.5,0,0.5,3.5,3.5,1.6,7.205,7.47,2,-2.295,7.47,0.5,0.014,3.476,0.1)) 30.39

I hope these are of help 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