# From the past: a traffic survey App for S60

Submitted by Nicola Rainiero on 2013-03-25 (last updated on 2013-04-22)

Two years ago I had to make many traffic surveys along a big road in Rovigo city during the rush hours: in the early morning and in the late afternoon. I simply had had to count the numbers of cars and long vehicles in the two directions of travel and in preset positions and times (15 minutes per station). To do this I wrote a Python program for my phone: a Nokia N78 (equipped with Symbian 60v3). Here is the full story, code and how it worked.

## Job goals

The work was easy but stressful and repetitive:

• fix the stations and the best time for the surveys in a map, i.e. identify the rush hours and the position useful to count the vehicles;
• make a photographic report and above all count the vehicles during 15 minutes reporting the different direction of travel and 2 types of length (normal and long vehicle).
 Stations on the map

I had only a very small problem: how should I have count the vehicles?

My colleagues were used to make a cross on a sheet of paper every times they saw a machine (see the image on the left), but for me was impossible and not handy.

I had to find a solution... First I thought about a manual counter like this, but I needed four of them, it would be difficult to use them simultaneously and I would have to buy them! Then I looked my phone, it was obsolete but it was a small computer and probably I could have found an useful application ready made for me, but nothing to do!

The only solution was to write directly a program for my phone. I known that existed a version of Python for S60 and I installed it in my phone. Only the version 1.4.5 works on it, the most recent doesn't install correctly1.

I found the version here and in these sites I learnt how to write my program:

• PyS60 tutorial by Jurgen Scheible;
• Tutorial python s60 (Italian language) by Alberto(akus).

Python is easy to learn, it has a large community and many books for free, the best introduction to this programming is Think Python book. I only had a problem the indentation! And another stupid one was that I had to use the Nokia SDK to make the process fastest.

I don't remember exactly, but I think that I took 2 or 3 afternoons to learn and write the software. I put both the code and the software in a zip, but I'm sure that at the present nobody are able to try it, how many people have today a S60 phone? Probably just me! So I put a short video of my extinct App.

Source code: Traffic_Surveys.zip (be careful: these files must be put in C:\Python\, if is not possible, you have to change the paths in the source)

## How it works

Below there is a short video that shows the Python application in action, if you want to try it in your browser, you can click here.

 A short video A typical saved file with the results

## Code of Traffic_Surveys.py

# # Manual Traffic Surveys# Python App for Nokia S60 Phones## Software open source by Nicola Rainiero, under GPLv3 license#            \\--->  rainnic.altervista.org  <---//import osimport sysimport timeimport appuifwfrom appuifw import *import e32from key_codes import *# use this to get your graphics stuff infrom graphics import *os.path=os.getcwd()class Keyboard(object):    def __init__(self,onevent=lambda:None):        self._keyboard_state={}        self._downs={}        self._onevent=onevent    def handle_event(self,event):        if event['type'] == appuifw.EEventKeyDown:            code=event['scancode']            if not self.is_down(code):                self._downs[code]=self._downs.get(code,0)+1            self._keyboard_state[code]=1        elif event['type'] == appuifw.EEventKeyUp:            self._keyboard_state[event['scancode']]=0        self._onevent()    def is_down(self,scancode):        return self._keyboard_state.get(scancode,0)    def pressed(self,scancode):        if self._downs.get(scancode,0):            self._downs[scancode]-=1            return True        return Falsekeyboard=Keyboard()def quit():    global running    running=0    appuifw.app.set_exit()appuifw.app.screen='full'# create an empty image (appears as white image)#img=Image.new((240,320))appuifw.app.title=u'Manual_traffic_surveys'appuifw.app.body= Noneappuifw.note(u'Manual Traffic Surveys by\nNicola Rainiero\n- rainnic.altervista.org-','info')appuifw.note(u'The keys are: 1 3 7 9 for counting, left & right command key for exiting','info')tempo_stazionamento = appuifw.query(u'Put the time of survey in minutes','number')iniziale = time.clock()data = time.localtimedt = str(time.localtime()[0])+'_'+str(time.localtime()[1])+'_'+str(time.localtime()[2])+'_'+str(time.localtime()[3])+'-'+str(time.localtime()[4])a1 = 0a3 = 0a7 = 0a9 = 0exit = 0# define a function that redraws the screen, in this case the image shall be# drawn again and again (use the .blit function for this)def handle_redraw(rect):    canvas.blit(img)running=1# define the canvas and include the redraw function as callback, and also the key scanning function (keyboard.handle_event)canvas=appuifw.Canvas(event_callback=keyboard.handle_event, redraw_callback=handle_redraw)# set the application body as canvasappuifw.app.body=canvasappuifw.app.exit_key_handler = lambda: None # it doesn't exit with the right key# app.exit_key_handler=  # to exit with the right key# keycapture.all_keys# create a loop to define stuff in it that needs to be run through again and againwhile running:    # clear the image (put as a white image)    img = Image.open('C:\\Python\\background_app.png')    # define a point and add it to the image, define its: current x,y coordinate, and its colour and size    img.text((19,72),u'%04d'%a1,fill = 0xffd700, font = 'title')    img.text((162,72),u'%04d'%a3,fill = 0xffd700, font = 'title')    img.text((19,297),u'%04d'%a7,fill = 0xffd700, font = 'title')    img.text((162,297),u'%04d'%a9,fill = 0xffd700, font = 'title')    adesso = time.clock()    rimasto = tempo_stazionamento*60 - (adesso - iniziale)    img.text((75,170),u'%01d seconds'%rimasto,fill = 0xff0000, font = 'title')    if exit:        img.text((20,190),u'(To escape ''left command key'')',fill = 0xc0c0c0, font = 'dense')    # redraw the image    # redraw the image    handle_redraw(())    # this is needed to start a short scheduler to allow checking for key events    e32.ao_yield()    # if left arrow key is pressed, change the x coordinate of the point by 1 dot    if keyboard.pressed(EScancode1):        a1 = a1 + 1    if keyboard.pressed(EScancode3):        a3 = a3 + 1    # if down arrow key is pressed, change the y coordinate of the point by 1 dot    if keyboard.pressed(EScancode7):        a7 = a7 + 1    if keyboard.pressed(EScancode9):        a9 = a9 + 1    if keyboard.pressed(EScancodeRightSoftkey):        exit = 1    if (exit and keyboard.pressed(EScancodeLeftSoftkey)):        # define the directory and file name to write the file into        imagedir=u'C:\\Python\\Survey_'+dt+'.txt'        passato= (adesso - iniziale)        passato=int(passato)        # create the file        file = open(imagedir,'w')        # write some text into it        file.write('Manual Traffic Survey\n')        file.write('On '+dt+' \n')        file.write(' \n')        file.write('CARS\n')        file.write('(down) '+str(a1)+'      (up) '+str(a3)+' \n')        file.write(' \n')	file.write('TIME ELAPSED:\n')	file.write(str(passato)+' seconds\n')        file.write(' \n')        file.write('HEAVY VEHICLES\n')        file.write('(down) '+str(a7)+'      (up) '+str(a9)+'\n')        # close the file        file.close()        quit()

## Related Content:

### Nicola Rainiero

A civil geotechnical engineer with the ambition to facilitate own work with free software for a knowledge and collective sharing. Also, I deal with green energy and in particular shallow geothermal energy. I have always been involved in web design and 3D modelling.