> For the complete documentation index, see [llms.txt](https://crystal-garage.gitbook.io/lustra-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://crystal-garage.gitbook.io/lustra-docs/additional-and-advanced-features/geometric-types.md).

# Geometric Types

Lustra supports PostgreSQL geometric types exposed by the `pg` shard.

Common model column types include:

* `PG::Geo::Point`
* `PG::Geo::Circle`
* `PG::Geo::Polygon`
* `PG::Geo::Box`
* `PG::Geo::Line`
* `PG::Geo::LineSegment`
* `PG::Geo::Path`

## Model Columns

Use the PostgreSQL type in the migration and the matching `PG::Geo` type in the model.

```crystal
create_table(:locations) do |t|
  t.column :name, :string, null: false
  t.column :coordinates, :point, null: false
  t.column :coverage_area, :circle
  t.column :service_boundary, :polygon
  t.column :bounding_box, :box
end
```

```crystal
class Location
  include Lustra::Model

  column name : String
  column coordinates : PG::Geo::Point
  column coverage_area : PG::Geo::Circle?
  column service_boundary : PG::Geo::Polygon?
  column bounding_box : PG::Geo::Box?
end
```

Assign geometric values with `PG::Geo` objects.

```crystal
Location.create!(
  name: "Downtown",
  coordinates: PG::Geo::Point.new(-74.0060, 40.7128),
  coverage_area: PG::Geo::Circle.new(-74.0060, 40.7128, 1000.0)
)
```

## Geometric Queries

Geometric helpers are available in the expression engine.

```crystal
target = PG::Geo::Point.new(-74.0060, 40.7128)

Location.query.where {
  coordinates.distance_from(target) <= 1000.0
}
```

Containment:

```crystal
point = PG::Geo::Point.new(-74.0, 40.7)

Location.query.where {
  coverage_area.contains?(point)
}
```

Other helpers map to PostgreSQL geometric operators:

| Helper                         | PostgreSQL operator         |
| ------------------------------ | --------------------------- |
| `distance_from`, `distance_to` | `<->`                       |
| `contains?`                    | `@>`                        |
| `contained_by?`, `within?`     | `@>` with reversed operands |
| `overlaps?`                    | `&&`                        |
| `intersects?`                  | `?#`                        |
| `left_of?`                     | `<<`                        |
| `right_of?`                    | `>>`                        |
| `above?`                       | \`                          |
| `below?`                       | \`<<                        |
| `same_as?`                     | `~=`                        |

Convenience helpers combine distance with comparisons.

```crystal
Location.query.where { coordinates.within_distance?(target, 1000.0) }
Location.query.where { coordinates.closer_than?(target, 1000.0) }
Location.query.where { coordinates.farther_than?(target, 1000.0) }
```

## Indexes And Constraints

For spatial workloads, use PostgreSQL indexes and constraints that match your query patterns.

```crystal
create_table(:stores) do |t|
  t.column :delivery_area, :polygon
  t.index :delivery_area, using: :gist
end
```

Lustra also provides migration helpers for some geometric constraints.

```crystal
add_exclusion_constraint("stores", "delivery_area")
add_containment_check("stores", "delivery_area", "((0,0),(100,100))")
drop_constraint("stores", "stores_delivery_area_exclusion")
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://crystal-garage.gitbook.io/lustra-docs/additional-and-advanced-features/geometric-types.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
