Downloading Images

Many of the modules in BrailsPlusPlus make use of images and for that reason modules are available to go get images. Currently two modules exist:

  1. GoogleSatellite

  2. GoogleStreetview

This following is an example of how to use both building. Building upon the examples for generating footprints, the examples take as input the footprint scraper and the location of interest. An AssetInventory is then generated. A random subset of this inventory is created and both satallite and street view images are obtained for this inventory.

 1# Written: fmk 4/23
 2# License: BSD-2
 3
 4"""
 5brails_dpenload_images.py
 6=========================
 7
 8 Purpose: Testing 1) get_class method of Importer
 9                  2) get_footprints method of scraper module
10                  3) get_images and print_info methods of GoogleSatellite and
11                     GoogleStreetview
12
13"""
14
15import os
16import argparse
17from brails import Importer
18
19# This script needs a Google API Key to run.
20# We suggest placing your API key in file api_key.txt in the directory above
21# this script if you plan to commit changes to this example. This way, you do
22# not risk accidentally uploading your API key (api_key.txt is in .gitignore,
23# so you have work to do to get it uploaded).
24# The api_key.txt is used in other examples.
25
26API_KEY_DIR = '../api_key.txt'
27if os.path.exists(API_KEY_DIR):
28    with open(API_KEY_DIR, 'r', encoding='utf-8') as file:
29        api_key = file.readline().strip()  # Read first line & strip whitespace
30
31
32from brails.utils.importer import Importer
33
34def download_images():
35    
36    # Create the argument parser
37    parser = argparse.ArgumentParser(description="Demonstrate Importer.")
38    parser.add_argument('scraper', type=str, help="Footprint Scraper")
39    parser.add_argument('location', type=str, help="Location")    
40
41    # Parse the arguments
42    args = parser.parse_args()
43
44    # Create the importer:
45    importer = Importer()
46
47    
48    # Select a region and create its RegionBoundary:
49    region_boundary_class = importer.get_class('RegionBoundary')
50    region_boundary_object = region_boundary_class({'type': 'locationName', 'data': args.location})
51
52    scraper_class = importer.get_class(args.scraper)    
53    scraper = scraper_class({"length": "ft"})
54    inventory = scraper.get_footprints(region_boundary_object)
55    print(f"num assets found: {len(inventory.inventory)} for {args.location} using {args.scraper}")
56    
57    # Subsample from the extracted assets to keep the image downloading step quick.
58    # Here, we are randomly sampling 20 buildings using a random seed value of 40:
59    small_inventory = inventory.get_random_sample(20, 40)
60
61    # Get aerial imagery for the selected subset using GoogleSatellite:
62    google_satellite_class = importer.get_class('GoogleSatellite')
63    google_satellite = google_satellite_class()
64    images_satellite = google_satellite.get_images(small_inventory,
65                                                   'tmp/satellite/')
66
67    # Get street level imagery for the selected subset using GoogleStreetview:
68    google_street_class = importer.get_class('GoogleStreetview')
69    google_input = {'apiKey': api_key}
70    google_street = google_street_class(google_input)
71    images_street = google_street.get_images(small_inventory, 'tmp/street/')
72
73    inventory.print_info()
74#    small_inventory.print_info()
75#    images_satellite.print_info()
76#    images_street.print_info()
77
78# Run the main function if this script is executed directly
79if __name__ == "__main__":
80    download_images()    

Note

  1. To run the script you will need to obtain a Google API key.

  2. Downloding of images can cost you money.

  3. The downloading of images and processing of such takes time and resources. This is why the get_random_sample() method. For assets for which processed data is subsequently missing, data imputation can be employed to fill in missng data.