0

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'

nmtoken
13.6k5 gold badges39 silver badges91 bronze badges
asked Aug 5, 2015 at 9:07
2
  • 1
    double check that you got the filename correct for your shapefile. osgeo.ogr.Open(srcFile) returns None if it can't find or open the file Commented Aug 5, 2015 at 9:14
  • 1
    srcFile = os.path.join("DISTAL-data", "TM_WORLD_BORDERS-0.3","C:\Users\n\Downloads\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() cur.execute("INSERT INTO countries (name,outline) " +"VALUES (%s, ST_GeometryFromText(%s, " +"4326))", (name.encode("utf8"), wkt)) conn.commit() Commented Aug 6, 2015 at 5:30

1 Answer 1

3

the problem is this line...

srcFile = os.path.join("DISTAL-data", "TM_WORLD_BORDERS-0.3","C:\Users\n\Downloads\TM_WORLD_BORDERS-0.3\TM_WORLD_BORDE‌​RS-0.3.shp")

os.path.join() will create this file path (under windows)

\DISTAL-data\TM_WORLD_BORDERS-0.3\C:\Users\n\Downloads\TM_WORLD_BORDERS-0.3\TM_WORLD_BORDERS-0.3.shp

which probably isn't a valid path. Just replace it with

srcFile = 'C:\Users\n\Downloads\TM_WORLD_BORDERS-0.3\TM_WORLD_BORDERS-0.3.shp'

and it should work.

answered Aug 6, 2015 at 11:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.