If you need to keep geo-data in the PostgreSQL database, you may use the PostGIS extension.
PostGIS provides spatial objects […], allowing storage and query information about location and mapping.
In the Spring Boot application, you should add necessary dependencies in pom.xml.
In the demo project, I’ll use Liquibase for filling a table with the test data.
Liquibase is a data migration tool. It has a maven plugin for manual/auto migration while the build process runs.
Maven plugin configuration:
For the Liquibase usage, you need to define changelog and SQL script itself.
In a root tag, you need to define the changeset. It must have a unique id and the path to the SQL file (relative or absolute).
The SQL file can contain DDL and DML queries.
The result of this SQL script execution will be inserted as many records as many elements you define in the array.
In this script, ‘geog’ is a column with the geography PostGIS type. The minimal configuration for geography is longitude, latitude, and SRID (Spatial Reference System Identifier).
Latitude and longitude are set for the point by calling ST_Point(double, double) function.
Then, set SRID via ST_SetSRID and cast to the geography type with ‘::’ or cast operator.
The next step is setting application properties.
Documentation for this project based on OpenApi v3, so we can use springdoc-openapi for Swagger UI.
You need to add the dependency in pom.xml.
For proper Jackson JSON conversion, you should add JtsModule Bean in the configuration.
The next step is model creation.
Geography is a binary PostGIS type defined by longitude, latitude, SRID. Other way is conversion from EWKT( Extended Well-Known Text/Binary). EWKT example: SRID=4326;POINT(37.617635 55.755814).
PostGIS provides a lot of useful functions for GIS operations.
You can create PostGIS objects in Java.
Then, use this object in the native query.
In this example, query result is three nearest objects to the provided point.
In alternative, you can create a fully native query.
Let’s deploy this application to the k8s cluster.
All that we need for installation of PostgreSQL + PostGIS extension in the k8s is configuration yaml.
For the PostgreSQL + PostGIS deployment, let’s create k8s config. This config contains the ConfigMap with
credentials, PersistentVolume and PersistentVolumeClaim, Deployment and Service.
You should save it to the k8s folder as postgres.yml.
Then, create a config for the application to define Deployment and Service.
With the local docker-machine, you should pay attention to imagePullPolicy property.
After all these preparations, build the application (by ‘mvn clean install’ command) and
follow these steps.
Go to the root of the application via cd.
Set the local docker repository.
Build a local docker image for the application with tag name example/postgis:2.