本文整理汇总了Python中safe_qgis.utilities.keyword_io.KeywordIO.write_keywords方法的典型用法代码示例。如果您正苦于以下问题:Python KeywordIO.write_keywords方法的具体用法?Python KeywordIO.write_keywords怎么用?Python KeywordIO.write_keywords使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类safe_qgis.utilities.keyword_io.KeywordIO
的用法示例。
在下文中一共展示了KeywordIO.write_keywords方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: KeywordsDialog
# 需要导入模块: from safe_qgis.utilities.keyword_io import KeywordIO [as 别名]
# 或者: from safe_qgis.utilities.keyword_io.KeywordIO import write_keywords [as 别名]
#.........这里部分代码省略.........
self.set_subcategory_list(
self.standard_exposure_list,
self.tr('Not Set'))
elif self.radHazard.isChecked():
if subcategory is not None and units is not None:
self.set_subcategory_list(
self.standard_hazard_list,
subcategory + ' [' + units + ']')
elif subcategory is not None:
self.set_subcategory_list(
self.standard_hazard_list,
subcategory)
else:
self.set_subcategory_list(
self.standard_hazard_list,
self.tr('Not Set'))
self.resize_dialog()
def resize_dialog(self):
"""Resize the dialog to fit its contents."""
# noinspection PyArgumentList
QtCore.QCoreApplication.processEvents()
LOGGER.debug('adjust ing dialog size')
self.adjustSize()
# prevents actions being handled twice
# noinspection PyPep8Naming
@pyqtSignature('QString')
def on_leTitle_textEdited(self, title):
"""Update the keywords list whenever the user changes the title.
This slot is not called if the title is changed programmatically.
:param title: New title keyword for the layer.
:type title: str
"""
self.add_list_entry('title', str(title))
# prevents actions being handled twice
# noinspection PyPep8Naming
@pyqtSignature('QString')
def on_leSource_textEdited(self, source):
"""Update the keywords list whenever the user changes the source.
This slot is not called if the source is changed programmatically.
:param source: New source keyword for the layer.
:type source: str
"""
if source is None or source == '':
self.remove_item_by_key('source')
else:
self.add_list_entry('source', str(source))
def get_keywords(self):
"""Obtain the state of the dialog as a keywords dict.
:returns: Keywords reflecting the state of the dialog.
:rtype: dict
"""
#make sure title is listed
if str(self.leTitle.text()) != '':
self.add_list_entry('title', str(self.leTitle.text()))
# make sure the source is listed too
if str(self.leSource.text()) != '':
self.add_list_entry('source', str(self.leSource.text()))
keywords = {}
for myCounter in range(self.lstKeywords.count()):
existing_item = self.lstKeywords.item(myCounter)
text = existing_item.text()
tokens = text.split(':')
key = str(tokens[0]).strip()
value = str(tokens[1]).strip()
keywords[key] = value
return keywords
def accept(self):
"""Automatic slot executed when the ok button is pressed.
It will write out the keywords for the layer that is active.
"""
self.apply_changes()
keywords = self.get_keywords()
try:
self.keyword_io.write_keywords(
layer=self.layer, keywords=keywords)
except InaSAFEError, e:
error_message = get_error_message(e)
# noinspection PyCallByClass,PyTypeChecker,PyArgumentList
QtGui.QMessageBox.warning(
self, self.tr('InaSAFE'),
((self.tr(
'An error was encountered when saving the keywords:\n'
'%s' % error_message.to_html()))))
if self.dock is not None:
self.dock.get_layers()
self.done(QtGui.QDialog.Accepted)
示例2: KeywordsDialog
# 需要导入模块: from safe_qgis.utilities.keyword_io import KeywordIO [as 别名]
# 或者: from safe_qgis.utilities.keyword_io.KeywordIO import write_keywords [as 别名]
#.........这里部分代码省略.........
# now make the rest of the safe_qgis reflect the list entries
self.update_controls_from_list()
def update_controls_from_list(self):
"""Set the ui state to match the keywords of the active layer."""
mySubcategory = self.get_value_for_key("subcategory")
myUnits = self.get_value_for_key("unit")
myType = self.get_value_for_key("datatype")
myTitle = self.get_value_for_key("title")
if myTitle is not None:
self.leTitle.setText(myTitle)
elif self.layer is not None:
myLayerName = self.layer.name()
self.lblLayerName.setText(self.tr("Keywords for %s" % myLayerName))
else:
self.lblLayerName.setText("")
if not is_polygon_layer(self.layer):
self.radPostprocessing.setEnabled(False)
# adapt gui if we are in postprocessing category
self.toggle_postprocessing_widgets()
if self.radExposure.isChecked():
if mySubcategory is not None and myType is not None:
self.set_subcategory_list(self.standardExposureList, mySubcategory + " [" + myType + "]")
elif mySubcategory is not None:
self.set_subcategory_list(self.standardExposureList, mySubcategory)
else:
self.set_subcategory_list(self.standardExposureList, self.tr("Not Set"))
elif self.radHazard.isChecked():
if mySubcategory is not None and myUnits is not None:
self.set_subcategory_list(self.standardHazardList, mySubcategory + " [" + myUnits + "]")
elif mySubcategory is not None:
self.set_subcategory_list(self.standardHazardList, mySubcategory)
else:
self.set_subcategory_list(self.standardHazardList, self.tr("Not Set"))
self.resize_dialog()
def resize_dialog(self):
"""Resize the dialog to fit its contents."""
# noinspection PyArgumentList
QtCore.QCoreApplication.processEvents()
LOGGER.debug("adjust ing dialog size")
self.adjustSize()
# prevents actions being handled twice
@pyqtSignature("QString")
def on_leTitle_textEdited(self, title):
"""Update the keywords list whenever the user changes the title.
This slot is not called if the title is changed programmatically.
:param title: New title keyword for the layer.
:type title: str
"""
self.add_list_entry("title", str(title))
def get_keywords(self):
"""Obtain the state of the dialog as a keywords dict.
:returns: Keywords reflecting the state of the dialog.
:rtype: dict
"""
# make sure title is listed
if str(self.leTitle.text()) != "":
self.add_list_entry("title", str(self.leTitle.text()))
myKeywords = {}
for myCounter in range(self.lstKeywords.count()):
myExistingItem = self.lstKeywords.item(myCounter)
myText = myExistingItem.text()
myTokens = myText.split(":")
myKey = str(myTokens[0]).strip()
myValue = str(myTokens[1]).strip()
myKeywords[myKey] = myValue
return myKeywords
def accept(self):
"""Automatic slot executed when the ok button is pressed.
It will write out the keywords for the layer that is active.
"""
self.apply_changes()
myKeywords = self.get_keywords()
try:
self.keywordIO.write_keywords(layer=self.layer, keywords=myKeywords)
except InaSAFEError, e:
myErrorMessage = get_error_message(e)
# noinspection PyCallByClass,PyTypeChecker,PyArgumentList
QtGui.QMessageBox.warning(
self,
self.tr("InaSAFE"),
((self.tr("An error was encountered when saving the keywords:\n" "%s" % myErrorMessage.to_html()))),
)
if self.dock is not None:
self.dock.get_layers()
self.done(QtGui.QDialog.Accepted)
示例3: Aggregator
# 需要导入模块: from safe_qgis.utilities.keyword_io import KeywordIO [as 别名]
# 或者: from safe_qgis.utilities.keyword_io.KeywordIO import write_keywords [as 别名]
#.........这里部分代码省略.........
polygonsProvider.featureAtId(myFeatId, myQgisFeat, True,
allPolygonAttrs)
mySHPWriter.addFeature(myQgisFeat)
self.preprocessedFeatureCount += 1
del mySHPWriter
# LOGGER.debug('Created: %s' % self.preprocessedFeatureCount)
myName = '%s %s' % (theQgisLayer.name(), self.tr('preprocessed'))
myOutLayer = QgsVectorLayer(myOutFilename, myName, 'ogr')
if not myOutLayer.isValid():
#TODO (MB) use a better exception
raise Exception('Invalid qgis Layer')
if self.showIntermediateLayers:
self.keywordIO.update_keywords(myOutLayer, {'title': myName})
QgsMapLayerRegistry.instance().addMapLayer(myOutLayer)
return myOutLayer
def _createPolygonLayer(self, crs=None, fields=None):
"""Creates an empty shape file layer"""
if crs is None:
crs = QgsCoordinateReferenceSystem()
crs.createFromEpsg(4326)
if fields is None:
fields = {}
myTempdir = temp_dir(sub_dir='preprocess')
myOutFilename = unique_filename(suffix='.shp',
dir=myTempdir)
mySHPWriter = QgsVectorFileWriter(myOutFilename,
'UTF-8',
fields,
QGis.WKBPolygon,
crs)
#flush the writer to write to file
del mySHPWriter
myName = self.tr('Entire area')
myLayer = QgsVectorLayer(myOutFilename, myName, 'ogr')
LOGGER.debug('created' + myLayer.name())
return myLayer
def _extentsToLayer(self):
"""Memory layer for aggregation by using canvas extents as feature.
We do this because the user elected to use no aggregation layer so we
make a 'dummy' one which covers the whole study area extent.
This layer is needed when postprocessing because we always want a
vector layer to store aggregation information in.
Returns:
QgsMapLayer - a memory layer representing the extents of the clip.
"""
# Note: this code duplicates from Dock.viewportGeoArray - make DRY. TS
myRect = self.iface.mapCanvas().extent()
myCrs = QgsCoordinateReferenceSystem()
myCrs.createFromEpsg(4326)
myGeoExtent = extent_to_geo_array(myRect, myCrs)
if not self.layer.isValid():
myMessage = self.tr(
'An exception occurred when creating the entire area layer.')
raise (Exception(myMessage))
myProvider = self.layer.dataProvider()
myAttrName = self.tr('Area')
myProvider.addAttributes(
[QgsField(myAttrName, QtCore.QVariant.String)])
self.layer.startEditing()
# add a feature the size of the impact layer bounding box
myFeature = QgsFeature()
# noinspection PyCallByClass,PyTypeChecker,PyArgumentList
myFeature.setGeometry(QgsGeometry.fromRect(
QgsRectangle(
QgsPoint(myGeoExtent[0], myGeoExtent[1]),
QgsPoint(myGeoExtent[2], myGeoExtent[3]))))
myFeature.setAttributeMap({0: QtCore.QVariant(
self.tr('Entire area'))})
myProvider.addFeatures([myFeature])
self.layer.commitChanges()
try:
self.keywordIO.update_keywords(
self.layer,
{self.defaults['AGGR_ATTR_KEY']: myAttrName})
except InvalidParameterError:
self.keywordIO.write_keywords(
self.layer,
{self.defaults['AGGR_ATTR_KEY']: myAttrName})
except KeywordDbError, e:
raise e
return self.layer