So, I'm building a simple mapping application with Leaflet and PostGIS stored data. I've succeeded in querying my layer and delivering the results to my client application but I'm struggling modifying the query to add additional fields to the properties. I have based my query on this helpful post:
"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.geom)::json As geometry
, row_to_json((SELECT l FROM (SELECT id, photo, type) As l
)) As properties
.\"Peng\" As lg ) As f ) As fc;";
The query produces valid GeoJSON with the expected properties of id, photo, and type.
The issue I'm having is adding my column names to (SELECT id, photo, type, desc)
produces SQL syntax error 42601. The error occurs at whichever position I try to add an additional table column to the inner select statement. Googling suggests that this an issue with mixing dynamic and regular SQL. Unfortunately, my SQL skills are not quite where they need to be to resolve this.
So, how do I add additional columns the select statement so that I can add more properties values to my GeoJSON? My goal is to add several more properties to populate a dashboard and some popupcontent.
EDIT
So, a sample of my JSON object is here:
{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[8.697130556,50.123908333,108.5]},"properties":{"id":3,"photo":"/Users/Nizz0k/Desktop/Peng/IMG_3475.jpeg","type":"Quote"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[8.691429401,50.115307684,105.7]},"properties":{"id":4,"photo":"/Users/Nizz0k/Desktop/Peng/IMG_3025.jpeg","type":"Quote"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[8.694072891,50.119541651,111.5]},"properties":{"id":5,"photo":"/Users/Nizz0k/Desktop/Peng/IMG_1274.jpeg","type":"Drawing and Tag"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[8.697102778,50.119533333,112.8]},"properties":{"id":7,"photo":"/Users/Nizz0k/Desktop/Peng/IMG_4975.jpeg","type":null}},{"type":"Feature","geometry":{"type":"Point","coordinates":[8.729794444,50.113963889,99.6]},"properties":{"id":9,"photo":"/Users/Nizz0k/Desktop/Peng/IMG_5149.jpeg","type":null}},
The full result pastes into geojson.io without issues. My goal is to add more values to the properties object within the JSON.
Create table Statement is here:
CREATE TABLE IF NOT EXISTS public."Peng"
(
id integer NOT NULL DEFAULT nextval('"Peng_id_seq"'::regclass),
geom geometry(PointZ,4326),
photo character varying(254) COLLATE pg_catalog."default",
filename character varying(254) COLLATE pg_catalog."default",
directory character varying(254) COLLATE pg_catalog."default",
altitude numeric,
direction numeric,
longitude character varying(254) COLLATE pg_catalog."default",
latitude character varying(254) COLLATE pg_catalog."default",
"timestamp" character varying(24) COLLATE pg_catalog."default",
weight bigint,
type character varying(244) COLLATE pg_catalog."default",
"desc" character varying(244) COLLATE pg_catalog."default",
surface character varying(244) COLLATE pg_catalog."default",
CONSTRAINT "Peng_pkey" PRIMARY KEY (id)
)
Ultimately, I want to add the "desc" and "surface" columns to the JSON. I will need to add more data later, so I'd really like to understand the syntax here.