svlad

Check-in [31fdc1d58d]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Added grafikon from Margarita suite.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:31fdc1d58d0f4a5a78b2020a4f5775aa27f168a2
User & Date: severak 2012-06-04 22:25:02
Context
2012-09-01
21:27
Changed plot app to enable turtle mode. check-in: 8136db8529 user: Hry tags: trunk
2012-06-04
22:25
Added grafikon from Margarita suite. check-in: 31fdc1d58d user: severak tags: trunk
2012-05-05
21:20
SQLITE logic now works. Table editing needs to be solved. check-in: 4fc6193906 user: severak tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added grafikon.lua.



























































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
require "luasql.sqlite3"

function rows (connection, sql_statement)
  local cursor = assert (connection:execute (sql_statement))
  return function ()
    local data=cursor:fetch({})
    if data then
      return data
    else
      cursor:close()
    end
  end
end


env=luasql.sqlite3()
db=assert(env:connect("szeged2.sqlite"))
print(db)
--prepare select
stopsSQL=[[SELECT s.stop_id, sp.stop_lon, sp.stop_lat
FROM stop_times s
JOIN stops sp ON s.stop_id=sp.stop_id
WHERE s.trip_id=(SELECT trip_id FROM trips WHERE route_id='45' LIMIT 1)
ORDER BY CAST(stop_sequence AS integer) ASC]]
--Calculating distances
distances={}
totalDistance=nil
orders={}
order=0
for data in rows(db,stopsSQL) do
  if totalDistance==nil then
    prevX=data[2]
    prevY=data[3]
    totalDistance=0
  end
  dist=math.sqrt( math.pow(data[3]-prevY,2), math.pow(data[2]-prevX,2) )
  print(data[1],dist)
  totalDistance=totalDistance+dist
  distances[data[1]]=totalDistance
  orders[data[1]]=order
  print(data[1],order)
  prevX=data[2]
  prevY=data[3]
  order=order+1
end

--now prepare main query
mainSQL=[[SELECT stop_id, departure_time, s.trip_id
FROM stop_times_fixed s
JOIN trips t ON t.trip_id=s.trip_id
WHERE t.route_id='45'
ORDER BY s.trip_id ASC, CAST(stop_sequence AS integer) ASC]]

--calculate sums
timeSum=24*60
tRes=200
maxOrder=order
print(maxOrder)

print("Preparing graph")

--preparing file
outp=io.open("grafikon.svg","w+")
outp:write('<svg xmlns="http://www.w3.org/2000/svg" width="'..timeSum..'" height="'..tRes..'">\n')

--createGrid
--hours
for i=1,24 do
  outp:write('<path stroke="gray" fill="none" d="M'..(i*60)..' 0 L'..(i*60)..' 200"/>\n')
end
--stops
for k,v in pairs(distances) do
--  outp:write('<path stroke="lightgray" fill="none" d="M0 '..(orders[k]/maxOrder)*tRes..' L '..timeSum..' '..(orders[k]/maxOrder)*tRes..'"/>\n')  
end


--create graph
lastTripId=0
d={}
for data in rows(db,mainSQL) do
  --print(data[1],data[2])
  hh,mm,ss=string.match(data[2],"(%d+):(%d+):(%d+)")
  --print(distances[data[1]],hh*mm)
  if lastTripId==data[3] and distances[data[1]] then
    d[#d+1]="L"..((hh*60)+mm).." "..(orders[data[1]]/maxOrder)*tRes
    --print((orders[data[1]]/maxOrder)*tRes)
    --print(hh,mm)
  else
    if #d>0 then
      local r=math.random(255)
      local g=math.random(255)
      local b=math.random(255)
      col=string.format("#%02x%02x%02x",r,g,b)
      outp:write('<path d="'..table.concat(d," ")..'" fill="none" stroke="'..col..'"/>\n')
      print("Trip "..data[3].." finished...")
    end
    d={}
    if data and distances[data[1]] and hh then
      d[1]="M "..((hh*60)+mm).." "..(orders[data[1]]/maxOrder)*tRes
    end     
  end
  lastTripId=data[3]
end

--outp:write('<path d="'..table.concat(d," ")..'" fill="none" stroke="black"/>\n')

outp:write('</svg>')
outp:close()
db:close()