Modify GeoJSON examples from views to functions (#551)
This commit is contained in:
@@ -638,23 +638,28 @@ Say you want to add areas in polygon format. The request using string representa
|
|||||||
]
|
]
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
Now, when you request the information, PostgREST will automatically cast the ``area`` column to ``JSON`` format. Although this output is useful, you will want to use the PostGIS functions to have more control on filters or casts. For these cases, creating a ``view`` is your best option. For example, let's use some of the functions to get the data in `GeoJSON format <https://geojson.org/>`_ and to calculate the area in square units:
|
Now, when you request the information, PostgREST will automatically cast the ``area`` column to ``JSON`` format. Although this output is useful, you will want to use the PostGIS functions to have more control on filters or casts. For these cases, creating a ``function`` is your best option. For example, let's use some of the functions to get the data in `GeoJSON format <https://geojson.org/>`_ and to calculate the area in square units:
|
||||||
|
|
||||||
.. code-block:: postgres
|
.. code-block:: postgres
|
||||||
|
|
||||||
create or replace view coverage_geo as
|
create or replace function coverage_geo(filter text) returns json as $$
|
||||||
select name,
|
select
|
||||||
-- Get the Geometry Object
|
json_build_object(
|
||||||
st_AsGeoJSON(c.area)::json as geo_geometry,
|
'name', c.name,
|
||||||
-- Get the Feature Object
|
-- Get the Geometry Object
|
||||||
st_AsGeoJSON(c.*)::json as geo_feature,
|
'geo_geometry', st_AsGeoJSON(c.area)::json,
|
||||||
-- Calculate the area in square units
|
-- Get the Feature Object
|
||||||
st_area(c.area) as square_units
|
'geo_feature', st_AsGeoJSON(c.*)::json,
|
||||||
from coverage c;
|
-- Calculate the area in square units
|
||||||
|
'square_units', st_area(c.area)
|
||||||
|
)
|
||||||
|
from coverage c
|
||||||
|
where c.name = filter;
|
||||||
|
$$ language sql;
|
||||||
|
|
||||||
-- Create another view for the FeatureCollection Object
|
-- Create another function for the FeatureCollection Object
|
||||||
-- for the sake of making the examples clearer
|
-- for the sake of making the examples clearer
|
||||||
create or replace view coverage_geo_collection as
|
create or replace function coverage_geo_collection() returns json as $$
|
||||||
select
|
select
|
||||||
json_build_object(
|
json_build_object(
|
||||||
'type', 'FeatureCollection',
|
'type', 'FeatureCollection',
|
||||||
@@ -662,6 +667,7 @@ Now, when you request the information, PostgREST will automatically cast the ``a
|
|||||||
)
|
)
|
||||||
as geo_feature_collection
|
as geo_feature_collection
|
||||||
from coverage c;
|
from coverage c;
|
||||||
|
$$ language sql;
|
||||||
|
|
||||||
Now the query will return the information as you expected:
|
Now the query will return the information as you expected:
|
||||||
|
|
||||||
@@ -669,39 +675,37 @@ Now the query will return the information as you expected:
|
|||||||
|
|
||||||
.. code-tab:: http
|
.. code-tab:: http
|
||||||
|
|
||||||
GET /coverage_geo?name=eq.big HTTP/1.1
|
GET /rpc/coverage_geo?filter=big HTTP/1.1
|
||||||
|
|
||||||
.. code-tab:: bash Curl
|
.. code-tab:: bash Curl
|
||||||
|
|
||||||
curl "http://localhost:3000/coverage_geo?name=eq.big"
|
curl "http://localhost:3000/rpc/coverage_geo?filter=big"
|
||||||
|
|
||||||
.. code-block:: json
|
.. code-block:: json
|
||||||
|
|
||||||
[
|
{
|
||||||
{
|
"name": "big",
|
||||||
"name": "big",
|
"geo_geometry": {
|
||||||
"geo_geometry": {
|
"type": "Polygon",
|
||||||
|
"coordinates": [
|
||||||
|
[[0,0],[10,0],[10,10],[0,10],[0,0]]
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"geo_feature": {
|
||||||
|
"type": "Feature",
|
||||||
|
"geometry": {
|
||||||
"type": "Polygon",
|
"type": "Polygon",
|
||||||
"coordinates": [
|
"coordinates": [
|
||||||
[[0,0],[10,0],[10,10],[0,10],[0,0]]
|
[[0,0],[10,0],[10,10],[0,10],[0,0]]
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"geo_feature": {
|
"properties": {
|
||||||
"type": "Feature",
|
"id": 2,
|
||||||
"geometry": {
|
"name": "big"
|
||||||
"type": "Polygon",
|
}
|
||||||
"coordinates": [
|
},
|
||||||
[[0,0],[10,0],[10,10],[0,10],[0,0]]
|
"square_units": 100
|
||||||
]
|
}
|
||||||
},
|
|
||||||
"properties": {
|
|
||||||
"id": 2,
|
|
||||||
"name": "big"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"square_units": 100
|
|
||||||
}
|
|
||||||
]
|
|
||||||
|
|
||||||
And for the Feature Collection format:
|
And for the Feature Collection format:
|
||||||
|
|
||||||
@@ -709,46 +713,44 @@ And for the Feature Collection format:
|
|||||||
|
|
||||||
.. code-tab:: http
|
.. code-tab:: http
|
||||||
|
|
||||||
GET /coverage_geo_collection HTTP/1.1
|
GET /rpc/coverage_geo_collection HTTP/1.1
|
||||||
|
|
||||||
.. code-tab:: bash Curl
|
.. code-tab:: bash Curl
|
||||||
|
|
||||||
curl "http://localhost:3000/coverage_geo_collection"
|
curl "http://localhost:3000/rpc/coverage_geo_collection"
|
||||||
|
|
||||||
.. code-block:: json
|
.. code-block:: json
|
||||||
|
|
||||||
[
|
{
|
||||||
{
|
"geo_feature_collection": {
|
||||||
"geo_feature_collection": {
|
"type": "FeatureCollection",
|
||||||
"type": "FeatureCollection",
|
"features": [
|
||||||
"features": [
|
{
|
||||||
{
|
"type": "Feature",
|
||||||
"type": "Feature",
|
"geometry": {
|
||||||
"geometry": {
|
"type": "Polygon",
|
||||||
"type": "Polygon",
|
"coordinates": [
|
||||||
"coordinates": [
|
[[0,0],[1,0],[1,1],[0,1],[0,0]]
|
||||||
[[0,0],[1,0],[1,1],[0,1],[0,0]]
|
]
|
||||||
]
|
|
||||||
},
|
|
||||||
"properties": {
|
|
||||||
"id": 1,
|
|
||||||
"name": "small"
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
{
|
"properties": {
|
||||||
"type": "Feature",
|
"id": 1,
|
||||||
"geometry": {
|
"name": "small"
|
||||||
"type": "Polygon",
|
|
||||||
"coordinates": [
|
|
||||||
[[0,0],[10,0],[10,10],[0,10],[0,0]]
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"properties": {
|
|
||||||
"id": 2,
|
|
||||||
"name": "big"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
]
|
},
|
||||||
}
|
{
|
||||||
|
"type": "Feature",
|
||||||
|
"geometry": {
|
||||||
|
"type": "Polygon",
|
||||||
|
"coordinates": [
|
||||||
|
[[0,0],[10,0],[10,10],[0,10],[0,0]]
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"properties": {
|
||||||
|
"id": 2,
|
||||||
|
"name": "big"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user