QGIS/data/counties.dbf | 补丁 | 查看 | 原始文档 | blame | 历史 | |
QGIS/data/counties.prj | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
QGIS/data/counties.shp | 补丁 | 查看 | 原始文档 | blame | 历史 | |
QGIS/data/counties.shp.mwsymb | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
QGIS/data/counties.shx | 补丁 | 查看 | 原始文档 | blame | 历史 | |
QGIS/main.py | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
QGIS/render.py | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
QGIS/ui.py | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
QGIS/说明.txt | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
QGIS/data/counties.dbfBinary files differ
QGIS/data/counties.prj
¶Ô±ÈÐÂÎļþ @@ -0,0 +1 @@ GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]] QGIS/data/counties.shpBinary files differ
QGIS/data/counties.shp.mwsymb
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,10 @@ <MapWinGIS OcxVersion="4.8" FileType="LayerFile" FileVersion="0" Description=""> <Layer LayerType="Shapefile" LayerName="counties" LayerVisible="1" LayerKey="" MinVisibleScale="1.000000" MaxVisibleScale="4901750.000000" Filename=""> <ShapefileClass> <DefaultDrawingOptions FillBgColor="0" FillColor="2928376" FillVisible="0" LineColor="4224439" PointShapeRatio="1.000000" /> <ShapefileCategoriesClass /> <LabelsClass FrameBackColor="16777215" FrameOutlineColor="1971946" FrameType="1" FrameVisible="-1" Generated="1" MaxVisibleScale="4901750.000000" MinVisibleScale="1.000000" AutoOffset="0" Positioning="1" Expression="[MWShapeID] " /> <ChartsClass ValuesFontName="" Visible="-1" /> </ShapefileClass> </Layer> </MapWinGIS> QGIS/data/counties.shxBinary files differ
QGIS/main.py
@@ -1,14 +1,35 @@ from qgis.core import * # from qgis.gui import (QgsLayerTreeMapCanvasBridge) from qgis.gui import * from qgis.PyQt.QtGui import (QColor) # QgsApplication.setPrefixPath("e:/", True) qgs = QgsApplication([], True) qgs.initQgis() # å建å®ä¾ project = QgsProject.instance() project.read(r'E:/data/1.Natural_Earth/Test.qgz') print("FileName: " + project.fileName()) # canvas = QgsMapCanvas() # canvas.show() # bridge = QgsLayerTreeMapCanvasBridge(QgsProject.instance().layerTreeRoot(), canvas) layers_list = {} for layer in project.mapLayers().values(): layers_list[layer.name()] = layer print(layers_list) openStreetMapLayer = project.mapLayersByName("OpenStreetMap")[0] print(openStreetMapLayer) root = project.layerTreeRoot() print(root.findLayerIds()[0]) # ä¿åï¼å¦å为 # project.write() # project.write("E:/new_qgis_project.qgz") qgs.exitQgis() # qgs.exitQgis() QGIS/render.py
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,62 @@ import os from qgis.core import * from qgis.gui import * from qgis.PyQt.QtGui import * from qgis.PyQt.QtCore import * from qgis.PyQt.QtWidgets import * from qgis.PyQt.QtXml import * def finished(): img = render.renderedImage() # QgsProject.instance().homePath() image_location = os.path.join("e:/", "render.png") # save the image; e.g. img.save("/Users/myuser/render.png","png") img.save(image_location, "png") QgsApplication.setPrefixPath("C:/Program Files/QGIS 3.16/bin", True) qgs = QgsApplication([], True) qgs.initQgis() # vlayer = iface.activeLayer() project = QgsProject.instance() project.read(r'E:/data/1.Natural_Earth/Test.qgz') layers = project.mapLayers().values() # root = project.layerTreeRoot() # layout = QgsPrintLayout(project) layout = QgsLayout(project) # layout.initializeDefaults() with open("E:/data/1.Natural_Earth/Layout.qpt", 'r', encoding='utf-8') as f: template_content = f.read() doc = QDomDocument() doc.setContent(template_content) # doc = QDomDocument("E:/data/1.Natural_Earth/Layout.qpt") #ele = layout.writeXml(doc, QgsReadWriteContext()) #layout.readLayoutXml(ele, doc, QgsReadWriteContext()) layout.loadFromTemplate(doc, QgsReadWriteContext(), False) map = QgsLayoutItemMap(layout) # # map.zoomToExtent(layers[0].extent()) layout.addItem(map) # collection = layout.pageCollection() # page = QgsLayoutItemPage(layout) # page.setPageSize('A4') # collection.addPage(page) layoutView = QgsLayoutView() layoutView.setPreviewModeEnabled(True) layoutView.setContentsMargins(0, 0, 0, 0) layoutView.setCurrentLayout(layout) # project.layoutManager().addLayout(layout) # map = QgsLayoutItemMap(layout) # map.zoomToExtent(iface.mapCanvas().extent()) exporter = QgsLayoutExporter(layout) img_path = os.path.join("e:/", "render.png") exporter.exportToImage(img_path, QgsLayoutExporter.ImageExportSettings()) qgs.exitQgis() QGIS/ui.py
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,48 @@ import os import sys from qgis.gui import * from qgis.core import * from qgis.PyQt.QtCore import * from qgis.PyQt.QtWidgets import * class MapViewer(QMainWindow): def __init__(self, shapefile): QMainWindow.__init__(self) self.setWindowTitle("Map Viewer") canvas = QgsMapCanvas() canvas.setCanvasColor(Qt.white) canvas.show() project = QgsProject.instance() project.read(r'E:/data/1.Natural_Earth/Test.qgz') layer = QgsVectorLayer(shapefile, "layer1", "ogr") if not layer.isValid(): raise IOError("Invalid shapefile") # project.addMapLayer(layer) # canvas.setExtent(layer.extent()) # canvas.setLayers([layer]) layers = project.mapLayers().values() canvas.setLayers(layers) layout = QVBoxLayout() layout.addWidget(canvas) contents = QWidget() contents.setLayout(layout) self.setCentralWidget(contents) if __name__ == "__main__": QgsApplication.setPrefixPath("qgis", True) qgs = QgsApplication([], True) qgs.initQgis() viewer = MapViewer("./data/Counties.shp") viewer.show() exitCode = qgs.exec() qgs.exitQgis() QGIS/˵Ã÷.txt
@@ -21,6 +21,7 @@ >>> import qgis.core ================================================================================ åºç®ä» æ ¼å¼åï¼ctrl + alt + l PyQGISåºçäºä¸ªå ï¼ qgis.core è¿æ ·å¯ä»¥è®¿é®æ´ä¸ªQGISä¸ä½¿ç¨çæ ¸å¿GISåè½ã qgis.gui è¿å®ä¹äºä¸ç³»åGUIå°é¨ä»¶ï¼æ¨å¯ä»¥å¨èªå·±çç¨åºä¸å å«è¿äºå°é¨ä»¶ã @@ -28,10 +29,12 @@ qgis.networkanalysis è¿æä¾äºæå»ºååæææçå·¥å ·ã qgis.utils è¿å®ç°äºåç§åè½ï¼å 许æ¨ä½¿ç¨Python使ç¨QGISåºç¨ç¨åºã ================================================================================ æ§è¡ exec(open('E:/terrait/TianJin/LFServer/QGIS/main.py', encoding='utf-8').read()) exec(open('E:/terrait/TianJin/LFServer/QGIS/render.py', encoding='utf-8').read()) python E:/terrait/TianJin/LFServer/QGIS/main.py ================================================================================ https://blog.csdn.net/qq_23034515/article/details/107643428 https://vimsky.com/examples/detail/python-ex-qgis.core-QgsPrintLayout-itemByUuid-method.html # ReadWriteXml 1.æå»ºQGIS+Pythonå¼åç¯å¢ 2.æ¿å¾·é«æ°åºææ¥å¹³å°ï¼å°å对讲éæå°ä¸»ç«ç¹(httpåè®®)ä¸ 3.qgis pythonäºæ¬¡å¼å