简体   繁体   中英

Calculate distance between geo points using sql oracle

who know and can demonstrate how to calculate distance using Pythagoras theorem (SQL oracle). table:

station_id        station_geom
395               POINT(-121.906834244728 37.3608541552538)
378               POINT(-121.890799999237 37.3477445655325)
312               POINT(-121.901782 37.329732)

Desire output:

station_id_start  station_id_end    distance 
395               378               2,23682150368419

honestly, what I did wasn't too smart: I separate column station_geom in two separate columns and then calculate the distance between stations for ex. SELECT sqrt(power(37.3608541552538-37.3477445655325,2)+power(-121.890799999237-(-121.906834244728),2))*108 as P_Distance , I do hope there is a simple solution how to do it. Thank you

As pointed out in the comments, your answer is already complete if the condition is you have to use Pythagoras's theorem. Why you have that condition I have no idea as it means your answer will be an approximation and not a very good one if the distances are long or you're not near the equator.

The simple solution to get a correct result is to ensure that your geometry field has correct projection data and then use the built in distance function. https://docs.oracle.com/database/121/SPATL/sdo_geom-sdo_distance.htm#SPATL1117

EDIT: Based on your comments we can forget about Pythagorus and try to use the built in Oracle spatial functions (assuming they are installed).

You first need to convert the well-known text WKT data (that is the name for data that looks like your points do) into a geometry object and tell Oracle that the numbers are lat long. You do this using the function SDO_GEOMETRY like so:

SELECT station_id, SDO_GEOMETRY(station_geom,4326) as station_geom_oracle FROM your_table

I'd suggest you make this a new table in your DB so you only have to do the conversion once. You can then use the SDO_DISTANCE function to calculate distances between these geometries as required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM