Spring Boot 3 Tutorial: Storing and Retrieving GEO Location Data in Database | JavaTechSolutions

preview_player
Показать описание
In this article, I will explain how can we save User Geo location into database and fetch nearest location for that USER. For working with location related things we need to use Hibernate Spatial Feature.

Spring Boot 3 Tutorial: Storing and Retrieving GEO Location Data in Database

💻 For Created Spring Boot project with below API
1. Save Location: localhost:8080/api/saveUserLocation?userName=demo_user_3&lat=22.572645&longitude=88.363892
2. Fetch Nearest Location: localhost:8080/api/findNearestLocation?userName=demo_user_1

💻 Maven dependencies required for setting up a project:
The hibernate-spatial dependency is the one that will provide support for the spatial data types.
Hibernate Spatial provides a standard interface for working with geographic data. Geographic data includes representation of entities like a Point, Line, Polygon. Such data types aren’t a part of the JDBC specification, hence the JTS (JTS Topology Suite) has become a standard for representing spatial data types.
Hibernate spatial supports different databases like Oracle, MySQL, PostgreSQL.

💻 What is geometry type?
Geometry is the base type for all the spatial types in JTS. This means that other types like Point, Polygon, and others extend from Geometry.
The Geometry type in java corresponds to the GEOMETRY type in MySql as well.

💻 Utility Class
By parsing a String representation of the type, we get an instance of Geometry. A utility class WKTReader provided by JTS can be used to convert any
well-known text representation to a Geometry type:

public Geometry wktToGeometry(String wellKnownText)
throws ParseException {

return new WKTReader().read(wellKnownText);
}

Table Creation Script

CREATE TABLE user_location (
ID INT (10),
USER_NAME VARCHAR (300),
COORDINATE POINT
);

SQL to use fetch Database

SELECT user_name, ST_asText(coordinate) FROM user_location;

RECOMMENDED COURSES

👋🏻 Connect with me:

🎉 Don't forget to like, subscribe, and hit the notification bell to stay updated with our programming tutorials. Happy coding! 💻👩‍⚕️👨‍⚕️

#tdd #springboot #javatechsolutions #springboot3 #hibernate #geometry #geolocation #mysql #location

Your Quires
how to find nearest location from user
wktreader
how to save location information into database using spring boot
hibernate spatial functions,Spring Boot 3 tutorial,GEO location data storage
Database management in Spring Boot,Spring Boot 3 GEO location tutorial
Storing and retrieving location data in Spring Boot
GEO data persistence in Spring Boot,Spring Boot 3 database tutorial
Location-based data in Spring Boot,Java GEO data storage
Geospatial data in Spring Boot
Рекомендации по теме
Комментарии
Автор

Wonderful tutorial sir🎉 learned a lot thank you

louiswambua
Автор

Thank you for this amazing tutorial! 😄 I've enjoyed watching this from the Netherlands. I didn't know I could query geometry (long/lat) data with MySQL. I was working on an application that needed this. Rolled my own brute force location finders and was thinking about implementing a QuadTree. The thing still left for me to do is to get a bounding box with the points I want and then do the Haversine within that box, so it will query fast and give me a radius.

wimh-el
Автор

Hii. SQL Error: 1416, SQLState: 22001
2024-10-09 17:47:11.166 ERROR 31344 --- [nio-8080-exec-1] : Data truncation: Cannot get geometry object from data you send to the GEOMETRY field im getting this error

thebongcoder
welcome to shbcf.ru