QGIS动作中的不敏感案例读取文件

时间:2017-03-20 09:40:44

标签: python action qgis

我编写了下面的代码,用于QGIS操作,从zip文件中提取shapefile并将其加载到组中。

from PyQt4.QtCore import QFileInfo,QSettings
from qgis.core import QgsRasterLayer, QgsCoordinateReferenceSystem
import zipfile

s = QSettings()
oldValidation = s.value( "/Projections/defaultBehaviour" )
s.setValue( "/Projections/defaultBehaviour", "useProject" )
vl = QgsMapLayerRegistry.instance().mapLayersByName('catalogo_CTR')[0]
qgis.utils.iface.setActiveLayer(vl)
root = QgsProject.instance().layerTreeRoot()
group_name = "CTRN_10000"
group = root.findGroup(group_name)
if group == None:
    group = root.addGroup("CTRN_10000")
else:
    pass


ctr= '\\\\rvphnas02pw\\Prodotti_Cartografici\\Dati_Vettoriali\\ctrn\\c0101_CartografiaBase\\c0101074_CTRN5000\\2008\\DB_SHAPE\\ovest\\[% A_CODICE %]o_p.zip'

fh = open(ctr, 'rb')
z = zipfile.ZipFile(fh)
for name in z.namelist():
    outpath = "D:\\zip\\"
    z.extract("[% A_CODICE %]o_p/fabbric.dbf", outpath)
    z.extract("[% A_CODICE %]o_p/FABBRIC.shx", outpath)
    z.extract("[% A_CODICE %]o_p/FABBRIC.shp", outpath)
fh.close()  


fileName = "D:\\zip\\[% A_CODICE %]o_p\\FABBRIC.shp"
fileInfo = QFileInfo(fileName)
baseName = '[% A_CODICE %]'
rlayer = QgsVectorLayer(fileName, baseName) #usare QgsVectorLayer nel caso si volesse  caricare un vettore
crs = QgsCoordinateReferenceSystem()
crs.createFromSrid(3003) 
rlayer.setCrs(crs)
QgsMapLayerRegistry.instance().addMapLayer(rlayer, False)
group.insertChildNode(-1, QgsLayerTreeLayer(rlayer)) 

s.setValue( "/Projections/defaultBehaviour", oldValidation )

它有效,但有问题。 文件FABBRIC.shp,.shx,dbf有时以大写形式写入,有时以小写形式写入(例如:FABBRIC.shp或fabbric.shp或fabbric.Shp ...)。 所以我应该改变这部分代码:

   z.extract("[% A_CODICE %]o_p/fabbric.dbf", outpath)
   z.extract("[% A_CODICE %]o_p/FABBRIC.shx", outpath)
   z.extract("[% A_CODICE %]o_p/FABBRIC.shp", outpath)

在这里:

fileName = "D:\\zip\\[% A_CODICE %]o_p\\FABBRIC.shp"

带有“不敏感案例”解决方案。

也许这很简单,但我没有解决方案就停在这里。

1 个答案:

答案 0 :(得分:1)

在给定档案update `cta_tasks` set `tsk_address` = replace(`tsk_address`, 'a:1:{s:3:"url";s:55:"', ''); update `cta_tasks` set `tsk_address` = replace(`tsk_address`, '";}', ''); 作为输入的情况下,这将确定要使用的正确shapefile成员名称。您应该为它返回的每个名称调用namelist()

z.extract()