def syncFeatures(self, features, vectorLayerDescriptor):        
     try:
         self.csvDataSourceHandler.syncFeaturesWithCsv(vectorLayerDescriptor, features)
         NotificationHandler.pushSuccess(QApplication.translate('VectorLayerController', 'CSV File updated'), QApplication.translate('VectorLayerController', 'Changes to layer "{}" successfully stored in csv file.').format(self.csvVectorLayer().qgsVectorLayer.name()))            
         return True
     except:                       
         VectorLayerSaveConflictController(self.csvVectorLayer(), self.csvDataSourceHandler).handleConflict()
         return False
 def addAttributes(self, attributes, vectorLayerDescriptor):        
     for attribute in attributes:
         # : :type attribute: QgsField  
         vectorLayerDescriptor.addAttribute(GeoCSVAttribute.createFromQgsField(attribute))
     try:
         self.csvDataSourceHandler.updateCsvtFile(vectorLayerDescriptor.getAttributeTypes())
         NotificationHandler.pushInfo(QApplication.translate('GeoCsvNewController', 'CSVT File created/updated.'), QApplication.translate('GeoCsvNewController', 'The CSVT file was successfully created/updated on disk.'))            
     except:
         NotificationHandler.pushWarning(QApplication.translate('GeoCsvNewController', 'CSVT File Error'), QApplication.translate('GeoCsvNewController', 'An error occured while trying to update the CSVT file according to the new attribute types. Please update the csvt file manually.'))            
 def _onConflictSaveAsButton(self):        
     filePath = QFileDialog.getSaveFileName(self.conflictDialog, QApplication.translate('VectorLayerSaveConflictController', 'Save File'), "", QApplication.translate('VectorLayerSaveConflictController', 'Files (*.csv *.tsv *.*)'));
     if filePath:
         self.conflictDialog.accept()
         try:
             self.csvDataSourceHandler.moveDataSourcesToPath(filePath)
             self.csvDataSourceHandler.syncFeaturesWithCsv(self.csvVectorLayer().vectorLayerDescriptor, self.features, filePath)
             self.csvVectorLayer().updateGeoCsvPath(filePath)
             NotificationHandler.pushSuccess(QApplication.translate('VectorLayerSaveConflictController', 'CSV File updated'), QApplication.translate('VectorLayerSaveConflictController', 'Changes to layer "{}" successfully stored in csv file.').format(self.csvVectorLayer().qgsVectorLayer.name()))
         except:                
             QMessageBox.information(None, QApplication.translate('VectorLayerSaveConflictController', 'Invalid path'), QApplication.translate('VectorLayerSaveConflictController', 'An error occured while trying to save file on new location. Please try again.'))            
 def _onCharsetConvert(self):
     csvFilePath = self.newDialog.filePath.text()
     encoding = self.newDialog.charsetDropDown.currentText()
     try:
         GeoCsvDataSourceHandler.createBackupFile(csvFilePath)
         NotificationHandler.pushInfo(QApplication.translate('GeoCsvNewController', 'backup created'), QApplication.translate('GeoCsvNewController', 'Created backup on "{}"').format(csvFilePath))
         GeoCsvDataSourceHandler.convertFileToUTF8(csvFilePath, encoding)
         NotificationHandler.pushSuccess(QApplication.translate('GeoCsvNewController', 'Converted'), QApplication.translate('GeoCsvNewController', 'Successfully converted to utf-8'))
         self._analyseCsv()
     except:
         self.newDialog.statusNotificationLabel.setText(QApplication.translate('GeoCsvNewController', 'error in converting'))
 def deleteAttributes(self, attributeIds, vectorLayerDescriptor):
     try:
         for attributeId in attributeIds:
             vectorLayerDescriptor.deleteAttributeAtIndex(attributeId)
     except:
         QMessageBox.information(None, QApplication.translate('VectorLayerSaveConflictController', 'Error while updating attributes happend'), QApplication.translate('VectorLayerSaveConflictController', 'An error occured while trying to update the attributes list. Nothing has been stored on disk.'))
     else:
         try:
             self.csvDataSourceHandler.updateCsvtFile(vectorLayerDescriptor.getAttributeTypes())
             NotificationHandler.pushInfo(QApplication.translate('GeoCsvNewController', 'CSVT File created/updated.'), QApplication.translate('GeoCsvNewController', 'The CSVT file was successfully created/updated on disk.'))
         except:
             QMessageBox.information(None, QApplication.translate('VectorLayerSaveConflictController', 'CSVT file could not be updated'), QApplication.translate('VectorLayerSaveConflictController', 'An error occured while trying to update the CSVT file according to the new attribute types. Please update the csvt file manually.'))
 def reconnectCsvVectorLayers(self, csvVectorLayers):
     layers = QgsMapLayerRegistry.instance().mapLayers()
     for qgsLayer in layers.itervalues():
         csvFilePath = qgsLayer.customProperty('editablegeocsv_path', '') 
         if csvFilePath:                
             try:        
                 dataSourceHandler = GeoCsvDataSourceHandler(csvFilePath)
                 vectorLayerDescriptor = dataSourceHandler.createCsvVectorDescriptorFromCsvt()
                 if not dataSourceHandler.hasPrj():
                     dataSourceHandler.updatePrjFile(qgsLayer.crs().toWkt())
                 csvVectorLayer = GeoCsvVectorLayerFactory.createCsvVectorLayer(dataSourceHandler, vectorLayerDescriptor, qgsLayer)
                 vectorLayerController = VectorLayerController(csvVectorLayer, dataSourceHandler)
                 csvVectorLayer.initController(vectorLayerController)                    
                 csvVectorLayers.append(csvVectorLayer)
                 NotificationHandler.pushSuccess(QApplication.translate('GeoCsvReconnectController', 'GeoCSV Layer reconnected'), QApplication.translate('GeoCsvReconnectController', 'Layer "{}" is successfully reconnected').format(qgsLayer.name()))                                                    
             except:                  
                 GeoCsvNewController.getInstance().createCsvVectorLayer(csvVectorLayers, qgsLayer, QApplication.translate('GeoCsvReconnectController', 'Couldn\'t automatically restore csv layer "{}"').format(qgsLayer.name()))
    def __init__(self, iface):          
#         pydevd.settrace()                                
        self._iface = iface        
        self.plugin_dir = os.path.dirname(__file__)        
        locale = QSettings().value('locale/userLocale')[0:2]
        locale_path = os.path.join(self.plugin_dir,'i18n','geocsv_{}.qm'.format(locale))

        if os.path.exists(locale_path):
            self.translator = QTranslator()
            self.translator.load(locale_path)

            if qVersion() > '4.3.3':
                QCoreApplication.installTranslator(self.translator)
        NotificationHandler.configureIface(iface)
        self.settings = QSettings("Editable GeoCSV","editablegeocsv")
        #container for all csv vector layers                        
        self.csvVectorLayers = []          
        #if the project file is successfully read, reconnect all CsvVectorLayers with its datasource
        self._iface.projectRead.connect(lambda: GeoCsvReconnectController.getInstance().reconnectCsvVectorLayers(self.csvVectorLayers))
        #connect to the qgis refresh button
        self._connectToRefreshAction()
示例#8
0
    def __init__(self, iface):          
#         pydevd.settrace()                                
        self._iface = iface        
        self.plugin_dir = os.path.dirname(__file__)                     
        locale = QSettings().value('locale/userLocale','en')[0:2]
        locale_path = os.path.join(self.plugin_dir,'i18n','geocsv_{}.qm'.format(locale))

        if os.path.exists(locale_path):
            self.translator = QTranslator()
            self.translator.load(locale_path)

            if qVersion() > '4.3.3':
                QCoreApplication.installTranslator(self.translator)
        NotificationHandler.configureIface(iface)
        self.settings = QSettings("Editable GeoCSV","editablegeocsv")
        #container for all csv vector layers                        
        self.csvVectorLayers = []          
        #if the project file is successfully read, reconnect all CsvVectorLayers with its datasource
        self._iface.projectRead.connect(lambda: GeoCsvReconnectController.getInstance().reconnectCsvVectorLayers(self.csvVectorLayers))
        #connect to the qgis refresh button
        self._connectToRefreshAction()
 def createCsvVectorLayer(self, csvVectorLayers, qgsVectorLayer=None, customTitle=None):
     if self.newDialog.isVisible():
         self.newDialog.reject()                                
     # enable help hyperlink
     self.newDialog.helpLabel.setOpenExternalLinks(True)                            
     self.dataSourceHandler = None
     self.vectorDescriptor = None 
     self.csvtFileIsDirty = False                       
     if customTitle:
         self.newDialog.setWindowTitle(customTitle)
     if qgsVectorLayer:
         csvPath = qgsVectorLayer.customProperty('editablegeocsv_path')
         if csvPath:
             self.newDialog.filePath.setText(csvPath)
     self._updateAcceptButton()
     self._analyseCsv()
     self.newDialog.show()        
     #wait for user input
     result = self.newDialog.exec_()
     #if user pressed ok
     if result == 1:
         if self.dataSourceHandler and self.vectorDescriptor:                                
             csvVectorLayer = GeoCsvVectorLayerFactory.createCsvVectorLayer(self.dataSourceHandler, self.vectorDescriptor, qgsVectorLayer)
             vectorLayerController = VectorLayerController(csvVectorLayer, self.dataSourceHandler)
             csvVectorLayer.initController(vectorLayerController)                                                                        
             QgsMapLayerRegistry.instance().addMapLayer(csvVectorLayer.qgsVectorLayer)
             NotificationHandler.pushSuccess(QApplication.translate('GeoCsvNewController', 'GeoCSV Layer created'), QApplication.translate('GeoCsvNewController', 'The layer "{}" was created successfully.').format(csvVectorLayer.qgsVectorLayer.name()))                
             csvVectorLayers.append(csvVectorLayer)
             if self.csvtFileIsDirty:
                 try:
                     self.dataSourceHandler.updateCsvtFile(self.vectorDescriptor.getAttributeTypes())
                     NotificationHandler.pushInfo(QApplication.translate('GeoCsvNewController', 'CSVT File created/updated.'), QApplication.translate('GeoCsvNewController', 'The CSVT file was successfully created/updated on disk.'))
                 except FileIOException:                                                
                     NotificationHandler.pushWarning(QApplication.translate('GeoCsvNewController', 'CSVT File Error'), QApplication.translate('GeoCsvNewController', 'The csvt file couldn\'t be updated on disk.'))         
             if not self.dataSourceHandler.hasPrj():
                 self.dataSourceHandler.updatePrjFile(csvVectorLayer.qgsVectorLayer.crs().toWkt())
                 NotificationHandler.pushInfo(QApplication.translate('GeoCsvNewController', 'PRJ File created.'), QApplication.translate('GeoCsvNewController', 'The PRJ file was successfully created on disk.'))