【发布时间】:2017-12-06 02:05:51
【问题描述】:
我正在尝试读取 geojson 文件并将记录插入到 postgres 表中 - 使用下面的 python 代码。
import json
import psycopg2
conn = psycopg2.connect(host="<<ip_address>>",database="DB1", user="<<id>>", password="pwd")
cur = conn.cursor()
with open('NTA_shape.json') as f:
Geojson_data = json.load(f)
for feature in Geojson_data['features']:
type_val=feature['geometry']['type']
geom=feature['geometry']['coordinates']
ntaname=feature['properties']['NTAName']
boroname=feature['properties']['BoroName']
data = {"type":type_val,"coordinates":geom}
sql ="""Insert into <<Table_NAME> (geom,ntaname,boroname) VALUES(ST_GeomFromGeoJSON(%s),%s,%s)"""
nta_boro=(json.dumps(data),ntaname,boroname)
cur.execute(sql,nta_boro)
conn.commit()
conn.close()
但是当我查询表时,很多记录都丢失了。 如果我打印 json.dumps(data) 变量 - 它会显示所有记录。
我不确定,在表格插入过程中我缺少什么 请帮忙。
【问题讨论】:
-
我能够通过以下更改修复它 nta_boro=(json.dumps(data,),ntaname,boroname)
标签: python postgresql gis geojson