本文整理汇总了Python中qgis.gui.QgsMapCanvas.mapSettings方法的典型用法代码示例。如果您正苦于以下问题:Python QgsMapCanvas.mapSettings方法的具体用法?Python QgsMapCanvas.mapSettings怎么用?Python QgsMapCanvas.mapSettings使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类qgis.gui.QgsMapCanvas
的用法示例。
在下文中一共展示了QgsMapCanvas.mapSettings方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testSaveMultipleCanvasesToProject
# 需要导入模块: from qgis.gui import QgsMapCanvas [as 别名]
# 或者: from qgis.gui.QgsMapCanvas import mapSettings [as 别名]
def testSaveMultipleCanvasesToProject(self):
# test saving/restoring canvas state to project with multiple canvases
c1 = QgsMapCanvas()
c1.setObjectName('c1')
c1.setDestinationCrs(QgsCoordinateReferenceSystem('EPSG:3111'))
c1.setRotation(45)
c2 = QgsMapCanvas()
c2.setObjectName('c2')
c2.setDestinationCrs(QgsCoordinateReferenceSystem('EPSG:4326'))
c2.setRotation(65)
doc = QDomDocument("testdoc")
elem = doc.createElement("qgis")
doc.appendChild(elem)
c1.writeProject(doc)
c2.writeProject(doc)
c3 = QgsMapCanvas()
c3.setObjectName('c1')
c4 = QgsMapCanvas()
c4.setObjectName('c2')
c3.readProject(doc)
c4.readProject(doc)
self.assertEqual(c3.mapSettings().destinationCrs().authid(), 'EPSG:3111')
self.assertEqual(c3.rotation(), 45)
self.assertEqual(c4.mapSettings().destinationCrs().authid(), 'EPSG:4326')
self.assertEqual(c4.rotation(), 65)
示例2: make_pdf
# 需要导入模块: from qgis.gui import QgsMapCanvas [as 别名]
# 或者: from qgis.gui.QgsMapCanvas import mapSettings [as 别名]
def make_pdf():
canvas = QgsMapCanvas()
# Load our project
QgsProject.instance().read(QFileInfo(project_path))
bridge = QgsLayerTreeMapCanvasBridge(
QgsProject.instance().layerTreeRoot(), canvas)
bridge.setCanvasLayers()
if canvas.layerCount() < 1:
print 'No layers loaded from this project, exiting.'
return
print canvas.mapSettings().extent().toString()
template_file = file(template_path)
template_content = template_file.read()
template_file.close()
document = QDomDocument()
document.setContent(template_content)
composition = QgsComposition(canvas.mapSettings())
# You can use this to replace any string like this [key]
# in the template with a new value. e.g. to replace
# [date] pass a map like this {'date': '1 Jan 2012'}
substitution_map = {
'DATE_TIME_START': TIME_START,
'DATE_TIME_END': TIME_STOP}
composition.loadFromTemplate(document, substitution_map)
# You must set the id in the template
map_item = composition.getComposerItemById('map')
map_item.setMapCanvas(canvas)
map_item.zoomToExtent(canvas.extent())
# You must set the id in the template
legend_item = composition.getComposerItemById('legend')
legend_item.updateLegend()
composition.refreshItems()
composition.exportAsPDF(
'/home/web/reports/pdf/%s/%s.pdf' % (TIME_SLICE, LABEL))
QgsProject.instance().clear()
示例3: testSaveMultipleCanvasesToProject
# 需要导入模块: from qgis.gui import QgsMapCanvas [as 别名]
# 或者: from qgis.gui.QgsMapCanvas import mapSettings [as 别名]
def testSaveMultipleCanvasesToProject(self):
# test saving/restoring canvas state to project with multiple canvases
c1 = QgsMapCanvas()
c1.setObjectName('c1')
c1.setDestinationCrs(QgsCoordinateReferenceSystem('EPSG:3111'))
c1.setRotation(45)
c1.expressionContextScope().setVariable('vara', 1111)
c1.expressionContextScope().setVariable('varb', 'bb')
c2 = QgsMapCanvas()
c2.setObjectName('c2')
c2.setDestinationCrs(QgsCoordinateReferenceSystem('EPSG:4326'))
c2.setRotation(65)
c2.expressionContextScope().setVariable('vara', 2222)
c2.expressionContextScope().setVariable('varc', 'cc')
doc = QDomDocument("testdoc")
elem = doc.createElement("qgis")
doc.appendChild(elem)
c1.writeProject(doc)
c2.writeProject(doc)
c3 = QgsMapCanvas()
c3.setObjectName('c1')
c4 = QgsMapCanvas()
c4.setObjectName('c2')
c3.readProject(doc)
c4.readProject(doc)
self.assertEqual(c3.mapSettings().destinationCrs().authid(), 'EPSG:3111')
self.assertEqual(c3.rotation(), 45)
self.assertEqual(set(c3.expressionContextScope().variableNames()), {'vara', 'varb'})
self.assertEqual(c3.expressionContextScope().variable('vara'), 1111)
self.assertEqual(c3.expressionContextScope().variable('varb'), 'bb')
self.assertEqual(c4.mapSettings().destinationCrs().authid(), 'EPSG:4326')
self.assertEqual(c4.rotation(), 65)
self.assertEqual(set(c4.expressionContextScope().variableNames()), {'vara', 'varc'})
self.assertEqual(c4.expressionContextScope().variable('vara'), 2222)
self.assertEqual(c4.expressionContextScope().variable('varc'), 'cc')
示例4: GeometryDiffViewerDialog
# 需要导入模块: from qgis.gui import QgsMapCanvas [as 别名]
# 或者: from qgis.gui.QgsMapCanvas import mapSettings [as 别名]
class GeometryDiffViewerDialog(QDialog):
def __init__(self, geoms, crs, parent = None):
super(GeometryDiffViewerDialog, self).__init__(parent)
self.geoms = geoms
self.crs = crs
self.initGui()
def initGui(self):
layout = QVBoxLayout()
self.tab = QTabWidget()
self.table = QTableView()
self.setLayout(layout)
self.canvas = QgsMapCanvas()
self.canvas.setCanvasColor(Qt.white)
settings = QSettings()
self.canvas.enableAntiAliasing(settings.value("/qgis/enable_anti_aliasing", False, type = bool))
self.canvas.useImageToRender(settings.value("/qgis/use_qimage_to_render", False, type = bool))
self.canvas.mapSettings().setDestinationCrs(self.crs)
action = settings.value("/qgis/wheel_action", 0, type = float)
zoomFactor = settings.value("/qgis/zoom_factor", 2, type = float)
self.canvas.setWheelAction(QgsMapCanvas.WheelAction(action), zoomFactor)
self.panTool = QgsMapToolPan(self.canvas)
self.canvas.setMapTool(self.panTool)
execute(self.createLayers)
model = GeomDiffTableModel(self.data)
self.table.setModel(model)
self.table.resizeColumnsToContents()
self.table.resizeRowsToContents()
self.tab.addTab(self.canvas, "Map view")
self.tab.addTab(self.table, "Table view")
layout.addWidget(self.tab)
self.resize(600, 500)
self.setWindowTitle("Geometry comparison")
def createLayers(self):
textGeometries = []
for geom in self.geoms:
text = geom.exportToWkt()
valid = " -1234567890.,"
text = "".join([c for c in text if c in valid])
textGeometries.append(text.split(","))
lines = difflib.Differ().compare(textGeometries[0], textGeometries[1])
self.data = []
for line in lines:
if line.startswith("+"):
self.data.append([None, line[2:]])
if line.startswith("-"):
self.data.append([line[2:], None])
if line.startswith(" "):
self.data.append([line[2:], line[2:]])
types = [("LineString", lineBeforeStyle, lineAfterStyle),
("Polygon", polygonBeforeStyle, polygonAfterStyle)]
layers = []
extent = self.geoms[0].boundingBox()
for i, geom in enumerate(self.geoms):
geomtype = types[int(geom.type() - 1)][0]
style = types[int(geom.type() - 1)][i + 1]
layer = loadLayerNoCrsDialog(geomtype + "?crs=" + self.crs.authid(), "layer", "memory")
pr = layer.dataProvider()
feat = QgsFeature()
feat.setGeometry(geom)
pr.addFeatures([feat])
layer.loadNamedStyle(style)
layer.updateExtents()
layers.append(layer)
QgsMapLayerRegistry.instance().addMapLayer(layer, False)
extent.combineExtentWith(geom.boundingBox())
layer = loadLayerNoCrsDialog("Point?crs=%s&field=changetype:string" % self.crs.authid(), "points", "memory")
pr = layer.dataProvider()
feats = []
for coords in self.data:
coord = coords[0] or coords[1]
feat = QgsFeature()
x, y = coord.strip().split(" ")
x, y = (float(x), float(y))
pt = QgsGeometry.fromPoint(QgsPoint(x, y))
feat.setGeometry(pt)
if coords[0] is None:
changetype = "A"
elif coords[1] is None:
changetype = "R"
else:
changetype = "U"
feat.setAttributes([changetype])
feats.append(feat)
pr.addFeatures(feats)
layer.loadNamedStyle(pointsStyle)
QgsMapLayerRegistry.instance().addMapLayer(layer, False)
layers.append(layer)
self.mapLayers = [QgsMapCanvasLayer(lay) for lay in layers]
self.canvas.setLayerSet(self.mapLayers)
#.........这里部分代码省略.........
示例5: PdfMaker
# 需要导入模块: from qgis.gui import QgsMapCanvas [as 别名]
# 或者: from qgis.gui.QgsMapCanvas import mapSettings [as 别名]
class PdfMaker(object):
"""A generator that takes a QGIS project file and a layout template and makes a pdf."""
def __init__(self, template_path, debug=False):
"""Constructor.
:param template_path: Absolute path to a QGIS composer template file.
:type template_path: str
"""
gui_flag = True
self.app = QgsApplication(sys.argv, gui_flag)
# Make sure QGIS_PREFIX_PATH is set in your env if needed!
self.app.initQgis()
if debug:
print QgsProviderRegistry.instance().pluginList()
self.canvas = QgsMapCanvas()
self.canvas.enableAntiAliasing(True)
self.template_path = template_path
def __del__(self):
"""Destructor."""
del self.app
def _load_template(self):
"""Load the template.
:return: QgsComposition containing the loaded template.
:rtype: QgsComposition
"""
template_file = file(self.template_path)
template_content = template_file.read()
template_file.close()
document = QDomDocument()
document.setContent(template_content)
composition = QgsComposition(self.canvas.mapSettings())
# You can use this to replace any string like this [key]
# in the template with a new value. e.g. to replace
# [date] pass a map like this {'date': '1 Jan 2012'}
substitution_map = {
'DATE_TIME_START': 'foo',
'DATE_TIME_END': 'bar'}
composition.loadFromTemplate(document, substitution_map)
return composition
def _load_layers(self):
"""Manually load all the layers for our project.
:return: A list of QgsMapLayer instances.
:rtype: list
"""
layers = []
# First the RW layer
host = 'db'
port = '5432'
user = 'docker'
password = 'docker'
dbname = 'gis'
uri = QgsDataSourceURI()
uri.setConnection(host, port, dbname, user, password)
schema = 'public'
table = 'flood_mapper_rw'
geometry_column = 'geometry'
where_clause = ''
title = 'RW'
uri.setDataSource(schema, table, geometry_column, where_clause)
layer = QgsVectorLayer(uri.uri(), title, 'postgres')
QgsMapLayerRegistry.instance().addMapLayer(layer, False)
canvas_layer = QgsMapCanvasLayer(layer)
layers.append(canvas_layer)
# Now the JK layer
path = './data/jk.shp'
title = 'JK'
layer = QgsVectorLayer(path, title, 'ogr')
QgsMapLayerRegistry.instance().addMapLayer(layer, False)
canvas_layer = QgsMapCanvasLayer(layer)
layers.append(canvas_layer)
return layers
def make_pdf(self, pdf_path):
"""Generate a pdf for the given project and template files.
:param pdf_path: Absolute path for the output PDF file.
:type pdf_path: str
"""
layers = self._load_layers()
self.canvas.setLayerSet(layers)
if self.canvas.layerCount() < 1:
#.........这里部分代码省略.........