没有名为ogr的模块 [英] No module named ogr

查看:237
本文介绍了没有名为ogr的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ogr模块,我尝试通过pip安装它时出现了错误:

找不到满足ogr要求的版本(来自版本:) 找不到与ogr

匹配的分布

试图安装包括GDAL在内的所有软件包,但仍然出现错误

没有名为ogr的模块.

在Google上找不到可行的解决方案.

那是我要运行的代码:

    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()

解决方案

对于Windows:

转到 gisinternals网站选择适用于您的体系结构和编译器的下载链接,然后搜索Generic installer for the GDAL core components msi,下载并安装为typical.

您有两个选择,第二个要求您安装pip,第一个必须设置路径变量.

1.)下载Installer for the GDAL python bindings (requires to install the GDAL core) msi,然后安装.

打开cmd并输入:

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"

*请注意,上面的每个命令都是一个单独的命令,因此请分别复制并输入每个命令.如果您下载的是x64版本,则只需删除(x86).

OR

2.)转到非官方的python wheel网站搜索对于与您已经安装的内核相匹配的版本,请下载. 然后,将路径复制到您刚刚下载的文件中,对于我而言:

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

输入路径后,输入Windows键加r,然后输入cmd,输入并输入:

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

就我而言,它看起来像:

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

要进行测试,只需运行一个cmd并输入:

python

一旦您进入python shell:

import gdal

如果您需要为Mac安装该库,请查看我编写的教程 ,或添加评论,以便我更新答案.

参考文献: UCLA

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()

解决方案

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

这篇关于没有名为ogr的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆