Search code examples
pythonogr

Unable to load PROJ.4 library


l m following a book and l type the example code but when l run it, it gave these errors.l m using Enthought Canopy along with all necessary packages. how can l solve this problem? l do not want to use another package as there are some other steps l need to use ogr.On Enthought Canopy ,l updated ogr but it did not help.

ERROR 6: Unable to load PROJ.4 library (proj.dll), creation of
OGRCoordinateTransformation failed.

here is example code:

from    __future__  import  print_function
import ogr
import osr

def open_shape_file(file_path):
    #Open   the shapefile,  get the first   layer   and returns
    #the    ogr datasource.

    datasource=ogr.Open(file_path)
    layer=datasource.GetLayerByIndex(0)
    print ("opening {}".format(file_path))
    print ("Number of feature:{}".format(layer.GetFeatureCount()))
    return datasource
def transform_geometries(datasource,    src_epsg,   dst_epsg):
#Transform  the coordinates of  all geometries  in  the
#first  layer.

#   Part    1
    src_srs =   osr.SpatialReference()
    src_srs.ImportFromEPSG(src_epsg)
    dst_srs =   osr.SpatialReference()
    dst_srs.ImportFromEPSG(dst_epsg)
    transformation  =   osr.CoordinateTransformation(src_srs,   dst_srs)
    layer   =   datasource.GetLayerByIndex(0)
#   Part    2
    geoms   =   []
    layer.ResetReading()
    for feature in  layer:
        geom    =   feature.GetGeometryRef().Clone()
        geom.Transform(transformation)
        geoms.append(geom)
    return  geoms

datasource=open_shape_file("D:/python/python_geospe/exampledata/TM_WORLD_BORDERS/TM_WORLD_BORDERS-0.3.shp")
layer   =   datasource.GetLayerByIndex(0)
feature =   layer.GetFeature(0)
print("Before   transformation:")
print(feature.GetGeometryRef())
transformed_geoms   =   transform_geometries(datasource,    4326,   3395)
print("After    transformation:")
print(transformed_geoms[0])

open_shape_file("D:/python/python_geospe/exampledata/TM_WORLD_BORDERS/TM_WORLD_BORDERS-0.3.shp")

Solution

  • Did you set your environment variables correctly ? proj.dll is typically located in C:\Program Files (x86)\GDAL. You need to set an environment variable with this path.

    I suggest that you follow this installation guide which explains the process of correctly installing GDAL/OGR in a Windows OS.

    Another guide: here.