-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsiteScanAPIExample.py
More file actions
214 lines (181 loc) · 8.4 KB
/
siteScanAPIExample.py
File metadata and controls
214 lines (181 loc) · 8.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
###############################################################################
# Description: This script demonstrates how to use the SiteScan API to create a
# project, mission, upload images, start processing, and check the processing
# status of the mission.
#
# Usage:
# - Update the api variables with your SiteScan API URL and token
# - Update the processing variables with the project name, mission name, and
# path to the images
# - Run the script (first time enable create mosaic, subsequent times enable add image)
#
# Author: J Totton
# Date: October 2025
# Version: 0.1
###############################################################################
###############################################################################
# imports #####################################################################
###############################################################################
import requests
import os
import shutil
import tempfile
from datetime import time
from arcgis.gis import GIS
from arcgis.raster.analytics import copy_raster
###############################################################################
# functions ###################################################################
###############################################################################
def getOrgID(token, url):
# Get organization id
orgRequestURL = f'{url}/organizations'
response = requests.get(url=orgRequestURL,
headers={'Authorization': f'Bearer {token}'})
data = response.json()
for item in data:
organization_id = item.get('id')
print(f"Organization ID: {organization_id}")
return organization_id
def createProject(token, url, organization_id, project_name):
# Create a new project in the organization
createProjectURL = f'{url}/organizations/{organization_id}/projects'
project_data = {"name": f"{project_name}"}
response = requests.post(url=createProjectURL,
headers={'Authorization': f'Bearer {token}'},
json=project_data)
if response.status_code == 201:
print("Project created successfully")
project = response.json()
project_id = project.get('id')
return project_id
else:
print(f"Failed to create project: {response.status_code}")
def createMission(token, url, project_id, mission_name):
# Create a new mission in the project
createMissionURL = f'{url}/projects/{project_id}/missions'
mission_data = {"name": f"{mission_name}"}
response = requests.post(url=createMissionURL,
headers={'Authorization': f'Bearer {token}'},
json=mission_data)
if response.status_code == 201:
print("Mission created successfully")
mission = response.json()
mission_id = mission.get('id')
return mission_id
else:
print(f"Failed to create mission: {response.status_code}")
def uploadImages(token, url, mission_id, imagesPath):
# Upload images to the mission
uploadURL = f'{url}/missions/{mission_id}/media'
for filename in os.listdir(imagesPath):
if filename.endswith('.jpg') or filename.endswith('.jpeg') or filename.endswith('.JPG') or filename.endswith('.JPEG'):
file = []
file_path = os.path.join(imagesPath, filename)
file.append(('file', (filename, open(file_path, 'rb'), 'image/jpeg')))
response = requests.post(url=uploadURL,
headers={'Authorization': f'Bearer {token}'},
files=file)
if response.status_code == 201:
print(f"{file_path} uploaded successfully!")
else:
print(f"Failed to upload images: {response.status_code}")
def startProcessing(token, url, mission_id):
# Start processing the mission
processURL = f'{url}/missions/{mission_id}/process/default'
response = requests.post(url=processURL,
headers={'Authorization': f'Bearer {token}'},
json={})
if response.status_code == 200:
print("Processing started successfully")
else:
print(f"Failed to start processing: {response.status_code}")
def checkProcessingStatus(token, url, mission_id):
# Check the processing status of the mission
missionURL = f'{url}/missions/{mission_id}'
response = requests.get(missionURL, headers={'Authorization': f'Bearer {token}'})
if response.status_code == 200:
productList = [['Ortho', 'ortho'], ['DTM', 'dtm'], ['DSM', 'dem']]
productURLs = []
for product in productList:
success = False
while success == False:
response = requests.get(missionURL, headers={'Authorization': f'Bearer {token}'})
mission_data = response.json()
productURL = mission_data.get('data', {}).get(product[1], {}).get('current', {}).get('url')
if productURL:
print(f"{product[0]} Created")
productURLs.append([product[0],productURL])
success = True
else:
print(f"{product[0]} in progress")
success = False
time.sleep(3600)
else:
print(f"Failed to get mission data: {response.status_code}")
print(response.text)
return productURLs
def downloadFiles(productURLs,temp_dir):
downloaded_files = []
for url in productURLs:
# Download the file
local_filename = os.path.join(temp_dir, url[1].split('/')[-1])
with requests.get(url[1], stream=True) as r:
r.raise_for_status()
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
print(f"Downloaded {local_filename}")
downloaded_files.append([url[0],local_filename])
return downloaded_files
def createTempFolder():
# Create a temporary directory
temp_dir = tempfile.mkdtemp()
print(f"Temporary directory created at {temp_dir}")
return temp_dir
def deleteTempFolder(temp_dir):
# Delete the temporary directory
shutil.rmtree(temp_dir)
print(f"Temporary directory {temp_dir} deleted")
###############################################################################
# api variables ###############################################################
###############################################################################
url = 'https://sitescan-api.arcgis.com/api/v2'
token = 'YOUR_API_TOKEN'
gis = GIS('home')
###############################################################################
# processing variables ########################################################
###############################################################################
project_name = "nameForProject"
mission_name = "nameForMission"
imagesPath = r"C:\images"
###############################################################################
# main ########################################################################
###############################################################################
organization_id = getOrgID(token, url)
project_id = createProject(token, url, organization_id, project_name)
mission_id = createMission(token, url, project_id, mission_name)
uploadImages(token, url, mission_id, imagesPath)
startProcessing(token, url, mission_id)
productURLs = checkProcessingStatus(token, url, mission_id)
temp_dir = createTempFolder()
localFiles = downloadFiles(productURLs,temp_dir)
print(localFiles)
for fileToUpload in localFiles:
if fileToUpload[0] in ('Ortho', 'DTM', 'DSM'):
print(f"Uploading {fileToUpload[1]} to ArcGIS Portal...")
base_name = os.path.splitext(os.path.basename(fileToUpload[1]))[0]
search = gis.content.search(base_name, max_items=1)
for item in search:
if item.title == base_name:
itemID = item.id
try:
item_for_deletion = gis.content.get(itemID)
item_for_deletion.protect(enable = False)
item_for_deletion.delete(permanent=True)
except:
pass
copy_raster(input_raster=fileToUpload[1],
output_name="test_naming")
print(f"Uploaded {fileToUpload[0]} to ArcGIS Portal!")
deleteTempFolder(temp_dir)
###############################################################################