question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Return the geography distance in query response

See original GitHub issue

How 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:closed
  • Created 4 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
webdebcommented, Jan 21, 2020

Since _st_d_within is a built-in type, it makes sense to also return the relative distance to the geographical object, which you pass into the query. This would allow to order the items by the returned distance. And in combination with limit 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 😄

2reactions
sawankumarbundelkhandicommented, Dec 8, 2020

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found