本文整理汇总了Python中safe.definitions.utilities.definition函数的典型用法代码示例。如果您正苦于以下问题:Python definition函数的具体用法?Python definition怎么用?Python definition使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了definition函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
开发者ID:ismailsunni,项目名称:inasafe,代码行数:59,代码来源:test_impact_function.py
示例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
开发者ID:ismailsunni,项目名称:inasafe,代码行数:55,代码来源:keyword_io.py
示例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']
开发者ID:inasafe,项目名称:inasafe,代码行数:31,代码来源:tools.py
示例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])
开发者ID:akbargumbira,项目名称:inasafe,代码行数:27,代码来源:util.py
示例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
开发者ID:ismailsunni,项目名称:inasafe,代码行数:32,代码来源:default_values.py
示例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')
开发者ID:inasafe,项目名称:inasafe,代码行数:25,代码来源:generic_expressions.py
示例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)
开发者ID:akbargumbira,项目名称:inasafe,代码行数:33,代码来源:step_kw25_classification.py
示例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)
开发者ID:ismailsunni,项目名称:inasafe,代码行数:27,代码来源:test_summary.py
示例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)
开发者ID:inasafe,项目名称:inasafe,代码行数:33,代码来源:step_kw10_hazard_category.py
示例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
开发者ID:akbargumbira,项目名称:inasafe,代码行数:34,代码来源:infographic.py
示例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)
开发者ID:inasafe,项目名称:inasafe,代码行数:9,代码来源:test_utilities.py
示例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
开发者ID:ismailsunni,项目名称:inasafe,代码行数:10,代码来源:step_kw15_layermode.py
示例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
开发者ID:ismailsunni,项目名称:inasafe,代码行数:11,代码来源:step_kw05_subcategory.py
示例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
开发者ID:timlinux,项目名称:inasafe,代码行数:11,代码来源:style.py
示例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
开发者ID:akbargumbira,项目名称:inasafe,代码行数:11,代码来源:step_kw25_classification.py
示例16: selected_unit
def selected_unit(self):
"""Obtain the unit selected by user.
:returns: Metadata of the selected unit.
:rtype: dict, None
"""
item = self.lstUnits.currentItem()
try:
return definition(item.data(QtCore.Qt.UserRole))
except (AttributeError, NameError):
return None
开发者ID:inasafe,项目名称:inasafe,代码行数:11,代码来源:step_kw20_unit.py
示例17: sum_fields
def sum_fields(layer, output_field_key, input_fields):
"""Sum the value of input_fields and put it as output_field.
:param layer: The vector layer.
:type layer: QgsVectorLayer
:param output_field_key: The output field definition key.
:type output_field_key: basestring
:param input_fields: List of input fields' name.
:type input_fields: list
"""
field_definition = definition(output_field_key)
output_field_name = field_definition['field_name']
# If the fields only has one element
if len(input_fields) == 1:
# Name is different, copy it
if input_fields[0] != output_field_name:
to_rename = {input_fields[0]: output_field_name}
# We copy only, it will be deleted later.
# We can't rename the field, we need to copy it as the same
# field might be used many times in the FMT tool.
copy_fields(layer, to_rename)
else:
# Name is same, do nothing
return
else:
# Creating expression
# Put field name in a double quote. See #4248
input_fields = ['"%s"' % f for f in input_fields]
string_expression = ' + '.join(input_fields)
sum_expression = QgsExpression(string_expression)
context = QgsExpressionContext()
context.setFields(layer.fields())
sum_expression.prepare(context)
# Get the output field index
output_idx = layer.fields().lookupField(output_field_name)
# Output index is not found
layer.startEditing()
if output_idx == -1:
output_field = create_field_from_definition(field_definition)
layer.addAttribute(output_field)
output_idx = layer.fields().lookupField(output_field_name)
# Iterate to all features
for feature in layer.getFeatures():
context.setFeature(feature)
result = sum_expression.evaluate(context)
feature[output_idx] = result
layer.updateFeature(feature)
layer.commitChanges()
开发者ID:inasafe,项目名称:inasafe,代码行数:53,代码来源:prepare_vector_layer.py
示例18: add_fields
def add_fields(
layer, absolute_values, static_fields, dynamic_structure):
"""Function to add fields needed in the output layer.
:param layer: The vector layer.
:type layer: QgsVectorLayer
:param absolute_values: The absolute value structure.
:type absolute_values: dict
:param static_fields: The list of static fields to add.
:type static_fields: list
:param dynamic_structure: The list of dynamic fields to add to the layer.
The list must be structured like this:
dynamic_structure = [
[exposure_count_field, unique_exposure]
]
where "exposure_count_field" is the dynamic to field to add and
"unique_exposure" is the list of unique values to associate with this
dynamic field. Because dynamic_structure is a ordered list, you can add
many dynamic fields.
:type dynamic_structure: list
"""
for new_dynamic_field in dynamic_structure:
field_definition = new_dynamic_field[0]
unique_values = new_dynamic_field[1]
for column in unique_values:
if (column == ''
or (hasattr(column, 'isNull')
and column.isNull())):
column = 'NULL'
field = create_field_from_definition(field_definition, column)
layer.addAttribute(field)
key = field_definition['key'] % column
value = field_definition['field_name'] % column
layer.keywords['inasafe_fields'][key] = value
for static_field in static_fields:
field = create_field_from_definition(static_field)
layer.addAttribute(field)
# noinspection PyTypeChecker
layer.keywords['inasafe_fields'][static_field['key']] = (
static_field['field_name'])
# For each absolute values
for absolute_field in list(absolute_values.keys()):
field_definition = definition(absolute_values[absolute_field][1])
field = create_field_from_definition(field_definition)
layer.addAttribute(field)
key = field_definition['key']
value = field_definition['field_name']
layer.keywords['inasafe_fields'][key] = value
开发者ID:inasafe,项目名称:inasafe,代码行数:53,代码来源:summary_tools.py
示例19: sum_fields
def sum_fields(layer, output_field_key, input_fields):
"""Sum the value of input_fields and put it as output_field.
:param layer: The vector layer.
:type layer: QgsVectorLayer
:param output_field_key: The output field definition key.
:type output_field_key: basestring
:param input_fields: List of input fields' name.
:type input_fields: list
"""
field_definition = definition(output_field_key)
output_field_name = field_definition['field_name']
# If the fields only has one element
if len(input_fields) == 1:
# Name is different, copy it
if input_fields[0] != output_field_name:
copy_fields(layer, {
input_fields[0]: output_field_name})
# Name is same, do nothing
else:
return
else:
# Creating expression
# Put field name in a double quote. See #4248
input_fields = ['"%s"' % f for f in input_fields]
string_expression = ' + '.join(input_fields)
sum_expression = QgsExpression(string_expression)
context = QgsExpressionContext()
context.setFields(layer.pendingFields())
sum_expression.prepare(context)
# Get the output field index
output_idx = layer.fieldNameIndex(output_field_name)
# Output index is not found
if output_idx == -1:
output_field = create_field_from_definition(field_definition)
layer.startEditing()
layer.addAttribute(output_field)
layer.commitChanges()
output_idx = layer.fieldNameIndex(output_field_name)
layer.startEditing()
# Iterate to all features
for feature in layer.getFeatures():
context.setFeature(feature)
result = sum_expression.evaluate(context)
feature[output_idx] = result
layer.updateFeature(feature)
layer.commitChanges()
开发者ID:timlinux,项目名称:inasafe,代码行数:52,代码来源:prepare_vector_layer.py
示例20: _check_value_mapping
def _check_value_mapping(layer, exposure_key=None):
"""Loop over the exposure type field and check if the value map is correct.
:param layer: The layer
:type layer: QgsVectorLayer
:param exposure_key: The exposure key.
:type exposure_key: str
"""
index = layer.fieldNameIndex(exposure_type_field['field_name'])
unique_exposure = layer.uniqueValues(index)
if layer.keywords['layer_purpose'] == layer_purpose_hazard['key']:
if not exposure_key:
message = tr('Hazard value mapping missing exposure key.')
raise InvalidKeywordsForProcessingAlgorithm(message)
value_map = active_thresholds_value_maps(layer.keywords, exposure_key)
else:
value_map = layer.keywords.get('value_map')
if not value_map:
# The exposure do not have a value_map, we can skip the layer.
return layer
if layer.keywords['layer_purpose'] == layer_purpose_hazard['key']:
if not exposure_key:
message = tr('Hazard classification is missing exposure key.')
raise InvalidKeywordsForProcessingAlgorithm(message)
classification = active_classification(layer.keywords, exposure_key)
else:
classification = layer.keywords['classification']
exposure_classification = definition(classification)
other = None
if exposure_classification['key'] != 'data_driven_classes':
other = exposure_classification['classes'][-1]['key']
exposure_mapped = []
for group in value_map.itervalues():
exposure_mapped.extend(group)
diff = list(set(unique_exposure) - set(exposure_mapped))
if other in value_map.keys():
value_map[other].extend(diff)
else:
value_map[other] = diff
layer.keywords['value_map'] = value_map
layer.keywords['classification'] = classification
return layer
开发者ID:timlinux,项目名称:inasafe,代码行数:51,代码来源:prepare_vector_layer.py
注:本文中的safe.definitions.utilities.definition函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论