QGIS Processing Script Stops Working After First RunGraphical Modeler to rotate points based on line layer...
Can 5 Aarakocra PCs summon an Air Elemental?
Is using an 'empty' metaphor considered bad style?
False written accusations not made public - is there law to cover this?
Plausible reason for gold-digging ant
How to assess the long-term stability of a college as part of a job search
Why are all my replica super soldiers young adults or old teenagers?
Square Root Distance from Integers
Current across a wire with zero potential difference
What game did these black and yellow dice come from?
Building an exterior wall within an exterior wall for insulation
Is the child responsible for the Parent PLUS Loan when the parent has passed away?
How to deal with possible delayed baggage?
How do you catch Smeargle in Pokemon Go?
Has Britain negotiated with any other countries outside the EU in preparation for the exit?
Does it take energy to move something in a circle?
How do you voice extended chords?
Why does magnet wire need to be insulated?
How do I prevent a homebrew Grappling Hook feature from trivializing Tomb of Annihilation?
Potential client has a problematic employee I can't work with
Has any human ever had the choice to leave Earth permanently?
Globe trotting Grandpa. Where is he going next?
I have trouble understanding this fallacy: "If A, then B. Therefore if not-B, then not-A."
How does Leonard in "Memento" remember reading and writing?
Do authors have to be politically correct in article-writing?
QGIS Processing Script Stops Working After First Run
Graphical Modeler to rotate points based on line layer azimuthCreating points from lines with length and rotation fields using script in QGIS?QGIS distance from point to line along a specific bearingCannot generate buffers in meters (even after “save as” in NAD83 / UTM 18N) using QGIS?spurious “.tif Exists” errors on QGIS Python script processingQGIS running processing algorithm in Python Console does not store Outputfile?NameError when running script QGIS Python 3Convert layer raster to vector: qgis:polygonizeSAGA Raster Calculator in QGIS Algorithm script produces 'NoneType' object has no attribute 'crs'How do I set QgsVectorFileWriter.SaveVectorOptions for QGIS 3?
I have a script that creates a multi-distance buffer rings and have managed to migrate it successfully to QGIS 3.4.5. This script requires me to select a layer, and a feature in the layer before I run it, as such:
from math import *
from qgis.core import *
from qgis.utils import iface
from PyQt5.QtCore import *
geom = iface.activeLayer().selectedFeatures()[0].geometry()
start_x = geom.asPoint().x()
start_y = geom.asPoint().y()
sides = 64
radius = 6378137.0 # meters
# create layer
vl = QgsVectorLayer("Polygon", "Distance Buffers", "memory")
pr = vl.dataProvider()
# changes are only possible when editing the layer
vl.startEditing()
# add fields
pr.addAttributes([QgsField("Distance", QVariant.Int), QgsField("Label", QVariant.String)])
distance = [250, 500, 1000, 2000, 3000, 4000, 5000, 10000, 15000, 20000, 25000, 30000, 40000, 50000]
for i in range(len(distance)):
points = []
dist = distance[i]
degrees = 0
while degrees <= 360:
degrees = degrees + 360 / sides
start_lon = start_x * pi / 180
start_lat = start_y * pi / 180
bearing = degrees * pi / 180
end_lat = asin((sin(start_lat) * cos(dist / radius)) + (cos(start_lat) * sin(dist / radius) * cos(bearing)))
end_lon = start_lon + atan2(sin(bearing) * sin(dist / radius) * cos(start_lat),
cos(dist / radius) - (sin(start_lat) * sin(end_lat)))
points.append(QgsPointXY(end_lon * 180 / pi, end_lat * 180 / pi))
fet_name = str(distance[i])
if distance[i] < 1000:
label = str(distance[i]) + "m"
else:
label = str(distance[i]/1000) + "km"
# add a feature
fet = QgsFeature()
geometry = QgsGeometry.fromPolygonXY([points])
fet.setGeometry(geometry)
fet.setAttributes([fet_name,label])
pr.addFeatures([fet])
# commit to stop editing the layer
vl.commitChanges()
# update layer's extent when new features have been added
# because change of extent in provider is not propagated to the layer
vl.updateExtents()
myDir = QgsProject.instance().readPath("./")
file_name = 'Distance Buffers' + '.shp'
ShapefileDir = myDir + "/Shapefiles/" + file_name
_writer = QgsVectorFileWriter.writeAsVectorFormat(vl, ShapefileDir, "utf-8", driverName="ESRI Shapefile")
# add layer to the legend
name = 'Distance Buffers'
BufferShp = QgsVectorLayer(ShapefileDir,name, 'ogr')
QgsProject.instance().addMapLayer(BufferShp)
# change active layer
lyr = QgsProject.instance().mapLayersByName(name)[0]
iface.setActiveLayer(lyr)
buffer_lyr = iface.activeLayer()
# load preset qgis style file
buffer_lyr.loadNamedStyle("W:/2. SGER-Economics/c. Project Resources/8. GIS/QGIS Style File/Buffer.qml")
buffer_lyr.triggerRepaint()
I have noticed a few strange behaviors:
When I 'Add Script to Toolbox', it immediately tries to run the script, rather than just updating the toolbox to include the selected script.
After I run it successfully for the first time, I see that the script is now added to my toolbox. However, when I try to run it again, nothing happens. To run it I have to manually open the script in the Processing Script Editor, then Run Script.
After the script is added, whenever I start a new instance of QGIS, I get an error message as such:
An error has occurred while executing Python code:
AttributeError: 'NoneType' object has no attribute 'selectedFeatures'
Traceback (most recent call last): File
"C:/PROGRA~1/QGIS3~1.4/apps/qgis-ltr/./python/pluginsprocessingscriptScriptAlgorithmProvider.py",
line 111, in loadAlgorithms
alg = ScriptUtils.loadAlgorithm(moduleName, filePath) File "C:/PROGRA~1/QGIS3~1.4/apps/qgis-ltr/./python/pluginsprocessingscriptScriptUtils.py",
line 68, in loadAlgorithm
spec.loader.exec_module(module) File "", line 728, in exec_module File "", line 219, in _call_with_frames_removed File
"file05Production2. SGER-Economicsc. Project Resources8.
GISQGIS Processing ScriptsBuffer_QGIS3.py", line 6, in
geom = iface.activeLayer().selectedFeatures()[0].geometry() AttributeError: 'NoneType' object has no attribute 'selectedFeatures'
I assume all of this happens because for some reason QGIS is trying to run my script on its own whenever it opens. Is there something wrong with my script or is this a bug with the new QGIS?
qgis pyqgis qgis-3 pyqgis-3
add a comment |
I have a script that creates a multi-distance buffer rings and have managed to migrate it successfully to QGIS 3.4.5. This script requires me to select a layer, and a feature in the layer before I run it, as such:
from math import *
from qgis.core import *
from qgis.utils import iface
from PyQt5.QtCore import *
geom = iface.activeLayer().selectedFeatures()[0].geometry()
start_x = geom.asPoint().x()
start_y = geom.asPoint().y()
sides = 64
radius = 6378137.0 # meters
# create layer
vl = QgsVectorLayer("Polygon", "Distance Buffers", "memory")
pr = vl.dataProvider()
# changes are only possible when editing the layer
vl.startEditing()
# add fields
pr.addAttributes([QgsField("Distance", QVariant.Int), QgsField("Label", QVariant.String)])
distance = [250, 500, 1000, 2000, 3000, 4000, 5000, 10000, 15000, 20000, 25000, 30000, 40000, 50000]
for i in range(len(distance)):
points = []
dist = distance[i]
degrees = 0
while degrees <= 360:
degrees = degrees + 360 / sides
start_lon = start_x * pi / 180
start_lat = start_y * pi / 180
bearing = degrees * pi / 180
end_lat = asin((sin(start_lat) * cos(dist / radius)) + (cos(start_lat) * sin(dist / radius) * cos(bearing)))
end_lon = start_lon + atan2(sin(bearing) * sin(dist / radius) * cos(start_lat),
cos(dist / radius) - (sin(start_lat) * sin(end_lat)))
points.append(QgsPointXY(end_lon * 180 / pi, end_lat * 180 / pi))
fet_name = str(distance[i])
if distance[i] < 1000:
label = str(distance[i]) + "m"
else:
label = str(distance[i]/1000) + "km"
# add a feature
fet = QgsFeature()
geometry = QgsGeometry.fromPolygonXY([points])
fet.setGeometry(geometry)
fet.setAttributes([fet_name,label])
pr.addFeatures([fet])
# commit to stop editing the layer
vl.commitChanges()
# update layer's extent when new features have been added
# because change of extent in provider is not propagated to the layer
vl.updateExtents()
myDir = QgsProject.instance().readPath("./")
file_name = 'Distance Buffers' + '.shp'
ShapefileDir = myDir + "/Shapefiles/" + file_name
_writer = QgsVectorFileWriter.writeAsVectorFormat(vl, ShapefileDir, "utf-8", driverName="ESRI Shapefile")
# add layer to the legend
name = 'Distance Buffers'
BufferShp = QgsVectorLayer(ShapefileDir,name, 'ogr')
QgsProject.instance().addMapLayer(BufferShp)
# change active layer
lyr = QgsProject.instance().mapLayersByName(name)[0]
iface.setActiveLayer(lyr)
buffer_lyr = iface.activeLayer()
# load preset qgis style file
buffer_lyr.loadNamedStyle("W:/2. SGER-Economics/c. Project Resources/8. GIS/QGIS Style File/Buffer.qml")
buffer_lyr.triggerRepaint()
I have noticed a few strange behaviors:
When I 'Add Script to Toolbox', it immediately tries to run the script, rather than just updating the toolbox to include the selected script.
After I run it successfully for the first time, I see that the script is now added to my toolbox. However, when I try to run it again, nothing happens. To run it I have to manually open the script in the Processing Script Editor, then Run Script.
After the script is added, whenever I start a new instance of QGIS, I get an error message as such:
An error has occurred while executing Python code:
AttributeError: 'NoneType' object has no attribute 'selectedFeatures'
Traceback (most recent call last): File
"C:/PROGRA~1/QGIS3~1.4/apps/qgis-ltr/./python/pluginsprocessingscriptScriptAlgorithmProvider.py",
line 111, in loadAlgorithms
alg = ScriptUtils.loadAlgorithm(moduleName, filePath) File "C:/PROGRA~1/QGIS3~1.4/apps/qgis-ltr/./python/pluginsprocessingscriptScriptUtils.py",
line 68, in loadAlgorithm
spec.loader.exec_module(module) File "", line 728, in exec_module File "", line 219, in _call_with_frames_removed File
"file05Production2. SGER-Economicsc. Project Resources8.
GISQGIS Processing ScriptsBuffer_QGIS3.py", line 6, in
geom = iface.activeLayer().selectedFeatures()[0].geometry() AttributeError: 'NoneType' object has no attribute 'selectedFeatures'
I assume all of this happens because for some reason QGIS is trying to run my script on its own whenever it opens. Is there something wrong with my script or is this a bug with the new QGIS?
qgis pyqgis qgis-3 pyqgis-3
add a comment |
I have a script that creates a multi-distance buffer rings and have managed to migrate it successfully to QGIS 3.4.5. This script requires me to select a layer, and a feature in the layer before I run it, as such:
from math import *
from qgis.core import *
from qgis.utils import iface
from PyQt5.QtCore import *
geom = iface.activeLayer().selectedFeatures()[0].geometry()
start_x = geom.asPoint().x()
start_y = geom.asPoint().y()
sides = 64
radius = 6378137.0 # meters
# create layer
vl = QgsVectorLayer("Polygon", "Distance Buffers", "memory")
pr = vl.dataProvider()
# changes are only possible when editing the layer
vl.startEditing()
# add fields
pr.addAttributes([QgsField("Distance", QVariant.Int), QgsField("Label", QVariant.String)])
distance = [250, 500, 1000, 2000, 3000, 4000, 5000, 10000, 15000, 20000, 25000, 30000, 40000, 50000]
for i in range(len(distance)):
points = []
dist = distance[i]
degrees = 0
while degrees <= 360:
degrees = degrees + 360 / sides
start_lon = start_x * pi / 180
start_lat = start_y * pi / 180
bearing = degrees * pi / 180
end_lat = asin((sin(start_lat) * cos(dist / radius)) + (cos(start_lat) * sin(dist / radius) * cos(bearing)))
end_lon = start_lon + atan2(sin(bearing) * sin(dist / radius) * cos(start_lat),
cos(dist / radius) - (sin(start_lat) * sin(end_lat)))
points.append(QgsPointXY(end_lon * 180 / pi, end_lat * 180 / pi))
fet_name = str(distance[i])
if distance[i] < 1000:
label = str(distance[i]) + "m"
else:
label = str(distance[i]/1000) + "km"
# add a feature
fet = QgsFeature()
geometry = QgsGeometry.fromPolygonXY([points])
fet.setGeometry(geometry)
fet.setAttributes([fet_name,label])
pr.addFeatures([fet])
# commit to stop editing the layer
vl.commitChanges()
# update layer's extent when new features have been added
# because change of extent in provider is not propagated to the layer
vl.updateExtents()
myDir = QgsProject.instance().readPath("./")
file_name = 'Distance Buffers' + '.shp'
ShapefileDir = myDir + "/Shapefiles/" + file_name
_writer = QgsVectorFileWriter.writeAsVectorFormat(vl, ShapefileDir, "utf-8", driverName="ESRI Shapefile")
# add layer to the legend
name = 'Distance Buffers'
BufferShp = QgsVectorLayer(ShapefileDir,name, 'ogr')
QgsProject.instance().addMapLayer(BufferShp)
# change active layer
lyr = QgsProject.instance().mapLayersByName(name)[0]
iface.setActiveLayer(lyr)
buffer_lyr = iface.activeLayer()
# load preset qgis style file
buffer_lyr.loadNamedStyle("W:/2. SGER-Economics/c. Project Resources/8. GIS/QGIS Style File/Buffer.qml")
buffer_lyr.triggerRepaint()
I have noticed a few strange behaviors:
When I 'Add Script to Toolbox', it immediately tries to run the script, rather than just updating the toolbox to include the selected script.
After I run it successfully for the first time, I see that the script is now added to my toolbox. However, when I try to run it again, nothing happens. To run it I have to manually open the script in the Processing Script Editor, then Run Script.
After the script is added, whenever I start a new instance of QGIS, I get an error message as such:
An error has occurred while executing Python code:
AttributeError: 'NoneType' object has no attribute 'selectedFeatures'
Traceback (most recent call last): File
"C:/PROGRA~1/QGIS3~1.4/apps/qgis-ltr/./python/pluginsprocessingscriptScriptAlgorithmProvider.py",
line 111, in loadAlgorithms
alg = ScriptUtils.loadAlgorithm(moduleName, filePath) File "C:/PROGRA~1/QGIS3~1.4/apps/qgis-ltr/./python/pluginsprocessingscriptScriptUtils.py",
line 68, in loadAlgorithm
spec.loader.exec_module(module) File "", line 728, in exec_module File "", line 219, in _call_with_frames_removed File
"file05Production2. SGER-Economicsc. Project Resources8.
GISQGIS Processing ScriptsBuffer_QGIS3.py", line 6, in
geom = iface.activeLayer().selectedFeatures()[0].geometry() AttributeError: 'NoneType' object has no attribute 'selectedFeatures'
I assume all of this happens because for some reason QGIS is trying to run my script on its own whenever it opens. Is there something wrong with my script or is this a bug with the new QGIS?
qgis pyqgis qgis-3 pyqgis-3
I have a script that creates a multi-distance buffer rings and have managed to migrate it successfully to QGIS 3.4.5. This script requires me to select a layer, and a feature in the layer before I run it, as such:
from math import *
from qgis.core import *
from qgis.utils import iface
from PyQt5.QtCore import *
geom = iface.activeLayer().selectedFeatures()[0].geometry()
start_x = geom.asPoint().x()
start_y = geom.asPoint().y()
sides = 64
radius = 6378137.0 # meters
# create layer
vl = QgsVectorLayer("Polygon", "Distance Buffers", "memory")
pr = vl.dataProvider()
# changes are only possible when editing the layer
vl.startEditing()
# add fields
pr.addAttributes([QgsField("Distance", QVariant.Int), QgsField("Label", QVariant.String)])
distance = [250, 500, 1000, 2000, 3000, 4000, 5000, 10000, 15000, 20000, 25000, 30000, 40000, 50000]
for i in range(len(distance)):
points = []
dist = distance[i]
degrees = 0
while degrees <= 360:
degrees = degrees + 360 / sides
start_lon = start_x * pi / 180
start_lat = start_y * pi / 180
bearing = degrees * pi / 180
end_lat = asin((sin(start_lat) * cos(dist / radius)) + (cos(start_lat) * sin(dist / radius) * cos(bearing)))
end_lon = start_lon + atan2(sin(bearing) * sin(dist / radius) * cos(start_lat),
cos(dist / radius) - (sin(start_lat) * sin(end_lat)))
points.append(QgsPointXY(end_lon * 180 / pi, end_lat * 180 / pi))
fet_name = str(distance[i])
if distance[i] < 1000:
label = str(distance[i]) + "m"
else:
label = str(distance[i]/1000) + "km"
# add a feature
fet = QgsFeature()
geometry = QgsGeometry.fromPolygonXY([points])
fet.setGeometry(geometry)
fet.setAttributes([fet_name,label])
pr.addFeatures([fet])
# commit to stop editing the layer
vl.commitChanges()
# update layer's extent when new features have been added
# because change of extent in provider is not propagated to the layer
vl.updateExtents()
myDir = QgsProject.instance().readPath("./")
file_name = 'Distance Buffers' + '.shp'
ShapefileDir = myDir + "/Shapefiles/" + file_name
_writer = QgsVectorFileWriter.writeAsVectorFormat(vl, ShapefileDir, "utf-8", driverName="ESRI Shapefile")
# add layer to the legend
name = 'Distance Buffers'
BufferShp = QgsVectorLayer(ShapefileDir,name, 'ogr')
QgsProject.instance().addMapLayer(BufferShp)
# change active layer
lyr = QgsProject.instance().mapLayersByName(name)[0]
iface.setActiveLayer(lyr)
buffer_lyr = iface.activeLayer()
# load preset qgis style file
buffer_lyr.loadNamedStyle("W:/2. SGER-Economics/c. Project Resources/8. GIS/QGIS Style File/Buffer.qml")
buffer_lyr.triggerRepaint()
I have noticed a few strange behaviors:
When I 'Add Script to Toolbox', it immediately tries to run the script, rather than just updating the toolbox to include the selected script.
After I run it successfully for the first time, I see that the script is now added to my toolbox. However, when I try to run it again, nothing happens. To run it I have to manually open the script in the Processing Script Editor, then Run Script.
After the script is added, whenever I start a new instance of QGIS, I get an error message as such:
An error has occurred while executing Python code:
AttributeError: 'NoneType' object has no attribute 'selectedFeatures'
Traceback (most recent call last): File
"C:/PROGRA~1/QGIS3~1.4/apps/qgis-ltr/./python/pluginsprocessingscriptScriptAlgorithmProvider.py",
line 111, in loadAlgorithms
alg = ScriptUtils.loadAlgorithm(moduleName, filePath) File "C:/PROGRA~1/QGIS3~1.4/apps/qgis-ltr/./python/pluginsprocessingscriptScriptUtils.py",
line 68, in loadAlgorithm
spec.loader.exec_module(module) File "", line 728, in exec_module File "", line 219, in _call_with_frames_removed File
"file05Production2. SGER-Economicsc. Project Resources8.
GISQGIS Processing ScriptsBuffer_QGIS3.py", line 6, in
geom = iface.activeLayer().selectedFeatures()[0].geometry() AttributeError: 'NoneType' object has no attribute 'selectedFeatures'
I assume all of this happens because for some reason QGIS is trying to run my script on its own whenever it opens. Is there something wrong with my script or is this a bug with the new QGIS?
qgis pyqgis qgis-3 pyqgis-3
qgis pyqgis qgis-3 pyqgis-3
asked 6 mins ago
Park Sang JinPark Sang Jin
615
615
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "79"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f313704%2fqgis-processing-script-stops-working-after-first-run%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Geographic Information Systems Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f313704%2fqgis-processing-script-stops-working-after-first-run%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown