本文整理汇总了Python中qgis.core.QgsExpressionContextUtils.setLayerVariable方法的典型用法代码示例。如果您正苦于以下问题:Python QgsExpressionContextUtils.setLayerVariable方法的具体用法?Python QgsExpressionContextUtils.setLayerVariable怎么用?Python QgsExpressionContextUtils.setLayerVariable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类qgis.core.QgsExpressionContextUtils
的用法示例。
在下文中一共展示了QgsExpressionContextUtils.setLayerVariable方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_shapefile
# 需要导入模块: from qgis.core import QgsExpressionContextUtils [as 别名]
# 或者: from qgis.core.QgsExpressionContextUtils import setLayerVariable [as 别名]
def load_shapefile(self, feature_type, base_path):
"""Load downloaded shape file to QGIS Main Window.
:param feature_type: What kind of features should be downloaded.
Currently 'buildings', 'building-points' or 'roads' are supported.
:type feature_type: str
:param base_path: The base path of the shape file (without extension).
:type base_path: str
:raises: FileMissingError - when buildings.shp not exist
"""
path = '%s.shp' % base_path
if not os.path.exists(path):
message = self.tr(
'%s does not exist. The server does not have any data for '
'this extent.' % path)
raise FileMissingError(message)
layer = self.iface.addVectorLayer(path, feature_type, 'ogr')
# Check if it's a building layer and if it's QGIS 2.14 about the 2.5D
if qgis_version() >= 21400 and feature_type == 'buildings':
layer_scope = QgsExpressionContextUtils.layerScope(layer)
if not layer_scope.variable('qgis_25d_height'):
QgsExpressionContextUtils.setLayerVariable(
layer, 'qgis_25d_height', 0.0002)
if not layer_scope.variable('qgis_25d_angle'):
QgsExpressionContextUtils.setLayerVariable(
layer, 'qgis_25d_angle', 70)
canvas_srid = self.canvas.mapSettings().destinationCrs().srsid()
on_the_fly_projection = self.canvas.hasCrsTransformEnabled()
if canvas_srid != 4326 and not on_the_fly_projection:
if QGis.QGIS_VERSION_INT >= 20400:
self.canvas.setCrsTransformEnabled(True)
else:
display_warning_message_bar(
self.iface,
self.tr('Enable \'on the fly\''),
self.tr(
'Your current projection is different than EPSG:4326. '
'You should enable \'on the fly\' to display '
'correctly your layers')
)
示例2: open_file
# 需要导入模块: from qgis.core import QgsExpressionContextUtils [as 别名]
# 或者: from qgis.core.QgsExpressionContextUtils import setLayerVariable [as 别名]
#.........这里部分代码省略.........
outputs[layer] = join(
output_dir, prefix_file + "_" + layer + ".geojson")
if isfile(outputs[layer]):
raise FileOutPutException(suffix='(' + outputs[layer] + ')')
# Legacy, waiting to remove the OsmParser for QGIS >= 3.6
# Change in osm_file_dialog.py L131 too
output_geom_legacy = [l.value.lower() for l in output_geom_types]
if not white_list_column:
white_list_column = {}
white_list_legacy = (
{l.value.lower(): csv for l, csv in white_list_column.items()}
)
LOGGER.info('The OSM file is: {}'.format(osm_file))
# Parsing the file
osm_parser = OsmParser(
osm_file=osm_file,
layers=output_geom_legacy,
white_list_column=white_list_legacy)
osm_parser.signalText.connect(dialog.set_progress_text)
osm_parser.signalPercentage.connect(dialog.set_progress_percentage)
start_time = time.time()
layers = osm_parser.parse()
elapsed_time = time.time() - start_time
parser_time = time.strftime("%Hh %Mm %Ss", time.gmtime(elapsed_time))
LOGGER.info('The OSM parser took: {}'.format(parser_time))
# Finishing the process with geojson or memory layer
num_layers = 0
for i, (layer, item) in enumerate(layers.items()):
dialog.set_progress_percentage(i / len(layers) * 100)
QApplication.processEvents()
if item['featureCount'] and LayerType(layer.capitalize()) in output_geom_types:
final_layer_name = layer_name
# If configOutputs is not None (from My Queries)
if config_outputs:
if config_outputs[layer]['namelayer']:
final_layer_name = config_outputs[layer]['namelayer']
if output_dir:
dialog.set_progress_text(tr('From memory to GeoJSON: ' + layer))
# Transforming the vector file
osm_geometries = {
'points': QgsWkbTypes.Point,
'lines': QgsWkbTypes.LineString,
'multilinestrings': QgsWkbTypes.MultiLineString,
'multipolygons': QgsWkbTypes.MultiPolygon}
memory_layer = item['vector_layer']
encoding = get_default_encoding()
writer = QgsVectorFileWriter(
outputs[layer],
encoding,
memory_layer.fields(),
osm_geometries[layer],
memory_layer.crs(),
"GeoJSON")
for f in memory_layer.getFeatures():
writer.addFeature(f)
del writer
# Loading the final vector file
new_layer = QgsVectorLayer(outputs[layer], final_layer_name, "ogr")
else:
new_layer = item['vector_layer']
new_layer.setName(final_layer_name)
# Try to set styling if defined
if config_outputs and config_outputs[layer]['style']:
new_layer.loadNamedStyle(config_outputs[layer]['style'])
else:
# Loading default styles
if layer == "multilinestrings" or layer == "lines":
if "colour" in item['tags']:
new_layer.loadNamedStyle(
join(dirname(dirname(abspath(__file__))),
"styles",
layer + "_colour.qml"))
# Add action about OpenStreetMap
add_actions(new_layer, item['tags'])
if final_query:
QgsExpressionContextUtils.setLayerVariable(
new_layer, 'quickosm_query', final_query)
QgsProject.instance().addMapLayer(new_layer)
num_layers += 1
return num_layers