Created
October 2, 2019 18:26
-
-
Save baskaufs/a9889f6990d3e6511bdd593972852d80 to your computer and use it in GitHub Desktop.
GIS text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Creating & editing a new vector layer | |
Code snippets follow along | |
vl = QgsVectorLayer("Point", "temp", "memory") | |
from qgis.PyQt.QtCore import QVariant | |
pr = vl.dataProvider() | |
pr.addAttributes([QgsField("name", QVariant.String), | |
QgsField("age", QVariant.Int), | |
QgsField("size", QVariant.Double)]) | |
vl.updateFields() | |
f = QgsFeature() | |
f.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(10,10))) | |
f.setAttributes(["Ada L.", 2, 0.3]) | |
pr.addFeature(f) | |
vl.updateExtents() | |
QgsProject.instance().addMapLayer(vl) | |
print("No. fields:", len(pr.fields())) | |
print("No. features:", pr.featureCount()) | |
e = vl.extent() | |
print("Extent:", e.xMinimum(), e.yMinimum(), e.xMaximum(), e.yMaximum()) | |
for f in vl.getFeatures(): | |
print("Feature:", f.id(), f.attributes(), f.geometry().asPoint()) | |
vl.startEditing() | |
my_field_name = 'new field' | |
vl.addAttribute(QgsField(my_field_name, QVariant.String)) | |
vl.updateFields() | |
for f in vl.getFeatures(): | |
print("Feature:", f.id(), f.attributes(), f.geometry().asPoint()) | |
my_field_value = 'Hello world!' | |
for f in vl.getFeatures(): | |
f[my_field_name] = my_field_value | |
vl.updateFeature(f) | |
vl.commitChanges() | |
for f in vl.getFeatures(): | |
print("Feature:", f.id(), f.attributes(), f.geometry().asPoint()) | |
iface.vectorLayerTools().stopEditing(vl) | |
my_field_name = 'new field' | |
my_field_value = 'Hello world!' | |
with edit(vl): | |
vl.addAttribute(QgsField(my_field_name, QVariant.String)) | |
vl.updateFields() | |
for f in vl.getFeatures(): | |
f[my_field_name] = my_field_value | |
vl.updateFeature(f) | |
uri = "/Users/johnssc5/Documents/natural_earth_vector.gpkg/packages/natural_earth_vector.gpkg|layername=ne_110m_admin_0_tiny_countries" | |
result = processing.run("native:buffer", | |
{'INPUT':uri,'DISTANCE':10,'SEGMENTS':5,'END_CAP_STYLE':0,'JOIN_STYLE':0,'MITER_LIMIT':2, | |
'DISSOLVE':False,'OUTPUT':'memory:'}) | |
result = processing.run("native:buffer", | |
{'INPUT':uri,'DISTANCE':10,'SEGMENTS':5,'END_CAP_STYLE':0,'JOIN_STYLE':0,'MITER_LIMIT':2, | |
'DISSOLVE':False,'OUTPUT':'memory:'}) | |
QgsProject.instance().addMapLayer(result['OUTPUT']) | |
Chaining Processing tools | |
my_gpkg = 'E:/Geodata/NaturalEarth/natural_earth_vector.gpkg' | |
rivers = '{}|layername=ne_110m_rivers_lake_centerlines'.format(my_gpkg) | |
places = '{}|layername=ne_110m_populated_places'.format(my_gpkg) | |
expression = "name = 'Donau'" | |
danube = processing.run("native:extractbyexpression", | |
{'INPUT':rivers,'EXPRESSION':expression,'OUTPUT':'memory:'} | |
)['OUTPUT'] | |
buffer_distance = 0.1 #degrees | |
buffered_danube = processing.run("native:buffer", | |
{'INPUT':danube,'DISTANCE':buffer_distance,'SEGMENTS':5,'END_CAP_STYLE':0, | |
'JOIN_STYLE':0,'MITER_LIMIT':2,'DISSOLVE':False,'OUTPUT':'memory:'} | |
)['OUTPUT'] | |
places_along_danube = processing.run("native:extractbylocation", | |
{'INPUT':places,'PREDICATE':[0],'INTERSECT':buffered_danube,'OUTPUT':'memory:'} | |
)['OUTPUT'] | |
QgsProject.instance().addMapLayer(places_along_danube) | |
for feature in places_along_danube.getFeatures(): | |
print(feature["name"]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment