1

I am trying to make a webmapping app that uses on the server side node.js and postgresql(PostGIS). what I am doing is retrieving data from the Postgresql database using node.js and sending the result in JSON (geoJSON) format to Openlayers 3, using this query :

SELECT row_to_json(fc)
FROM ( SELECT 'FeatureCollection' As type, array_to_json(array_agg(f)) As features
    FROM (
    SELECT 'Feature' As type
    , ST_AsGeoJSON(lg.geometry)::json As geometry
    , row_to_json((attribute1, attribute2, attributeN)) As properties
    FROM myTable As lg   ) As f )  As fc;

my problem is that in the JSON data retrieved the "properties" are defined as "f1", "f2", "fN" instead of the actual names of the attributes, I saw this answer here in dba.stackexchange but it doesn't consider the fact that the attributes are within the "properties"

  • How to set the actual names of my attributes in the JSON returned instead of "f1", "f2", "fN" ?

I could get used to calling my properties in OL3 with "f1", "f2", "fN" since I am working alone on this app but that would be an obedient and kind of lazy solution.

5

I like to use the json_build_object function.

SELECT row_to_json(fc)
FROM ( SELECT 'FeatureCollection' As type, array_to_json(array_agg(f)) As features
    FROM (
    SELECT 'Feature' As type
    , ST_AsGeoJSON(lg.geometry)::json As geometry
    , json_build_object(
                       'attribute1', attribute1, 
                       'attribute2', attribute2,
                       'attributeN', attributeN)) As properties
    FROM myTable As lg   ) As f )  As fc;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.