Ejemplo n.º 1
0
def download(start_idx, end_idx, api_key):
    with open(os.path.join(base_dir, "roads_dictionnary.csv"), 'a') as f:
        for i in range(start_idx, end_idx):  #range(0,tt.shape[0])

            print('%.2f' % ((i - start_idx) * 100 / (end_idx - start_idx)) +
                  " %")

            # get feature geometry and bearing from shapefile
            feature = layer[ransample[i]]

            lon = feature.GetGeometryRef().GetX()

            lat = feature.GetGeometryRef().GetY()

            heading = feature.GetField("BEARING")

            # Get the number of panaoramas at the location
            panIds = streetview.panoids(lat, lon)

            # Randomly select one of the n panoramas at this location
            if len(panIds) > 0:

                pid = random.randint(0, len(panIds) - 1)
                f.write("{},{},{},{}\n".format(lon, lat, heading,
                                               panIds[pid]["panoid"]))
                img = streetview.api_download(panIds[pid]["panoid"],
                                              heading,
                                              DIRECTORY,
                                              api_key,
                                              fov=80,
                                              pitch=0)
Ejemplo n.º 2
0
def runOttawa(n_loc):
    """
    Downloads Google StreetView images of random points in Ottawa
    
    :param n_loc: number of locations to download. CAUTION: some points have no images, so it's not the exact number of subdirectories created
    """

    DIRECTORY = "../voteimages"

    ds = ogr.Open('C:/Users/msawada/Desktop/Urban_RAT/Urban_RAT_inventory.dbf')
    layer = ds.GetLayer()

    for i in range(n_loc):
        print('%.2f' % (i * 100 / n_loc) + " %")

        index = random.randint(0, len(layer))
        feature = layer[index]

        lon = feature.GetGeometryRef().GetX()
        lat = feature.GetGeometryRef().GetY()

        bearing_road = feature.bearing

        if bearing_road == None:
            heading = ''
        else:
            heading = bearing_road + 90 * np.sign(random.random() - 0.5)

        folder = DIRECTORY

        panIds = streetview.panoids(lat, lon)

        if len(panIds) > 0:

            for pan in panIds:
                img = streetview.api_download(pan["panoid"],
                                              heading,
                                              folder,
                                              API_KEY,
                                              fov=80,
                                              pitch=0,
                                              year=pan["year"])
                if img != None:
                    full_size = misc.imread(img)
                    resized = misc.imresize(full_size, (64, 64))
                    desc = gist.extract(resized)
                    np.savetxt(img + '.txt', desc)
Ejemplo n.º 3
0
def runOttawa():
    """
    Downloads Google StreetView images of random points in Ottawa
    
    :param n_loc: number of locations to download. CAUTION: some points have no images, so it's not the exact number of subdirectories created
    """

    DIRECTORY = "D:\Amaury\Desktop\ottawa_image_db"

    ds = ogr.Open(
        'C:/Users/msawada/Desktop/Urban_RAT/Urban_RAT_inventory_4326.dbf')
    layer = ds.GetLayer()

    n_loc = len(layer)
    #done: range(3000)
    for i in range(3000, 6000):
        print('%.2f' % (i * 100 / n_loc) + " %")

        index = i
        feature = layer[index]

        lon = feature.GetGeometryRef().GetX()
        lat = feature.GetGeometryRef().GetY()

        folder = DIRECTORY + '/%2.6f,%2.6f' % (lat, lon)

        panIds = streetview.panoids(lat, lon)

        if len(panIds) > 0:
            if not os.path.exists(folder):
                os.makedirs(folder)
                for pan in panIds:
                    img = streetview.api_download(pan["panoid"],
                                                  folder,
                                                  API_KEY,
                                                  fov=80,
                                                  pitch=0,
                                                  year=pan["year"],
                                                  month=pan["month"])
                    if img != None:
                        full_size = misc.imread(img)
                        resized = misc.imresize(full_size, (64, 64))
                        desc = gist.extract(resized)
                        np.savetxt(img + '64.txt', desc)
Ejemplo n.º 4
0
 def save_penoid_images(penoid_list, heading, flat_dir, key):
     """function to save penoid images"""
     for penoid_ind in penoid_list:
         streetview.api_download(penoid_ind, heading, flat_dir, key)
Ejemplo n.º 5
0
        print('%.2f' % (i * 100 / prog) + " %")

        # get feature geometry and bearing from shapefile
        feature = layer[ransample[i]]

        lon = feature.GetGeometryRef().GetX()

        lat = feature.GetGeometryRef().GetY()

        heading = feature.GetField("BEARING")

        f.write("{},{},{}\n".format(lon, lat, heading))

        # Get the number of panaoramas at the location
        panIds = streetview.panoids(lat, lon)

        # Randomly select one of the n panoramas at this location
        if len(panIds) > 0:

            pid = random.randint(0, len(panIds) - 1)

            img = streetview.api_download(panIds[pid]["panoid"],
                                          heading,
                                          DIRECTORY,
                                          API_KEY,
                                          fov=80,
                                          pitch=0,
                                          year=panIds[pid]["year"])

        prog += 1
Ejemplo n.º 6
0
    def download_gsv(self, gsv_key, start=0, end=-1):
        """
        Downloads Google Street View (GSV) images points in Ottawa from a shapefile layer.

        WARNING: the filename format is currently not compatible with the class Image definition.

        :param gsv_key: GSV API key
        :type gsv_key: str
        :param start: index of the feature of the layer from where download starts
        :type start: int
        :param end: index of the feature of the layer from where download ends
        :type end: int 
        """

        # Creation of directory
        self.gsv_dir = safe_folder_creation(self.gsv_dir)

        #  Convert layer file
        ds = ogr.Open(self.layer_path)
        layer = ds.GetLayer()

        # Determine the number of locations to download
        loc_max = len(layer)
        if start < end < len(layer):
            stop = end
        else:
            stop = loc_max
        n_loc = stop - start

        # Display advancement of downloading
        pbar = progressbar.ProgressBar()
        for i in pbar(range(start, stop)):
            # Get location
            feature = layer[i]
            lon = feature.GetGeometryRef().GetX()
            lat = feature.GetGeometryRef().GetY()

            # Get the closest panoramas from the location
            pano_id = streetview.panoids(lat, lon, closest=True)

            # Check if there is a pano
            if len(pano_id):
                # Create filename
                image_key = pano_id[0]["panoid"]
                if pano_id[0]["month"] < 10:
                    image_date = str(pano_id[0]["year"]) + "-0" + str(pano_id[0]["month"]) + "-01T00-00-00"
                else:
                    image_date = str(pano_id[0]["year"]) + "-" + str(pano_id[0]["month"]) + "-01T00-00-00"
                image_lon = "{0:.6f}".format(lon)
                image_lat = "{0:.6f}".format(lat)
                image_filename = '{}_{}_{}_{}'.format(image_lon, image_lat, image_key, image_date)
                # Download one image
                try:
                    streetview.api_download(image_key, 90, self.gsv_dir, gsv_key, fov=80, pitch=0,
                                            fname=image_filename)
                    self.nb_gsv += 1
                except Exception as err:
                    print(err)
                    print("Error on feature {}, lat = {}, lon = {} ".format(i, lat, lon))
                    continue

        # Display information
        print("Number of locations         : {}".format(n_loc))
        print("Number of images downloaded : {}".format(self.nb_gsv))
        print("Ratio : {}%".format((self.nb_gsv / n_loc) * 100))
Ejemplo n.º 7
0
def runOttawa(n_loc):
    """
    Downloads Google StreetView images of random points in the shapefile, those points
    have weighted probabilities to be chosen in their attributes.
    
    :param n_loc: number of locations to download. CAUTION: some points have no images, so it's not the exact number of subdirectories created
    """
    DIRECTORY = "trainottawa"
    
    # Opening the layer and fetching the weights, and the normalizing them
    ds = ogr.Open('ottawashp/ottawa_4326_clipped_weighted_points.shp')
    layer = ds.GetLayer()
    weights = np.array([feature.weight for feature in layer])
    weights_sum = np.sum(weights)
    weights = weights / weights_sum
    
    indices = np.array(range(len(layer)))
    
    for i in range(n_loc):
        print('%.2f' %(i*100/n_loc) + " %")
        
        # We select randomly the index according to the weights
        index = int(choice(indices, p=weights))
        feature = layer[index]
        
        # We fetch the coordinates
        lon = feature.GetGeometryRef().GetX()
        lat = feature.GetGeometryRef().GetY()
        
        # We fetch the bearing of the road
        bearing_road = feature.bearing
        
        # If there is non, we let the heading at its default value
        if bearing_road == None:
            heading = ''
        # Else, we take randomly a heading of +90 or -90 degrees, so the image faces a building
        else:
            heading = bearing_road + 90*np.sign(random.random() - 0.5)
        
        # Creating the folder name
        folder = DIRECTORY + '/new-%2.6f,%2.6f' %(lat,lon)
        oldfolder = DIRECTORY + '/%2.6f,%2.6f' %(lat,lon)
        
        # using the streetview module to fetch the panoids corresponding to the position
        panIds = streetview.panoids(lat, lon)
        
        if len(panIds) > 0:
            # We only proceed if the folder doesn't already exist
            if not os.path.exists(oldfolder):
                if not os.path.exists(folder):
                    os.makedirs(folder)
                    
                    # We download each image
                    for pan in panIds:
                        img = streetview.api_download(pan["panoid"], heading, folder, 
                                                      API_KEY, fov=90, pitch=10, year=pan["year"])
                        if img != None:
                            # We process and the GIST vector at different scales, and save them
                            # as text files in the same folder
                            full_size = misc.imread(img)
                            resized = misc.imresize(full_size, (256,256))
                            desc = gist.extract(resized)
                            np.savetxt(img + '256.txt', desc)
                            resized = misc.imresize(full_size, (128,128))
                            desc = gist.extract(resized)
                            np.savetxt(img + '128.txt', desc)
                            resized = misc.imresize(full_size, (64,64))
                            desc = gist.extract(resized)
                            np.savetxt(img + '64.txt', desc)
                            resized = misc.imresize(full_size, (32,32))
                            desc = gist.extract(resized)
                            np.savetxt(img + '32.txt', desc)
                            # Processing and saving the dense sift vector
                            sift_desc = dsift(misc.imresize(img, (64,64)))
                            np.savetxt(img + '.nfo', sift_desc)
Ejemplo n.º 8
0
def fetch(request):
    if request.method == "POST":
        latlng = request.POST.get('latlng', False)

        if not latlng:
            return JsonResponse({'status':'error','message':'Latitude, Longitude Not Provided Properly'}, status=404)

        if not settings.GOOGLE_MAP_STREET_API:
            print("NO GOOGLE MAP STREET API IN SERVER")
            return JsonResponse({'status':'error','message':'No Google Map API Available'}, status=404)
        
        saveto = None
        if not os.path.exists(os.path.join('media/street_view_images')):
            saveto = os.environ.get('PROJECT_FOLDER','') + '/media/street_view_images'
        else:
            saveto = os.path.join('media/street_view_images')

        filelist = []
        latlngList = []
        panoFileLatLngList = []
        panoIdsList = []
        
        try:
            # If Map Polygon points array arrives
            latlngList = json.loads(latlng)
        except Exception as e:
            # If Point of string lat,lng arrives
            latlngList = [x.strip() for x in latlng.split(',')]
            latlngList = [{"lat":latlngList[0], "lng":latlngList[1]}]
        
        count = 0
        max_fetch = 200
        if settings.DEBUG:
            max_fetch = 5

        for coords in latlngList:
            if not coords.get('lat', False) or not coords.get('lng', False):
                continue # If lat, lng does not exists some how
            print('Trying to fetch streetview image at: ' + str(coords.get('lat')) + ',' + str(coords.get('lng')))

            if (count > max_fetch):
                break

            panoids = streetview.panoids(lat=str(coords.get('lat')), lon=str(coords.get('lng')))
            # print(panoids)
            for pano in panoids:
                if (count < max_fetch):
                    # IF panoroma is older then 2018 ignore it
                    if pano.get('year', False) and pano.get('year') < 2018:
                        continue

                    if pano.get('panoid') in panoIdsList:
                        continue

                    panoIdsList = panoIdsList + [pano.get('panoid')]
                    
                    # GET ALL Tiles images info
                    # print(streetview.tiles_info(pano.get('panoid')))
                    
                    # heading recommendation: 0, 90, 180, or 270
                    for heading in [30, 220, 0, 90, 180, 270]:
                        file = None
                        try:
                            file = streetview.api_download(pano.get('panoid'), heading, saveto, settings.GOOGLE_MAP_STREET_API, year=pano.get('year','now'))
                        except Exception as e:
                            print('Failed to download this streetview image')

                        if file:
                            filelist.append(file)
                            panoFileLatLngList.append(str(pano.get('lat'))+','+str(pano.get('lon')))
                            count += 1
                else:
                    messages.info(request, 'Max Street View Images ('+str(max_fetch)+') was fetched earlier.')
                    break

        return JsonResponse({'status':'ok','message':'Images Saved','data':filelist,'coords':panoFileLatLngList}, status=200)
    else:
        return JsonResponse({'status':'error','message':'Invalid Request'}, status=404)
Ejemplo n.º 9
0
import streetview
import secrets

panoids = streetview.panoids(lat=-33.85693857571269, lon=151.2144895142714)

direc = './'  # where to put image. put in current working dir
panoid = panoids[0]['panoid']  # get first image from this location
heading = 0  # see docs for info on this. controls which direction we're looking
flat_dir = direc
key = secrets.svkey

r = streetview.api_download(panoid, heading, flat_dir, key)