def __init__(self, parent=None): super(BatchGeocodingDialog, self).__init__(parent) self.setupUi(self) self.setFixedSize(self.size()) #SIGNALS QObject.connect(self.btnRun, SIGNAL('clicked()'), self.processing) QObject.connect(self.cmbLayer, SIGNAL('currentIndexChanged(QString)'), self.fill_form) #INIT CONTROLS VALUES for geocoder_name in GeocoderFactory.get_geocoders_names(): icon_path = GeocoderFactory.get_geocoder(geocoder_name).icon_path self.cmbGeocoder.addItem(QIcon(icon_path), geocoder_name) self.cmbLayer.addItems(get_layer_names([QGis.Point])) for region in regions_helper.get_regions_names(): self.cmbRegion.addItem(region['name'], region)
def __init__(self): QDialog.__init__(self) self.setupUi(self) self.setFixedSize(self.size()) #SIGNALS QObject.connect(self.btnRun, SIGNAL('clicked()'), self.processing) QObject.connect(self.cmbLayer, SIGNAL('currentIndexChanged(QString)'), self.fill_form) #INIT CONTROLS VALUES self.cmbGeocoder.addItems(GeocoderFactory.get_geocoders_names()) self.cmbLayer.addItems(get_layer_names([QGis.Point])) for region in regions_helper.get_regions_names(): self.cmbRegion.addItem(region['name'], region)
def __init__(self, iface): QDockWidget.__init__(self, iface.mainWindow()) self.setupUi(self) self.iface = iface self.search_threads = None # [] self.result_renderer = RubberBandResultRenderer(iface) if hasattr(self.txtSearch, 'setPlaceholderText'): self.txtSearch.setPlaceholderText(self.tr("Address...")) self.txtSearch.textChanged.connect(self.start_geocode) self.cmbGeocoder.currentIndexChanged.connect(self.start_geocode) geocoders = GeocoderFactory.get_geocoders_names() geocoders.sort() for geocoder_name in geocoders: icon_path = GeocoderFactory.get_geocoder(geocoder_name).icon_path self.cmbGeocoder.addItem(QIcon(icon_path), geocoder_name) self.lstSearchResult.currentItemChanged.connect(self.result_selected) self.lstSearchResult.itemDoubleClicked.connect(self.result_selected) self.visibilityChanged.connect(self.result_renderer.clear)
def processing(self): #checks if not self.cmbLayer.currentText(): QMessageBox.warning(self, self.tr('RuGeocoder'), self.tr('You need to choose a point layer!')) return if self.cmbAddress.isEnabled() and not self.cmbAddress.currentText(): QMessageBox.warning(self, self.tr('RuGeocoder'), self.tr( 'You need to select the field containing the addresses!')) return if self.chkDistrict.isChecked() and self.rbDisctrictName.isChecked() and not self.txtDistrictName.text(): QMessageBox.warning(self, self.tr('RuGeocoder'), self.tr('You need to enter the district name!')) return if self.chkDistrict.isChecked() and self.rbDistrictField.isChecked() and not self.cmbDistrictField.currentText(): QMessageBox.warning(self, self.tr('RuGeocoder'), self.tr( 'You need to select the field containing the names of districts!')) return if self.chkSettlement.isChecked() and self.rbSettlName.isChecked() and not self.txtSettlName.text(): QMessageBox.warning(self, self.tr('RuGeocoder'), self.tr('You need to enter the city name!')) return if self.chkSettlement.isChecked() and self.rbSettlField.isChecked() and not self.cmbSettlField.currentText(): QMessageBox.warning(self, self.tr('RuGeocoder'), self.tr( 'You need to select the field containing the names of cities!')) return if self.chkStreet.isChecked() and not self.cmbStreet.currentText(): QMessageBox.warning(self, self.tr('RuGeocoder'), self.tr( 'You need to select the field containing the names of streets!')) return if self.chkStreet.isChecked() and not self.cmbBuildingNum.currentText(): QMessageBox.warning(self, self.tr('RuGeocoder'), self.tr( 'You need to select the field containing the numbers of buildings!')) return layer = get_vector_layer_by_name(self.cmbLayer.currentText()) if not layer: QMessageBox.critical(self, self.tr('RuGeocoder'), self.tr( 'Selected layer was not found! Maybe it was removed from the project. Please select another layer.')) return if not layer.isEditable(): QMessageBox.warning(self, self.tr('RuGeocoder'), self.tr('Layer is not in edit mode! Please start editing the layer!')) return #setup ui data_provider = layer.dataProvider() features_for_update = data_provider.featureCount() if features_for_update > 0: self.prgProcess.setMaximum(features_for_update) self.prgProcess.setValue(0) start = datetime.now() #get num of fields addr_index = data_provider.fieldNameIndex(self.cmbAddress.currentText()) district_index = data_provider.fieldNameIndex(self.cmbDistrictField.currentText()) settl_index = data_provider.fieldNameIndex(self.cmbSettlField.currentText()) street_index = data_provider.fieldNameIndex(self.cmbStreet.currentText()) build_index = data_provider.fieldNameIndex(self.cmbBuildingNum.currentText()) geocoded_index = data_provider.fieldNameIndex('geocoded') #define geocoder coder = GeocoderFactory.get_geocoder(self.cmbGeocoder.currentText()) #define region region = None if self.chkRegion.isChecked(): geocoder_name = unicode(self.cmbGeocoder.currentText()) region_id = self.cmbRegion.itemData(self.cmbRegion.currentIndex())['id'] region = regions_helper.get_specific_region_name(geocoder_name, region_id) for feat in data_provider.getFeatures(): #get values for geocoding addr = unicode(feat[addr_index]) district = None if self.chkDistrict.isChecked(): if self.rbDisctrictName.isChecked(): district = unicode(self.txtDistrictName.text()) else: district = unicode(feat[district_index]) settl = None if self.chkSettlement.isChecked(): if self.rbSettlName.isChecked(): settl = unicode(self.txtSettlName.text()) else: settl = unicode(feat[settl_index]) if self.chkStreet.isChecked(): street = unicode(feat[street_index]) build_num = unicode(feat[build_index]) else: street = addr # ugly! maybe need one more method for geocoders??? build_num = None #geocode try: pt, desc = coder.geocode_components(region, district, settl, street, build_num) except URLError: if QMessageBox.critical(self, self.tr('RuGeocoder'), (self.tr('Network error!\n{0}\nIgnore the error and continue?')) .format(unicode(sys.exc_info()[1])), QMessageBox.Ignore | QMessageBox.Cancel) == QMessageBox.Ignore: self.prgProcess.setValue(self.prgProcess.value() + 1) continue else: self.prgProcess.setValue(0) return except Exception: if QMessageBox.critical(self, self.tr('RuGeocoder'), (self.tr('Error of processing!\n{0}: {1}\nIgnore the error and continue?')) .format(unicode(sys.exc_info()[0].__name__)), unicode(sys.exc_info()[1]), QMessageBox.Ignore | QMessageBox.Cancel) == QMessageBox.Ignore: self.prgProcess.setValue(self.prgProcess.value() + 1) continue else: self.prgProcess.setValue(0) return #set geom layer.changeGeometry(feat.id(), QgsGeometry.fromPoint(pt)) #set additional fields if geocoded_index >= 0: layer.changeAttributeValue(feat.id(), geocoded_index, desc) self.prgProcess.setValue(self.prgProcess.value() + 1) stop = datetime.now() #workaround for python < 2.7 td = stop - start if sys.version_info[:2] < (2, 7): total_sec = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6 else: total_sec = td.total_seconds() QMessageBox.information(self, self.tr('Geocoding successfully completed'), self.tr('Geoceded {0} features for {1} seconds') .format(unicode(features_for_update), unicode(total_sec)))
def __init__(self, search_text, geocoder_name, parent=None): QThread.__init__(self, parent) self.search_text = search_text self.geocoder_name = geocoder_name #define geocoder self.coder = GeocoderFactory.get_geocoder(geocoder_name)
def processing(self): #checks if not self.cmbLayer.currentText(): QMessageBox.warning(self, self.tr('RuGeocoder'), self.tr('You need to choose a point layer!')) return if self.cmbAddress.isEnabled() and not self.cmbAddress.currentText(): QMessageBox.warning( self, self.tr('RuGeocoder'), self.tr( 'You need to select the field containing the addresses!')) return if self.chkDistrict.isChecked() and self.rbDisctrictName.isChecked( ) and not self.txtDistrictName.text(): QMessageBox.warning( self, self.tr('RuGeocoder'), self.tr('You need to enter the district name!')) return if self.chkDistrict.isChecked() and self.rbDistrictField.isChecked( ) and not self.cmbDistrictField.currentText(): QMessageBox.warning( self, self.tr('RuGeocoder'), self. tr('You need to select the field containing the names of districts!' )) return if self.chkSettlement.isChecked() and self.rbSettlName.isChecked( ) and not self.txtSettlName.text(): QMessageBox.warning(self, self.tr('RuGeocoder'), self.tr('You need to enter the city name!')) return if self.chkSettlement.isChecked() and self.rbSettlField.isChecked( ) and not self.cmbSettlField.currentText(): QMessageBox.warning( self, self.tr('RuGeocoder'), self. tr('You need to select the field containing the names of cities!' )) return if self.chkStreet.isChecked() and not self.cmbStreet.currentText(): QMessageBox.warning( self, self.tr('RuGeocoder'), self. tr('You need to select the field containing the names of streets!' )) return if self.chkStreet.isChecked( ) and not self.cmbBuildingNum.currentText(): QMessageBox.warning( self, self.tr('RuGeocoder'), self. tr('You need to select the field containing the numbers of buildings!' )) return layer = get_vector_layer_by_name(self.cmbLayer.currentText()) if not layer: QMessageBox.critical( self, self.tr('RuGeocoder'), self. tr('Selected layer was not found! Maybe it was removed from the project. Please select another layer.' )) return if not layer.isEditable(): QMessageBox.warning( self, self.tr('RuGeocoder'), self. tr('Layer is not in edit mode! Please start editing the layer!' )) return #setup ui data_provider = layer.dataProvider() features_for_update = data_provider.featureCount() if features_for_update > 0: self.prgProcess.setMaximum(features_for_update) self.prgProcess.setValue(0) start = datetime.now() #get num of fields addr_index = data_provider.fieldNameIndex( self.cmbAddress.currentText()) district_index = data_provider.fieldNameIndex( self.cmbDistrictField.currentText()) settl_index = data_provider.fieldNameIndex( self.cmbSettlField.currentText()) street_index = data_provider.fieldNameIndex( self.cmbStreet.currentText()) build_index = data_provider.fieldNameIndex( self.cmbBuildingNum.currentText()) geocoded_index = data_provider.fieldNameIndex('geocoded') #define geocoder coder = GeocoderFactory.get_geocoder(self.cmbGeocoder.currentText()) #define region region = None if self.chkRegion.isChecked(): geocoder_name = unicode(self.cmbGeocoder.currentText()) region_id = self.cmbRegion.itemData( self.cmbRegion.currentIndex())['id'] region = regions_helper.get_specific_region_name( geocoder_name, region_id) for feat in data_provider.getFeatures(): #get values for geocoding addr = unicode(feat[addr_index]) district = None if self.chkDistrict.isChecked(): if self.rbDisctrictName.isChecked(): district = unicode(self.txtDistrictName.text()) else: district = unicode(feat[district_index]) settl = None if self.chkSettlement.isChecked(): if self.rbSettlName.isChecked(): settl = unicode(self.txtSettlName.text()) else: settl = unicode(feat[settl_index]) if self.chkStreet.isChecked(): street = unicode(feat[street_index]) build_num = unicode(feat[build_index]) else: street = addr # ugly! maybe need one more method for geocoders??? build_num = None #geocode try: pt, desc = coder.geocode_components(region, district, settl, street, build_num) except URLError: if QMessageBox.critical( self, self.tr('RuGeocoder'), (self.tr( 'Network error!\n{0}\nIgnore the error and continue?') ).format(unicode(sys.exc_info()[1])), QMessageBox.Ignore | QMessageBox.Cancel) == QMessageBox.Ignore: self.prgProcess.setValue(self.prgProcess.value() + 1) continue else: self.prgProcess.setValue(0) return except Exception: if QMessageBox.critical( self, self.tr('RuGeocoder'), (self.tr( 'Error of processing!\n{0}: {1}\nIgnore the error and continue?' )).format(unicode(sys.exc_info()[0].__name__)), unicode(sys.exc_info()[1]), QMessageBox.Ignore | QMessageBox.Cancel) == QMessageBox.Ignore: self.prgProcess.setValue(self.prgProcess.value() + 1) continue else: self.prgProcess.setValue(0) return #set geom layer.changeGeometry(feat.id(), QgsGeometry.fromPoint(pt)) #set additional fields if geocoded_index >= 0: layer.changeAttributeValue(feat.id(), geocoded_index, desc) self.prgProcess.setValue(self.prgProcess.value() + 1) stop = datetime.now() #workaround for python < 2.7 td = stop - start if sys.version_info[:2] < (2, 7): total_sec = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6 else: total_sec = td.total_seconds() QMessageBox.information( self, self.tr('Geocoding successfully completed'), self.tr('Geoceded {0} features for {1} seconds').format( unicode(features_for_update), unicode(total_sec)))