我试图手动浏览并查明我的边缘是导致此错误的原因,但由于数据的大小,我无法找到它。
有没有办法以编程方式捕获这个?为什么这个错误也会发生?这是来自shapefile,那么元素是否有可能不是2或3元组?
File "/usr/local/lib/python2.7/dist-packages/networkx/classes/graph.py", line 865, in add_edges_from
"Edge tuple %s must be a 2-tuple or 3-tuple." % (e,))
NetworkXError: Edge tuple (437,) must be a 2-tuple or 3-tuple.
我使用以下代码手动将geojson转换为ESRI shapefile:
def geojson_to_shp(request,):
routes = Route.objects.filter(route_long__contains="Test")
routes_json = serialize('geojson', routes, fields=('route_type', 'route_long', 'route_id', 'wkb_geometry',))
routes_geojson = json.loads(routes_json)
routes_geojson.pop('crs', None)
#routes_geojson = json.dumps(routes_geojson)
schema = {'geometry': 'LineString', 'properties': {'route_id': 'str', 'route_type':'str', 'route_long':'str'}}
now = datetime.date.today()
outfilepath = 'files/{0}/{1}.shp'.format(str(now), str(now))
check_if_path_exists(outfilepath)
#this_file = Path(filepath)
#if this_file.is_file() and filepath.lower().endswith(('.geojson', '.json')):
with collection(outfilepath, "w", "ESRI Shapefile", schema) as outfile:
routes = routes_geojson["features"]
# rlinestring_shp = asShape(geom)
for r in routes:
rlinestring_shp = asShape(r['geometry'])
outfile.write({
'properties': r['properties'],
'geometry': mapping(rlinestring_shp)
})
render_network()
return render(request, 'plexus/network/shapefile.html', {'test':routes})
def render_network():
directory = os.path.join(settings.BASE_DIR, 'files/2017-10-25/2017-10-25.shp')
#G = ox.graph_from_polygon(directory, network_type="drive")
net = nx.DiGraph()
shp = ogr.Open(directory)
for lyr in shp:
fields = [x.GetName() for x in lyr.schema]
#G = nx.read_shp(directory, simplify=False)
#print("NODES: " + str(len(G.nodes())))
#print("EDGES: " + str(len(G.edges())))
#print(nx.is_directed(G))
def check_if_path_exists(filepath):
if not os.path.exists(os.path.dirname(filepath)):
try:
os.makedirs(os.path.dirname(filepath))
except OSError as exc:
if exc.errno != os.errno.EEXIST:
此外,我在mapshaper.org中加载shapefile以验证它,并绘制形状而没有任何错误。