No module named ogr

扶醉桌前 提交于 2020-07-03 12:59:11

问题


I'm trying to use ogr module, I tried to install it with pip got the errors :

Could not find a version that satisfies the requirement ogr (from versions: ) No matching distribution found for ogr

tried to install all the package including GDAL and still getting an error

"No module named ogr.

didn't find a working solution on google.

That's the code i'm trying to run:

    import ogr,csv,sys
import shapefile
shpfile=sys.argv[1]
# csvfile=r'C:\Temp\test.csv' #sys.argv[2]
#Open files
csvfile=open('converterOutput.csv','wb')
ds=ogr.Open(shpfile)
lyr=ds.GetLayer()

#Get field names
dfn=lyr.GetLayerDefn()
nfields=dfn.GetFieldCount()
fields=[]
for i in range(nfields):
    fields.append(dfn.GetFieldDefn(i).GetName())
fields.append('kmlgeometry')
csvwriter = csv.DictWriter(csvfile, fields)
try:csvwriter.writeheader() #python 2.7+
except:csvfile.write(','.join(fields)+'\n')

# Write attributes and kml out to csv
for feat in lyr:
    attributes=feat.items()
    geom=feat.GetGeometryRef()
    attributes['kmlgeometry']=geom.ExportToKML()
    csvwriter.writerow(attributes)

#clean up
del csvwriter,lyr,ds
csvfile.close()

回答1:


For Windows:

Go to the gisinternals site select the download link for you architecture and compiler, search for the Generic installer for the GDAL core components msi, download and install as typical.

You have two options, the second requires that you have pip installed and the first one must set the path variables.

1.) Download the Installer for the GDAL python bindings (requires to install the GDAL core) msi, and install.

Open cmd and type:

setx PATH "%PATH%;C:\Program Files (x86)\GDAL"
setx PATH "%GDAL_DATA%;C:\Program Files (x86)\GDAL\gdal-data"
setx PATH "%GDAL_DRIVER_PATH%;C:\Program Files (x86)\GDAL\gdalplugins"

*Note that each one of the commands above is a separate one, so copy-paste and enter, each one separately. If you downloaded the x64 version just remove the (x86).

OR

2.) Go to the unofficial python wheels site search for the version that matches the core you have already installed, download. Then, copy the path to the file you just donwloaded, in my case:

C:\Users\admin\donwloads\GDAL‑2.2.4‑cp27‑cp27m‑win_amd64.whl

Once you have the path, type windows key plus r, than cmd, enter and type:

pip install "your path to the wheel you have downloaded"

in my case it looks something like:

pip install C:\Users\admin\donwloads\GDAL‑2.2.4‑cp27‑cp27m‑win_amd64.whl (look where the file is located).

To test just run a cmd and type:

python

Once you are in the python shell:

import gdal

If you need to install the library for Mac, take a look at the tutorial written by me, or add a comment so that I update the answer.

References: UCLA




回答2:


Please first install python GDAL package then import something like this

 from osgeo import ogr

Or you can follow below link [https://pypi.org/project/GDAL/]



来源:https://stackoverflow.com/questions/58059373/no-module-named-ogr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!