Go to content Go to navigation and search

Home

Current Oracle Spatial Blog Articles


Search

Browse

RSS / Atom

Email me

textpattern

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.

How to calculate cumulative length of a linestring

Tuesday July 17 2012 at 21:44

Keywordsvectorize,cumulative length,linestring,getVertices,Oracle Spatial
Summary

Calculating cumulative length of a linestring (or polygon ring) is fairly straightforwards as this article shows.

Sometimes one needs to be able to calculate the cumulative length of a linestring. That is, the contribution of the vertex-to-vertex segments of a linestring to its total length.

This can be achieved by two different methods.

The first uses ordinary Locator functionality as follows:

  1. SELECT segment_id, segment_length,
  2.        SUM(segment_length) OVER (partition BY 1 ORDER BY segment_id) AS cumulative_length
  3. FROM (
  4. SELECT segment_id,
  5.        sdo_geom.sdo_distance(
  6.             sdo_geometry(2001,8307,sdo_point_type(f.x1,f.y1,NULL),NULL,NULL),
  7.             sdo_geometry(2001,8307,sdo_point_type(f.x2,f.y2,NULL),NULL,NULL),0.05) AS segment_length
  8.   FROM (SELECT v.id AS segment_id,
  9.                v.x AS x1,v.y AS y1,
  10.                lead(v.x,1) OVER (ORDER BY v.id) AS x2,
  11.                lead(v.y,1) OVER (ORDER BY v.id) AS y2
  12.           FROM TABLE(sdo_util.getvertices(
  13.                     SDO_GEOMETRY(2002,8307,NULL,
  14.                             SDO_ELEM_INFO_ARRAY(1,2,1),
  15.                             SDO_ORDINATE_ARRAY(2.543362, 49.019445,
  16.                                                2.543362, 49.01940,
  17.                                                2.543037, 49.01935)))) v
  18.       ) f
  19.   WHERE f.x2 IS NOT NULL AND f.y2 IS NOT NULL
  20.   ) g;
  21. -- Results
  22. --
  23. SEGMENT_ID SEGMENT_LENGTH CUMULATIVE_LENGTH
  24. ---------- -------------- -----------------
  25.          1 5.00479112866628  5.00479112866628
  26.          2 24.4133261026822 29.41811723134848

The other method is to use my GetVector or ST_Vectorize functions as follows:

  1. SELECT segment_id, segment_length,
  2.        SUM(segment_length) OVER (partition BY 1 ORDER BY segment_id) AS cumulative_length
  3.   FROM (SELECT v.id AS segment_id,
  4.                sdo_geom.sdo_length(v.AsSdoGeometry(8307),0.005) AS segment_length
  5.           FROM TABLE(LINEAR.ST_Vectorize(
  6.                               SDO_GEOMETRY(2002,8307,NULL,
  7.                                       SDO_ELEM_INFO_ARRAY(1,2,1),
  8.                                       SDO_ORDINATE_ARRAY(2.543362, 49.019445,
  9.                                                          2.543362, 49.01940,
  10.                                                          2.543037, 49.01935)))) v
  11.       );
  12. -- Results
  13. --
  14. SEGMENT_ID SEGMENT_LENGTH CUMULATIVE_LENGTH
  15. ---------- -------------- -----------------
  16.          1 5.00479112866628  5.00479112866628
  17.          2 24.4133261026822 29.41811723134848

I hope this is 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

Comment