API with NestJS #184. Storing PostGIS Polygons in PostgreSQL with Drizzle ORM

NestJS SQL

This entry is part 184 of 184 in the API with NestJS

PostgreSQL, together with PostGIS, allows us to store various types of geographical data. Besides working with simple coordinates, we can also store entire areas in the form of polygons. In this article, we learn how to handle polygons with PostgreSQL and the Drizzle ORM. While Drizzle ORM does not support it out of the box, we can create a custom type to handle it.

If you want to know the basics of working with geographical data using PostGIS and the Drizzle ORM, check out the following articles:

Polygons

A polygon is a two-dimensional object that represents a flat area with a defined boundary. A straightforward way of defining a polygon with PostGIS is to use the function. We need to provide the coordinates of each point of our polygon.

Above, we use SRID to let PostGIS know that we defined coordinates with latitude and longitude.

It’s important to note that the first and last pair of coordinates must be identical. This ensures that our polygon is a properly enclosed geometric shape.

The outer and inner ring

Each polygon needs an outer ring. It defines the outer boundary of the polygon. Optionally, we can provide inner rings that represent holes in the polygon. Each inner ring should be fully contained within the outer ring and shouldn’t overlap or touch the outer boundary.

The GeoJSON format

Alternatively, we can use the GeoJSON type to represent our polygon. It includes the type of our geometry data and an array of rings in our polygon.

The first array defines the outer ring. The following arrays define the inner rings. Finally, each coordinates pair is an array with two element. The first one is the longitude, the second one is the latitude.

Using polygons with the Drizzle ORM and NestJS

Unfortunately, the Drizzle ORM does not support polygons out of the box. Let’s create a custom type to handle it.

database-schema.ts

Thanks to writing the function, creating a migration results in the following SQL query:

Storing data

The most straightforward way to store our polygons in the database while using Drizzle ORM is to provide them in the GeoJSON format. To let the users provide them as an array, we need to write the method that converts the polygon data into a valid GeoJSON format.

database-schema.ts

As we’ve learned before, the polygon in the GeoJSON data is an three-dimensional array.

Now, we can use Drizzle to store polygons in the database.

Validating the data

When using NestJS, we often use the library to validate the data sent by the user. While there is no built-in decorator to handle polygons, we can create a custom validator.

area.dto.ts

To do it, we can create a class using the method.

are-polygon-coordinates.ts

Our method verifies if every polygon is valid.

If you want to implement more strict validation, you can check if the first and last pair of coordinates in a polygon is identical.

are-polygon-coordinates.ts

The function uses the and functions built into the library.

are-polygon-coordinates.ts

We can now use our class in the controller to validate the data sent by the user.

areas.controller.ts

We can also use it in our service.

areas.service.ts

Retrieving data

By default, PostgreSQL returns the polygon data in the WKB (Well-Known Binary) format.

However, this format is not very readable. To retrieve the data in the GeoJSON format, we can use the function built into PostGIS.

Unfortunately, right now, we can’t tell Drizzle ORM to use the when retrieving the data from a particular column. Until this PR is merged, we can use the library in our method to convert the WKB format to GeoJSON.

database-schema.ts

Thanks to this change, our data is converted to GeoJSON, which is much easier to read.

Summary

In this lesson, we’ve learned how to store PostGIS polygons in a PostgreSQL database using the Drizzle ORM. Since it’s not supported out of the box, we had to create a custom Drizzle type to handle it. We also wrote a custom validator to ensure that users provide the polygons in the correct format. All that gives us solid foundations to work with geographical data effectively in our applications.

Series Navigation<< API with NestJS #183. Distance and radius in PostgreSQL with Drizzle ORM
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments