back

Per recuperare direttamente da internet le informazioni relative alle coordinate da inserire nel file, creeremo un semplice script, nappy.py, che richiederà all'utente la digitazione dell'indirizzo completo (ad es: Catania viale africa 168), recupererà le coordinate da internet e le inserirà nel file MyPoi.txt.
Da quel momento in poi sarà possibile visualizzare sulla mappa i poi inseriti.

#!/usr/bin/python

# nappy.py
# script to add pois to navit maps
# author: pmate 
# email: pmatehome@gmail.com

# look at the MyPoi.txt address: \
here is set to ~/opt/navit/navit/maps/MyPoi.txt
# if you need a different address of the file, change the variable MyPoiFile
# with the desired location

import urllib2
import string

# address of MyPoi.txt file
MyPoiFile = "~/opt/navit/navit/maps/MyPoi.txt"

# gets data from user
address = raw_input("write city, address and number separated with spaces: ")
description = raw_input("write the description of the poi: ")

# builds the url to pass to yahoo map service
url = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=capelinks \
&location=" + address.replace(" ", "+") + "&Geocode=Geocode"

# sends the request and gets the response
req = urllib2.Request(url)
response = urllib2.urlopen(req)
xml = response.read()

# parses the results
a = string.find(xml, "<Latitude>")
b = string.find(xml, "<Longitude>")
lat = xml[a+10:a+19]
lon = xml[b+11:b+20]

print "lat = " + str(lat)
print "lon = " + str(lon)

# set the strings to pass to MyPoi.txt file
l_first = 'type="poi_viewpoint" label="' + description \
+ '" description="' + description + '"'
l_second = str(lon) + " " + str(lat)

# check if the poi already exists
exists=""
fileHandle = open ( "/home/glider/opt/navit/navit/maps/MyPoi.txt" )
fileList = fileHandle.readlines()
fileHandle.close()

(continua nella prossima pagina)

back