Reputation: 45
I am trying to put a query in a python script but get error:
psycopg2.ProgrammingError: syntax error at end of input LINE 1: ...LOAT*(db_lo+db_hi) ) ) END) AS Lx_pop FROM R2
My query is as follow:
sql= "DROP MATERIALIZED VIEW IF EXISTS "+schema1+"."+vue_out+";\
CREATE MATERIALIZED VIEW "+schema1+"."+vue_out+" AS\
WITH\
-- Select Population Table and transform MultiPolygons into Polygons (ST_Dump)\
P0 AS (\
SELECT\
gid\
,(ST_Dump(the_geom)).geom AS geom\
--,(ST_Dump(the_geom)).geom::geometry(Polygon,31370) AS geom\
,ST_NumGeometries(the_geom) AS Ngeom\
,pop_2014 AS tot_pop\
,ST_area(the_geom) AS tot_Area\
FROM\
"+schema2+"."+table_in_2+"\
ORDER BY ST_NumGeometries(the_geom) DESC\
),\
-- Compute Area and distribution population over Split multipolygons:\
P AS (\
SELECT\
P0.*\
,ST_area(geom) AS area\
,tot_pop*ST_area(geom)/tot_area AS pop\
FROM P0\
),\
-- Select and filter out (2016) Power Profile and transform MultiPolygons into Polygons (ST_Dump)\
N AS (\
SELECT\
gid\
,(ST_Dump(the_geom)).geom AS geom\
--,(ST_Dump(the_geom)).geom::geometry(Polygon,31370) AS geom\
,year\
,db_lo\
,db_hi\
,ST_area(the_geom) AS Area\
FROM\
"+schema1+"."+table_in_1+"\
WHERE\
year = 2016\
),\
-- Compute Intersection after joining only overlapping polygons\
R0 AS (\
SELECT\
P.gid AS pgid\
,P.pop AS tot_pop\
,P.area AS tot_area\
,N.gid AS ngid\
,N.year\
,N.db_lo\
,N.db_hi\
,(ST_Dump(ST_intersection(P.geom, N.geom))).geom AS geom\
FROM\
P JOIN N ON ST_intersects(N.geom, P.geom)\
),\
-- Compute area of intersections:\
R1 AS (\
SELECT\
row_number()over() AS gid\
,pgid\
,ngid\
,year\
,tot_pop\
,tot_area\
,db_lo\
,db_hi\
,geom::geometry(Polygon, 31370) AS geom\
,ST_area(geom) AS area\
FROM\
R0\
),\
-- Distribution population over polygons: \
R2 AS (\
SELECT\
R1.*\
,area/tot_area*tot_pop AS Pop \
FROM R1\
)\
-- Final Aggregates:\
SELECT\
R2.* \
,(CASE WHEN Pop = 0 THEN '-infinity'::FLOAT ELSE 10::FLOAT*log( pop*10::FLOAT^( 0.05::FLOAT*(db_lo+db_hi) ) ) END) AS Lx_pop\
FROM R2"
Why am I getting this error?
Upvotes: 3
Views: 2099
Reputation: 4706
I had a similar issue. In my case, it was resolved by removing the commented-out lines. (The ones with --
in them.)
Upvotes: 1