Search code examples
javascriptapigeolocationflickryql

Extract just town/city from flickr api/YQL


I am trying to write a script that returns just a persons village, town or city. I am using the following YQL query, but the results from the console are too specific, they give me the neighbourhood first.

select * from flickr.places where lat=51.558418 and lon=-1.781985 and api_key=YOU_FLICKR_API_KEY

Here is the results of my query from the console.

<?xml version="1.0" encoding="UTF-8"?>
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng"
    yahoo:count="1" yahoo:created="2011-10-25T09:35:50Z" yahoo:lang="en-US">
    <diagnostics>
        <publiclyCallable>true</publiclyCallable>
        <url execution-start-time="1" execution-stop-time="123" execution-time="122"><![CDATA[http://api.flickr.com/services/rest/?method=flickr.places.findByLatLon&lat=51.558418&lon=-1.781985]]></url>
        <user-time>124</user-time>
        <service-time>122</service-time>
        <build-version>22535</build-version>
    </diagnostics> 
    <results>
        <places accuracy="16" latitude="51.558418" longitude="-1.781985" total="1">
            <place latitude="51.547" longitude="-1.802"
                name="West Leaze, Swindon, England, GB, United Kingdom"
                place_id="sddCvK9SW703gg" place_type="neighbourhood"
                place_type_id="22"
                place_url="/United+Kingdom/England/Swindon/West+Leaze"
                timezone="Europe/London" woeid="39722"/>
        </places>
    </results>
</query>

I want to be able to change the place_type_id in the query. I know I could use regex to isolate the info, but I am not sure if that will work for all locations so I would much prefer to have the API just returning my desired result.


Solution

  • Flickr's flickr.places.findByLatLon method, which is being called in the background by your YQL query, accepts an accuracy key.

    accuracy (Optional)

    Recorded accuracy level of the location information. World level is 1, Country is ~3, Region ~6, City ~11, Street ~16. Current range is 1-16. The default is 16.

    See » Flickr docs for flickr.places.FindByLatLon.

    Putting that to use is as simple as providing accuracy =n in your where clause. A value of around 10 looks to be about the accuracy that you are looking for, but experiment to see what is best.

    select * from flickr.places where 
    lat=51.558418 and lon=-1.781985 and api_key=API_KEY_HERE
    and accuracy=10
    

    Aside

    Documentation for the data sources / services being used by YQL tables can generally be obtained by looking at the table description.

    desc flickr.places
    

    This gives lots of information, usually including at least one link to documentation pages. See the meta section for the documentationURL items.

    desc flickr.places