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.

Compute Location from known Lat/Long point using delta easting and northing in miles

Saturday May 12 2012 at 17:46

Keywordscompute new point delta easting northing longitude latitude point
Summary

This article shows how to compute a new point location when all you have is a known or existing point in longitude/latitude (geographic/geodetic) and a delta easting and northing value in miles (or in other units).

Over on the Oracle Spatial Forum at the Technology Network, Wojciech asked the following question:

My problem looks that way:
lets say we have a point:

 8307, -- SDO_SRID
 SDO_ORDINATE_ARRAY
 (
 -77, -- Longitude
 39 -- Latitude
 )

I want to get a location of a point 50 miles east and 30 miles north from the point presented here.

Do you know how to achieve this?

There is a function in the SDO_UTIL package – SDO_UTIL.POINT_AT_BEARING -that allows you to create a new point at a bearing and distance from a known point, but there is no function that allows one to compute a new point from an existing point using a delta distance in x and y.

Obviously for projected data one can do this via Pythagoras on the X and Y ordinates but for geographic/geodetic there is no method available to do this accurately. You have to write it yourself.

Which is what this blog article is all about.

Approach

Now, the function presented will only generate an approximate location for the new point but it may suffice for some.

The approach requires us to convert miles into decimal degrees in both longitude and latitude at the actual location of the known point. Why? Because the length subtended by a degree of longitude changes as you move from the Equator to the Pole. And at any one point on the globe that length in longitude is different from that in latitude.

So, how can I compute this value. The following method takes the starting point and creates two points a whole number of degrees of longitude and latitude away from it (in the case below 0.1 degrees). Then, Oracle’s sdo_geom.sdo_distance method is used to find out how many miles each of these decimal degree values subtends.

  1. WITH longLat AS (
  2.   SELECT sdo_geometry(2001,8307,sdo_point_type(-77.0     ,39.0,NULL),NULL,NULL) AS pt1,
  3.          sdo_geometry(2001,8307,sdo_point_type(-77.0+unit,39.0,NULL),NULL,NULL) AS pt2,
  4.          sdo_geometry(2001,8307,sdo_point_type(-77.0     ,39.0+unit,NULL),NULL,NULL) AS pt3
  5.     FROM (SELECT 0.1 AS unit FROM dual)
  6. )
  7. SELECT sdo_geom.sdo_distance(l.pt1,l.pt2,0.005,'unit=MILE') AS unitLongMiles,
  8.        sdo_geom.sdo_distance(l.pt1,l.pt3,0.005,'unit=MILE') AS unitLatMiles
  9.   FROM longLat l;
  10. -- Results
  11. UNITLONGMILES    UNITLATMILES
  12. ---------------- ----------------
  13. 5.38271491729028 6.89824146515357

OK, now that we know the distances in miles we can compute the number of degrees of longitude and latitude covered by the required delta of 50 miles east and 30 miles north from that point.

  1. WITH longLat AS (
  2.   SELECT sdo_geometry(2001,8307,sdo_point_type(-77.0     ,39.0,NULL),NULL,NULL) AS pt1,
  3.          sdo_geometry(2001,8307,sdo_point_type(-77.0+unit,39.0,NULL),NULL,NULL) AS pt2,
  4.          sdo_geometry(2001,8307,sdo_point_type(-77.0     ,39.0+unit,NULL),NULL,NULL) AS pt3,
  5.          50.0 AS eastMile,
  6.          30.0 AS northMile,
  7.          unit
  8.     FROM (SELECT 0.1 AS unit FROM dual)
  9. )
  10. SELECT round(sdo_geom.sdo_distance(l.pt1,l.pt2,0.005,'unit=MILE'),9) AS unitLongMiles,
  11.        round(sdo_geom.sdo_distance(l.pt1,l.pt3,0.005,'unit=MILE'),9) AS unitLatMiles,
  12.        round(l.eastMile /sdo_geom.sdo_distance(l.pt1,l.pt2,0.005,'unit=MILE') * l.unit,9) AS eastDeltaInDegrees,
  13.        round(l.northMile/sdo_geom.sdo_distance(l.pt1,l.pt3,0.005,'unit=MILE') * l.unit,9) AS northDeltaInDegrees
  14.   FROM longLat l;
  15. -- Results
  16. UNITLONGMILES UNITLATMILES EASTDELTAINDEGREES NORTHDELTAINDEGREES
  17. ------------- ------------ ------------------ -------------------
  18. 5.382714917   6.898241465  0.928899278        0.434893446

We can compute the new point via the following SQL:

  1. WITH longLat AS (
  2.   SELECT sdo_geometry(2001,8307,sdo_point_type(-77.0     ,39.0,NULL),NULL,NULL) AS pt1,
  3.          sdo_geometry(2001,8307,sdo_point_type(-77.0+unit,39.0,NULL),NULL,NULL) AS pt2,
  4.          sdo_geometry(2001,8307,sdo_point_type(-77.0     ,39.0+unit,NULL),NULL,NULL) AS pt3,
  5.          50.0 AS eastMile,
  6.          30.0 AS northMile,
  7.          unit
  8.     FROM (SELECT 0.1 AS unit FROM dual)
  9. )
  10. SELECT f.pt1,f.pt2,
  11.        ROUND(sdo_geom.sdo_distance(f.pt1,f.pt2,0.005,'unit=MILE'),3) AS dist,
  12.        ROUND(SQRT(50.0*50.0+30.0*30.0),3) AS roughDistCheck
  13.   FROM (SELECT b.pt1,
  14.                sdo_geometry(2001,8307,
  15.                             sdo_point_type(ROUND(b.pt1.sdo_point.x+b.longDelta,8),
  16.                                            ROUND(b.pt1.sdo_point.y+b.latDelta,8),NULL),
  17.                             NULL,NULL) AS pt2
  18.           FROM (SELECT round(l.eastMile/sdo_geom.sdo_distance(l.pt1, l.pt2,0.005,'unit=MILE')*l.unit,9) AS longDelta,
  19.                        round(l.northMile/sdo_geom.sdo_distance(l.pt1,l.pt3,0.005,'unit=MILE')*l.unit,9) AS latDelta,
  20.                        l.pt1
  21.                   FROM longLat l
  22.               ) b
  23.       ) f;
  24. -- Results
  25. PT1                                                           PT2                                                                             DIST   ROUGHDISTCHECK
  26. ------------------------------------------------------------- ------------------------------------------------------------------------------- ------ --------------
  27. SDO_GEOMETRY(2001,8307,SDO_POINT_TYPE(-77,39,NULL),NULL,NULL) SDO_GEOMETRY(2001,8307,SDO_POINT_TYPE(-76.07110072,39.43489345,NULL),NULL,NULL) 58.178 58.31

Now, let’s put this all together into a function.

  1. CREATE OR REPLACE
  2. FUNCTION pointFromDelta(p_geometry IN mdsys.sdo_geometry,
  3.                         p_delta_x  IN NUMBER,
  4.                         p_delta_y  IN NUMBER,
  5.                         p_unit_deg IN NUMBER,
  6.                         p_units    IN varchar2 := NULL,
  7.                         p_tolerance IN NUMBER  := 0.05,
  8.                         p_round_factor IN pls_integer := 9)
  9. RETURN sdo_geometry deterministic
  10. AS
  11.   v_pt           mdsys.sdo_geometry := p_geometry;
  12.   v_ptX          mdsys.sdo_geometry;
  13.   v_ptY          mdsys.sdo_geometry;
  14.   v_round_factor NUMBER :=  NVL(p_round_factor,9);
  15.   v_units        varchar2(100) := CASE WHEN p_units IS NOT NULL
  16.                                        THEN CASE WHEN INSTR(UPPER(p_units),'UNIT=')=0
  17.                                                  THEN 'UNIT=' || UPPER(p_units)
  18.                                                  ELSE p_units
  19.                                              END
  20.                                    END;
  21. BEGIN
  22.    IF (p_geometry IS NULL OR p_geometry.sdo_point IS NULL OR p_unit_deg IS NULL) THEN
  23.       RETURN p_geometry;
  24.    END IF;
  25.   v_pt  := NEW mdsys.sdo_geometry(p_geometry.sdo_gtype,
  26.                                   p_geometry.sdo_srid,
  27.                                   sdo_point_type(p_geometry.sdo_point.x,
  28.                                                  p_geometry.sdo_point.y,
  29.                                                  NULL),
  30.                                   NULL,NULL);
  31.   v_ptX := NEW mdsys.sdo_geometry(p_geometry.sdo_gtype,
  32.                                   p_geometry.sdo_srid,
  33.                                   sdo_point_type(p_geometry.sdo_point.x+p_unit_deg,
  34.                                                  p_geometry.sdo_point.y,
  35.                                                  NULL),
  36.                                   NULL,NULL);
  37.   v_ptY := NEW mdsys.sdo_geometry(p_geometry.sdo_gtype,
  38.                                   p_geometry.sdo_srid,
  39.                                   sdo_point_type(p_geometry.sdo_point.x,
  40.                                                  p_geometry.sdo_point.y+p_unit_deg,
  41.                                                  NULL),
  42.                                   NULL,NULL);
  43.    RETURN CASE WHEN v_units IS NULL
  44.                THEN sdo_geometry(2001,
  45.                        p_geometry.sdo_srid,
  46.                        sdo_point_type(ROUND(v_pt.sdo_point.x+(p_delta_x/sdo_geom.sdo_distance(v_pt,v_ptX,p_tolerance)*p_unit_deg),v_round_factor),
  47.                                       ROUND(v_pt.sdo_point.y+(p_delta_y/sdo_geom.sdo_distance(v_pt,v_ptY,p_tolerance)*p_unit_deg),v_round_factor),
  48.                                       NULL),
  49.                        NULL,NULL)
  50.                ELSE sdo_geometry(2001,
  51.                        p_geometry.sdo_srid,
  52.                        sdo_point_type(ROUND(v_pt.sdo_point.x+(p_delta_x/sdo_geom.sdo_distance(v_pt,v_ptX,p_tolerance,v_units)*p_unit_deg),v_round_factor),
  53.                                       ROUND(v_pt.sdo_point.y+(p_delta_y/sdo_geom.sdo_distance(v_pt,v_ptY,p_tolerance,v_units)*p_unit_deg),v_round_factor),
  54.                                       NULL),
  55.                        NULL,NULL)
  56.           END;
  57. END pointFromDelta;
  58. /
  59. FUNCTION pointFromDelta compiled
  60. SHOW errors
  61. No Errors.

Testing this we get:

  1. WITH pt AS (
  2.   SELECT sdo_geometry(2001,8307,sdo_point_type(-77,39,NULL),NULL,NULL) AS pt,
  3.          0.1 AS unit
  4.     FROM dual
  5. )
  6. SELECT p.pt,
  7.        pointFromDelta(p.pt,50.0,30.0,p.unit,'UNIT=MILE',0.05,9),
  8.        ROUND(sdo_geom.sdo_distance(p.pt,
  9.                                    pointFromDelta(p.pt,50.0,30.0,p.unit,'UNIT=MILE',0.05,9),
  10.                                    0.005,'unit=MILE'),4) AS dist,
  11.        ROUND(SQRT(50.0*50.0+30.0*30.0),4) AS roughDistCheck
  12.   FROM pt p;
  13. -- Results
  14. PT                                                            POINTFROMDELTA(P.PT,50.0,30.0,0.1,'UNIT=MILE',0.05,9)                             DIST   ROUGHDISTCHECK
  15. ------------------------------------------------------------- --------------------------------------------------------------------------------- ------ --------------
  16. SDO_GEOMETRY(2001,8307,SDO_POINT_TYPE(-77,39,NULL),NULL,NULL) SDO_GEOMETRY(2001,8307,SDO_POINT_TYPE(-76.071100722,39.434893446,NULL),NULL,NULL) 58.178 58.3095

Don’t forget that the ROUGHDISTCHECK is just a Pythagoras of the delta east and north on a flat plane. This approach isn’t all that accurate.

Alternate method using SDO_UTIL POINT_AT_BEARING

There is an alternate approach that writing this article reminded me was possible. And that is to use the SDO_UTIL.POINT_AT_BEARING function as follows:

  1. WITH longLat AS (
  2.   SELECT sdo_geometry(2001,8307,sdo_point_type(-77.0,39.0,NULL),NULL,NULL) AS pt1,
  3.          50.0 AS eastMile,
  4.          30.0 AS northMile,
  5.          unit
  6.     FROM (SELECT 0.1 AS unit FROM dual)
  7. )
  8. SELECT f.pt2,
  9.        ROUND(sdo_geom.sdo_distance(f.pt1,f.pt2,0.005,'unit=MILE'),3) AS dist
  10.   FROM (SELECT l.pt1,
  11.                SDO_UTIL.POINT_AT_BEARING(
  12.                         SDO_UTIL.POINT_AT_BEARING(l.pt1,
  13.                                                   DECODE(SIGN(l.eastMile),1.570796326794895,0,-1.570796326794895),
  14.                                                   codesys.geom.Convert_Unit( 'MILE',l.eastMile, 'Meter')),
  15.                                          DECODE(SIGN(l.northMile),3.14159265358979,0,-3.14159265358979),
  16.                                          codesys.geom.Convert_Unit('MILE',l.northMile, 'Meter')) AS pt2
  17.          FROM longLat l
  18.       ) f;
  19. -- Results
  20. PT2                                                                                                            DIST
  21. -------------------------------------------------------------------------------------------------------------- ------
  22. SDO_GEOMETRY(2001,8307,NULL,SDO_ELEM_INFO_ARRAY(1,1,1),SDO_ORDINATE_ARRAY(-77.9288670026001,38.5613888205277)) 58.571

That is, go either 90 degrees to the East for +Eastings and 0 degrees to the North for +northings (or -90/270 for -Eastings and 180 degrees south for -northings).

Note that the distance difference between the two methods is 58.178 for my method and 58.571 for the method using the Oracle function. Knowing that a distance in longitude changes by latitude this may be affecting the second method as the conversion routine from Miles to Meters (SDO_UTIL.POINT_AT_BEARING requires distances in Meters) will not take this into account.

Something could be done using spherical or spheroidal math, but you would have to approach me directly to see if I would be interested coding something.

Anyway, 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