简体   繁体   中英

cannot add QGIS polygon in pgAdmin using python

I have a csv which include a easting and northing, easting2 and northing2, easting3 and northing3 to make a polygon in the qgis, which all easting and northing property is numeric.

The code is shown:

 import os
 import csv
 import psycopg2
 import time
 conn = psycopg2.connect (host="xxxx", database="xxxx", user="xxxx" 
 ,password="xxxx", port="xxxx")

 delete="ALTER Table highloading_qgis DROP COLUMN geom;"

 tem_delete="DELETE FROM public.highloading_qgis;"

 tem_carrier="copy highloading_qgis 
 FROM'/home/ftpuser/Report/highloading_new.csv' With DELIMITER ',' CSV  
 HEADER;"

 add_col="ALTER TABLE highloading_qgis ADD COLUMN geom geometry;"


 update_gpis="UPDATE highloading_qgis SET geom = 
(ST_MakePolygon(ST_GeomFromText('LINESTRING('Easting' 'Northing','Easting2' 
'Northing2','Easting3' 'Northing3')')));"

cursor = conn.cursor()
cursor.execute(delete)
cursor.execute(tem_delete)
cursor.execute(tem_carrier)
cursor.execute(add_col)
cursor.execute(update_gpis)
cursor.close()
conn.commit()
conn.close()

But I found error when execute cursor.execute(update_gpis)"

ProgrammingError: syntax error at or near "Easting"
LINE 1: ...om = (ST_MakePolygon(ST_GeomFromText('LINESTRING('Easting' '...

                                                   ^

Can any one can help how to correct to add a polygon geomerty to the pgadmin??

I am not a expert on this.But i think you need atleast 4 points for the polygen.Above you have given only 3.

Also you could try the query like the one given below.But not sure about it

update_gpis="UPDATE highloading_qgis SET geom = (ST_MakePolygon(ST_GeomFromText(ST_AsText(ST_MakeLine(Array[ST_MakePoint(Easting,Northing),ST_MakePoint(Easting2,Northing2),ST_MakePoint(Easting3,Northing3),ST_MakePoint(Easting4,Northing4)])))));"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM