[Qgis-developer] Using Processing in standalone PYQGIS scripts

Eric Goddard egoddard1010 at gmail.com
Fri Sep 30 07:30:17 PDT 2016


Thank you Victor and Etienne, the capital Initialize() got me :( It's
always the little things - sorry about that! Now the processing script
runs successfully as a standalone script but still has the issue of
not selecting anything in the input layer. I was able to use the
QgsVectorLayer variables as inputs to the tool, but it still didn't
select the features.

Thanks,
Eric

On Fri, Sep 30, 2016 at 9:24 AM, Victor Olaya <volayaf at gmail.com> wrote:
> Not sure why it is not selecting your features. I will try to
> reproduce it and will let you know
>
> About the layers and how they are found, here is some info: Input
> layers for Processing are data sources (if a layer is file based, that
> means the filepath to it). If you enter something that is not that,
> Processing tries to figure out what you mean, basically checking if
> any of your layers has the name you entered, and then using it's
> layer.source() value. But for that, the layer must be loaded. To avoid
> that, you can directly enter the path to your layer, instead of a
> layer name
>
> I will let you know once i can reproduce your issue and find out what
> is happening
>
> Cheers
>
> 2016-09-30 16:07 GMT+02:00 Etienne Trimaille <etienne.trimaille at gmail.com>:
>>> AttributeError: class Processing has no attribute 'Initialize'
>>
>> Did you try : 'initialize' ?
>> According to
>> https://github.com/qgis/QGIS/blob/master/python/plugins/processing/core/Processing.py#L146
>>
>> 2016-09-30 20:51 GMT+07:00 Eric Goddard <egoddard1010 at gmail.com>:
>>>
>>> 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
>>> _______________________________________________
>>> 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