[Qgis-developer] Using Processing in standalone PYQGIS scripts

Eric Goddard egoddard1010 at gmail.com
Fri Sep 30 06:51:43 PDT 2016


Thanks Victor, I had tried that within the QGIS Python console and got
the same error about the number of arguments. When I drop the OUTPUT
parameter it runs, so now my call looks like this:

processing.runalg('qgis:selectbylocation', "photos", "boundaries",
u'intersect', 0.0, 0)

If I run just this line in the interpreter, it outputs:  {'OUTPUT':
u'/home/eric/Development/csi_data_collection/scripts/photos.gpkg'}

However, it doesn't select any of the points within the selected
polygon boundary. If I run the tool from the Processing Toolbox, it
does select the points within the boundary. When running that tool
from python should it use selected features in the intersection layer?
I tried it with no boundaries selected and it still didn't select
anything.

Thanks again for the response. Making progress, but it would still be
good to get clarification on how/if Processing can be used from
standalone scripts. in the call to to runalg I did have to change the
layer inputs from the variables containing references to the
QgsVectorLayer objects to the layer name that I passed to those vector
layers on creation. Processing also couldn't find the layers until I
added them to the QgsMapLayerRegistry instance, which I think means
when I instantiate the QgsApplication in the standalone script I need
to pass True instead of False for it to use the GUI?



On Thu, Sep 29, 2016 at 4:25 PM, Victor Olaya <volayaf at gmail.com> wrote:
> looks like the docs are outdated and the way to call the algorithm is
> now different
>
> ALGORITHM: Select by location
>     INPUT <ParameterVector>
>     INTERSECT <ParameterVector>
>     PREDICATE <ParameterGeometryPredicate>
>     PRECISION <ParameterNumber>
>     METHOD <ParameterSelection>
>     OUTPUT <OutputVector>
>
> The PRECISION parameter is not documented in the help file.
>
> Hope this helps
>
> 2016-09-29 22:39 GMT+02:00 Eric Goddard <egoddard1010 at gmail.com>:
>> Hello everyone,
>>
>> I'm using QGIS 2.16.3 on Ubuntu 14.04 (from the qgis.org/ubuntugis
>> repo with ubuntugis-unstable ppa). I have a geopackage layer (I
>> originally started out with a spatialitelayer, but I kept getting
>>
>> ASSERT: "i.value() == handle" in file
>> /tmp/buildd/qgis-2.16.3+20trusty/src/providers/spatialite/qgsspatialiteconnection.cpp,
>> line 780
>> Aborted (core dumped)
>>
>> when calling getFeatures() on the layer) with two columns that I want
>> to populate. One contains the month and year a photo was taken,
>> extracted from EXIF metadata. The other is for the name of the polygon
>> that contains the point feature. Extracting the date from the metadata
>> works fine, but I'm having some serious difficulties with running the
>> 'qgis:selectbylocation' processing algorithm in a standalone script.
>> Stack Overflow has several posts on using Processing in standalone
>> scripts, the most recent of which [1] I tried. It crashes on line 15
>> in my script [2](Processing.initialize()) with the following error
>> message:
>>
>> The current error message I'm getting is:
>> Error in sys.excepthook:
>> Traceback (most recent call last):
>>   File "/usr/lib/python2.7/dist-packages/qgis/utils.py", line 196, in
>> qgis_excepthook
>>     showException(type, value, tb, None, messagebar=True)
>>   File "/usr/lib/python2.7/dist-packages/qgis/utils.py", line 107, in
>> showException
>>     open_stack_dialog(type, value, tb, msg)
>>   File "/usr/lib/python2.7/dist-packages/qgis/utils.py", line 142, in
>> open_stack_dialog
>>     iface.messageBar().popWidget()
>> AttributeError: 'NoneType' object has no attribute 'messageBar'
>>
>> Original exception was:
>> Traceback (most recent call last):
>>   File "extract_image_dates.py", line 14, in <module>
>>     Processing.Initialize()
>> AttributeError: class Processing has no attribute 'Initialize'
>>
>>
>> Since I was unable to get it running in the standalone script, I
>> loaded the script I wrote in the QGIS Python console. It ran, but it
>> said that the number of arguments to 'qgis:selectbylocation' was
>> wrong, but my call to the algorithim on line 55 matches the docs [3].
>>
>> Any pointers on how to get this working would be greatly appreciated,
>> and I'd be happy to make a PR on the QGIS Documentation to add a
>> section about it once it works.
>>
>>
>> Thanks!
>> Eric
>>
>> [1] http://gis.stackexchange.com/questions/129915/cannot-run-standalone-qgis-script
>> [2] https://gist.github.com/egoddard/c188c62115f2fe8df24f32b3119e51b3
>> [3] http://docs.qgis.org/2.14/en/docs/user_manual/processing_algs/qgis/vector_selection_tools.html#select-by-location
>> _______________________________________________
>> Qgis-developer mailing list
>> Qgis-developer at lists.osgeo.org
>> List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
>> Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer


More information about the Qgis-developer mailing list