caesar0301 / s2g

(S)hapefile to(2) (G)raph/network converter in Python
https://pypi.python.org/pypi/s2g
MIT License
24 stars 8 forks source link

Error when generating components #5

Open lobisquit opened 5 years ago

lobisquit commented 5 years ago

Hello, I have a problem during conversion, specifically running the following code on this dataset.

import fiona
from s2g import ShapeGraph
from shapely.geometry import LineString, shape

path = 'extracted_roads.shp'

with fiona.open(path) as source:
    geoms = []
    for r in source:
        s = shape(r['geometry'])
        if isinstance(s, LineString):
            geoms.append(s)

# create ShapeGraph object from a list of lines
sg = ShapeGraph(geoms, to_graph=False)

# detect major components
mc = sg.gen_major_components()

# convert graph to json
G = json_graph.node_link_data(sg.to_networkx())

for node in G['nodes']:
    node['lat'], node['long'] = sg.node_xy[node['id']]

with open('connected_roads.json', 'w') as output:
    output.write(json.dumps(G))

More or less after 400000 entries are processed the conversion stops with this error.

INFO:root:Validating pair-wise line connections of raw shapefiles (total 9756 lines)
100% (47584890 of 47584890) |#################################################################################| Elapsed Time: 0:03:57 Time:  0:03:57
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-0290bc8fe669> in <module>()
----> 1 mc = sg.gen_major_components()

/home/ubuntu/.local/lib/python3.6/site-packages/s2g/shapegraph.py in gen_major_components(self)
    264                 bar.update(k + 1)
    265                 i, j = neighbors[k]
--> 266                 if self.validate_pairwise_connectivity(i, j):
    267                     graph.add_edge(i, j)
    268

/home/ubuntu/.local/lib/python3.6/site-packages/s2g/shapegraph.py in validate_pairwise_connectivity(self, ainx, binx)
    223             valid = True
    224
--> 225         touched = self.validate_intersection(binx, a1)
    226         if touched is not None:
    227             self._pseudo_edges.append([(binx, touched), (ainx, a1)])

/home/ubuntu/.local/lib/python3.6/site-packages/s2g/shapegraph.py in validate_intersection(self, line_index, point)
    195         if line.intersects(buffered_point):
    196             cut = point_projects_to_line(point, line)
--> 197             touched = coords[cut]
    198             self._update_cut(line_index, cut)
    199         return touched

TypeError: list indices must be integers or slices, not NoneType

How can I debug this issue? Thank you very much.