# Convert world coordinates of area into Google tile coordinates.
tile_x1, tile_y1, pix_x1, pix_y1 = lib.world_coordinates_to_tile_coordinates(lat1, lng1, zoom=ZOOM,
                                                                             tile_size=TILE_SIZE)
tile_x2, tile_y2, pix_x2, pix_y2 = lib.world_coordinates_to_tile_coordinates(lat2, lng2, zoom=ZOOM,
                                                                             tile_size=TILE_SIZE)
# Download aerial view image from Google.
try:
    lib.download_aerial_views(data_dir, lat1, lng1, lat2, lng2, ZOOM, 256, key)
except lib.ServerException as exc:
    # If a server exception is encountered, rename the folder of this area's data and continue.
    print exc
    os.rename(CITY_DATA_ROOT + area_name, CITY_DATA_ROOT + 'x' + area_name)

# Put individual tiles of aerial view image together to one big aerial view image.
img = lib.make_big_aerial_image(data_dir, lat1, lng1, lat2, lng2, zoom=ZOOM, tile_size=256, ext='.png')
img.save(data_dir + area_name + '_wholeSatImage.png')
print "Saved image " + area_name + '_wholeSatImage.png in ' + data_dir + '.'

# Delete individual tile images to save disc space.
lib.delete_tile_images(image_dir=data_dir)
print "Deleted small tile images in " + data_dir + '.'

# Open aerial view image and get its pixel size.
img = Image.open(data_dir + area_name + '_wholeSatImage.png')
hgh_img = img.size[1]
wdt_img = img.size[0]

# Calculate world coordinates of upper left corner (ulc) and lower left corner (llc_isprs) of the big aerial view image.
# Note that these are not the same as the coordinates read from 'coordinates.txt'.
ulc_lat, ulc_lng = lib.tile_coordinates_to_world_coordinates(tile_x=tile_x1,
    )
    tile_x2, tile_y2, pix_x2, pix_y2 = lib.world_coordinates_to_tile_coordinates(
        lrc_lat, lrc_lng, zoom=ZOOM, tile_size=TILE_SIZE
    )
    # Download aerial view images from Google for this area.
    try:
        lib.download_aerial_views(data_dir, ulc_lat, ulc_lng, lrc_lat, lrc_lng, ZOOM, TILE_SIZE, key)
    except lib.ServerException as exc:
        # If a server exception is encountered, rename the folder of this area's data and continue.
        print exc
        os.rename(CITY_DATA_ROOT + area_name, CITY_DATA_ROOT + "x" + area_name)
        continue

    # Assemble individual tile images to one big aerial image.
    img = lib.make_big_aerial_image(
        data_dir, ulc_lat, ulc_lng, lrc_lat, lrc_lng, zoom=ZOOM, tile_size=TILE_SIZE, ext=".png"
    )

    # Crop big aerial image according to 'pix_x1', 'pix_y1', 'pix_x2', 'pix_y2'. This is, we want
    # that our arial image corresponds exactly to the geographic coordinates, which are specified in
    # 'areas'.
    img_np = np.asarray(img)
    img_np_shape = img_np.shape
    img_np = img_np[
        pix_y1 : img_np_shape[0] - (TILE_SIZE - 1 - pix_y2), pix_x1 : img_np_shape[1] - (TILE_SIZE - 1 - pix_x2)
    ]
    img = Image.fromarray(img_np)
    img.save(data_dir + area_name + "_wholeSatImage.png")
    print "Saved image " + area_name + "_wholeSatImage.png in '" + data_dir + "'."

    # Delete individual tile images to save disk space.
    # Convert world coordinates of area into Google tile coordinates.
    tile_x1, tile_y1, pix_x1, pix_y1 = lib.world_coordinates_to_tile_coordinates(ulc_lat, ulc_lng, zoom=ZOOM,
                                                                                 tile_size=TILE_SIZE)
    tile_x2, tile_y2, pix_x2, pix_y2 = lib.world_coordinates_to_tile_coordinates(lrc_lat, lrc_lng, zoom=ZOOM,
                                                                                 tile_size=TILE_SIZE)
    # Download aerial view images from Google.
    try:
        lib.download_aerial_views(data_dir, ulc_lat, ulc_lng, lrc_lat, lrc_lng, ZOOM, 256, key)
    except lib.ServerException as exc:
        # If a server exception is encountered, rename the folder of this area's data and continue.
        print exc
        os.rename(CITY_DATA_ROOT + area_name, CITY_DATA_ROOT + 'x' + area_name)
        continue

    # Assemble individual tile images to one big aerial image.
    img = lib.make_big_aerial_image(data_dir, ulc_lat, ulc_lng, lrc_lat, lrc_lng, zoom=ZOOM, tile_size=256, ext='.png')

    # Crop big aerial image according to 'pix_x1', 'pix_y1', 'pix_x2', 'pix_y2'. This is, we want
    # that our arial image corresponds exactly to the geographic coordinate, which is specified in
    # 'zoom_info'.
    img_np = np.asarray(img)
    img_np_shape = img_np.shape

    img_np = img_np[pix_y1:img_np_shape[0]-(TILE_SIZE-1-pix_y2), pix_x1:img_np_shape[1]-(TILE_SIZE-1-pix_x2)]
    img = Image.fromarray(img_np)
    img.save(data_dir + area_name + '_wholeSatImage.png')

    print "Saved image " + area_name + "_wholeSatImage.png in '" + data_dir + "'."

    # Delete individual tile images to save disc space.
    lib.delete_tile_images(image_dir=data_dir)