sujinleeme
5/16/2019 - 3:37 PM

README.md

How to download Google Maps Street View Static Images based on geojson

Google Maps Street View Static API

os.environ['GOOGLE_MAPS_KEY'] = 'YOUR KEY'
  • Run the script with python3 static-street-image.py.
Abingdon Road.
6 images are going to be download.....
success: 6/6 **** fail: 0/6
=======================================
Adam Drive.
15 images are going to be download.....
success: 10/15 **** fail: 5/15
=======================================
Adam Park.
19 images are going to be download.....
....
  • All images might be uploaded inside images folder.

e.g)

.
├── Abingdon\ Road
│   ├── 103.9787469-1.3692966.jpg
│   ├── 103.9787969-1.3692413.jpg
│   ├── 103.979376-1.3685506.jpg
│   ├── 103.9794005-1.3685049.jpg
│   └── 103.9794143-1.3684698.jpg
├── Adam\ Drive
│   ├── 103.8116705-1.3365496.jpg
│   ├── 103.8119119-1.3365496.jpg
│   ├── 103.8121614-1.3366166.jpg
│   ├── 103.8122767-1.3367507.jpg
│   ├── 103.812376-1.3369867.jpg
│   ├── 103.8125208-1.3372978.jpg
│   ├── 103.8174696-1.3342135.jpg
│   ├── 103.8175177-1.3342015.jpg
│   ├── 103.8175498-1.3341896.jpg
│   ├── 103.817576-1.3341744.jpg
│   ├── 103.8175954-1.3341525.jpg
│   ├── 103.8176052-1.3339948.jpg
│   ├── 103.8176081-1.3341221.jpg
│   ├── 103.8176107-1.3340427.jpg
│   └── 103.8176123-1.33409.jpg
├── Adam\ Park
│   ├── 103.8123316-1.331977.jpg
│   ├── 103.8125838-1.331864.jpg
│   ├── 103.8131354-1.3317404.jpg
│   ├── 103.813454-1.3315882.jpg
│   ├── 103.81363-1.3313742.jpg
│   ├── 103.813713-1.3303406.jpg
│   ├── 103.81374-1.3310596.jpg
│   ├── 103.8137425-1.3302173.jpg
│   ├── 103.8137841-1.3300845.jpg
│   ├── 103.8139726-1.3289442.jpg
│   ├── 103.8139745-1.3288971.jpg
│   ├── 103.8139981-1.3288462.jpg
│   ├── 103.8140378-1.3288127.jpg
│   ├── 103.8140529-1.3296554.jpg
│   ├── 103.8140937-1.3287749.jpg
│   ├── 103.8141061-1.3291786.jpg
│   ├── 103.8141172-1.3294503.jpg
│   ├── 103.8141291-1.3292492.jpg
│   └── 103.8141346-1.3293362.jpg
├── Adam\ Road
│   ├── 103.8140913-1.3282751.jpg
│   ├── 103.8140937-1.3287749.jpg
│   ├── 103.8141326-1.3284244.jpg
│   ├── 103.814175-1.3285513.jpg
│   ├── 103.8142173-1.3286633.jpg
│   ├── 103.814247-1.3291124.jpg
│   ├── 103.8144745-1.3294515.jpg
│   ├── 103.8145377-1.3295436.jpg
│   ├── 103.8161537-1.33193.jpg
│   ├── 103.816335-1.3322289.jpg
│   ├── 103.8165165-1.3325033.jpg
│   ├── 103.8166993-1.3327336.jpg
│   ├── 103.8196739-1.3382658.jpg
│   └── 103.8197248-1.3383892.jpg
├── Adis\ Road
│   ├── 103.8473761-1.2999338.jpg
│   ├── 103.8474352-1.2999231.jpg
│   ├── 103.8474616-1.3018907.jpg
│   ├── 103.8474634-1.3018376.jpg
│   ├── 103.8474823-1.3019426.jpg
│   ├── 103.847508-1.2999259.jpg
│   ├── 103.8475341-1.3017785.jpg
│   ├── 103.8475485-1.3020338.jpg
│   ├── 103.8477678-1.2999911.jpg
│   ├── 103.8478728-1.3000191.jpg
│   ├── 103.8480048-1.300081.jpg
│   ├── 103.8480976-1.3013509.jpg
│   ├── 103.8481052-1.3001347.jpg
│   ├── 103.8481822-1.3001954.jpg
│   ├── 103.8481982-1.3012517.jpg
│   ├── 103.8482453-1.3002771.jpg
│   ├── 103.8482787-1.3011458.jpg
│   ├── 103.8482873-1.3003471.jpg
│   ├── 103.8483323-1.3010291.jpg
│   ├── 103.848339-1.3004405.jpg
│   ├── 103.8483672-1.3009433.jpg
│   ├── 103.8483725-1.3005518.jpg
│   ├── 103.8483846-1.3008548.jpg
│   ├── 103.848386-1.300655.jpg
│   └── 103.84839-1.3007556.jpg
├── Admiralty\ Drive
│   ├── 103.8144727-1.4481926.jpg
│   ├── 103.8145197-1.4481451.jpg
│   ├── 103.8149691-1.4498961.jpg
│   ├── 103.8152239-1.4499777.jpg
│   ├── 103.8176211-1.4508496.jpg
│   ├── 103.817718-1.4508926.jpg
│   ├── 103.8196917-1.4517672.jpg
│   └── 103.8198963-1.4518805.jpg
├── Admiralty\ Lane
│   ├── 103.8148011-1.4591936.jpg
│   ├── 103.8148997-1.4591367.jpg
│   ├── 103.8153398-1.4588782.jpg
│   ├── 103.8156053-1.4587495.jpg
│   ├── 103.8175822-1.4577312.jpg
│   ├── 103.8179268-1.4575287.jpg
│   ├── 103.8180808-1.4574006.jpg
│   ├── 103.8186362-1.4569611.jpg
│   ├── 103.8195956-1.4559905.jpg
│   └── 103.819724-1.4558317.jpg
├── Admiralty\ Link
│   ├── 103.8151495-1.4544973.jpg
│   ├── 103.815953-1.4545836.jpg
│   ├── 103.8181803-1.4559985.jpg
│   └── 103.8183584-1.4562348.jpg
└── Admiralty\ Road
    ├── 103.7710264-1.4410374.jpg
    ├── 103.7711543-1.4410394.jpg
    ├── 103.7719245-1.4410656.jpg
    ├── 103.7745389-1.4415389.jpg
    ├── 103.7746305-1.4415933.jpg
    └── 103.7751878-1.4420243.jpg
{
    "features": [
        {
            "geometry": {
                "coordinates": [
                    [
                        103.9787969,
                        1.3692413
                    ],
                    [
                        103.9787469,
                        1.3692966
                    ],
                    [
                        103.9794143,
                        1.3684698
                    ],
                    [
                        103.9794005,
                        1.3685049
                    ],
                    [
                        103.979376,
                        1.3685506
                    ],
                    [
                        103.9787969,
                        1.3692413
                    ]
                ],
                "type": "LineString"
            },
            "id": 1,
            "properties": {
                "name": "Abingdon Road"
            },
            "type": "Feature"
        },
        {
            "geometry": {
                "coordinates": [
                    [
                        103.8176052,
                        1.3339948
                    ],
                    [
                        103.8176107,
                        1.3340427
                    ],
                    [
                        103.8176123,
                        1.33409
                    ],
                    [
                        103.8176081,
                        1.3341221
                    ],
                    [
                        103.8175954,
                        1.3341525
                    ],
                    [
                        103.817576,
                        1.3341744
                    ],
                    [
                        103.8175498,
                        1.3341896
                    ],
                    [
                        103.8175177,
                        1.3342015
                    ],
                    [
                        103.8174696,
                        1.3342135
                    ],
                    [
                        103.8116705,
                        1.3365496
                    ],
                    [
                        103.8119119,
                        1.3365496
                    ],
                    [
                        103.8121614,
                        1.3366166
                    ],
                    [
                        103.8122767,
                        1.3367507
                    ],
                    [
                        103.812376,
                        1.3369867
                    ],
                    [
                        103.8125208,
                        1.3372978
                    ]
                ],
                "type": "LineString"
            },
            "id": 2,
            "properties": {
                "name": "Adam Drive"
            },
            "type": "Feature"
        },
        {
            "geometry": {
                "coordinates": [
                    [
                        103.8140937,
                        1.3287749
                    ],
                    [
                        103.8140378,
                        1.3288127
                    ],
                    [
                        103.8139981,
                        1.3288462
                    ],
                    [
                        103.8139745,
                        1.3288971
                    ],
                    [
                        103.8139726,
                        1.3289442
                    ],
                    [
                        103.8141061,
                        1.3291786
                    ],
                    [
                        103.8141291,
                        1.3292492
                    ],
                    [
                        103.8141346,
                        1.3293362
                    ],
                    [
                        103.8141172,
                        1.3294503
                    ],
                    [
                        103.8140529,
                        1.3296554
                    ],
                    [
                        103.8137841,
                        1.3300845
                    ],
                    [
                        103.8137425,
                        1.3302173
                    ],
                    [
                        103.813713,
                        1.3303406
                    ],
                    [
                        103.81374,
                        1.3310596
                    ],
                    [
                        103.81363,
                        1.3313742
                    ],
                    [
                        103.813454,
                        1.3315882
                    ],
                    [
                        103.8131354,
                        1.3317404
                    ],
                    [
                        103.8125838,
                        1.331864
                    ],
                    [
                        103.8123316,
                        1.331977
                    ]
                ],
                "type": "LineString"
            },
            "id": 3,
            "properties": {
                "name": "Adam Park"
            },
            "type": "Feature"
        },
        {
            "geometry": {
                "coordinates": [
                    [
                        103.8166993,
                        1.3327336
                    ],
                    [
                        103.8165165,
                        1.3325033
                    ],
                    [
                        103.816335,
                        1.3322289
                    ],
                    [
                        103.8161537,
                        1.33193
                    ],
                    [
                        103.8142173,
                        1.3286633
                    ],
                    [
                        103.814175,
                        1.3285513
                    ],
                    [
                        103.8141326,
                        1.3284244
                    ],
                    [
                        103.8140913,
                        1.3282751
                    ],
                    [
                        103.8140937,
                        1.3287749
                    ],
                    [
                        103.814247,
                        1.3291124
                    ],
                    [
                        103.8144745,
                        1.3294515
                    ],
                    [
                        103.8145377,
                        1.3295436
                    ],
                    [
                        103.8197248,
                        1.3383892
                    ],
                    [
                        103.8196739,
                        1.3382658
                    ]
                ],
                "type": "LineString"
            },
            "id": 4,
            "properties": {
                "name": "Adam Road"
            },
            "type": "Feature"
        },
        {
            "geometry": {
                "coordinates": [
                    [
                        103.8475485,
                        1.3020338
                    ],
                    [
                        103.8474823,
                        1.3019426
                    ],
                    [
                        103.8474616,
                        1.3018907
                    ],
                    [
                        103.8474634,
                        1.3018376
                    ],
                    [
                        103.8474634,
                        1.3018376
                    ],
                    [
                        103.8475341,
                        1.3017785
                    ],
                    [
                        103.8480976,
                        1.3013509
                    ],
                    [
                        103.8481982,
                        1.3012517
                    ],
                    [
                        103.8482787,
                        1.3011458
                    ],
                    [
                        103.8483323,
                        1.3010291
                    ],
                    [
                        103.8483672,
                        1.3009433
                    ],
                    [
                        103.8483846,
                        1.3008548
                    ],
                    [
                        103.84839,
                        1.3007556
                    ],
                    [
                        103.848386,
                        1.300655
                    ],
                    [
                        103.8483725,
                        1.3005518
                    ],
                    [
                        103.848339,
                        1.3004405
                    ],
                    [
                        103.8482873,
                        1.3003471
                    ],
                    [
                        103.8482453,
                        1.3002771
                    ],
                    [
                        103.8481822,
                        1.3001954
                    ],
                    [
                        103.8481052,
                        1.3001347
                    ],
                    [
                        103.8480048,
                        1.300081
                    ],
                    [
                        103.8478728,
                        1.3000191
                    ],
                    [
                        103.8477678,
                        1.2999911
                    ],
                    [
                        103.847508,
                        1.2999259
                    ],
                    [
                        103.8474352,
                        1.2999231
                    ],
                    [
                        103.8473761,
                        1.2999338
                    ]
                ],
                "type": "LineString"
            },
            "id": 5,
            "properties": {
                "name": "Adis Road"
            },
            "type": "Feature"
        },
        {
            "geometry": {
                "coordinates": [
                    [
                        103.8152239,
                        1.4499777
                    ],
                    [
                        103.8149691,
                        1.4498961
                    ],
                    [
                        103.8196917,
                        1.4517672
                    ],
                    [
                        103.8198963,
                        1.4518805
                    ],
                    [
                        103.8144727,
                        1.4481926
                    ],
                    [
                        103.8145197,
                        1.4481451
                    ],
                    [
                        103.8176211,
                        1.4508496
                    ],
                    [
                        103.817718,
                        1.4508926
                    ]
                ],
                "type": "LineString"
            },
            "id": 6,
            "properties": {
                "name": "Admiralty Drive"
            },
            "type": "Feature"
        },
        {
            "geometry": {
                "coordinates": [
                    [
                        103.8156053,
                        1.4587495
                    ],
                    [
                        103.8153398,
                        1.4588782
                    ],
                    [
                        103.8148997,
                        1.4591367
                    ],
                    [
                        103.8148011,
                        1.4591936
                    ],
                    [
                        103.8180808,
                        1.4574006
                    ],
                    [
                        103.8179268,
                        1.4575287
                    ],
                    [
                        103.8186362,
                        1.4569611
                    ],
                    [
                        103.8180808,
                        1.4574006
                    ],
                    [
                        103.819724,
                        1.4558317
                    ],
                    [
                        103.8195956,
                        1.4559905
                    ],
                    [
                        103.8179268,
                        1.4575287
                    ],
                    [
                        103.8175822,
                        1.4577312
                    ],
                    [
                        103.8156053,
                        1.4587495
                    ]
                ],
                "type": "LineString"
            },
            "id": 7,
            "properties": {
                "name": "Admiralty Lane"
            },
            "type": "Feature"
        },
        {
            "geometry": {
                "coordinates": [
                    [
                        103.8183584,
                        1.4562348
                    ],
                    [
                        103.8181803,
                        1.4559985
                    ],
                    [
                        103.815953,
                        1.4545836
                    ],
                    [
                        103.8151495,
                        1.4544973
                    ]
                ],
                "type": "LineString"
            },
            "id": 8,
            "properties": {
                "name": "Admiralty Link"
            },
            "type": "Feature"
        },
        {
            "geometry": {
                "coordinates": [
                    [
                        103.7745389,
                        1.4415389
                    ],
                    [
                        103.7746305,
                        1.4415933
                    ],
                    [
                        103.7751878,
                        1.4420243
                    ],
                    [
                        103.7719245,
                        1.4410656
                    ],
                    [
                        103.7711543,
                        1.4410394
                    ],
                    [
                        103.7710264,
                        1.4410374
                    ]
                ],
                "type": "LineString"
            },
            "id": 9,
            "properties": {
                "name": "Admiralty Road"
            },
            "type": "Feature"
        }
    ],
    "type": "FeatureCollection"
}
import json
import os
import shutil
import requests
from random import choice

BASE_URL = 'https://maps.googleapis.com/maps/api/streetview?'
METADATA_URL = 'https://maps.googleapis.com/maps/api/streetview/metadata?'
SIZE = '200x200'
GOOGLE_MAPS_KEY = os.environ['GOOGLE_MAPS_KEY']
BASE_DIR = 'images100'

desktop_agents = ['Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36',
                  'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36',
                  'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36',
                  'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/602.2.14 (KHTML, like Gecko) Version/10.0.1 Safari/602.2.14',
                  'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36',
                  'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36',
                  'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36',
                  'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36',
                  'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36',
                  'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0']


def random_headers():
    return {'User-Agent': choice(desktop_agents), 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'}


if not os.path.exists(BASE_DIR):
    os.makedirs(BASE_DIR)
else:
    shutil.rmtree(BASE_DIR)

with open('geojson.json', 'r') as f:
    body = json.load(f)

streets = body['features']
for street in streets:
    street_name = street['properties']['name']
    print(f'{street_name}.')
    coordinates = street['geometry']['coordinates']
    img_dir = f'{BASE_DIR}/{street_name}'
    if not os.path.exists(img_dir):
        os.makedirs(img_dir)
    print(f'{len(coordinates)} images are going to be download.....')
    success_images_num = 0
    fail_images_num = 0
    for corrdinate in coordinates:
        latitude = corrdinate[1]
        longtitude = corrdinate[0]
        img_url = f'{BASE_URL}size={SIZE}&location={latitude},{longtitude}&key={GOOGLE_MAPS_KEY}'
        meta_data_url = f'{METADATA_URL}size={SIZE}&location={latitude},{longtitude}&key={GOOGLE_MAPS_KEY}'
        meta_data_request = requests.get(meta_data_url)
        meta_data_content = meta_data_request.json()
        if meta_data_content['status'] == "OK":
            success_images_num += 1
            img_path = f'{img_dir}/{longtitude}-{latitude}.jpg'
            r = requests.get(img_url, timeout=100, headers=random_headers())
            open(img_path, 'wb').write(r.content)
        else:
            fail_images_num += 1
    print(
        f'success: {success_images_num}/{len(coordinates)} **** fail: {fail_images_num}/{len(coordinates)}')
    print("=======================================")