Exemple #1
0
def get_missing_position(data, town):
    """
    Dohleda GPS pozici pro ta data, ktere maji v CSV tabulce uvedenu jen adresu.
    """
    out = []
    for item in data:
        if item['gps']:
            # GPS souradnice jsou zadany uz ve vstupnich CSV datech
            out.append(item)
            continue

        # GPS souradnice nebyly zadany, zkusime to je zjistit pres Google
        time.sleep(1)
        q = u'%s, %s' % (item['street'], town)
        print 'Looking for geo location for %s' % q
        json = geocode(q, '') # TODO: API key
        try:
            location = json['Placemark'][0]['Point']['coordinates'][:-1]
            location = dict(zip(['lng', 'lat'], location))
        except (KeyError, IndexError):
            location = None
        if not location:
            print "  Sorry, location wasn't found"
            continue

        # GPS pozice se nasla
        item['gps'] = [location]
        out.append(item)

    return out
Exemple #2
0
def get_missing_position(data, town):
    """
    Dohleda GPS pozici pro ta data, ktere maji v CSV tabulce uvedenu jen adresu.
    """
    out = []
    for item in data:
        if item['gps']:
            # GPS souradnice jsou zadany uz ve vstupnich CSV datech
            out.append(item)
            continue

        # GPS souradnice nebyly zadany, zkusime to je zjistit pres Google
        time.sleep(1)
        q = u'%s, %s' % (item['street'], town)
        print 'Looking for geo location for %s' % q
        json = geocode(q, '')  # TODO: API key
        try:
            location = json['Placemark'][0]['Point']['coordinates'][:-1]
            location = dict(zip(['lng', 'lat'], location))
        except (KeyError, IndexError):
            location = None
        if not location:
            print "  Sorry, location wasn't found"
            continue

        # GPS pozice se nasla
        item['gps'] = [location]
        out.append(item)

    return out
Exemple #3
0
 def find_town(self, pos):
     """
     Pokusi se pres Google Geocode service zjistit kteremu mestu odpovida
     zadana pozice `pos`.
     """
     json = geocode("%(lat)s, %(lon)s" % pos, '')
     try:
         town = json['Placemark'][0]['AddressDetails']['Country']['AdministrativeArea']['SubAdministrativeArea']['SubAdministrativeAreaName']
     except (KeyError, IndexError):
         town = None
     return town
Exemple #4
0
 def find_town(self, pos):
     """
     Pokusi se pres Google Geocode service zjistit kteremu mestu odpovida
     zadana pozice `pos`.
     """
     json = geocode(pos['lat'], pos['lon'])
     if json['status'] == 'OK':
         parts = [i['address_components'] for i in json['results'] if 'locality' in i['types']]
         if not len(parts):
             return None
         town = [i['long_name'] for i in parts[0] if 'locality' in i['types']]
         if not town:
             return None
         return town[0]
     return None
Exemple #5
0
 def find_town(self, pos):
     """
     Pokusi se pres Google Geocode service zjistit kteremu mestu odpovida
     zadana pozice `pos`.
     """
     json = geocode(pos['lat'], pos['lon'])
     if json['status'] == 'OK':
         parts = [
             i['address_components'] for i in json['results']
             if 'locality' in i['types']
         ]
         if not len(parts):
             return None
         town = [
             i['long_name'] for i in parts[0] if 'locality' in i['types']
         ]
         if not town:
             return None
         return town[0]
     return None
Exemple #6
0
def get_address(row, places):
    """
    Vytahne z radku mesto a ulici, a k ni zjisti GPS pozici (pokud ji nema
    v cache, tak ji vytahne online pres Google Geocode).
    """
    # vytahneme si mesto..
    town = row[KEY_TOWN].strip()
    if type(town) == type(''):
        town = town.decode('utf-8')
    if town not in places:
        places[town] = {}

    # ..vytahneme si ulici a pokusime se zjistit gps pozici
    street = row[KEY_STREET]
    if street and street.strip():
        if type(street) == type(''):
            street = street.decode('utf-8')
    else:
        street = u''
    q = u'%s, %s' % (street, town)
    if street not in places[town]:
        sys.stdout.write('  Geocode: Looking for "%s"\n' % q)
        json = geocode(q, '')
        try:
            gps = json['Placemark'][0]['Point']['coordinates'][:-1]
            gps = ",".join([str(i) for i in gps])
            sys.stdout.write('    Position found! (%s)\n' % gps)
        except (KeyError, IndexError):
            sys.stderr.write('    Position for address "%s" not found!\n' % q)
            gps = None
        places[town][street] = gps  # lng/lat
    else:
        sys.stdout.write('  Geocode: Address "%s" already in cache\n' % q)
        gps = places[town][street]

    return town, street, gps
Exemple #7
0
def get_address(row, places):
    """
    Vytahne z radku mesto a ulici, a k ni zjisti GPS pozici (pokud ji nema
    v cache, tak ji vytahne online pres Google Geocode).
    """
    # vytahneme si mesto..
    town = row[KEY_TOWN].strip()
    if type(town) == type(''):
        town = town.decode('utf-8')
    if town not in places:
        places[town] = {}

    # ..vytahneme si ulici a pokusime se zjistit gps pozici
    street = row[KEY_STREET]
    if street and street.strip():
        if type(street) == type(''):
            street = street.decode('utf-8')
    else:
        street = u''
    q = u'%s, %s' % (street, town)
    if street not in places[town]:
        sys.stdout.write('  Geocode: Looking for "%s"\n' % q)
        json = geocode(q, '')
        try:
            gps = json['Placemark'][0]['Point']['coordinates'][:-1]
            gps = ",".join([str(i) for i in gps])
            sys.stdout.write('    Position found! (%s)\n' % gps)
        except (KeyError, IndexError):
            sys.stderr.write('    Position for address "%s" not found!\n' % q)
            gps = None
        places[town][street] = gps # lng/lat
    else:
        sys.stdout.write('  Geocode: Address "%s" already in cache\n' % q)
        gps = places[town][street]

    return town, street, gps