The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
adds dart sample code
dshukertjr210b8022cb55
diff --git a/apps/docs/pages/guides/database/extensions/postgis.mdx b/apps/docs/pages/guides/database/extensions/postgis.mdxindex b99a15fca33..9b7d3d6340b 100644--- a/apps/docs/pages/guides/database/extensions/postgis.mdx+++ b/apps/docs/pages/guides/database/extensions/postgis.mdx@@ -6,15 +6,17 @@ export const meta = {description: 'Working with geo-spatial data in Postgres',}-[`PostGIS`](https://postgis.net/) is a Postgres extension that allows you to interact with Geo data within Postgres. You can sort your data by geographic location, get data within certain geographic boundaries, and do much more with it.+[PostGIS](https://postgis.net/) is a Postgres extension that allows you to interact with Geo data within Postgres. You can sort your data by geographic location, get data within certain geographic boundaries, and do much more with it.## OverviewWhile you may be able to store simple lat/long geographic coordinates as a set of decimals, it does not scale very well when you try to query through a large data set. PostGIS comes with special data types that are efficient, and indexable for high scalability.-The additional data types that PostGIS provides include [`Point`](https://postgis.net/docs/using_postgis_dbmanagement.html#Point), [`Polygon`](https://postgis.net/docs/using_postgis_dbmanagement.html#Polygon), [`Linestring`](https://postgis.net/docs/using_postgis_dbmanagement.html#LineString), and many more to represent different types of geographical data. In this guide, we will mainly focus on how to interact with `Point` type, which represents a single set of latitude and longitude. If you are interested in digging deeper, you can learn more about different data types on the [data management section of PostGIS docs](https://postgis.net/docs/using_postgis_dbmanagement.html).+The additional data types that PostGIS provides include [Point](https://postgis.net/docs/using_postgis_dbmanagement.html#Point), [Polygon](https://postgis.net/docs/using_postgis_dbmanagement.html#Polygon), [Linestring](https://postgis.net/docs/using_postgis_dbmanagement.html#LineString), and many more to represent different types of geographical data. In this guide, we will mainly focus on how to interact with `Point` type, which represents a single set of latitude and longitude. If you are interested in digging deeper, you can learn more about different data types on the [data management section of PostGIS docs](https://postgis.net/docs/using_postgis_dbmanagement.html).-## Enabling PostGIS for your Supabase project+## Usage++### Enable the extensionYou can get started with PostGIS by enabling the PostGIS extension in your Supabase dashboard.@@ -76,34 +78,66 @@ You can insert geographical data through SQL or through our API.type="underlined"defaultActiveId="sql">+<TabPanel id="data" label="Data">++<h4>Restaurants</h4>++| id | name | location |+|----|-------------|-----------------------------|+| 1 | Supa Burger | latitude: 40.807416, longitude: -73.946823 |+| 2 | Supa Pizza | latitude: 40.807475, longitude: -73.94581 |+| 3 | Supa Taco | latitude: 40.80629, longitude: -73.945826 |++</TabPanel><TabPanel id="sql" label="SQL">```sqlinsert into public.restaurants(name, location)-values ('Supa Burger', st_point(-73.946823 40.807416));+values+ ('Supa Burger', st_point(-73.946823 40.807416)),+ ('Supa Pizza', st_point(-73.94581 40.807475)),+ ('Supa Taco', st_point(-73.945826 40.80629));```</TabPanel><TabPanel id="js" label="JavaScript">```js-const location = { lat: 40.807416, long: -73.946823 }-const { error } = await supabase.from('restaurants').insert({- name: 'Supa Burger',- location: `POINT(${location.long} ${location.lat})`,-})+const { error } = await supabase.from('restaurants').insert([+ {+ name: 'Supa Burger',+ location: 'POINT(-73.946823 40.807416)',+ },+ {+ name: 'Supa Pizza',+ location: 'POINT(-73.94581 40.807475)',+ },+ {+ name: 'Supa Taco',+ location: 'POINT(-73.945826 40.80629)',+ },+])```</TabPanel><TabPanel id="dart" label="Dart">```dart-final location = { 'lat': 40.807416, 'long': -73.946823 };-await supabase.from('restaurants').insert({- name: 'Supa Burger',- location: 'POINT(${location['long']} ${location['lat']})',-});+await supabase.from('restaurants').insert([+ {+ 'name': 'Supa Burger',+ 'location': 'POINT(-73.946823 40.807416)',+ },+ {+ 'name': 'Supa Pizza',+ 'location': 'POINT(-73.94581 40.807475)',+ },+ {+ 'name': 'Supa Taco',+ 'location': 'POINT(-73.945826 40.80629)',+ },+]);```</TabPanel>@@ -111,17 +145,17 @@ await supabase.from('restaurants').insert({Notice the order in which you pass the latitude and longitude. Longitude comes first, and is because longitude represents the x-axis of the location. Another thing to watch for is that there is no comma between the two values, just a single space.-At this point, if you go into your Supabase dashboard and look at the data, you will notice that the data in the location column looks something like this.+At this point, if you go into your Supabase dashboard and look at the data, you will notice that the value of the `location` column looks something like this.```0101000020E6100000A4DFBE0E9C91614044FAEDEBC0494240```-We can query the `restaurants` table directly, but it will return the `locations` column in the format you see above. We will create [database functions](https://supabase.com/docs/guides/database/functions) so that we can use the [st_astext()](https://postgis.net/docs/ST_AsText.html) function to convert it back to a human-readable format like `POINT(-73.946713 40.807313)`.+We can query the `restaurants` table directly, but it will return the `location` column in the format you see above. We will create [database functions](https://supabase.com/docs/guides/database/functions) so that we can use the [st_astext()](https://postgis.net/docs/ST_AsText.html) function to convert it back to a human-readable format like `POINT(-73.946713 40.807313)`.### Order by distance-Sorting datasets from closest to farthest, sometimes called nearest-neighbor sort, is a very common use case in Geo-queries. PostGIS can handle it very easily with the use of the `[<->](https://postgis.net/docs/geometry_distance_knn.html)` operator. `<->` operator returns the two-dimensional distance between two geometries and will utilize the spatial index when used within `order by` clause. You can create the following database function to sort the restaurants from closest to farthest by passing the current locations as parameters.+Sorting datasets from closest to farthest, sometimes called nearest-neighbor sort, is a very common use case in Geo-queries. PostGIS can handle it very easily with the use of the [<->](https://postgis.net/docs/geometry_distance_knn.html) operator. `<->` operator returns the two-dimensional distance between two geometries and will utilize the spatial index when used within `order by` clause. You can create the following database function to sort the restaurants from closest to farthest by passing the current locations as parameters.```sqlcreate or replace function nearby_restaurants(lat float, long float)@@ -134,13 +168,36 @@ as $$$$;```+You can call this function from your client using `rpc()` like this:++<Tabs+ scrollable+ size="small"+ type="underlined"+ defaultActiveId="js"+>+<TabPanel id="js" label="JavaScript">+```js-const { data, error } = await supabase.rpc('nearby_restaurants_with_distance', {- lat: 35.6762,- long: 140.6503,+const { data, error } = await supabase.rpc('nearby_restaurants', {+ lat: 40.807313,+ long: -73.946713,})```+</TabPanel>+<TabPanel id="dart" label="Dart">++```dart+final data = await supabase.rpc('nearby_restaurants',params: {+ 'lat': 40.807313,+ 'long': -73.946713,+});+```++</TabPanel>+<TabPanel id="result" label="Result">+```json[{@@ -164,6 +221,9 @@ const { data, error } = await supabase.rpc('nearby_restaurants_with_distance', {]```+</TabPanel>+</Tabs>+### Finding all data points within a bounding box@@ -181,12 +241,20 @@ as $$$$;```-[&&](https://postgis.net/docs/geometry_overlaps.html) operator used in the where statement here returns a boolean of whether the bounding box of the two geometries intersect or not. We basically are creating a bounding box from the two points and finding those points that fall under the bounding box. We are also utilizing a few different PostGIS functions here.+[&&](https://postgis.net/docs/geometry_overlaps.html) operator used in the `where` statement here returns a boolean of whether the bounding box of the two geometries intersect or not. We basically are creating a bounding box from the two points and finding those points that fall under the bounding box. We are also utilizing a few different PostGIS functions here.- [ST_MakeBox2D](https://postgis.net/docs/ST_MakeBox2D.html): Creates a 2-dimensional box from two points.- [ST_SetSRID](https://postgis.net/docs/ST_SetSRID.html): Sets the [SRID](https://postgis.net/docs/manual-dev/using_postgis_dbmanagement.html#spatial_ref_sys), which is an identifier of what coordinate system to use, for the geometry. 4326 the standard longitude and latitude coordinate systems.-You can call this function from your client like this:+You can call this function from your client using `rpc()` like this:++<Tabs+ scrollable+ size="small"+ type="underlined"+ defaultActiveId="js"+>+<TabPanel id="js" label="JavaScript">```jsconst { data, error } = await supabase.rpc('restaurants_in_view', {@@ -197,6 +265,21 @@ const { data, error } = await supabase.rpc('restaurants_in_view', {})```+</TabPanel>+<TabPanel id="dart" label="Dart">++```dart+final data = await supabase.rpc('restaurants_in_view', params: {+ 'min_lat': 40.807,+ 'min_long': -73.946,+ 'max_lat': 40.808,+ 'max_long': -73.945,+});+```++</TabPanel>+<TabPanel id="result" label="Result">+```json[{@@ -207,6 +290,9 @@ const { data, error } = await supabase.rpc('restaurants_in_view', {]```+</TabPanel>+</Tabs>+## Resources- [Official PostGIS documentation](https://postgis.net/documentation/)