Beispiel #1
0
 def index(self):
     from pylons import config
     import os,re
     rg = re.compile('^(?P<id>[0-9]*) \t(?P<city_name>[^\t]*)\t(?P<mahoz>[^\t]*)\t(?P<population>[0-9,]*)')
     rt=''
     fname = os.path.join(config['pylons.paths']['root'],'towns.txt')
     rt+='reading from %s\n'%fname
     f = open(fname)
     for line in f:
         res = rg.search(line)
         if not res:
             rt+='cannot parse "%s"\n'%line
             continue
         ct = City()
         ct.name=res.group('city_name').strip()
         if meta.Session.query(City).filter_by(name=ct.name).all():
             rt+='city %s ALREADY EXISTS! skipping\n'%ct.name
             continue
         ct.population = int(re.compile('([,]+)').sub('',res.group('population').strip()))
         if request.params.get('getlonlat'):
             lonlat = search_city(ct.name)
             if lonlat['successful']:
                 ct.lon = lonlat['result']['center']['lon']
                 ct.lat = lonlat['result']['center']['lat']
         meta.Session.add(ct)
         rt+='inserted city %s'%ct.name
     meta.Session.commit()
     c.output=rt
     return render('/parse_towns.html')
Beispiel #2
0
def search_city(city_name,country,insdata=None):
    if not insdata and country!='il':
        return search_city_google(city_name,country)
    if not insdata:
        import urllib
        params = urllib.urlencode({'city_name':city_name}) #,'street_name':u'מכבי'.encode('utf8')})
        murl = 'http://www.waze.co.il/WAS/search?%s'%params
        f = urllib.urlopen(murl)
        import simplejson
        resp = simplejson.load(f)
    else:
        resp = insdata

    if resp['successful']:
        from greencouriers.model import City,meta
        commit=False
        qres=meta.Session.query(City).filter_by(name=city_name,country=country).all()
        if qres:
            ct = qres[0]
        else: 
            ct = City()
            ct.country=country
            ct.name = city_name
            meta.Session.add(ct)
            commit=True
        if ct and not ct.lon:
            ct.lon = resp['result']['center']['lon']
            ct.lat = resp['result']['center']['lat']
            ct.extent_top=resp['result']['extent']['top']
            ct.extent_bottom=resp['result']['extent']['bottom']
            ct.extent_right=resp['result']['extent']['right']
            ct.extent_left=resp['result']['extent']['left']
            ct.population=-1
            commit=True
        if commit:meta.Session.commit()
    return resp