――Since I have just started studying GIS (QGIS), there are many doubts about my understanding.
--Select "Python Console" from "Plugins" in the QGIS menu bar --Select the editor display from the Python console icon ――After that, do not use the interactive console, but write and execute the code with an editor.
test.py
import platform
print(platform.python_version())
--The execution result is displayed on the console → 2.7.5
――It seems to be Python of 2.x series
test.py
import os
print(os.getcwd())
--Execution result → C: \ PROGRA ~ 1 \ QGIS2 ~ 1.18 \ bin
- C:\Program Files\QGIS 2.18\bin
test.py
filePath='C:/Users/xxxx/Documents/GIS DataBase/PyTest/lay01.shp'
iface.addVectorLayer(filePath,'test','ogr')
--First argument: File path
--The file path can also be described using "slash" instead of "".
--Be careful of escape sequences when using ""
--Second argument: The name you want to give to the layer
--In the case of the above code, the layer name on QGIS will be "test lay01 Polygon"
--Second argument + SP + file name + SP + type
-
--Third argument: Unknown for now ʻogr.
providerKey --Don't make it ʻorg
.
--Select the layer with the mouse etc. in advance.
test.py
layer=iface.activeLayer()
print('\n'.join(dir(layer)))
--You can get the member list (list of available methods etc.) of xxx object with dir (xxx)
geometryType()
、getFeatures()
--For details, see QgsVectorLayer Class Reference――It seems that points (points), lines (polylines, lines), and polygons (faces) cannot be mixed in the vector layer of QGIS ?. --Therefore, the type of vector layer can be (probably) point, line, or polygon. -
--Points, lines, and polygons are collectively called geometry.
test.py
layer=iface.activeLayer()
type = layer.geometryType()
if type == QGis.Point :
print('Point')
elif type == QGis.Line :
print('Line')
elif type == QGis.Polygon:
print('Polygon')
--Assuming that the feature has an attribute column called ʻid`. -
--Suppose a feature is selected singularly or multiple times.
test.py
layer=iface.activeLayer()
features = layer.selectedFeatures()
for feature in features:
print(feature['id'])
--Use layer.getFeatures ()
if you want to target all features on the active layer, with or without selection.
test.py
layer=iface.activeLayer()
features = layer.getFeatures()
for feature in features:
print(feature['id'])
test.py
# -*- coding: utf-8 -*-
layer=iface.activeLayer()
if layer is None :
print(u'There is no active layer.')
if layer.geometryType() != QGis.Polygon:
print(u'The geometry type of the active layer is not polygon.')
print(u'Active layer{0}Executes the process for.'.format(layer.name()))
print('')
for feature in layer.getFeatures():
print(u'ID={0}'.format(feature['id']))
if feature.geometry().isMultipart():
polygons = feature.geometry().asMultiPolygon()
else:
polygons = [ feature.geometry().asPolygon() ]
for polygon in polygons:
for vertices in polygon:
for vertex in vertices:
print('X={0:.9f} Y={1:.9f}'.format(vertex.x(),vertex.y()))
print('')
--The above vertex
is an instance of class'qgis._core.QgsPoint'
--For details, see QgsPoint Class Reference
--Get the bounding box for all features in the layer
--The bounding box is the QgsRectangle
class
--For details, see QgsRectangle Class Reference
test.py
# -*- coding: utf-8 -*-
layer=iface.activeLayer()
if layer is None :
print(u'There is no active layer.')
box = layer.extent()
print('xMinimum={0:.9f}'.format(box.xMinimum()))
print('xMaximum={0:.9f}'.format(box.xMaximum()))
print('yMinimum={0:.9f}'.format(box.yMinimum()))
print('yMaximum={0:.9f}'.format(box.yMaximum()))
print('width={0:.9f}'.format(box.width()))
print('height={0:.9f}'.format(box.height()))
-Introduction to QGIS Programming 2016 Osaka Edition
Recommended Posts