Last active
October 25, 2019 08:27
-
-
Save ao-picterra/c77e925893d3689b72b1b48e01578cd3 to your computer and use it in GitHub Desktop.
upload and process images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def upload_and_commit(file_path, file_name): | |
| api_key = "123456789" # Get it on the platform | |
| server_url = "https://app.picterra.ch/public/api/v1" | |
| headers = { 'X-Api-Key': api_key } | |
| url = server_url + "/rasters/upload/file/" # Endpoint to get remote upload URL | |
| r = requests.post(url, headers=headers, data={'name': file_name}) | |
| response = r.json() | |
| upload_url = response["upload_url"] # Save upload URL | |
| raster_id = response["raster_id"] # Save raster identifier | |
| size = os.stat(file_path).st_size | |
| with open(file_path, 'rb') as f: | |
| data = f.read() | |
| f.close() | |
| headers = { 'Content-Length': str(size) } | |
| requests.put(upload_url, headers=headers, data=data) # Upload raster | |
| headers = { 'X-Api-Key': api_key } | |
| url = server_url + ("/rasters/%s/commit/" % raster_id) # Process raster | |
| r = requests.post(url, headers=headers) | |
| poll_interval = r.json()["poll_interval"] | |
| headers = { 'X-Api-Key': api_key } | |
| url = server_url + ("/rasters/%s/" % raster_id) | |
| while True: # Wait until the raster is processed | |
| time.sleep(poll_interval) | |
| r = requests.get(url, headers=headers) | |
| if r.json()["status"] == "ready": | |
| break | |
| print("Raster uploaded and processed, you can now detect on it") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment