Return the geography distance in query response
See original GitHub issueHow can I return the distance from the geography(location) I gave in query variable to the city (cities table) geography (Basically, the distance of the city from my location). I need to show the city as nearest to farthest.
Below is my table schema
CREATE TABLE cities (
id SERIAL PRIMARY KEY,
name TEXT,
location GEOGRAPHY(Point)
);
Below is the query I am executing to get the cities near my location. I am passing my current location and a distance
query cities_near($point: geography!, $distance: Float!) {
cities(
where: {location: {
_cast: {geography: {
_st_d_within: {from: $point, distance: $distance}
}}
}}
) {
name
}
}
Below is the query variables
{
"point": {
"type": "Point",
"coordinates": [1, 50]
},
"distance": 1000000
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (4 by maintainers)
Top Results From Across the Web
Query to return the rows that are within a certain geographic ...
I have a table with multiple records, each contains a field called "coords". This field has been updated with a geography point. UPDATE...
Read more >Return rows where distance is within X meters
ST_DWithin takes 2 geometries or geographies. To use a distance in meters, you can use the geography datatype.
Read more >SQL SERVER - Geography Data Type - Day 18 of 35
The STDistance( ) method calculates the shortest distance (in meters) between two Geography data points. To have STDistance( ) return the ...
Read more >Geography functions | BigQuery - Google Cloud
Returns a GEOGRAPHY containing a point on geography_1 with the smallest possible distance to geography_2 . This implies that the distance between the...
Read more >Calculating Distance Between 2 Locations of Latitude ...
The simple way of calculating a distance between two points is using the Pythagorean formula to calculate the hypotenuse of a triangle (A²...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Since
_st_d_within
is a built-in type, it makes sense to also return the relativedistance
to the geographical object, which you pass into the query. This would allow toorder
the items by the returneddistance
. And in combination withlimit
could result in more efficient queries.@0x777 really not a simple way to implement it for the
_st_d_within
type? I would like to take action, if needed, just give me some advises which parts are necessary to tweak, since I do not have any haskell experience yet 😄@Edjevw12 for now I work around it. I have followed @webdeb answer above.
I created a small table with Id and distance column and added a foreign key reference of the Id column to the original table with all the data. Then I created a function which takes the user lat, long and calculates the distance between the lat long passed to the function and all the location in the main table. The function returns a set of the small table with distance calculated and the Id of the row for which the distance is calculated.