Allenfenqu/xfun.py

176 lines
7.7 KiB
Python

import numpy as np
def xfun(yanlinks, node):
stationidwan = np.load('stationidwan.npy')
stationidlst = np.load('stationidlst.npy')
stationidtrip = np.load('stationidtrip.npy')
luwang = np.load('luwang.npy')
stationid = np.sort(stationid, axis=0)
a = max(np.where(stationid[:, 0] < 524000000000)[0]) + 1
b = max(np.where(stationid[:, 0] < 525000000000)[0])
stationid = stationid[a:b, :]
fm = 0
adr = np.matmul(luwang, np.array([range(1, 71)]).T)
a = np.array([[value, len(np.where(yanlinks[:, 11] == value)[0])] for value in np.unique(yanlinks[:, 11])])
a = a[np.argsort(a[:, 1]), :]
for i in a[:, 0]:
sub_yanlinks = yanlinks[yanlinks[:, 11] == i, :]
a = adr[sub_yanlinks[:, 4] - 1, :]
chu = 1
yisou = np.array([chu])
m = 0
while len(yisou) > m:
m = len(yisou)
b = np.where(np.sum(a[yisou, :], axis=0) > 0)[0]
yisou = np.union1d(yisou, b)
if len(yisou) != len(a):
fm = 1
break
if fm == 0:
i = np.arange(len(stationid[:, 0]))
stationid[i, 7] = yanlinks[stationid[i, 5], 11]
route = np.unique(np.floor(stationid[:, 0] / 1000000))
ss = np.empty((0, 16), dtype=float)
for i in range(len(route)):
vehrecord = stationid[np.where(np.floor(stationid[:, 0] / 1000000) == route[i])]
if len(vehrecord[:, 0]) > 10:
vp = np.abs((np.arange(1, len(vehrecord[:, 0]) + 1) - np.flipud(
np.arange(1, len(vehrecord[:, 0]) + 1)) + 0.5) * vehrecord[:, 6])
fd, y = np.min(vp), np.argmin(vp)
for j in range(len(vehrecord[:, 0])):
n = 1
if j != len(vehrecord[:, 0]) - 1:
while vehrecord[j, 7] == vehrecord[j + n, 7]:
if j + n == y or j + n == len(vehrecord[:, 0]):
break
if abs(vehrecord[j + n, 0] - vehrecord[j + n + 1, 0]) > 500000:
break
n += 1
vehrecord[j, 8] = vehrecord[j + n, 7]
else:
vehrecord[j, 8] = vehrecord[-1, 7]
n = 1
if j != 0:
while vehrecord[j, 7] == vehrecord[j - n, 7]:
if j - n == y - 1 or j - n == 0:
break
if abs(vehrecord[j - n, 0] - vehrecord[j - n - 1, 0]) > 500000:
break
n += 1
vehrecord[j, 9] = vehrecord[j - n, 7]
else:
vehrecord[j, 9] = vehrecord[0, 7]
ss = np.concatenate((ss, vehrecord), axis=0)
for i in range(len(ss[:, 1])):
if ss[i, 9] - ss[i, 8] == 0 and ss[i, 10] - ss[i, 8] == 0:
ss[i, 15] = ss[i, 8]
ss[i, 16] = ss[i, 8]
else:
if ss[i, 9] - ss[i, 8] == 0 and ss[i, 10] - ss[i, 8] != 0:
ss[i, 15] = ss[i, 10]
ss[i, 16] = ss[i, 8]
if ss[i, 9] - ss[i, 8] != 0 and ss[i, 10] - ss[i, 8] == 0:
ss[i, 15] = ss[i, 8]
ss[i, 16] = ss[i, 9]
if ss[i, 9] - ss[i, 8] != 0 and ss[i, 10] - ss[i, 8] != 0:
ss[i, 15] = ss[i, 10]
ss[i, 16] = ss[i, 9]
import numpy as np
adregion = np.load('adregion.npy')
yanlinks = np.load('yanlinks.npy')
for i in range(len(yanlinks[:, 1])):
adregion[:, yanlinks[i, 5]] = adregion[:, yanlinks[i, 5]] * yanlinks[i, 12]
adr = np.zeros((np.max(yanlinks[:, 12]), np.max(yanlinks[:, 12])))
for i in range(len(adregion[:, 1])):
a = adregion[i, :]
a = np.unique(a)
a = a[a != 0]
x, y = a.shape
if y > 1:
for j in range(len(a)):
for u in range(len(a)):
if j != u:
adr[a[j] - 1, a[u] - 1] = adr[a[j] - 1, a[u] - 1] + 1
adr[a[u] - 1, a[j] - 1] = adr[a[u] - 1, a[j] - 1] + 1
adr[adr > 1] = 1
adregion = np.load('adregion.npy')
stroad = np.load('stroad.npy')
y = []
import numpy as np
max_val = np.max(yanlinks[:, 11])
y = np.array([])
for i in range(1, max_val + 1):
irecord = ss[np.where(ss[:, 7] == i), :][0]
wu = np.concatenate([np.where(adr[:, i] == 1)[0], [i]]) # 邻接区域搜索
for j in wu: # 起点区域
for u in wu: # 终点区域
a = np.where(irecord[:, 14] == j)[0]
b = np.where(irecord[:, 15] == u)[0]
pairrecord = irecord[np.intersect1d(a, b), :]
road = np.array([]) # 路段及方向搜索
for o in range(pairrecord.shape[0]):
r = np.unique(stationidtrip[np.where(stationidtrip[:, 0] == pairrecord[o, 0]), 1][0])
ro = pairrecord[o, 5]
r = r[np.where(r != ro)]
iroad = [ro]
for t in range(
np.unique(stationidtrip[np.where(stationidtrip[:, 0] == pairrecord[o, 0]), 1]).shape[
0] - 1):
m = adregion[ro, :]
n = np.where(m == 1)[0]
for m1 in range(r.shape[0]):
fm = np.where(n == r[m1])[0]
if fm.shape[0] > 0:
break
ro = r[m1]
iroad.append(ro)
r = r[np.where(r != ro)]
iroad = np.vstack((iroad[:-1], iroad[1:])).T
for t in range(iroad.shape[0]):
mt = np.vstack((luwang[iroad[t, 0], [3, 4]], luwang[iroad[t, 0], [9, 10]],
luwang[iroad[t, 1], [9, 10]], luwang[iroad[t, 1], [3, 4]]))
mtt = np.unique(mt, axis=0)
for tt in range(mtt.shape[0]):
a = np.where(mt[:, 0] == mtt[tt, 0])[0]
b = np.where(mt[:, 1] == mtt[tt, 1])[0]
c = np.intersect1d(a, b)
mtt[tt, 2] = c.shape[0]
mtt = mtt[mtt[:, 2].argsort(), :] # np.sort(mtt, axis=0, kind='mergesort')
iroad[t, [2, 3]] = mtt[-1, [0, 1]]
road = np.vstack((road, iroad))
if road.shape[0] < 1:
continue
road = np.unique(road[:, 1:], axis=0)
s = []
for o in range(road.shape[0]):
a = np.where(stroad[:, 3] == road[o, 0])[0]
b = np.where(stroad[:, 4] == road[o, 1])[0]
c = np.where(stroad[:, 5] == road[o, 2])[0]
s.append(np.intersect1d(np.intersect1d(a, b), np.intersect1d(a, c)))
stin = np.unique(stroad[s, 0])
vs = []
for o in range(stin.shape[0]):
vs.append(stationid[np.where(stationid[:, 0] == stin[o, 0]), 6])
vs = np.vstack(vs)
vs = vs[np.where(vs > 0)[0]]
y = np.vstack((y, [np.var(vs), i, j, u]))
y = np.sum(y[:, 0])
else:
y = 1000000000000000