python中带有psycopg2和PostGIS的IF语句

2024-09-29 21:44:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在python中使用psycopg2运行SQL查询。该查询应该为每个多边形(在{nome2}标识符中,该标识符是从segmentos.json创建的,我仍然在使用它来运行for循环)创建一组点(在{nome1}标识符中),它们相交并将相交保存到一个新表中(在{nome0}标识符中)。这个钻头很好用

问题是我希望新的{nome0}表保持删除状态,以防没有与我正在迭代的多边形相交的点。运行此代码时,出现以下错误:

SyntaxError: syntax error at or near "IF"

我应该使用函数来运行psycopg2中的IF语句吗?我错过了什么

提前谢谢

intersecta="""DROP TABLE IF EXISTS {nome0};
    IF ST_Intersection({nome2}.geom,{nome1}.geom)=true THEN

    SELECT {nome1}.*, {nome2}.nome INTO {nome0}
    FROM {nome2} INNER JOIN {nome1} ON ST_Intersects({nome2}.geom, {nome1}.geom) AND {nome2}.nome=%s;
END IF;
"""
for feature in segmentos.json()['features']:
    nomedosegmento=feature['properties']['nome']
    nomecompleto=str(feature['properties']['nome']+'_'+tabelagerada)

    cur.execute(sql.SQL(intersecta)
                .format(nome0=sql.Identifier(nomecompleto),nome1=sql.Identifier(tabelagerada),nome2=sql.Identifier(segmentnome)),[nomedosegmento,])
    #print(cur.query)


conn.commit()

Tags: jsonsqlif标识符多边形psycopg2featureidentifier

热门问题