如何检查图像是否在flickr-python上有地理信息

时间:2016-02-09 13:07:49

标签: python flickr

到目前为止,这是我的代码:

import flickrapi
import osmapi
import geopy
from geopy.geocoders import Nominatim
import overpy
import requests
import xmltodict
from time import sleep

api_key = "xxxxxxxxxxxxxxxxxxxx"
secret_api_key = "xxxxxxx"
flickr = flickrapi.FlickrAPI(api_key, secret_api_key)

def obtainImages3():

    group_list = flickr.groups.search (api_key=api_key, text = 'Paris', per_page = 10)

    for group in group_list[0]:

        group_images = flickr.groups.pools.getPhotos (api_key=api_key, group_id = group.attrib['nsid'], extras = 'geo, tags, url_s')

        for image in group_images[0]:

            url = str(image.attrib['url_s'])
            tags = (image.attrib['tags']).encode('utf-8')

            #ONLY RUN THIS CODE IF IMAGE HAS GEO INFO

                photo_location = flickr.photos_geo_getLocation(photo_id=image.attrib['id'])
                lat = float(photo_location[0][0].attrib['latitude'])
                lon = float(photo_location[0][0].attrib['longitude'])

                geolocator = Nominatim()
                location = geolocator.reverse("{}, {}".format(lat, lon))
                dict = location.raw
                osmid = dict.get('osm_id', 'default_value_if_null_here')
                osmtype = dict.get('osm_type', 'default_value_if_null_here')
                osmaddress = dict.get('display_name', 'default_value_if_null_here')
                sleep(1)

                if(osmtype == 'node'):
                    node_info = requests.get("http://api.openstreetmap.org/api/0.6/node/"+ osmid)
                    #print node_info.content
                    d = xmltodict.parse(node_info.content)
                    amenity_tags = [tag for tag in d['osm']['node']['tag'] if tag['@k'] == 'amenity']
                    amenity_name = [tag for tag in d['osm']['node']['tag'] if tag['@k'] == 'name']

                    if len(amenity_tags) != 0:
                        amenity_type = amenity_tags[0]['@v']
                        amenity_type_name = amenity_name[0]['@v']
                        print amenity_type
                        print amenity_type_name
                        print url

obtainImages3()

我想在评论后面运行代码,当且仅当获得的图像上有地理信息时。在我的group_images api调用中,我要求返回地理信息,如果它存在的话。

我已尝试过image.attrib['geo'] != None,并尝试了一个尝试/异常块,但都返回了错误,尤其是关键错误。

任何人都可以建议一种简单的方法来确定图像是否具有地理信息,以便评论下方的代码只有在可用时才会运行?

0 个答案:

没有答案