This repository was archived by the owner on Dec 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathanalysis.py
172 lines (141 loc) · 5.35 KB
/
analysis.py
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
"""An Analysis is a set of operations which take projects as inputs and output raster imagery"""
import requests
from .. import NOTEBOOK_SUPPORT
from .export import Export
from .project import Project
from ..decorators import check_notebook
from ..exceptions import GatewayTimeoutException
if NOTEBOOK_SUPPORT:
from ipyleaflet import (
Map,
SideBySideControl,
TileLayer,
)
class Analysis(object):
"""A Raster Foundry Analysis"""
TILE_PATH_TEMPLATE = '/tools/{id}/{{z}}/{{x}}/{{y}}/'
EXPORT_TEMPLATE = '/tools/{analysis}/raw/'
def __repr__(self):
return'<Analysis - {}>'.format(self.name)
def __init__(self, analysis, api):
"""Instantiate a new Analysis
Args:
analysis (Analysis): generated Analysis object from specification
api (API): api used to make requests on behalf of the analysis
"""
self._analysis = analysis
self.api = api
self.name = analysis.name
self.id = analysis.id
def get_thumbnail(self, bbox, zoom, raw=False):
export_path = self.EXPORT_TEMPLATE.format(analysis=self.id)
request_path = '{scheme}://{host}{export_path}'.format(
scheme=self.api.scheme, host=self.api.tile_host, export_path=export_path
)
response = requests.get(
request_path,
params={
'bbox': bbox,
'zoom': zoom,
'token': self.api.api_token,
'colorCorrect': 'false' if raw else 'true'
}
)
if response.status_code == requests.codes.gateway_timeout:
raise GatewayTimeoutException(
'The export request timed out. '
'Try decreasing the zoom level or using a smaller bounding box.'
)
response.raise_for_status()
return response
def create_export(self, bbox, zoom=10, **exportOpts):
"""Download this Analysis as a single band tiff
Args:
bbox (str): Bounding box(formatted as 'x1,y1,x2,y2') for the download
zoom (int): Zoom level for the download
exportOpts (dict): Additional parameters to pass to an async export job
Returns:
Export
"""
return Export.create_export(self.api, bbox=bbox, zoom=zoom, analysis=self, **exportOpts)
def tms(self, node=None):
"""Returns a TMS URL for this project
Args:
node (string): UUID for the node to view, defaulting to the full analysis
Returns:
str
"""
tile_path = self.TILE_PATH_TEMPLATE.format(id=self.id)
return '{scheme}://{host}{tile_path}?token={token}'.format(
scheme=self.api.scheme, host=self.api.tile_host,
tile_path=tile_path, token=self.api.api_token
)
@check_notebook
def get_layer(self):
"""Returns a TileLayer for display using ipyleaflet"""
return TileLayer(url=self.tms())
@check_notebook
def compare(self, other, leaflet_map):
"""Add a slider to compare two projects
This project determines the map center.
Args:
other (Project): the project to compare with this project
leaflet_map (Map): map to add the slider to
"""
control = SideBySideControl(
leftLayer=self.get_layer(), rightLayer=other.get_layer()
)
leaflet_map.add_control(control)
@check_notebook
def add_to(self, leaflet_map):
"""Add this project to a leaflet map
Args:
leaflet_map (Map): map to add this layer to
"""
leaflet_map.add_layer(self.get_layer())
@check_notebook
def get_map(self, **kwargs):
"""Return an ipyleaflet map centered at the analysis's center
Args:
**kwargs: additional arguments to pass to map initializations
"""
default_url = (
'https://cartodb-basemaps-{s}.global.ssl.fastly.net/'
'light_all/{z}/{x}/{y}.png'
)
return Map(
default_tiles=TileLayer(url=kwargs.get('url', default_url)),
center=self.get_center(),
scroll_wheel_zoom=kwargs.get('scroll_wheel_zoom', True),
**kwargs
)
def get_inputs(self):
"""Get the input nodes for the analysis"""
dag_root = self._analysis.executionParameters
nodes = [dag_root]
inputs = []
while len(nodes) > 0:
current = nodes.pop()
args = current.get('args')
if current.get('type') == 'projectSrc':
inputs.append(current)
if type(args) == list:
for arg in args:
nodes.append(arg)
return inputs
def get_center(self):
"""Get the center of this analysis's first input's extent"""
# get analysis input UUIDS
inputs = self.get_inputs()
if len(inputs) > 0:
# fetch first one from api and use that project's coordinates
input = inputs.pop()
project = Project(
self.api.client.Imagery.get_projects_projectID(
projectID=input.get('projId')
).result(),
self.api
)
return project.get_center()
else:
raise ValueError('An analysis must have inputs in order to be valid')