Example #1
0
def geocodeFromCacheById(cacheId, inMemoryOnly=None):
    if inMemoryOnly is None:
        inMemoryOnly = False

    if 'importance_rating' in cacheId:
        importanceRating = cacheId['importance_rating']
        cacheId = dict(cacheId) # don't modify what was passed in.
        del cacheId['importance_rating'] # remove so doesn't conflict with mongoDB query.
    else:
        importanceRating = None

    if isinstance(cacheId, list):
        success = False
        for item in cacheId:
            if isIntendedForDirectUse(GeocodeResultAbstract.getProviderIdFromCacheId(item)):
                cacheId = item
                success = True
                break

        if not success:
            logger.error('Could not find useful ID from ID list %s' % (unicode(cacheId)))
            return None

    if isIntendedForDirectUse(GeocodeResultAbstract.getProviderIdFromCacheId(cacheId)):
        tup = GeocodeResultAbstract.buildTupleFromCacheId(cacheId)
        returnVal = inMemoryCacheGeocodeData.get(tup,None)

        if returnVal is None:
            if not inMemoryOnly:
                db = getDatabase()
                assert isinstance(db, Database)
                result = db.place.find_one({'_id' : cacheId})
                if result is None:
                    logger.warn('Could not find place cache ID in database: %s' % unicode(cacheId))
                    return None
            else:
                return None

            returnVal = buildGeocodeResult(result['place_data'], cacheId['providerId'], importanceRating)

        # Always update, this will move the item to the back of the ordered dict
        # meaning we have a 'least recently used' cache.
        if returnVal is not None:
            inMemoryCacheGeocodeData[tup] = returnVal

        return returnVal
    else:
        geocode = GeocodeResultAbstract.getGnsByPlaceId(GeocodeResultAbstract.getPlaceIdFromCacheId(cacheId))
        if geocode is None:
            logger.error('Failed to retrieve GNS data with cache ID: %s' % unicode(cacheId))

        return geocode
Example #2
0
def geocodeSearch(providerId, placeName, maxResults = 10):
    db = getDatabase()
    assert isinstance(db, Database)

    logger.info('Searching for location: %s' % placeName)

    regularExpression = re.compile('%s' % placeName,re.IGNORECASE)
    mongoPath = '.'.join(['place_data'] + getGeocodeSearchNamePath(providerId))
    search = {'_id.providerId' : providerId, mongoPath : regularExpression}
    cursor = db.place.find(search)

    results = list()
    count = 0
    for item in cursor:
        result = buildGeocodeResult(item['place_data'],providerId)
        if result is not None:
            results.append(result)
            count += 1
            if count > maxResults:
                break

    return results