Node.js: Import geo-based Flickr data into CouchDB


/ Published in: JavaScript
Save to your folder(s)

The Flickr URL creates a JSON feed with geo-based photos. Using Node.js, CouchDB, and cradle (npm install cradle), you can ingest the JSON data into CouchDB and perform complicated spatial indexing queries. The command for the import is bash$ node name_of_this_javascript.js


Copy this code and paste it in your HTML
  1. var cradle = require("cradle"),
  2. sys = require("sys"),
  3. fs = require("fs");
  4.  
  5. var connection = new(cradle.Connection)("localhost", 5984);
  6. var db = connection.database('geoexample');
  7.  
  8. data = fs.readFileSync("./json/data_flickr.json", "utf-8");
  9.  
  10. flickr = JSON.parse(data);
  11.  
  12. for(p in flickr.photos.photo){
  13. photo = flickr.photos.photo[p];
  14.  
  15. photo.geometry = {"type":"Point",
  16. "coordinates": [ photo.longitude, photo.latitude ]
  17. };
  18.  
  19. // Save the url to the flickr image.
  20. // http://farm{farm-id}.static.flickr.com/{server-id}/{id}_{secret}_[mstzb].jpg
  21.  
  22. photo.image_url_small = "http://farm"+photo.farm+".static.flickr.com/"+photo.server+"/"+photo.id+"_"+photo.secret+"_s.jpg";
  23.  
  24. db.save(photo.id, photo, function(er, ok) {
  25. if (er) {sys.puts("error: "+er); return;}
  26. });
  27. }

URL: http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=API_KEY&text=kitty+kat&has_geo=true&extras=geo&per_page=1000&format=json&nojsoncallback=1

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.