trim_and_warnings.diff

Alexander Bruy, 2010-04-14 01:55 AM

Download (7.7 KB)

View differences:

python/plugins/fTools/tools/doJoinAttributes.py (working copy)
103 103
      outPath = self.outShape.text()
104 104
      self.compute(inName, inField, joinName, joinField, outPath, keep, useTable, self.progressBar)
105 105
      self.outShape.clear()
106
      addToTOC = QMessageBox.question(self, self.tr("Join Attributes"), self.tr("Created output shapefile:\n%1\n\nWould you like to add the new layer to the TOC?").arg( unicode(self.shapefileName) ), QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton)
107
      if addToTOC == QMessageBox.Yes:
108
        if not ftools_utils.addShapeToCanvas( unicode( outPath ) ):
109
          QMessageBox.warning( self, self.tr("Geoprocessing"), self.tr( "Error loading output shapefile:\n%1" ).arg( unicode( outPath ) ))
106
      if res:
107
        addToTOC = QMessageBox.question(self, self.tr("Join Attributes"),
108
            self.tr("Created output shapefile:\n%1\n\nWould you like to add the new layer to the TOC?")
109
            .arg( unicode(self.shapefileName) ), QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton)
110
        if addToTOC == QMessageBox.Yes:
111
          if not ftools_utils.addShapeToCanvas( unicode( outPath ) ):
112
            QMessageBox.warning( self, self.tr("Geoprocessing"), self.tr( "Error loading output shapefile:\n%1" )
113
            .arg( unicode( outPath ) ))
110 114
    self.progressBar.setValue(0)
111 115

  
112 116
  def outFile(self):
......
171 175
    seq = range(0, len(fieldList1) + len(fieldList2))
172 176
    fieldList1.extend(fieldList2)
173 177
    fieldList1 = dict(zip(seq, fieldList1))
178
    # check for correct field names
179
    longNames = ftools_utils.checkFieldNameLenght( fieldList1 )
180
    if not longNames.isEmpty():
181
      QMessageBox.warning( self, self.tr( 'Incorrect field names' ),
182
                  self.tr( 'No output will be created.\nFollowing field names are longer then 10 characters:\n%1' )
183
                  .arg( longNames.join( '\n' ) ) )
184
      return False
174 185
    sRs = provider1.crs()
175 186
    progressBar.setValue(13)
176 187
    check = QFile(self.shapefileName)
177 188
    if check.exists():
178 189
      if not QgsVectorFileWriter.deleteShapeFile(self.shapefileName):
179
        return
190
        QMessageBox.warning( self, self.tr( 'Error deleting shapefile' ),
191
                    self.tr( "Can't delete existing shapefile\n%1" ).arg( self.shapefileName ) )
192
        return False
180 193
    writer = QgsVectorFileWriter(self.shapefileName, self.encoding, fieldList1, provider1.geometryType(), sRs)
181 194
    inFeat = QgsFeature()
182 195
    outFeat = QgsFeature()
......
215 228
      nElement += 1
216 229
      progressBar.setValue(nElement)
217 230
    del writer
231
    return True
218 232

  
219 233
  def createFieldList(self, table, joinField):
220 234
    fieldList = {}
......
236 250
      fieldList[item] = field
237 251
      item = item + 1
238 252
    return (fieldList, index2)
239

  
240
  def testForUniqueness(self, fieldList1, fieldList2):
241
    changed = True
242
    while changed:
243
      changed = False
244
      for i in fieldList1:
245
        for j in fieldList2:
246
          if j.name() == i.name():
247
            j = ftools_utils.createUniqueFieldName(j)
248
            changed = True
249
    return fieldList2
python/plugins/fTools/tools/doSpatialJoin.py (working copy)
88 88
                outName = outName.left(outName.length() - 4)
89 89
            self.compute(inName, joinName, outPath, summary, sumList, keep, self.progressBar)
90 90
            self.outShape.clear()
91
            addToTOC = QMessageBox.question(self, self.tr("Spatial Join"), self.tr("Created output shapefile:\n%1\n\nWould you like to add the new layer to the TOC?").arg(unicode(outPath)), QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton)
92
            if addToTOC == QMessageBox.Yes:
91
            if res:
92
              addToTOC = QMessageBox.question(self, self.tr("Spatial Join"),
93
                      self.tr("Created output shapefile:\n%1\n\nWould you like to add the new layer to the TOC?")
94
                      .arg(unicode(outPath)), QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton)
95
              if addToTOC == QMessageBox.Yes:
93 96
                self.vlayer = QgsVectorLayer(outPath, unicode(outName), "ogr")
94 97
                QgsMapLayerRegistry.instance().addMapLayer(self.vlayer)
95 98
        self.progressBar.setValue(0)
......
135 138
            fieldList1.extend(fieldList)
136 139
            seq = range(0, len(fieldList1))
137 140
            fieldList1 = dict(zip(seq, fieldList1))
138

  
141
        
142
        # check for correct field names
143
        longNames = ftools_utils.checkFieldNameLenght( fieldList1 )
144
        if not longNames.isEmpty():
145
            QMessageBox.warning( self, self.tr( 'Incorrect field names' ),
146
                        self.tr( 'No output will be created.\nFollowing field names are longer then 10 characters:\n%1' )
147
                        .arg( longNames.join( '\n' ) ) )
148
            return False
149
        
139 150
        sRs = provider1.crs()
140 151
        progressBar.setValue(13)
141 152
        check = QFile(self.shapefileName)
142 153
        if check.exists():
143 154
            if not QgsVectorFileWriter.deleteShapeFile(self.shapefileName):
144
                return
155
                QMessageBox.warning( self, self.tr( 'Error deleting shapefile' ),
156
                            self.tr( "Can't delete existing shapefile\n%1" ).arg( self.shapefileName ) )
157
                return False
145 158
        writer = QgsVectorFileWriter(self.shapefileName, self.encoding, fieldList1, provider1.geometryType(), sRs)
146 159
        #writer = QgsVectorFileWriter(outName, "UTF-8", fieldList1, provider1.geometryType(), sRs)
147 160
        inFeat = QgsFeature()
......
215 228
            start = start + add
216 229
            progressBar.setValue(start)
217 230
        del writer
231
        return True
python/plugins/fTools/tools/ftools_utils.py (working copy)
12 12
# extractPoints( QgsGeometry )
13 13
# testForUniqueness( QList *QgsField, QList *QgsField )
14 14
# createUniqueFieldName( QgsField.name() )
15
# checkFieldNameLenght( QgsFieldMap )
15 16
# getLayerNames( QGis.vectorType() )
16 17
# getFieldNames( QgsVectorLayer )
17 18
# getVectorLayerByName( QgsVectorLayer.name() )
......
144 145
# Create a unique field name based on input field name
145 146
def createUniqueFieldName( field ):
146 147
    check = field.name().right( 2 )
148
    shortName = field.name().left( 8 )
147 149
    if check.startsWith("_"):
148 150
        ( val, test ) = check.right( 1 ).toInt()
149 151
        if test:
......
151 153
                val = 2
152 154
            else:
153 155
                val = val + 1
154
            field.setName( field.name().left( len( field.name() )-1 ) + unicode( val ) )
156
            field.setName( shortName.left( len( shortName )-1 ) + unicode( val ) )
155 157
        else:
156
            field.setName( field.name() + "_2" )
158
            field.setName( shortName + "_2" )
157 159
    else:
158
        field.setName( field.name() + "_2" )
160
        field.setName( shortName + "_2" )
159 161
    return field
160 162

  
163
# Return list of field names with more than 10 characters length
164
def checkFieldNameLenght( fieldList ):
165
    longNames = QStringList()
166
    for num, field in fieldList.iteritems():
167
        if field.name().size() > 10:
168
            longNames << unicode( field.name() )
169
    return longNames
170

  
161 171
# Return list of names of all layers in QgsMapLayerRegistry
162 172
def getLayerNames( vTypes ):
163 173
    layermap = QgsMapLayerRegistry.instance().mapLayers()