How to filter items by distance/radius?

Hello Community,

I’m having trouble with geographic filtering in Directus and hope you can help me out!

My Situation:

I have a collection with items that each have a location field (Geometry/Point). The data looks like this:

{
  "data": [
    {
      "id": 5,
      "title": "Test 1",
      "address": "Johannisberger Straße, Berlin",
      "location": {
        "coordinates": [13.3108006, 52.4692396],
        "type": "Point"
      }
    },
    {
      "id": 6,
      "title": "Test 2", 
      "address": "Saßnitzer Straße, Berlin",
      "location": {
        "coordinates": [13.2913654, 52.4728478],
        "type": "Point"
      }
    }
    // ... more items
  ]
}

What I want to achieve:

I want to get only the items that are within a 10km radius around specific coordinates:

  • Center: 52.488738587941015, 13.299292557084074
  • Radius: 10 kilometers

What I’ve already tried:

?filter[location][_intersects]=circle(13.299292557084074,52.488738587941015,10000)

Unfortunately, this doesn’t work. I get no results back, even though there should definitely be items within the radius.

Welcome to the community @ImperatorTheo :waving_hand:

Great question.

Circles are unfortunately not supported by GeoJSON and that syntax is not going to work with the _intersects parameter.

You can however use a Polygon.

?filter[point][_intersects]={"type": "Polygon","coordinates": [[[19.076,73.77702],[18.448697,73.758821],[17.849383,73.705038],[17.304278,73.618065],[16.836276,73.501722],[16.463771,73.361028],[16.199948,73.20191],[16.052545,73.030897],[16.02401,72.854805],[16.111957,72.68045],[16.309792,72.514386],[16.607415,72.362682],[16.991917,72.230743],[17.448221,72.123168],[17.959649,72.043637],[18.508408,71.994831],[19.076,71.97838],[19.643592,71.994831],[20.192351,72.043637],[20.703779,72.123168],[21.160083,72.230743],[21.544585,72.362682],[21.842208,72.514386],[22.040043,72.68045],[22.12799,72.854805],[22.099455,73.030897],[21.952052,73.20191],[21.688229,73.361028],[21.315724,73.501722],[20.847722,73.618065],[20.302617,73.705038],[19.703303,73.758821],[19.076,73.77702]]]}

And you could build a quick script to convert your circle to a polygon.

Here’s a quick and dirty Python script from a team member I consulted about this.


import json
from geojson import Feature, Point
from turfpy.transformation import circle

# Define the circle center and radius
center = Feature(geometry=Point((19.0760, 72.8777)))
radius = 100

# Calculate the polygon
polygon = circle(center, radius=radius, steps=32)  # Use more steps for higher precision

# Print the GeoJSON output
print(json.dumps(polygon, indent=2, sort_keys=True))

Thanks for the help one more hint it must be of TYPE Geometry (ALL) I had tried it with JSON.