답변:
순수 파이썬에서, (os.system을가되지 않습니다)를 호출하는 서브 프로세스 모듈을 사용하지 않고 ogr2ogr
또는 shp2pgsql
예를 들어)
import os.path
import psycopg2
import osgeo.ogr
connection = psycopg2.connect("dbname=... user=...")
cursor = connection.cursor()
cursor.execute("DELETE FROM countries")
srcFile = os.path.join("DISTAL-data", "TM_WORLD_BORDERS-0.3","TM_WORLD_BORDERS-0.3.shp")
shapefile = osgeo.ogr.Open(srcFile)
layer = shapefile.GetLayer(0)
for i in range(layer.GetFeatureCount()):
feature = layer.GetFeature(i)
name = feature.GetField("NAME").decode("Latin-1")
wkt = feature.GetGeometryRef().ExportToWkt()
cursor.execute("INSERT INTO countries (name,outline) " +"VALUES (%s, ST_GeometryFromText(%s, " +"4326))", (name.encode("utf8"), wkt))
connection.commit()
ogr2ogr -f "PostgreSQL" PG:”dbname=DBNAME host=localhost" file.shp -nln TABLENAME