本文整理汇总了Python中safe.definitions.utilities.definition函数的典型用法代码示例。如果您正苦于以下问题:Python definition函数的具体用法?Python definition怎么用?Python definition使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了definition函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_provenance_without_aggregation
def test_provenance_without_aggregation(self):
"""Test provenance of impact function without aggregation."""
hazard_layer = load_test_vector_layer(
'gisv4', 'hazard', 'classified_vector.geojson')
exposure_layer = load_test_vector_layer(
'gisv4', 'exposure', 'building-points.geojson')
hazard = definition(hazard_layer.keywords['hazard'])
exposure = definition(exposure_layer.keywords['exposure'])
hazard_category = definition(hazard_layer.keywords['hazard_category'])
expected_provenance = {
'gdal_version': gdal.__version__,
'host_name': gethostname(),
'map_title': get_map_title(hazard, exposure, hazard_category),
'map_legend_title': exposure['layer_legend_title'],
'inasafe_version': get_version(),
'pyqt_version': PYQT_VERSION_STR,
'qgis_version': QGis.QGIS_VERSION,
'qt_version': QT_VERSION_STR,
'user': getpass.getuser(),
'os': readable_os_version(),
'aggregation_layer': None,
'aggregation_layer_id': None,
'exposure_layer': exposure_layer.source(),
'exposure_layer_id': exposure_layer.id(),
'hazard_layer': hazard_layer.source(),
'hazard_layer_id': hazard_layer.id(),
'analysis_question': get_analysis_question(hazard, exposure),
'aggregation_keywords': None,
'exposure_keywords': deepcopy(exposure_layer.keywords),
'hazard_keywords': deepcopy(hazard_layer.keywords),
}
# Set up impact function
impact_function = ImpactFunction()
impact_function.exposure = exposure_layer
impact_function.hazard = hazard_layer
status, message = impact_function.prepare()
self.assertEqual(PREPARE_SUCCESS, status, message)
status, message = impact_function.run()
self.assertEqual(ANALYSIS_SUCCESS, status, message)
self.maxDiff = None
expected_provenance.update({
'action_checklist': impact_function.action_checklist(),
'analysis_extent': impact_function.analysis_extent.exportToWkt(),
'impact_function_name': impact_function.name,
'impact_function_title': impact_function.title,
'notes': impact_function.notes(),
'requested_extent': impact_function.requested_extent,
'data_store_uri': impact_function.datastore.uri_path,
'start_datetime': impact_function.start_datetime,
'end_datetime': impact_function.end_datetime,
'duration': impact_function.duration
})
self.assertDictEqual(expected_provenance, impact_function.provenance)
示例2: _dict_to_row
def _dict_to_row(keyword_value):
"""Helper to make a message row from a keyword where value is a dict.
.. versionadded:: 3.2
Use this when constructing a table from keywords to display as
part of a message object. This variant will unpack the dict and
present it nicely in the keyword value area as a nested table in the
cell.
We are expecting keyword value would be something like this:
"{'high': ['Kawasan Rawan Bencana III'], "
"'medium': ['Kawasan Rawan Bencana II'], "
"'low': ['Kawasan Rawan Bencana I']}"
Or by passing a python dict object with similar layout to above.
i.e. A string representation of a dict where the values are lists.
:param keyword_value: Value of the keyword to be rendered. This must
be a string representation of a dict, or a dict.
:type keyword_value: basestring, dict
:returns: A table to be added into a cell in the keywords table.
:rtype: safe.messaging.items.table
"""
if isinstance(keyword_value, basestring):
keyword_value = literal_eval(keyword_value)
table = m.Table(style_class='table table-condensed')
for key, value in keyword_value.items():
row = m.Row()
# First the heading
if definition(key):
name = definition(key)['name']
else:
name = tr(key.capitalize())
row.add(m.Cell(m.ImportantText(name)))
# Then the value. If it contains more than one element we
# present it as a bullet list, otherwise just as simple text
if isinstance(value, (tuple, list, dict, set)):
if len(value) > 1:
bullets = m.BulletedList()
for item in value:
bullets.add(item)
row.add(m.Cell(bullets))
elif len(value) == 0:
row.add(m.Cell(""))
else:
row.add(m.Cell(value[0]))
else:
row.add(m.Cell(value))
table.add(row)
return table
示例3: __init__
def __init__(
self, coordinate_reference_system, geometry_type, exposure_key):
"""Constructor for the size calculator.
:param coordinate_reference_system: The Coordinate Reference System of
the layer.
:type coordinate_reference_system: QgsCoordinateReferenceSystem
:param exposure_key: The geometry type of the layer.
:type exposure_key: qgis.core.QgsWkbTypes.GeometryType
"""
self.calculator = QgsDistanceArea()
self.calculator.setSourceCrs(
coordinate_reference_system,
QgsProject.instance().transformContext()
)
self.calculator.setEllipsoid('WGS84')
if geometry_type == QgsWkbTypes.LineGeometry:
self.default_unit = unit_metres
LOGGER.info('The size calculator is set to use {unit}'.format(
unit=distance_unit[self.calculator.lengthUnits()]))
else:
self.default_unit = unit_square_metres
LOGGER.info('The size calculator is set to use {unit}'.format(
unit=distance_unit[self.calculator.areaUnits()]))
self.geometry_type = geometry_type
self.output_unit = None
if exposure_key:
exposure_definition = definition(exposure_key)
self.output_unit = exposure_definition['size_unit']
示例4: layer_definition_type
def layer_definition_type(layer):
"""Returned relevant layer definition based on layer purpose.
Returned the the correct definition of layer based on its purpose.
For example, if a layer have layer_purpose: exposure, and exposure: roads
then it will return definition for exposure_roads.
That's why it only supports hazard layer or exposure layer.
:param layer: hazard layer or exposure layer
:type layer: qgis.core.QgsVectorLayer
:return: Layer definitions.
:rtype: dict
.. versionadded:: 4.0
"""
layer_purposes = ['exposure', 'hazard']
layer_purpose = [p for p in layer_purposes if p in layer.keywords]
if not layer_purpose:
return None
layer_purpose = layer_purpose[0]
return definition(layer.keywords[layer_purpose])
示例5: get_inasafe_default_value_qsetting
def get_inasafe_default_value_qsetting(
qsetting, category, inasafe_field_key):
"""Helper method to get the inasafe default value from qsetting.
:param qsetting: QSetting.
:type qsetting: QSetting
:param category: Category of the default value. It can be global or
recent. Global means the global setting for default value. Recent
means the last set custom for default value from the user.
:type category: str
:param inasafe_field_key: Key for the field.
:type inasafe_field_key: str
:returns: Value of the inasafe_default_value.
:rtype: float
"""
key = 'inasafe/default_value/%s/%s' % (category, inasafe_field_key)
default_value = qsetting.value(key)
if default_value is None:
if category == GLOBAL:
# If empty for global setting, use default one.
inasafe_field = definition(inasafe_field_key)
default_value = inasafe_field.get('default_value', {})
return default_value.get('default_value', zero_default_value)
return zero_default_value
try:
return float(default_value)
except ValueError:
return zero_default_value
示例6: hazard_extra_keyword
def hazard_extra_keyword(keyword, feature, parent):
"""Given a keyword, it will return the value of the keyword
from the hazard layer's extra keywords.
For instance:
* hazard_extra_keyword( 'depth' ) -> will return the value of 'depth'
in current hazard layer's extra keywords.
"""
_ = feature, parent # NOQA
hazard_layer_path = QgsExpressionContextUtils. \
projectScope(QgsProject.instance()).variable(
'hazard_layer')
hazard_layer = load_layer(hazard_layer_path)[0]
keywords = KeywordIO.read_keywords(hazard_layer)
extra_keywords = keywords.get('extra_keywords')
if extra_keywords:
value = extra_keywords.get(keyword)
if value:
value_definition = definition(value)
if value_definition:
return value_definition['name']
return value
else:
return tr('Keyword %s is not found' % keyword)
return tr('No extra keywords found')
示例7: set_widgets
def set_widgets(self):
"""Set widgets on the Classification tab."""
self.clear_further_steps()
purpose = self.parent.step_kw_purpose.selected_purpose()['name']
subcategory = self.parent.step_kw_subcategory.\
selected_subcategory()['name']
self.lstClassifications.clear()
self.lblDescribeClassification.setText('')
self.lblSelectClassification.setText(
classification_question % (subcategory, purpose))
classifications = self.classifications_for_layer()
for classification in classifications:
if not isinstance(classification, dict):
classification = definition(classification)
item = QListWidgetItem(
classification['name'],
self.lstClassifications)
item.setData(QtCore.Qt.UserRole, classification['key'])
self.lstClassifications.addItem(item)
# Set values based on existing keywords (if already assigned)
classification_keyword = self.parent.get_existing_keyword(
'classification')
if classification_keyword:
classifications = []
for index in xrange(self.lstClassifications.count()):
item = self.lstClassifications.item(index)
classifications.append(item.data(QtCore.Qt.UserRole))
if classification_keyword in classifications:
self.lstClassifications.setCurrentRow(
classifications.index(classification_keyword))
self.auto_select_one_item(self.lstClassifications)
示例8: test_analysis_earthquake_summary
def test_analysis_earthquake_summary(self):
"""Test we can compute summary after an EQ on population."""
hazard = load_test_raster_layer('gisv4', 'hazard', 'earthquake.asc')
exposure = load_test_raster_layer(
'gisv4', 'exposure', 'raster', 'population.asc')
aggregation = load_test_vector_layer(
'gisv4', 'aggregation', 'small_grid.geojson')
impact_function = ImpactFunction()
impact_function.hazard = hazard
impact_function.exposure = exposure
impact_function.aggregation = aggregation
status, message = impact_function.prepare()
self.assertEqual(PREPARE_SUCCESS, status, message)
status, message = impact_function.run()
self.assertEqual(ANALYSIS_SUCCESS, status, message)
layer = impact_function.analysis_impacted
classification = hazard.keywords['classification']
classes = definition(classification)['classes']
for hazard_class in classes:
field_name = hazard_count_field['field_name'] % hazard_class['key']
message = '%s is not found in the EQ summary layer.' % field_name
self.assertNotEqual(-1, layer.fieldNameIndex(field_name), message)
check_inasafe_fields(impact_function.analysis_impacted)
check_inasafe_fields(impact_function.aggregation_summary)
示例9: set_widgets
def set_widgets(self):
"""Set widgets on the Hazard Category tab."""
self.clear_further_steps()
# Set widgets
self.lstHazardCategories.clear()
self.lblDescribeHazardCategory.setText('')
self.lblSelectHazardCategory.setText(
hazard_category_question)
hazard_categories = self.hazard_categories_for_layer()
for hazard_category in hazard_categories:
if not isinstance(hazard_category, dict):
# noinspection PyTypeChecker
hazard_category = definition(hazard_category)
# noinspection PyTypeChecker
item = QListWidgetItem(
hazard_category['name'],
self.lstHazardCategories)
# noinspection PyTypeChecker
item.setData(QtCore.Qt.UserRole, hazard_category['key'])
self.lstHazardCategories.addItem(item)
# Set values based on existing keywords (if already assigned)
category_keyword = self.parent.get_existing_keyword('hazard_category')
if category_keyword:
categories = []
for index in range(self.lstHazardCategories.count()):
item = self.lstHazardCategories.item(index)
categories.append(item.data(QtCore.Qt.UserRole))
if category_keyword in categories:
self.lstHazardCategories.setCurrentRow(
categories.index(category_keyword))
self.auto_select_one_item(self.lstHazardCategories)
示例10: inasafe_field_header
def inasafe_field_header(field, feature, parent):
"""Retrieve a header name of the field name from definitions.
For instance:
inasafe_field_header('minimum_needs__clean_water') -> 'Clean water'
"""
_ = feature, parent # NOQA
age_fields = [under_5_displaced_count_field, over_60_displaced_count_field]
symbol_mapping = {
'over': '>',
'under': '<'
}
field_definition = definition(field, 'field_name')
if field_definition:
if field_definition in age_fields:
header_format = tr('{symbol} {age} y.o')
field_name = field_definition.get('field_name')
if field_name:
symbol, age = field_name.split('_')[:2]
if symbol.lower() in symbol_mapping.keys():
header_name = header_format.format(
symbol=symbol_mapping[symbol.lower()],
age=age
)
return header_name
header_name = field_definition.get('header_name')
name = field_definition.get('name')
if header_name:
return header_name.capitalize()
else:
return name.capitalize()
return None
示例11: test_definition
def test_definition(self):
"""Test we can get definitions for keywords.
.. versionadded:: 3.2
"""
keyword = 'hazards'
keyword_definition = definition(keyword)
self.assertTrue('description' in keyword_definition)
示例12: selected_layermode
def selected_layermode(self):
"""Obtain the layer mode selected by user.
:returns: selected layer mode.
:rtype: string, None
"""
item = self.lstLayerModes.currentItem()
try:
return definition(item.data(QtCore.Qt.UserRole))
except (AttributeError, NameError):
return None
示例13: selected_subcategory
def selected_subcategory(self):
"""Obtain the subcategory selected by user.
:returns: Metadata of the selected subcategory.
:rtype: dict, None
"""
item = self.lstSubcategories.currentItem()
try:
return definition(item.data(QtCore.Qt.UserRole))
except (AttributeError, NameError):
return None
示例14: layer_title
def layer_title(layer):
"""Set the layer title according to the standards.
:param layer: The layer to style.
:type layer: QgsVectorLayer
"""
exposure_type = layer.keywords['exposure_keywords']['exposure']
exposure_definitions = definition(exposure_type)
title = exposure_definitions['layer_legend_title']
layer.setTitle(title)
layer.keywords['title'] = title
示例15: selected_classification
def selected_classification(self):
"""Obtain the classification selected by user.
:returns: Metadata of the selected classification.
:rtype: dict, None
"""
item = self.lstClassifications.currentItem()
try:
return definition(item.data(QtCore.Qt.UserRole))
except (AttributeError, NameError):
return None