User:Jules/geoIPimage

From XPUB & Lens-Based wiki
< User:Jules
Revision as of 16:38, 25 March 2015 by Jules (talk | contribs) (Created page with "This script retrieves the geographic coordinates of a url and retrieves the closest geolocated picture available on panoramio. You need to get PygeoIP to be able to use it. <...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This script retrieves the geographic coordinates of a url and retrieves the closest geolocated picture available on panoramio. You need to get PygeoIP to be able to use it.


import pygeoip
import os
import urllib, urlparse 
import json


#what database?
gi = pygeoip.GeoIP('pathtodatabase/GeoLiteCity.dat')
#url input
url = raw_input("Where shall we go now? ")

#recuperation complete des donnees
infos = gi.record_by_name(url)

#on garde les coordonnees
Latitude = infos.get('latitude')
Longitude = infos.get('longitude')

print Longitude
print Latitude

add = 0.002

def someFunction(add):

	#etablir le perimetre
	LatitudeMin = Latitude - add
	LatitudeMax = Latitude + add
	LongitudeMin = Longitude - add
	LongitudeMax = Longitude + add

	#transformation chaine caracteres
	LatitudeMin = str(LatitudeMin)
	LatitudeMax = str(LatitudeMax)
	LongitudeMin = str(LongitudeMin)
	LongitudeMax = str(LongitudeMax)

	# query for images
	url = 'http://www.panoramio.com/map/get_panoramas.php?set=full&from=0&to=1&minx='+ LongitudeMin +'&miny='+ LatitudeMin +'&maxx='+ LongitudeMax +'&maxy='+ LatitudeMax +'&size=original'

	print url
	c = urllib.urlopen(url)

	# get the urls of individual images from JSON
	j = json.loads(c.read()) 
	imurls = []
	for im in j['photos']:
	    imurls.append(im['photo_file_url'])

	# download images
	for url in imurls:
	    image = urllib.URLopener()
	    image = urlparse.urlparse(url).path
	    print 'downloading:', url

	if len(imurls) ==0:
		add+=0.002
		someFunction(add)

someFunction(add)