I have a database of shops and in that database I have coordinates for these shops saved. I would like to get the list of shops in a 10km radius however I am not sure how to write the Postgres query since I am using Postgres database.
My database :
I am trying to add the query to a springboot geolocation microservice :
Repository code :
@Repository
public interface SellerGeolocationRepository extends CrudRepository<Seller_Geolocation, Long> {
@Query(value="SELECT * FROM seller_geolocation_ms WHERE
// get coordinates that are in the vicinity
", nativeQuery = true)
public Set<Seller_Geolocation> findAllSellersInRange(double longitude, double latitude);
}
SellerObject :
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "seller_geolocation_ms")
public class Seller_Geolocation {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private static final long serialVersionUID = 2L;
private double latitude;
private double longitude;
private String country;
private String city;
private String zipCode;
private String town;
private String address;
private Long sellerId;
}
Just for anyone else wondering, the 1609.34 figure is meters per mile, which is the base unit postgres is using. So to do kilometers, obviously multiply by 1000. – 1mike12 Jun 27 '18 at 22:01 4 Pedantic note: it's 1609.344 exactly (by definition) – user1462 Aug 17 '18 at 16:32 Add a comment | 10
PostgreSQL is an efficient and flexible database that fits a wide variety of use cases. In this guide, you will learn about different ways to query a PostgreSQL database so that you can create a simple and expressive backend to your app or library. Let's dive in!
The view is PostgreSQL's way of encapsulating queries such that they can be reused in different parts of your database schema. A view makes it so that your query can be used by other queries, triggers, or functions all across your database. The below SQL is an example of a view that is used to query our mocked person table.
Returning US Census geographies within range of coordinate using PostGIS and Tiger Decoder? -1 Given a lat/long find all points within a certain radius Related 4 Attempting to use ST_DWithin to locate objects within a given distance of meters using SRID 4326 2 Finding row that has shapefile containing lat/long point in PostGIS geometry column 1
You can use the haversine formula, using your current coordinates (target_latitude_deg/longitude) and the column name (latitude_deg, longitude_deg), both expressed in degrees
SELECT *
FROM myTable
WHERE acos(
sin(radians(target_latitude_deg))
* sin(radians(latitude_deg))
+ cos(radians(target_latitude_deg))
* cos(radians(latitude_deg))
* cos( radians(target_longitude_deg)
- radians(longitude_deg))
) * 6371 <= 10;
Alternatively, have a look at the PostGIS extension
SELECT *
FROM MyTable
WHERE ST_Dwithin(geom, st_point(target_longitude, target_latitude,10000);
For this to work you will need to install postgis :
brew install postgis
afterwards you will need to install postgis on your postgres database "seller_geolocation_ms":
1. $ sudo -i -u postgres
2. $ psql
3. postgres=# \c seller_geolocation_ms;
4. seller_geolocation_ms=# CREATE EXTENSION postgis;
5. \q
After these steps postgis should be installed on your database. then the next thing to is to write the query on your Springboot repository interface like this:
@Repository
public interface SellerGeolocationRepository extends CrudRepository<Seller_Geolocation, Long> {
@Query(value="SELECT * FROM seller_geolocation_ms WHERE ST_DWithin(cast(seller_geolocation_ms.location as geography),ST_SetSRID(ST_Point(?2, ?1),4326), 10000);", nativeQuery = true)
public Set<Seller_Geolocation> findAllSellersInRange(double longitude, double latitude);
}
To get a more indepth understanding of how ST_DWithin please follow this link :here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With