-
Notifications
You must be signed in to change notification settings - Fork 1
/
insightsworkbook.py
623 lines (587 loc) · 28 KB
/
insightsworkbook.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
""" Class for interacting with ArcGIS Insights """
import json
import random
import sys
from urllib.error import HTTPError
from arcgis.gis import GIS
__version__ = '0.1'
class InsightsWorkbook(object):
""" An object representing an ArcGIS Insights workbook
Users can use this class to create new Insights workbooks, connect to
existing workbooks, and modify them. Currently, it serves as a proof of
concept to demonstrate adding data, creating a map, and creating a chart,
but new functionality can easily be added in the future.
================ ========================================================
**Argument** **Description**
---------------- --------------------------------------------------------
gis GIS connection object from arcgis.gis (ArcGIS API for
Python). Connection should be set up before working with
this class.
---------------- --------------------------------------------------------
title Optional string. Title for the Insights workbook
---------------- --------------------------------------------------------
workbook_id Optional string. Random ID used for Hosted storage.
---------------- --------------------------------------------------------
workspace_id Optional string. Workbook Item ID in ArcGIS - generated
by ArcGIS upon creation of a workbook.
---------------- --------------------------------------------------------
workspace_url Optional string. URL where the Insights workbook
internal hosted workspace resides. Structure differs
between ArcGIS Online and ArcGIS Portal.
---------------- --------------------------------------------------------
props Optional dict. Dictionary of workbook properties that
represents the full JSON data object that is stored in
ArcGIS.
================ ========================================================
.. code-block:: python
# Usage Example 1: Anonymous Login to ArcGIS Online
"""
def __init__(self, gis, title=None, workbook_id=None, workspace_id=None,
workspace_url=None, props=None):
"""
Constructs the Workbook given the aforementioned parameters. Normally,
the Workbook object will be created with either the new() or open()
class methods below.
"""
self._gis = gis
self._title = title
self._agol = False
# Check whether it's AGOL or Portal
if gis._url.find('arcgis.com') >= 0:
self._agol = True
self._workbookID = workbook_id
self._workspaceID = workspace_id
self._workspaceURL = workspace_url
self.props = props
@classmethod
def new(cls, gis, title):
"""
Creates a new Insights Workbook in ArcGIS using the provided title.
================== =================================================
**Argument** **Description**
------------------ -------------------------------------------------
gis Required arcgis.gis.GIS. Connection should be set
up before working with this class.
------------------ -------------------------------------------------
title Optional string. Title to be assigned to the new
Workbook.
================== =================================================
:return:
New InsightsWorkbook object with the provided title.
"""
# Random 8-digit hex number for ID
workbook_id = '%08x' % random.randrange(16**8)
# Hosted service URL differs between Portal and AGOL
insights_service_url = gis._url.lower() + \
'/arcgis/rest/services/Hosted/'
if gis._url.find('arcgis.com') >= 0:
insights_service_url = 'https://insightsservices.arcgis.com/' +\
gis._portal._properties['id'] + \
'/arcgis/rest/services/'
workspace_url = insights_service_url + workbook_id + "/WorkspaceServer"
# Path for REST API to create new Workspace Service (ArcGIS Insights
# internal data storage)
path = gis._portal.url + '/sharing/rest/content/users/' + \
gis.users.me.username + '/createService'
post_data = {'f': 'json',
'createParameters': '{"name": "' + workbook_id + '"}',
'targetType': 'workspaceService'}
# Catch any errors from POSTing
try:
# The first call creates the workspace, but the Workbook is not
# functional until after the second POST below
resp = gis._portal.con.post(path, post_data)
# Depending on whether it's on Portal or AGOL, it will use a
# different key for ID.
if gis._url.find('arcgis.com') < 0:
workspace_id = resp['itemId']
else:
workspace_id = resp['serviceItemId']
item_props = {
"f": "json",
"id": workspace_id,
"type": "Insights Workbook",
"title": title}
# For some reason this is not needed for Portal, but it is for AGOL
if gis._url.find('arcgis.com') >= 0:
item_props["url"] = workspace_url
# Full set of default JSON data properties for an ArcGIS Insights
# workbook item.
props = {
"format": 9,
"title": title,
"pages": [{
"title": "Page 1",
"model": {
"items": []
},
"cards": [],
"layout": [],
"contents": []
}],
"activePage": 0,
"workspace": {
"datasets": {}
},
"_ssl": True,
"created": 0,
"modified": 0,
"guid": None,
"type": "Insights Workbook",
"typeKeywords": ["Application", "ArcGIS",
"Insights Workbook", "Hosted Service"],
"description": None,
"tags": [],
"snippet": None,
"thumbnail": None,
"documentation": None,
"extent": [],
"categories": [],
"spatialReference": None,
"accessInformation": None,
"licenseInfo": None,
"culture": "english (united states)",
"properties": None,
"proxyFilter": None,
"access": "private",
"size": 0,
"appCategories": [],
"industries": [],
"languages": [],
"largeThumbnail": None,
"banner": None,
"screenshots": [],
"listed": False,
"ownerFolder": None,
"protected": False,
"commentsEnabled": True,
"numComments": 0,
"numRatings": 0,
"avgRating": 0,
"numViews": 3,
"itemControl": "admin",
"scoreCompleteness": 0,
"groupDesignations": None
}
# After the first call sets up the workspace, this second call sets
# up the actual Workbook with all the data props, title, etc.
gis._portal.update_item(workspace_id, item_props,
json.dumps(props))
# Now that it's created, store the relevant properties in this class
# for use in other functions (e.g. add data, create map, etc.)
return cls(gis, title, workbook_id,
workspace_id, workspace_url, props)
except:
print("Error creating workbook: " + sys.exc_info()[0])
raise Exception()
@classmethod
def open(cls, existing_workbook):
"""
Creates a new Insights Workbook in ArcGIS using the provided title.
================== =================================================
**Argument** **Description**
------------------ -------------------------------------------------
gis Required arcgis.gis.Item. Must pass a previously-
created ArcGIS Insights Workbook. The ArcGIS API
for Python Item object should be of the
"Insights Workbook" type.
================== =================================================
:return:
InsightsWorkbook object that points to this existing Workbook
"""
gis = existing_workbook._gis
title = existing_workbook.title
workbook_id = existing_workbook.name
# Hosted service URL differs between Portal and AGOL
insights_service_url = gis._url.lower() + \
'/arcgis/rest/services/Hosted/'
if gis._url.find('arcgis.com') >= 0:
insights_service_url = 'https://insightsservices.arcgis.com/' +\
gis._portal._properties['id'] + \
'/arcgis/rest/services/'
workspace_url = insights_service_url + workbook_id + "/WorkspaceServer"
workspace_id = existing_workbook.id
# Path to get the JSON data for this Workbook item
path = gis._url.lower() + '/sharing/rest/content/items/' + \
workspace_id + '/data'
try:
resp = gis._portal.con.get(path, {'f': 'json'})
props = resp
return cls(gis, title, workbook_id,
workspace_id, workspace_url, props)
except:
print("Error retrieving workbook data: " + sys.exc_info()[0])
raise Exception()
def add_feature_layer(self, lyr, sublayer=0):
"""
Adds a feature layer as a dataset to this Workbook
================== =================================================
**Argument** **Description**
------------------ -------------------------------------------------
lyr Required arcgis.gis.Item. Must pass a feature
layer from the ArcGIS API for Python.
------------------ -------------------------------------------------
sublayer Optional int. Index of the sublayer to add.
================== =================================================
:return:
String name of new internal Insights Workbook dataset
"""
# Random 7-digit hex suffix for dataset name
dataset_name = lyr.title + '_%07x' % random.randrange(16**7)
post_data = {'f': 'json',
'tools': '[{"name":"add-data","params":{"data":{' +
'"type":"feature-layer",' +
'"url":"' + lyr.url + '/' + str(sublayer) +
'"}},"outDataset":"' + dataset_name + '"}]',
'outDatasets': '["' + dataset_name + '"]'}
try:
# Execute the add-data operation within ArcGIS Insights. Note: data
# is not automatically saved in the Workbook. Must manually call
# save in order to make it permanent.
resp = self._gis._portal.con.post(self._workspaceURL + '/execute',
post_data)
# In addition to calling execute to create the dataset, it must also
# be placed in the Workbook Item properties in several places:
self.props['pages'][0]['model']['items'].append({
'operation': 'add-data',
'params': {
'data': {
'type': 'feature-layer',
'url': lyr.url + '/' + str(sublayer)
}
},
'outDataset': dataset_name
})
self.props['pages'][0]['contents'].append({
'dataset': dataset_name
})
my_extent = json.loads(lyr.layers[sublayer].properties.extent.__repr__())
self.props['workspace']['datasets'][dataset_name] = {
'data': resp[dataset_name],
'owner': lyr.id,
'fields': {
'shape': {
'alias': 'Location'
}
},
'extent': my_extent,
'origin': True
}
return dataset_name
except:
print("Error adding feature layer: " + sys.exc_info()[0])
raise Exception()
def update_dataset(self, lyr, sublayer=0):
"""
Updates all references to the provided feature layer within this
Workbook
================== =================================================
**Argument** **Description**
------------------ -------------------------------------------------
lyr Required arcgis.gis.Item. Must pass a feature
layer from the ArcGIS API for Python.
------------------ -------------------------------------------------
sublayer Optional int. Index of the sublayer to add.
================== =================================================
:return:
String name of internal Insights Workbook dataset
"""
# Get the current Workbook model items
model_items = self.props['pages'][0]['model']['items']
# Find the specific model item related to adding this dataset
add_op = [x for x in model_items if ('outDataset' in x and
'params' in x and
'data' in x['params'] and
'url' in x['params']['data'] and
x['params']['data']['url'].find(lyr.url) >= 0)]
# Check to see if this layer actually exists in this workbook
if len(add_op) > 0:
# Get internal name of this dataset
dataset_name = add_op[0]['outDataset']
post_data = {'f': 'json',
'tools': '[{"name":"add-data","params":{"data":{' +
'"type":"feature-layer",' +
'"url":"' + add_op[0]['params']['data']['url'] +
'"}},"outDataset":"' + dataset_name + '"}]',
'outDatasets': '["' + dataset_name + '"]'}
try:
# Execute the add-data operation within ArcGIS Insights. This
# runs identically to the add data operation - it just
# generates a new data ID for us to work with.
resp = self._gis._portal.con.post(self._workspaceURL +
'/execute', post_data)
old_data = self.props['workspace']['datasets'][dataset_name]['data']
# Update existing workspace entry in JSON with new data ID
my_extent = json.loads(lyr.layers[sublayer].properties.extent.__repr__())
self.props['workspace']['datasets'][dataset_name] = {
'data': resp[dataset_name],
'owner': lyr.id,
'fields': {
'shape': {
'alias': 'Location'
}
},
'extent': my_extent,
'origin': True
}
# Find all references to old data ID and replace with new
for ds in self.props['workspace']['datasets'].keys():
try:
ds_tools = self.props['workspace']['datasets'][ds]['data']['tools']
for i in range(len(ds_tools)):
if self.props['workspace']['datasets'][ds]['data']['tools'][i]['params']['dataset'] == old_data:
self.props['workspace']['datasets'][ds]['data']['tools'][i]['params']['dataset'] = resp[dataset_name]
except (KeyError, TypeError):
# If particular dataset doesn't have this key, move to
# next.
continue
# Send back the dataset name
return dataset_name
except HTTPError:
print("Error updating feature layer: " + sys.exc_info()[0])
else:
print("Layer does not exist within this Workbook")
raise Exception()
def add_map(self, dataset):
"""
Adds a map card for the specified feature layer dataset
================== =================================================
**Argument** **Description**
------------------ -------------------------------------------------
dataset Required string. Internal dataset name for the
feature layer to add to the map. Can get this
value when feature layer is added to the
Workbook. Alternatively, can look it up in the
props.
================== =================================================
"""
# Grab full dataset info from the props
layer_dataset = self.props['workspace']['datasets'][dataset]
if layer_dataset:
# Get current number of cards
card_ct = len(self.props['pages'][0]['cards'])
# Get extent of this layer
my_extent = self.props['workspace']['datasets'][dataset]['extent']
# Add map, with layer and extent
self.props['pages'][0]['cards'].append({
'type': 'map',
'title': 'Card ' + str(card_ct+1),
'content': {
'layers': [{'datasetId': dataset}],
'extent': my_extent
}
})
# Just place new map to the right of the last card
layout_x = card_ct * 20 + card_ct
self.props['pages'][0]['layout'].append({
"x": layout_x,
"y": 0,
"w": 20,
"h": 20
})
else:
print("Invalid dataset name")
raise Exception()
def aggregate(self, in_dataset, groupby_field, groupby_field_type,
stat_type, stat_field, stat_field_type, out_name=None):
"""
Aggregates data based on the group-by layer using the specified
statistic over the specified field.
================== =================================================
**Argument** **Description**
------------------ -------------------------------------------------
in_dataset Required str. Internal name of dataset to use for
aggregating.
------------------ -------------------------------------------------
groupby_field Required str. Name of the field to group by.
------------------ -------------------------------------------------
groupby_field_type Required str. Type of field. Acceptable values
include esriFieldTypeString, esriFieldTypeDouble,
esriFieldTypeInteger, etc.
------------------ -------------------------------------------------
stat_type Required str. Type of aggregation statistic.
Acceptable values include avg, sum, count, min
and max.
------------------ -------------------------------------------------
stat_field Required str. Name of the field to use in
calculation.
------------------ -------------------------------------------------
stat_field_type Required str. Type of field for output.
Acceptable values include esriFieldTypeString,
esriFieldTypeDouble, esriFieldTypeInteger, etc.
------------------ -------------------------------------------------
out_name Optional str. Name of dataset to show to user.
================== =================================================
:return:
String name of new internal Insights Workbook dataset for this
aggregation
"""
# Get base name of dataset so we can generate a new suffix for new
# aggregate dataset
in_dataset_base = in_dataset[:in_dataset.find('_')]
# Generate new dataset name with 7 random hex digits as suffix
out_dataset = in_dataset_base + '_%07x' % random.randrange(16**7)
# New field for storing aggregation
out_field = stat_field.lower() + '_' + stat_type
if stat_type == 'count':
out_type = 'esriFieldTypeInteger'
elif stat_type == 'avg':
out_type = 'esriFieldTypeDouble'
else:
out_type = stat_field_type
# Perform aggregate operation
self.props['pages'][0]['model']['items'].append({
'operation': 'aggregate',
'params': {
'dataset': in_dataset,
'groupBy': [groupby_field],
'statistics': [{
'type': stat_type,
'field': stat_field,
'outField': out_field
}],
'totals': False
},
'outDataset': out_dataset
})
in_data_id = self.props['workspace']['datasets'][in_dataset]['data']
# If no name specified for this dataset just use internal ID
if not out_name:
out_name = out_dataset
# Add new dataset to workspace
self.props['workspace']['datasets'][out_dataset] = {
'data': {
'metadata': {
'fields': [
{
'name': groupby_field,
'alias': groupby_field,
'type': groupby_field_type,
'entity': 'e0'
},
{
'name': out_field,
'alias': out_field,
'type': out_type
}
],
'entities': [{
'fields': [
groupby_field,
out_field
],
'keyFields': [groupby_field]
}]
},
'tools': [{
'name': 'aggregate',
'params': {
'dataset': in_data_id,
'groupBy': [groupby_field],
'statistics': [{
'type': stat_type,
'field': stat_field,
'outField': out_field
}],
'materialize': False
},
'outDataset': out_dataset
}],
'outDataset': out_dataset
},
'name': out_name,
'fields': {
out_field: {
'alias': stat_type.title() + " of " + in_dataset_base
},
groupby_field: {}
}
}
return out_dataset
def add_chart(self, chart_type, in_dataset, groupby_field,
groupby_field_type, stat_type, stat_field, stat_field_type):
"""
Aggregates data based on the group-by layer using the specified
statistic over the specified field.
================== =================================================
**Argument** **Description**
------------------ -------------------------------------------------
chart_type Required str. Type of chart to create. Acceptable
values are bar and column.
------------------ -------------------------------------------------
in_dataset Required str. Internal name of dataset to use for
aggregating.
------------------ -------------------------------------------------
groupby_field Required str. Name of the field to group by.
------------------ -------------------------------------------------
groupby_field_type Required str. Type of field. Acceptable values
include esriFieldTypeString, esriFieldTypeDouble,
esriFieldTypeInteger, etc.
------------------ -------------------------------------------------
stat_type Required str. Type of aggregation statistic.
Acceptable values include avg, sum, count, min
and max.
------------------ -------------------------------------------------
stat_field Required str. Name of the field to use in
calculation.
------------------ -------------------------------------------------
stat_field_type Required str. Type of field for output.
Acceptable values include esriFieldTypeString,
esriFieldTypeDouble, esriFieldTypeInteger, etc.
================== =================================================
"""
# Get count of cards
card_ct = len(self.props['pages'][0]['cards'])
# Set chart title
chart_title = chart_type.title() + " Chart " + str(card_ct+1)
# Create aggregation dataset for this chart
out_dataset = self.aggregate(in_dataset, groupby_field,
groupby_field_type, stat_type, stat_field,
stat_field_type, chart_title)
# Create the chart card
self.props['pages'][0]['cards'].append({
'title': 'Card ' + str(card_ct+1),
'type': 'chart',
'content': {
'layers': [{
'datasetId': out_dataset,
'chartLines': {
'mean': True
},
'colors': {}
}],
'type': chart_type
}
})
# Place it to the right of any existing cards
layout_x = card_ct * 20 + card_ct
self.props['pages'][0]['layout'].append({
"x": layout_x,
"y": 0,
"w": 20,
"h": 20
})
def save(self):
"""
Saves the Insights Workbook to ArcGIS with all the current properties.
"""
# A few properties have to be manually set at save (doesn't work to
# just set them on initial Workbook creation).
self.props["id"] = self._workspaceID
self.props["owner"] = self._gis.users.me.username
self.props["name"] = self._workbookID
self.props["url"] = self._workspaceURL
post_data = {
'f': 'json',
'title': self._title,
'text': json.dumps(self.props)}
# Basically just a standard ArcGIS item update with the updated JSON
# properties
update_url = self._gis._url.lower() + '/sharing/rest/content/users/' + \
self._gis.users.me.username + '/items/' + \
self._workspaceID + '/update'
try:
self._gis._portal.con.post(update_url, post_data)
except:
print("Error saving workspace: " + sys.exc_info()[0])
raise Exception()