I am not able to import a shapefile to PostGIS using the same library (OGR). I am using below link but it gives me error. Import shp to Postgis using Python and ogr
Error:
Traceback (most recent call last):
File "C:/Users/n/.qgis2/python/plugins\Importtool\Import_tool.py", line 223, in select_output_file_5
layer = shapefile.GetLayer(0)
AttributeError: 'NoneType' object has no attribute 'GetLayer'
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()
cur.execute("INSERT INTO countries (name,outline) " +"VALUES (%s, ST_GeometryFromText(%s, " +"4326))", (name.encode("utf8"), wkt)) conn.commit() – Ashwin Bhayal Aug 06 '15 at 05:30