It is a pure Python problem and not an GDAL/OGR problem. But if you want to program in Python,you need to understand the difference between an iterator and an iterable (many, many references on the Web, but one of the best is Loop Like A Native by Ned Batchelder). These are fundamental concepts in Python.
"The important operation on an iterable is iter(), which will return an iterator. And the only operation available on an iterator is next(), which either returns the next value, or raises StopIteration, a special exception that means the iteration is finished." (from Ned Batchelder)
An iterable produces an iterator and an iterator produces a stream of values.
So what's the difference between the for loop and pointsLayer.GetNextFeature() ?
the for loops = iterator and pointsLayer= iterable
for feature in iterable:
statements
so with your example:
from osgeo import ogr
source = ogr.Open('yourpointshapefile.shp')
pointsLayer = source.GetLayer()
for feature in pointsLayer:
geom =feature.GetGeometryRef()
xy = geom.GetPoint()
print xy
(272022.68669955182, 155404.12013724342, 0.0)
(272904.99338241993, 152881.6706538822, 0.0)
.....
(272796.14718989708, 152075.00336062044, 0.0)
But we can create another type of iterator with iter() and next():
source = ogr.Open('yourpointshapefile.shp')
pointsLayer = source.GetLayer()
iterator = iter(pointsLayer)
# first feature in pointsLayer
feature = iterator.next()
geom = feature.GetGeometryRef()
xy = geom.GetPoint()
print xy
(272022.68669955182, 155404.12013724342, 0.0)
# second feature in pointsLayer
feature = iterator.next()
geom = feature.GetGeometryRef()
xy = geom.GetPoint()
print xy
(272904.99338241993, 152881.6706538822, 0.0)
....
# end raises StopIteration error to signal that iteration is complete
feature = iterator.next()
Traceback (most recent call last):
...
StopIteration
Unlike the for loop that handles the StopIteration error, you need here another control structure (while loop,if,...)
Thus, what is pointsLayer.GetNextFeature() ?
It is an iterator and you can replace iterator = iter(pointsLayer) and iterator.next() by
feature = pointsLayers.GetNextFeature()
geom = feature.GetGeometryRef()
xy = geom.GetPoint()
.....
feature = pointsLayers.GetNextFeature()
....
I hope that I have been able to explain why you should not mix the for loops (iterator) with .GetNextFeature() (another iterator).
This can happen if you import data with missing geometries. I was able to reproduce this error in Points2One with the following CSV... note the 'woot' entry which is missing data.
Place,x,y
Fibble,1.0,2.0
Flubble,-13.0,29.9
Ploot,45.2,-14.0
Woot,,
Flurb,55.5,45.5
QGIS will happily load this without warnings.
Calling Point2One on this throws a similar exception:-
File "/home/steven/.qgis2/python/plugins/points2one/p2o_engine.py", line 121, in iter_points
geom = feature.geometry().asPoint()
AttributeError: 'NoneType' object has no attribute 'asPoint'
I suspect the same thing will be happening with the other plugins.
You should make sure you :-
- import with geometry (make sure you don't have the 'No geometry (attribute only)' selected)
- have geometry values for ALL rows : either an x and y field, or a WKT field, and none of these fields are blank
If you're using Excel (or similar) you can use sort/filter to export a new copy of your csv with all such rows excluded, and see if that works.
Best Answer
From the comments:
The error was that the tif image couldn't be opened because the
userIntervals
didn't have the right path defined.