<div dir="ltr"><div>ogr2ogr command line tool can read from PostgreSQL and write to GeoJSON <span style="font-size:12.8px">FeatureCollection</span>. Is this an option for you? Or do you need to further process the resulting GeoJSON inside PostgreSQL?</div><div><br></div><div>I recently wanted to achieve the same and used ogr2ogr to export a PostgreSQL table into GeoJSON file. Example command line:<br></div><div><br></div>ogr2ogr -f GeoJSON output.json PG:"dbname=my_db" "my_table(geom)"<div><br></div><div>François</div><div><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Dec 12, 2017 at 10:31 PM, Jorge Gustavo Rocha <span dir="ltr"><<a href="mailto:jgr@di.uminho.pt" target="_blank">jgr@di.uminho.pt</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Rémi,<br>
<br>
If I understand you requirement, to get a proper GeoJSON<br>
FeatureCollection you need a more complicate query.<br>
<br>
I use these kind of queries, using a INNER JOIN, to send data to OpenLayers.<br>
<br>
Maybe we could create something like st_asfeaturecollection to return<br>
both the geometries and the properties.<br>
<br>
Queries to return a FeatureCollection looks like:<br>
<br>
var sql = `SELECT row_to_json(fc) as geojson<br>
FROM ( SELECT 'FeatureCollection' As type, array_to_json(array_agg(f))<br>
As features<br>
FROM (<br>
-- query<br>
SELECT 'Feature' As type, ST_AsGeoJSON(lgeom.geom)::json As geometry,<br>
row_to_json(lprop) As properties<br>
FROM sentinel.products As lgeom<br>
INNER JOIN (<br>
SELECT id, uuid, identifier, instrument, product_type, cloud_cov_per,<br>
cloud_shad_per, dark_feat_per, format, gen_qual, high_prob_cloud,<br>
format_correction, sensor_start, sensor_stop, ice_per, cirrus_per,<br>
unc_per, veg_per, water_per, to_download, downloaded, published<br>
FROM sentinel.products<br>
where sensor_start::timestamp::date >= '${sensor_start}' and<br>
sensor_start::timestamp::date <= '${sensor_end}'<br>
) As lprop<br>
ON <a href="http://lgeom.id" rel="noreferrer" target="_blank">lgeom.id</a> = <a href="http://lprop.id" rel="noreferrer" target="_blank">lprop.id</a><br>
-- end of query<br>
) As f )  As fc`;<br>
<br>
The output is a well formed FeatureCollection.<br>
<br>
I've tried to isolate the proper query between the comments:<br>
-- query<br>
-- end of query<br>
<br>
Regards,<br>
<br>
J. Gustavo<br>
<div><div class="h5"><br>
On 12-12-2017 20:56, Rémi Desgrange wrote:<br>
> Hi,<br>
><br>
> this is my first mail on this list, I didn't know if I should have<br>
> posted on dev list or this one. Don't hesitate to correct.<br>
><br>
> With postgis 2.4 we now have the possibility to generate mvt format<br>
> directly in SQL, what I really like is the ability to construct a mvt<br>
> response with geometrie + data. The sql result is directly well formated<br>
> and ready to use. When I need to use geojson in my app I need something<br>
> like that :<br>
><br>
> { "type": "Feature", <br>
>   "geometry": <br>
>       { <br>
>         "type": "Point", <br>
>         "coordinates": [125.6, 10.1] <br>
>       }, <br>
>       "properties": { "name": "Dinagat Islands" }<br>
>  }<br>
><br>
> In order to do that I do :<br>
> SELECT name, st_geojson(geom) as geom FROM mytable<br>
><br>
> And then I my code I manipulate "name" to put it in the json under<br>
> "properties" there hack around to directly return geojson, but they're<br>
> hack not actual solutions.<br>
><br>
> What I imagine is something like that :<br>
><br>
> SELECT st_geojson(x) FROM (SELECT name, geom FROM mytable) x<br>
><br>
> the signature would be :<br>
><br>
> text St_AsGeoJSON(anyelement set row)<br>
><br>
> I do not know if this question has already been asked, I found nothing<br>
> when I googled. Do you think that this is a legitimate feature request ?<br>
><br>
> Thanks.<br>
><br>
><br>
><br>
><br>
</div></div>> ______________________________<wbr>_________________<br>
> postgis-users mailing list<br>
> <a href="mailto:postgis-users@lists.osgeo.org">postgis-users@lists.osgeo.org</a><br>
> <a href="https://lists.osgeo.org/mailman/listinfo/postgis-users" rel="noreferrer" target="_blank">https://lists.osgeo.org/<wbr>mailman/listinfo/postgis-users</a><br>
><br>
<br>
J. Gustavo<br>
--<br>
Jorge Gustavo Rocha<br>
Departamento de Informática<br>
Universidade do Minho<br>
4710-057 Braga<br>
Tel: <a href="tel:%2B351%20253604480" value="+351253604480">+351 253604480</a><br>
Fax: <a href="tel:%2B351%20253604471" value="+351253604471">+351 253604471</a><br>
Móvel: <a href="tel:%2B351%20910333888" value="+351910333888">+351 910333888</a><br>
skype: nabocudnosor<br>
______________________________<wbr>_________________<br>
postgis-users mailing list<br>
<a href="mailto:postgis-users@lists.osgeo.org">postgis-users@lists.osgeo.org</a><br>
<a href="https://lists.osgeo.org/mailman/listinfo/postgis-users" rel="noreferrer" target="_blank">https://lists.osgeo.org/<wbr>mailman/listinfo/postgis-users</a></blockquote></div><br></div>