QGIS的基本使用,教程推荐
https://study.163.com/course/courseLearn.htm?courseId=1005249002#/learn/video?lessonId=1052147225&courseId=1005249002
这个教程介绍了qgis的功能使用,以项目的形式介绍,快速上手。
读官方文档更好https://docs.qgis.org/3.16/en/docs/user_manual/preamble/preamble.html
设置中文
点击设置选项
选择语言。
使用python控制台
点击显示编辑器, 在里面进行完成业务逻辑。
QGIS使用python控制台加载项目
from qgis.core import (
QgsProject,
QgsPathResolver
)
from qgis.gui import (
QgsLayerTreeMapCanvasBridge,
)
#得到现在的项目名称
project = QgsProject.instance()
print(project.fileName())
#打开其他的项目
#project.read(r'D:\pc\北京.qgz')
#print(project.fileName())
#保存原有项目
project.write()
#保存到一个新的文件
project.write("path/my_new_qgis_project.qgz")
加载图层Layer
import os
from qgis.core import (
QgsVectorLayer,
QgsProject,
QgsApplication,
)
#Vector Layer
path_to_airports_layer = r"F:\桌面\文档\遗传算法\qgis\网上下载的资料\a级景区\等级景区.shp"
vlayer = QgsVectorLayer(path_to_airports_layer,"Airports layer","ogr")
if not vlayer.isValid():
print("Layer failed to load!")
else:
QgsProject.instance().addMapLayer(vlayer)
#加载面
# get the path to a geopackage e.g. /usr/share/qgis/resources/data/world_map.gpkg
path_to_gpkg = os.path.join(QgsApplication.pkgDataPath(), "resources", "data", "world_map.gpkg")
# append the layername part
gpkg_countries_layer = path_to_gpkg + "|layername=countries"
# e.g. gpkg_places_layer = "/usr/share/qgis/resources/data/world_map.gpkg|layername=countries"
vlayer = QgsVectorLayer(gpkg_countries_layer, "Countries layer", "ogr")
if not vlayer.isValid():
print("Layer failed to load!")
else:
QgsProject.instance().addMapLayer(vlayer)
# list of layer names using list comprehension
l = [layer.name() for layer in QgsProject.instance().mapLayers().values()]
# dictionary with key = layer name and value = layer object
layers_list = {}
for l in QgsProject.instance().mapLayers().values():
layers_list[l.name()] = l
print(layers_list)
创建临时图层
from qgis.core import (
QgsProject,
QgsPathResolver
)
from qgis.gui import (
QgsLayerTreeMapCanvasBridge,
)
root = QgsProject.instance().layerTreeRoot()
# create a temporary layer
layer1 = QgsVectorLayer("path_to_layer", "Layer 1", "memory")
# add the layer to the legend, last position
root.addLayer(layer1)
# add the layer at given position
root.insertLayer(5, layer1)
调用算法
https://www.osgeo.cn/qgisdoc/docs/user_manual/processing_algs/qgis/networkanalysis.html#shortest-path-point-to-point
import os
from qgis.core import (
QgsApplication, QgsDataSourceUri, QgsCategorizedSymbolRenderer,
QgsClassificationRange, QgsPointXY, QgsProject, QgsExpression,
QgsField, QgsFields, QgsFeature, QgsFeatureRequest, QgsFeatureRenderer,
QgsGeometry, QgsGraduatedSymbolRenderer, QgsMarkerSymbol,
QgsMessageLog, QgsRectangle, QgsRendererCategory, QgsRendererRange, QgsSymbol,
QgsVectorDataProvider, QgsVectorLayer, QgsVectorFileWriter, QgsWkbTypes,
QgsSpatialIndex, QgsVectorLayerUtils)
from qgis.core.additions.edit import edit
from qgis.PyQt.QtGui import (
QColor,
)
import processing
# 初始化项目
init_pro = QgsProject.instance()
police_points = init_pro.mapLayersByName('layer1')[0]
# 获取点的属性
police_features = police_points.getFeatures()
# 获取点图层
event_points = init_pro.mapLayersByName('layer2')[0]
# 获取属性
events_features = event_points.getFeatures()
# 初始化列表
events_list = []
# 判断事故点状态,是否被处理或者被显示
grid_map = "导航线的路径 线图层"
event_point_status = event_points.isValid() # 判断是否在图层中显示
if event_point_status:
for event_p in events_features:
attrs = event_p.attributes()
state = attrs[2]
e_geom = event_p.geometry() # 是个点的地理信息
geomSingleType = QgsWkbTypes.isSingleType(e_geom.wkbType())
# 判断是否空闲状态,空闲加入队列
if e_geom.type() == QgsWkbTypes.PointGeometry and state == "waiting":
e_x, e_y = e_geom.asPoint().x(), e_geom.asPoint().y()
for police_p in police_features:
attrs = police_p.attributes()
state = attrs[2]
# 判断是否空闲状态,空闲加入队列
p_geom = police_p.geometry() # 是个点的地理信息
geomSingleType = QgsWkbTypes.isSingleType(p_geom.wkbType())
if p_geom.type() == QgsWkbTypes.PointGeometry and state == "free":
p_x, p_y = p_geom.asPoint().x(), p_geom.asPoint().y()
grid_map = init_pro.mapLayersByName('road2')[0] # 通过名字找线图层
start_point = str(p_x) + "," +str(p_y) + " [EPSG:3857]"
end_point = str(e_x) + "," +str(e_y) + " [EPSG:3857]"
map_features = grid_map.getFeatures()
print(p_geom)
print(e_geom)
param = {'DEFAULT_DIRECTION': 2,
'DEFAULT_SPEED': 50,
'DIRECTION_FIELD': '',
'END_POINT': end_point,
'INPUT': 'road2.geojson',
'OUTPUT': 'TEMPORARY_OUTPUT', # 创建临时图层 (TEMPORARY_OUTPUT )保存到文件。。。保存到地理包。。。保存到PostGIS表。。。
'SPEED_FIELD': '',
'START_POINT': start_point, # 起点(x,y)
'STRATEGY': 0, # 0最短, 1最快
'TOLERANCE': 0,
'VALUE_BACKWARD': '',
'VALUE_BOTH': '',
'VALUE_FORWARD': ''}层 (TEMPORARY_OUTPUT )保存到文件。。。保存到地理包。。。保存到PostGIS表。。。
}
processing.run("native:shortestpathpointtopoint", param)
# 参数一般容易配错,不会写参数的话可以在qgis中运行一次算法,然后看上面的运行信息
输入输出都有
其他的看官网,一个套路