API with NestJS #185. Operations with PostGIS Polygons in PostgreSQL and Drizzle

NestJS SQL

This entry is part 185 of 186 in the API with NestJS

When using PostgreSQL with PostGIS, we can do various operations using polygons. In this article, we learn how to do that both through raw SQL queries and the Drizzle ORM.

Storing polygons using the Drizzle ORM

To store polygons using the Drizzle ORM, we use a custom data type and the table.

database-schema.ts

If you want to read a detailed step-by-step explanation, check out API with NestJS #184. Storing PostGIS Polygons in PostgreSQL with Drizzle ORM

Checking if two polygons overlap

One of the operations on polygons we can do is to check if two different polygons overlap. Let’s first insert a few polygons into our database.

The Central Park is in Manhattan in New York. Above, we’re providing the data in the GeoJSON format. If you want to know more, check out API with NestJS #184. Storing PostGIS Polygons in PostgreSQL with Drizzle ORM

To check if two polygons overlap, we can use the function built into PostGIS.

When we run it, the database returns a boolean indicating whether they overlap.

To check if polygons overlap with Drizzle ORM, we need to use the above SQL query in our service.

areas.service.ts

Checking if one polygon contains another polygon

When using , we check if the polygons overlap or touch each other. The order of the provided polygons does not matter. If the first polygon intersects with the second one, the second one also intersects with the first one.

Alternatively, we can use the function to check whether one polygon is entirely within another. This function checks whether the first polygon is entirely within the boundaries of the second polygon.

It’s a bit different than using . For example, Central Park is within Manhattan, but Manhattan is not within Central Park.

Checking if a coordinate is within a polygon

Similarly, we can check if a single coordinate is within a polygon. To do that, we can also use the function.

Above, we’re checking if the Empire State Building is within Manhattan.

We should specify the Spatial Reference System Identifier (SRID) when working with geographical data. It tells PostGIS how to interpret the provided coordinates. When we defined the column, we used the SRID 4326, which means the coordinates are defined in latitude and longitude. Because of that, we use the function to indicate that the coordinate we specify uses latitude and longitude as well.

If you want to know more about SRID, check out API with NestJS #183. Distance and radius in PostgreSQL with Drizzle ORM

To perform the above with the Drizzle ORM, we also need to provide a piece of a raw SQL query.

areas.service.ts

Getting the intersection of two polygons

We can also get an intersection of two polygons, which is the area that both polygons share. As an example, we will use the Yellowstone National Park, which is located in the northwest corner of Wyoming and extends into Montana and Idaho.

When we calculate the intersection of Wyoming and the Yellowstone National Park, we get the part of the Yellowstone National Park in Wyoming. It excludes the parts of the park that are in Montana and Idaho. To do that, we need to use the function built into PostGIS.

To make the output of the function more straightforward to read, we can convert its output to GeoJSON using the function.

To perform the above operation with the Drizzle ORM, we need to provide a raw SQL query.

areas.service.ts

With the above solution, our method returns the GeoJSON representation of the intersection of two different areas.

Summary

In this article, we went through various operations we can do using polygons with PostGIS, PostgreSQL, and the Drizzle ORM. It included checking if two polygons overlap, getting an intersection of two polygons, and checking if a certain coordinate is within the polygon. While the Drizzle ORM does not provide functions to perform the above operations, we can create raw SQL queries and use them with Drizzle. All of the above gives us a solid understanding of the basics of various calculations we can do with polygons using Drizzle. Feel free to play with it more and experiment.

Series Navigation<< API with NestJS #184. Storing PostGIS Polygons in PostgreSQL with Drizzle ORMAPI with NestJS #186. What’s new in Express 5? >>
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments