Harvesting the Flickr API for geo-referenced data

Flickr is a fotosharing web service owned by Yahoo. It has about 100 million users and is among top 150 sites as of Wikipedia http://en.wikipedia.org/wiki/Flickr Other than that Flickr is a wonderfull source of georeferenced information. Many of the pictures uplodaded to Flickr are geo-regerenced. It is possible to have this data using the Flickr API and this guide is on how to harvest this information using the Flickr Rest API and store it in a tab separated values file.

First of all you need an API key from Flickr and that means you have to make a yahoo account. With that you can accede Flickr as well. Once you are on Flickr in the top menu Explore -> App garden as in the fig bellow.



Go to 'Get an API' key and then fill the form. After that you should see a page like the following with the API keys.



Now you have to prepare the java client that will query the Flick API given the API keys we got previously. Download the java project I have prepared from this link and import it in Eclipse. It is the same as the project you find in github https://github.com/callmeal/Flickr4Java but modified in order to run in eclipse so you need no maven to built it. You can also consult the documentation there but if you want just to make the java client then read as follows. Once that you import it in eclipse we’ll proceed in making the java client.

The steps for creating the Flickr client are :

1-create a Flickr object and the fileds for the API key and secret and include it in the FlickrClient class.

        static int picXpage = 1;
        static String apiKey = "apikey";
        static String sharedSecret = "secret";
    
        Flickr f;
        REST rest;
        RequestContext requestContext;
    
        public FlickrClient() throws ParserConfigurationException, IOException {
           f = new Flickr(apiKey,sharedSecret,new REST());
           requestContext = RequestContext.getRequestContext();
        }

2-create the location (as bounding box area) in which we want to take the data, and declare it in the client. There is a tool for making this step very easy. Go to http://boundingbox.klokantech.com/ and make a bounding box area. In the drop down left menu choose CSV format for the coordinates of the area. In the figure bellow the bounding box area is focused over the city of Bern in Swiss. For creating the location go to the package com.aetrion.flickr and find the class Location which is an enumerations class. Inside it there are many other locations. Let’s create a new location that is BERN. Be careful to switch longitude with latitude in the coordinates of the bounding box as shown in the code bellow.

 
 BERN("BERN", 46.6953,6.7304,
		     47.1645,7.8059),

3-use the method showActivityBB() for querying the Flickr API using as input parameters the bounding box area and the time interval. The method showActivityBB interacts with the Flickr API and takes as parameters of input the bounding box longitude and latitude, the starting date and the finishing date of the time interval during which we want to inquire about activity of Flickr users making pictures in the area delimited by the bounding box itself.

4-Run and check the results. To run the code create a method main in the FlickrClient class as follows :

  public static void main (String [] args) {
	
     try{
    	 
         TestMyFlickrAppBB15 tfa = new TestMyFlickrAppBB15();

         Locations l = Locations.BERN;
         String city = l.getDescription();
         String directoryName = l.getDescription()+"_bbox";
         File dataDir = new File(directoryName);
 		
   	  // if the directory does not exist, create it
   	     if (!dataDir.exists()){
   	       System.out.println("Creo la directory: " + directoryName);
   	          dataDir.mkdir();
   	}
   	
   try{
          // the showActivity() method that interacts with the Flickr API
   	    tfa.showActivityBB(city,l.getV1x() , l.getV1y() ,l.getV3x(), l.getV3y(),
   	    				new GregorianCalendar(2015,2,2,0,1,1),
   	    				new GregorianCalendar(2015,2,2,23,59,59),
   	    			directoryName+"/"+2015+"- - - -output "+2+" "+2+".csv");
        }catch (Exception e) {
          e.printStackTrace();
        }
   	 
    //System.exit(0);	
}
  

At the end the code will create a folder with a csv file inside that will contain the records of data from Flickr. There is the username, the pic description, latitude and longitude, the accuracy level of the location and the date in which the pic was done.

One last note : all this is already implemented in the project you downloaded previously. The main purpose of the guide was to explain how the code works.

You can find the project in Github here and it can be downloaded or imported in eclipse

In the analyzing and visualizing sections you find interesting IDEAS on how to analyse this data and visualize it to get insights and usefull information.

If you enjoyed this article guide please share

Tweet

or follow ..

Follow @datamap2t