コード例 #1
0
def mapboxUpload(filename):
  """
  See: https://mapbox-mapbox.readthedocs-hosted.com/en/latest/uploads.html#uploads
  """

  #raise NotImplementedError("MapboxUpload is not implemented yet")

  service = Uploader()
  service.session.params['access_token'] = mapbox_access_token
  mapid = 'uploads-test' # 'uploads-test'
  with open(filename, 'rb') as src:
    upload_resp = service.upload(src, mapid)

    if upload_resp.status_code == 422:
      for i in range(5):
        sleep(5)
        with open(filename, 'rb') as src:
          upload_resp = service.upload(src, mapid)
        if upload_resp.status_code != 422:
          break
    
    upload_id = upload_resp.json()['id']
    for i in range(5):
      status_resp = service.status(upload_id).json()
      if status_resp['complete']:
        print(status_resp)
        print("Finished uploading tileset " + mapid)
        break
      sleep(5)
コード例 #2
0
    def upload_as_tileset(self, dataset_filename, map_id=None):
        map_id = uuid.uuid1().hex if not map_id else map_id
        service = Uploader()
        with open(os.path.join(DATASETS_PATH, dataset_filename)) as src:
            upload_resp = service.upload(src, map_id)

        if upload_resp.status_code == 201:
            upload_id = upload_resp.json()['id']

            for i in xrange(0, 15):
                status_resp = service.status(upload_id).json()
                if 'complete' == status_resp['complete']:
                    logger.info("Tileset completed for dataset %s",
                                dataset_filename)
                    break
                else:
                    logger.info("Waiting for upload to complete")
                    sleep(5)
            logger.info(
                "Upload did not complete in the last 75 seconds. Check dashboard."
            )
コード例 #3
0
def upload_shapefile(data, dataset_name):
    service = Uploader()
    upload_resp = service.upload(data, dataset_name)
    if upload_resp.status_code != 201:
        raise MapboxException(
            f"Upload failed with status {upload_resp.status_code}")

    upload_id = upload_resp.json()["id"]

    # wait on status to change
    while True:
        status = service.status(upload_id)
        if status.status_code != 200:
            raise MapboxException(
                f"Status check failed with status {status.status_code}")
        status = status.json()
        if status["complete"]:
            break
        if status["error"]:
            raise MapboxException("mapbox error: " + status["error"])
        time.sleep(10)
コード例 #4
0
def upload_shapefile(data, dataset_name):
    service = Uploader()
    upload_resp = service.upload(data, dataset_name)
    if upload_resp.status_code == 201:
        upload_id = upload_resp.json()["id"]
        while True:
            status = service.status(upload_id)
            if status.status_code != 200:
                break
            print(status.status_code)
            status = status.json()
            if status["complete"]:
                break
            if status["error"]:
                print("mapbox error:", status["error"])
                break
            else:
                print(status)
            time.sleep(10)
    else:
        print("Upload failed with status", upload_resp.status_code)
コード例 #5
0
def update_mapbox_tileset(dataset, lang):
    print('Uploading {} version to Mapbox'.format(lang.upper()))

    MAPBOX_API_KEY = secrets['MAPBOX_API_KEY']
    TILESET_ID = secrets['TILESET_ID_' + lang.upper()]

    def init_upload():
        with open('src/hexes_crime_{}.geojson'.format(lang), 'rb') as src:
            upload_resp = uploader.upload(src, TILESET_ID)
            return upload_resp

    # Write geodataframe to JSON file to be read into Mapbox
    with open('src/hexes_crime_{}.geojson'.format(lang), 'w',
              encoding='utf-8') as out:
        out.write(dataset.to_json())

    uploader = Uploader(MAPBOX_API_KEY)
    upload_resp = init_upload()

    # Keep trying if upload doesn't succeed
    if upload_resp.status_code == 422:
        print('Update unsuccessful. Retrying...')
        while True:
            time.sleep(5)
            upload_resp = init_upload()
            if upload_resp.status_code != 422:
                break

    # If upload successful, check on status
    if upload_resp.status_code == 201:
        upload_id = upload_resp.json()['id']
        while True:
            status_resp = uploader.status(upload_id).json()
            if status_resp['complete']:
                print('Mapbox tileset update successful.')
                break
            time.sleep(5)
    else:
        raise Exception('Unable to connect to Mapbox.')
コード例 #6
0
    os.chdir(data_dir)

    from mapbox import Uploader
    service = Uploader(access_token=token)

    try:
        with open(seg_out_file_name + '.zip', 'r') as src:
            upload_resp_seg = service.upload(src, 'ADV_all_segments')
        with open(act_out_file_name + '.zip', 'r') as src:
            upload_resp_act = service.upload(src, 'ADV_all_activities')

        upload_id_act = upload_resp_act.json()['id']
        upload_id_seg = upload_resp_seg.json()['id']

        status_resp_act = service.status(upload_id_act).json()
        status_resp_seg = service.status(upload_id_seg).json()
        logging.info('successfully uploaded files to Mapbox account')

    except Exception as e:
        logging.exception('error uploading Mapbox files using Uploads API to acount')
        pass

    try:
        for i in range(10):
            status_resp_seg = service.status(upload_id_seg).json()
            status_resp_act = service.status(upload_id_act).json()
            if (status_resp_seg['complete'] and status_resp_act['complete']):
                logging.info(
                    'processing complete for all uploaded files on Mapbox!')
                break
コード例 #7
0
# from mapbox import Geocoder
# >>> geocoder = Geocoder(access_token="pk.YOUR_ACCESS_TOKEN")
from mapbox import Uploader
service = Uploader()
from time import sleep
from random import randint
mapid = getfixture('uploads_dest_id')  # 'uploads-test'
with open('tests/twopoints.geojson', 'rb') as src:
    upload_resp = service.upload(src, mapid)

if upload_resp.status_code == 422:
    for i in range(5):
        sleep(5)
        with open('tests/twopoints.geojson', 'rb') as src:
            upload_resp = service.upload(src, mapid)
        if upload_resp.status_code != 422:
            break

upload_resp.status_code

upload_id = upload_resp.json()['id']
for i in range(5):
    status_resp = service.status(upload_id).json()
    if status_resp['complete']:
        break
    sleep(5)

mapid in status_resp['tileset']