Discussion:
[Community] cascaded_union : 'LineString' has no len()
Ari Simmons
2014-06-09 22:55:35 UTC
Permalink
This is my first time trying a cascaded_union, and I am getting the
following "..no len()..." error. I don't seem to have any 'NoneType'
geometry..so I'm not sure what it is picking up on...anybody else see it?
File "<stdin>", line 1, in <module>
File "I:/It_25/116609_global_hwy_shields/dissolve/dissolve.py", line
17, in >>dissolve
dissolve_feat = cascaded_union(geom)
File "C:\Python27\lib\site-packages\shapely\ops.py", line 112, in
cascaded_union
L = len(geoms)
TypeError: object of type 'LineString' has no len()
import fiona
from shapely.geometry import shape
from shapely.ops import cascaded_union

def dissolve(inFile, outFile):
with fiona.open(inFile, 'r', encoding='utf-8') as input:
input_driver = input.driver
input_crs = input.crs
input_schema = input.schema.copy()
with fiona.open(outFile, 'w', driver=input_driver, crs=input_crs,
schema=input_schema, encoding='utf-8') as output:
for item in input:
geom = shape(item['geometry'])
dissolve_feat = cascaded_union(geom)
output.write({'geometry':mapping(dissolve_feat),
'properties': item['properties']})
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gispython.org/pipermail/community/attachments/20140609/2dd4584d/attachment.htm>
Sean Gillies
2014-06-09 23:33:41 UTC
Permalink
Hi Ari,

cascaded_union() expects the argument to be a sequence of geometries,
either a multi-part geometry or a list of geometries. The problem with
OGR's shapefile driver is that it can give you LineStrings and
MultiLineStrings from a single file. Until I have a patch for Shapely or a
new release, I'll have to ask you to test the input items and wrap them in
a list if they are single-part geometries. Something like this:

for item in input:
geom = shape(item['geometry'])
if not geom.type.startswith('Multi'):
geom = [geom]
dissolve_feat = cascaded_union(geom)
...
Post by Ari Simmons
This is my first time trying a cascaded_union, and I am getting the
following "..no len()..." error. I don't seem to have any 'NoneType'
geometry..so I'm not sure what it is picking up on...anybody else see it?
File "<stdin>", line 1, in <module>
File "I:/It_25/116609_global_hwy_shields/dissolve/dissolve.py", line
17, in >>dissolve
dissolve_feat = cascaded_union(geom)
File "C:\Python27\lib\site-packages\shapely\ops.py", line 112, in
cascaded_union
L = len(geoms)
TypeError: object of type 'LineString' has no len()
import fiona
from shapely.geometry import shape
from shapely.ops import cascaded_union
input_driver = input.driver
input_crs = input.crs
input_schema = input.schema.copy()
with fiona.open(outFile, 'w', driver=input_driver, crs=input_crs,
geom = shape(item['geometry'])
dissolve_feat = cascaded_union(geom)
output.write({'geometry':mapping(dissolve_feat),
'properties': item['properties']})
_______________________________________________
Community mailing list
Community at lists.gispython.org
http://lists.gispython.org/mailman/listinfo/community
--
Sean Gillies
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gispython.org/pipermail/community/attachments/20140609/c5c00d35/attachment.htm>
Ari Simmons
2014-06-10 16:52:58 UTC
Permalink
Hi Sean.

Thanks, that worked like a charm.

Sooo...something I am trying to wrap my head around here...it seems that in
my 'input_schema' whether I put 'LineString' or 'MultiLineString' the
result is always the same (which is: a 'MultiLineString').

For ex: I was expecting if I put 'LineString' in as the ( 'geometry':
'LineString' ) that the lines (**if they were geometrically connected and
of course had the same key**) would be dissolved into a single feature (as
oppose to a 'MultiLineString').

Also...same thing happened when I used linemerge() in place of

cascaded_union() ..
def dissolve(inFile, outFile):
# dictionary for union
uniqueRefs = {}
with fiona.open(inFile, 'r', encoding='utf-8') as input:
input_driver = input.driver
input_crs = input.crs
# it seems whether this is a 'LineString' or a
'MultiLineString' it will always
# return a 'MultiLineString'
input_schema = {'geometry': 'LineString','properties':
{'ref'.encode("utf-8"): 'str:254'}}
with fiona.open(outFile, 'w', driver=input_driver,
crs=input_crs, schema=input_schema, encoding='utf-8') as output:
for item in input:

# extract the key
key = item['properties']['ref']
geom = shape(item['geometry'])

if not geom.type.startswith('Multi'):
geom = [geom]
for g in geom:
if key in uniqueRefs:
uniqueRefs[key].append(g)
else:
uniqueRefs[key] = [g]

for key in uniqueRefs:
dissolve_feat = cascaded_union(uniqueRefs[key])
output.write({'geometry':mapping(dissolve_feat),
'properties': {'ref': key}})
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gispython.org/pipermail/community/attachments/20140610/8e0dd572/attachment.htm>
Sean Gillies
2014-06-10 16:55:25 UTC
Permalink
I'm glad it did!

I fixed linemerge, too. Details at
https://github.com/Toblerity/Shapely/issues/135. I don't have the next
Shapely release scheduled, but there's nothing much blocking it, so maybe
the end of the week.
Post by Ari Simmons
Hi Sean.
Thanks, that worked like a charm.
Sooo...something I am trying to wrap my head around here...it seems that
in my 'input_schema' whether I put 'LineString' or 'MultiLineString' the
result is always the same (which is: a 'MultiLineString').
'LineString' ) that the lines (**if they were geometrically connected and
of course had the same key**) would be dissolved into a single feature (as
oppose to a 'MultiLineString').
Also...same thing happened when I used linemerge() in place of
cascaded_union() ..
# dictionary for union
uniqueRefs = {}
input_driver = input.driver
input_crs = input.crs
# it seems whether this is a 'LineString' or a 'MultiLineString' it will always
# return a 'MultiLineString'
input_schema = {'geometry': 'LineString','properties': {'ref'.encode("utf-8"): 'str:254'}}
# extract the key
key = item['properties']['ref']
geom = shape(item['geometry'])
geom = [geom]
uniqueRefs[key].append(g)
uniqueRefs[key] = [g]
dissolve_feat = cascaded_union(uniqueRefs[key])
output.write({'geometry':mapping(dissolve_feat), 'properties': {'ref': key}})
_______________________________________________
Community mailing list
Community at lists.gispython.org
http://lists.gispython.org/mailman/listinfo/community
--
Sean Gillies
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gispython.org/pipermail/community/attachments/20140610/ab279a13/attachment.htm>
Loading...