args = vars(parser.parse_args()) count = args.get('count') print 'We are gonna download %d negative images' % count # We need the elements print 'Loading %s...' % ELEMENTS_FILENAME with open(ELEMENTS_FILENAME, 'r') as f: elements = json.load(f) # Randomize elements list to make sure we don't download all pics from the # same sample. Then cut it. shuffle(elements) # Now we're gonna download the satellite images for these locations mapbox_static = MapboxStatic( namespace='negative', root_folder='satellite/negative') total_downloaded = 0 for element in elements: if total_downloaded >= count: break if element.get('type') != 'node': continue # Move the latlon a random amount, random() is in the range [0.0, 1.0) target_lat = element.get('lat') + (random.random() - 0.5) target_lon = element.get('lon') + (random.random() - 0.5) url = mapbox_static.get_url(latitude=target_lat, longitude=target_lon) print url success = mapbox_static.download_tile( element_id=element.get('id'),
# We need the elements print 'Loading %s...' % ELEMENTS_FILENAME with open(ELEMENTS_FILENAME, 'r') as f: elements = json.load(f) # We need the elements print 'Loading %s...' % WAYS_DATA_FILENAME with open(WAYS_DATA_FILENAME, 'r') as f: ways_data = json.load(f) # Randomize elements list to make sure we don't download all pics from the # same sample shuffle(elements) # Now we're gonna download the satellite images for these locations mapbox_static = MapboxStatic(namespace='pitch', root_folder='satellite') total_downloaded = 0 for element in elements: # They're strings in the dict now element_id_str = unicode(element.get('id')) sport = element.get('tags', {}).get('sport', 'unknown').lower() if element_id_str in ways_data and sport == target_sport: if total_downloaded >= count: break print '> Element: %s (%s)' % (element.get('id'), sport) url = mapbox_static.get_url( latitude=ways_data[element_id_str].get('lat'), longitude=ways_data[element_id_str].get('lon')) print url element_id_sport = '%s_%s' % (sport, element_id_str)
print 'Loading %s...' % ELEMENTS_FILENAME with open(ELEMENTS_FILENAME, 'r') as f: elements = json.load(f) # We need the elements print 'Loading %s...' % WAYS_DATA_FILENAME with open(WAYS_DATA_FILENAME, 'r') as f: ways_data = json.load(f) # Randomize elements list to make sure we don't download all pics from the # same sample shuffle(elements) # Now we're gonna download the satellite images for these locations mapbox_static = MapboxStatic( namespace='pitch', root_folder='satellite') total_downloaded = 0 for element in elements: # They're strings in the dict now element_id_str = unicode(element.get('id')) sport = element.get('tags', {}).get('sport', 'unknown').lower() if element_id_str in ways_data and sport == target_sport: if total_downloaded >= count: break print '> Element: %s (%s)' % (element.get('id'), sport) url = mapbox_static.get_url( latitude=ways_data[element_id_str].get('lat'), longitude=ways_data[element_id_str].get('lon')) print url
def get_satellite(inputFile, mapboxtoken=None, count=1000, zoomLevel=17, outputFolder='data', xpixel=480, ypixel=360, epsg=None, elements=None, randomImages=False): ''' Get satellite data in order to input GIS information. Parameters: 'inputFile': Input file (GeoJSON format or parsed into GeoJSON) 'mapboxtoken': Access token for Mapbox (go to mapbox.com to create one) 'count': Number of satellite images to be downloaded 'zoomLevel': Zoom level (see libs/zoomLevel.csv for resolutions) 'outputFolder': Folder to store output data in 'xpixel': Number of pixels of satellite images (width) 'ypixel': Number of pixels of satellite images (height) 'epsg': EPSG code for coordinate system in GIS data (will try to find automatically if not provided) 'elements': GIS data can also be input directly 'randomImages': Get center of random polygons (False) or within Boundary Box of data (True) ''' if (not inputFile) and (not elements): print "Error: Provide input file." exit() if not mapboxtoken: print "Error: Provide mapbox token (more informations on www.mapbox.com)." exit() #parser.add_argument('--sport', # type=str, default='baseball', # help='Sport tag, for example: baseball, tennis, or soccer.') # We need the elements if not elements: print 'Loading %s...' % inputFile with open(inputFile, 'r') as f: elements = json.load(f) #get coordinate system myCoordConvert = CoordConvert() code = myCoordConvert.getCoordSystem(elements, epsg) #create folders subpath = outputFolder + "/" + os.path.split(inputFile)[-1][:-5] if not os.path.isdir(subpath): os.mkdir(subpath) print 'Directory', subpath, 'created' if not os.path.isdir(subpath + satDataFolder): os.mkdir(subpath + satDataFolder) print 'Directory', subpath + satDataFolder, 'created' if not os.path.isdir(subpath + testDataFolder): os.mkdir(subpath + testDataFolder) print 'Directory', subpath + testDataFolder, 'created' #Write metadata with open(subpath + satDataFolder + "meta.csv", "a+") as f: f.write("ZoomLevel,," + str(zoomLevel) + "\n") #get bbox if set to random if randomImages: xlist = [] ylist = [] for element in elements['features']: minxe, maxxe, minye, maxye = getBBox(element) xlist.append(minxe) xlist.append(maxxe) ylist.append(minye) ylist.append(maxye) minx = min(xlist) maxx = max(xlist) miny = min(ylist) maxy = max(ylist) element_list = [] index_list = range(len(elements['features'])) #featue map # Randomize elements list to make sure we don't download all pics from the shuffle(index_list) for i in index_list: element_list.append(elements['features'][i]) #feature map # Now we're gonna download the satellite images for these locations namespace = os.path.split( inputFile)[-1][:-5] #get input file name as namespace mapbox_static = MapboxStatic(namespace=namespace, root_folder=subpath + satDataFolder[0:-1]) total_downloaded = 0 c = 0 print "------------------- Getting Satellite data -------------------" for element in element_list: if randomImages: randomValue = random() av_lon = minx + ((maxx - minx) * randomValue) av_lat = miny + ((maxy - miny) * randomValue) element_id_str = 1000000 + c #1000000 indicates random value with open(subpath + satDataFolder + "meta.csv", "a+") as f: f.write( str(element_id_str) + "," + str(av_lon) + "," + str(av_lat) + "\n") else: element_id_str = index_list[c] #figure out center of polygon av_lon, av_lat = latLon(element) #Convert to standard format if code != 4319: # if not already in wgs84 standard format lotlan = myCoordConvert.convert(av_lon, av_lat) longitude = lotlan[0] latitude = lotlan[1] else: #if already in wgs84 format latitude = av_lat longitude = av_lon #get url print "Coordinates WSG64: " + str(longitude) + ',' + str(latitude) if (av_lon != longitude) and (av_lat != latitude): print "Coordinates Native: " + str(av_lon) + ',' + str(av_lat) url = mapbox_static.get_url(latitude=latitude, longitude=longitude, mapbox_zoom=zoomLevel, access_token=mapboxtoken, width=xpixel, height=ypixel) #download data success = mapbox_static.download_tile(element_id=element_id_str, url=url, verbose=True) if success: total_downloaded += 1 print total_downloaded, '/', count c += 1 if total_downloaded >= count: break