def network(self, type, start, end, factor=1.0, color=None, src=None):
"""create network of crimes
Args:
type: data type
start: start character
end: end character
factor: size, default 1
color: colour, default None
src: default None
"""
self.load()
df = pd.read_sql_query(con=self.con, sql=GEN_SEARCH_QUERY.format("Data", "Type = '{0}' "
"AND Latitude is NOT NULL "
"AND Longitude is NOT NULL "
"AND date(Date) > date('{1}') "
"AND date(Date) <= date('{2}')".format(type,
start,
end)))
dg = nx.Graph()
queue = deque()
crimes = [Vertex(row["Case"], row["Type"], row["Longitude"], row["Latitude"]) for index, row in df.iterrows()]
if len(crimes) == 0:
print("none found")
return
copy = [vertex for vertex in crimes]
if src is None:
queue.append(crimes[0])
else:
queue.append(list(filter(lambda x: x.id == src, crimes)))
visited = []
for crime in crimes:
dg.add_node(crime.id)
while len(queue) > 0:
current = queue.popleft()
crimes.remove(current)
for crime in crimes:
if crime.distance_to(current) < factor and crime not in visited:
dg.add_edge(current.id, crime.id, weight=crime.distance_to(current))
queue.append(crime)
visited.append(crime)
visited.append(current)
if len(queue) == 0 and len(crimes) > 0:
queue.append(crimes[0])
pos = {copy[i].id: self.base_map(copy[i].lon, copy[i].lat) for i in range(len(copy))}
if color is None:
color = "red"
nx.draw_networkx(dg, pos=pos, node_size=5, with_labels=False, ax=self.ax, node_color=color)
plt.show()
评论列表
文章目录