https://issues.qgis.org/https://issues.qgis.org/favicon.ico2019-02-07T02:41:21ZQGIS Issue TrackingQGIS Application - Bug report #21195: processing.run('native:clip') creates empty shapefile when called externally (via Python bindings)https://issues.qgis.org/issues/21195?journal_id=996502019-02-07T02:41:21ZChristoph Noltenolte.christoph@gmail.com
<ul></ul><p>Apologies - seems I didn't get the formatting right the first time. Here's how my code looks like:</p>
<pre>
import os, sys
QGIS_ENV = os.path.expanduser('~/Anaconda/envs/gis/')
sys.path.append(QGIS_ENV + 'QGIS.app/Contents/Resources/python')
sys.path.append(QGIS_ENV + 'QGIS.app/Contents/Resources/python/plugins')
from qgis.core import *
QgsApplication.setPrefixPath(os.path.join(QGIS_ENV, 'Contents'))
qgs = QgsApplication([], False)
qgs.initQgis()
import processing
from processing.core.Processing import Processing
Processing.initialize()
from qgis.analysis import QgsNativeAlgorithms
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
# Shortcut to working folder
wf = lambda x: os.path.expanduser('~/Dropbox/Data/GIS/USA/GDB/- temp/' + x)
# Shortcut for QgsVectorLayer
QVL = lambda filepath: QgsVectorLayer(filepath, '', 'ogr')
processing.run('native:clip',
{'INPUT': QVL(wf('MA_rivers.shp')),
'OUTPUT': wf('clipped.shp'),
'OVERLAY': QVL(wf('25017_county_2016.shp'))})
</pre> QGIS Application - Bug report #21195: processing.run('native:clip') creates empty shapefile when called externally (via Python bindings)https://issues.qgis.org/issues/21195?journal_id=996512019-02-07T03:56:41ZNyall Dawson
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Feedback</i></li></ul><p>Do you get any console output when running your script?</p> QGIS Application - Bug report #21195: processing.run('native:clip') creates empty shapefile when called externally (via Python bindings)https://issues.qgis.org/issues/21195?journal_id=997102019-02-07T16:17:46ZChristoph Noltenolte.christoph@gmail.com
<ul></ul><p>I usually run this in Jupyter, but I just ran it in the Python shell. The only console outputs I get are "True" after telling QGIS to load the native algorithms, and then the output dictionary returned from 'native:clip'. The resulting shapefile is still empty.</p>
<pre>
>>> import os, sys
>>> QGIS_ENV = os.path.expanduser('~/Anaconda/envs/gis/')
>>> sys.path.append(QGIS_ENV + 'QGIS.app/Contents/Resources/python')
>>> sys.path.append(QGIS_ENV + 'QGIS.app/Contents/Resources/python/plugins')
>>>
>>> from qgis.core import *
>>>
>>> QgsApplication.setPrefixPath(os.path.join(QGIS_ENV, 'Contents'))
>>> qgs = QgsApplication([], False)
>>> qgs.initQgis()
>>>
>>> import processing
>>> from processing.core.Processing import Processing
>>> Processing.initialize()
>>>
>>> from qgis.analysis import QgsNativeAlgorithms
>>> QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
True
>>>
>>> # Shortcut to working folder
... wf = lambda x: os.path.expanduser('~/Dropbox/Data/GIS/USA/GDB/- temp/' + x)
>>> # Shortcut for QgsVectorLayer
... QVL = lambda filepath: QgsVectorLayer(filepath, '', 'ogr')
>>>
>>> processing.run('native:clip',
... {'INPUT': QVL(wf('MA_rivers.shp')),
... 'OUTPUT': wf('clipped.shp'),
... 'OVERLAY': QVL(wf('25017_county_2016.shp'))})
{'OUTPUT': '/Users/production/Dropbox/Data/GIS/USA/GDB/- temp/clipped.shp'}
>>>
</pre> QGIS Application - Bug report #21195: processing.run('native:clip') creates empty shapefile when called externally (via Python bindings)https://issues.qgis.org/issues/21195?journal_id=997182019-02-07T22:36:08ZNyall Dawson
<ul></ul><p>I would double check that layers can be read correctly - you may be missing environment setups.</p>
<p>Try adding:</p>
<pre><code>vl = QgsVectorLayer('/path/to/some/existing/shapefile.shp', '', 'ogr')<br /> assert vl.isValid()</code></pre>
<p>and make sure that layers can indeed be read</p> QGIS Application - Bug report #21195: processing.run('native:clip') creates empty shapefile when called externally (via Python bindings)https://issues.qgis.org/issues/21195?journal_id=997232019-02-07T23:53:42ZChristoph Noltenolte.christoph@gmail.com
<ul></ul><p>This is going into the right direction. The QgsVectorLayer isn't valid. However, the filepath is valid (exists). The shapefile is valid (clipping works if done in the QGIS GUI). What would be the next step to troubleshoot this?</p>
<pre>
>>>os.path.exists(wf('25017_county_2016.shp'))
True
>>>QgsVectorLayer(wf('25017_county_2016.shp'), '', 'ogr').isValid()
False
</pre> QGIS Application - Bug report #21195: processing.run('native:clip') creates empty shapefile when called externally (via Python bindings)https://issues.qgis.org/issues/21195?journal_id=997242019-02-08T00:10:52ZChristoph Noltenolte.christoph@gmail.com
<ul></ul><p>Thank you for pointing me in the right direction. I actually did set the PrefixPath incorrectly. I guess I anticipated that I would get an error if the PrefixPath wasn't correct. In the end, replacing the above "setPrefixPath" line with this one solved the issue:</p>
<pre>
QgsApplication.setPrefixPath(QGIS_ENV + 'QGIS.app/Contents/MacOS', True)
</pre>
<p>Would it make sense to create a warning for the user when the PrefixPath is wrong, instead of running the clipping, and make it appear as if everything worked? Some clipping does actually result in legitimate empty files, so it would be good to be warned if there is an issue with loading the layers.</p>
<p>Either way, thanks a lot for your time! I truly appreciate it and can't wait to port my spatial work to standalone scripts.</p> QGIS Application - Bug report #21195: processing.run('native:clip') creates empty shapefile when called externally (via Python bindings)https://issues.qgis.org/issues/21195?journal_id=997392019-02-08T11:14:01ZGiovanni Manghigiovanni.manghi@gmail.com
<ul></ul><p>Christoph Nolte wrote:<br /> line with this one solved the issue:</p>
<p>closing?</p> QGIS Application - Bug report #21195: processing.run('native:clip') creates empty shapefile when called externally (via Python bindings)https://issues.qgis.org/issues/21195?journal_id=997492019-02-08T12:52:55ZChristoph Noltenolte.christoph@gmail.com
<ul></ul><p>Yes, this particular question has been solved.</p>
<p>I do think it makes sense to have processing/qgis warn users when the loaded shapefile is not valid, instead of just running the algorithm and then creating an empty output. But that's a different feature request.</p> QGIS Application - Bug report #21195: processing.run('native:clip') creates empty shapefile when called externally (via Python bindings)https://issues.qgis.org/issues/21195?journal_id=997542019-02-08T13:01:00ZGiovanni Manghigiovanni.manghi@gmail.com
<ul><li><strong>Resolution</strong> set to <i>worksforme</i></li><li><strong>Status</strong> changed from <i>Feedback</i> to <i>Closed</i></li></ul>