Modify GeoJSON examples from views to functions (#551)

This commit is contained in:
Laurence Isla
2022-06-21 17:38:29 -05:00
committed by GitHub
parent 360cfa3fbd
commit 65c2754719
@@ -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
json_build_object(
'name', c.name,
-- Get the Geometry Object -- Get the Geometry Object
st_AsGeoJSON(c.area)::json as geo_geometry, 'geo_geometry', st_AsGeoJSON(c.area)::json,
-- Get the Feature Object -- Get the Feature Object
st_AsGeoJSON(c.*)::json as geo_feature, 'geo_feature', st_AsGeoJSON(c.*)::json,
-- Calculate the area in square units -- Calculate the area in square units
st_area(c.area) as square_units 'square_units', st_area(c.area)
from coverage c; )
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,15 +675,14 @@ 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": {
@@ -701,7 +706,6 @@ Now the query will return the information as you expected:
}, },
"square_units": 100 "square_units": 100
} }
]
And for the Feature Collection format: And for the Feature Collection format:
@@ -709,15 +713,14 @@ 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",
@@ -751,4 +754,3 @@ And for the Feature Collection format:
] ]
} }
} }
]