def get_img(outfile,lat,lon): attempts = 1 lat_lon = str(lat) + "," + str(lon) url = GOOGLE_URL + "&location=" + lat_lon r = requests.get(url) with open(outfile,'wb') as f: f.write(r.content) with open(outfile,'rb') as f: color = getcolor.get_color(f) if color[0] == '#e3e2dd' or color[0] == "#e3e2de": attempts+=1 os.remove(outfile) #print("No Image") return False else: print("Img %s downloaded."%(outfile)) return True
def get_img(outfile, lat, lon): attempts = 1 lat_lon = str(lat) + "," + str(lon) url = GOOGLE_URL + "&location=" + lat_lon r = requests.get(url) with open(outfile, 'wb') as f: f.write(r.content) with open(outfile, 'rb') as f: color = getcolor.get_color(f) if color[0] == '#e3e2dd' or color[0] == "#e3e2de": attempts += 1 os.remove(outfile) #print("No Image") return False else: print("Img %s downloaded." % (outfile)) return True
IMG_PREFIX + lat_lon + IMG_SUFFIX) url = GOOGLE_URL + "&location=" + lat_lon if args.heading: url += "&heading=" + args.heading if args.pitch: url += "&pitch=" + args.pitch try: urllib.urlretrieve(url, outfile) except KeyboardInterrupt: sys.exit("exit") except: pass if os.path.isfile(outfile): print lat_lon # get_color returns the main color of image color = getcolor.get_color(outfile) print color if color[0] == '#e3e2dd' or color[0] == "#e3e2de": print " No imagery" imagery_misses += 1 os.remove(outfile) else: print " ========== Got one! ==========" imagery_hits += 1 if imagery_hits == args.images_wanted: break if country_hits == MAX_URLS: break except KeyboardInterrupt: print "Keyboard interrupt"
print " In country" country_hits += 1 lat_lon = str(rand_lat) + "," + str(rand_lon) outfile = os.path.join( args.country, IMG_PREFIX + lat_lon + IMG_SUFFIX) url = GOOGLE_URL + "&location=" + lat_lon try: urllib.urlretrieve(url, outfile) except KeyboardInterrupt: sys.exit("exit") except: pass if os.path.isfile(outfile): print lat_lon # get_color returns the main color of image color = getcolor.get_color(outfile) print color if color[0] == '#e3e2dd' or color[0] == "#e3e2de": print " No imagery" imagery_misses += 1 os.remove(outfile) else: print " ========== Got one! ==========" imagery_hits += 1 if imagery_hits == IMAGES_WANTED: break if country_hits == MAX_URLS: break except KeyboardInterrupt: print "Keyboard interrupt"
def getImage(country): HOME_PATH = os.path.dirname(os.path.realpath(__file__)) API_KEY = config.getKey() GOOGLE_URL = ("http://maps.googleapis.com/maps/api/streetview?sensor=false&" "size=640x640&key=" + API_KEY) IMG_PREFIX = "img_" IMG_SUFFIX = ".jpg" print "Loading borders" shape_file = HOME_PATH + "/TM_WORLD_BORDERS-0.3.shp" if not os.path.exists(shape_file): print("Cannot find " + shape_file + ". Please download it from " "http://thematicmapping.org/downloads/world_borders.php " "and try again.") sys.exit() sf = shapefile.Reader(shape_file) shapes = sf.shapes() print "Finding country" for i, record in enumerate(sf.records()): if record[2] == country.upper(): print record[2], record[4] print shapes[i].bbox min_lon = shapes[i].bbox[0] min_lat = shapes[i].bbox[1] max_lon = shapes[i].bbox[2] max_lat = shapes[i].bbox[3] borders = shapes[i].points break print "Getting images" attempts, country_hits, imagery_hits, imagery_misses = 0, 0, 0, 0 MAX_URLS = 25000 IMAGES_WANTED = 1 try: while(True): attempts += 1 rand_lat = random.uniform(min_lat, max_lat) rand_lon = random.uniform(min_lon, max_lon) # print attempts, rand_lat, rand_lon # Is (lat,lon) inside borders? if point_inside_polygon(rand_lon, rand_lat, borders): print " In country" country_hits += 1 lat_lon = str(rand_lat) + "," + str(rand_lon) outfile = os.path.join( HOME_PATH + '/images', IMG_PREFIX + 'test' + IMG_SUFFIX) url = GOOGLE_URL + "&location=" + lat_lon try: urllib.urlretrieve(url, outfile) except KeyboardInterrupt: sys.exit("exit") except: pass if os.path.isfile(outfile): print lat_lon # get_color returns the main color of image color = getcolor.get_color(outfile) print color if color[0] == '#e3e2dd' or color[0] == "#e3e2de": print " No imagery" imagery_misses += 1 os.remove(outfile) else: print " ========== Got one! ==========" imagery_hits += 1 if imagery_hits == IMAGES_WANTED: break if country_hits == MAX_URLS: break except KeyboardInterrupt: print "Keyboard interrupt" print "Attempts:\t", attempts print "Country hits:\t", country_hits print "Imagery misses:\t", imagery_misses print "Imagery hits:\t", imagery_hits return lat_lon
def getImage(country): HOME_PATH = os.path.dirname(os.path.realpath(__file__)) API_KEY = config.getKey() GOOGLE_URL = ( "http://maps.googleapis.com/maps/api/streetview?sensor=false&" "size=640x640&key=" + API_KEY) IMG_PREFIX = "img_" IMG_SUFFIX = ".jpg" print "Loading borders" shape_file = HOME_PATH + "/TM_WORLD_BORDERS-0.3.shp" if not os.path.exists(shape_file): print( "Cannot find " + shape_file + ". Please download it from " "http://thematicmapping.org/downloads/world_borders.php " "and try again.") sys.exit() sf = shapefile.Reader(shape_file) shapes = sf.shapes() print "Finding country" for i, record in enumerate(sf.records()): if record[2] == country.upper(): print record[2], record[4] print shapes[i].bbox min_lon = shapes[i].bbox[0] min_lat = shapes[i].bbox[1] max_lon = shapes[i].bbox[2] max_lat = shapes[i].bbox[3] borders = shapes[i].points break print "Getting images" attempts, country_hits, imagery_hits, imagery_misses = 0, 0, 0, 0 MAX_URLS = 25000 IMAGES_WANTED = 1 try: while (True): attempts += 1 rand_lat = random.uniform(min_lat, max_lat) rand_lon = random.uniform(min_lon, max_lon) # print attempts, rand_lat, rand_lon # Is (lat,lon) inside borders? if point_inside_polygon(rand_lon, rand_lat, borders): print " In country" country_hits += 1 lat_lon = str(rand_lat) + "," + str(rand_lon) outfile = os.path.join(HOME_PATH + '/images', IMG_PREFIX + 'test' + IMG_SUFFIX) url = GOOGLE_URL + "&location=" + lat_lon try: urllib.urlretrieve(url, outfile) except KeyboardInterrupt: sys.exit("exit") except: pass if os.path.isfile(outfile): print lat_lon # get_color returns the main color of image color = getcolor.get_color(outfile) print color if color[0] == '#e3e2dd' or color[0] == "#e3e2de": print " No imagery" imagery_misses += 1 os.remove(outfile) else: print " ========== Got one! ==========" imagery_hits += 1 if imagery_hits == IMAGES_WANTED: break if country_hits == MAX_URLS: break except KeyboardInterrupt: print "Keyboard interrupt" print "Attempts:\t", attempts print "Country hits:\t", country_hits print "Imagery misses:\t", imagery_misses print "Imagery hits:\t", imagery_hits return lat_lon