/
t_utils.py
633 lines (610 loc) · 26.1 KB
/
t_utils.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
624
625
626
627
628
629
630
631
632
633
def get_chains():
chain_def = {
"map_tiles": [
{
"algorithm": "getmaptiles_roi",
"parameter_source": "user"
}, {
"algorithm": "stitch_tiles",
"parameters": {
"image_filenames": {
"source": "chain_ledger",
"source_algorithm": "getmaptiles_roi",
"key": "image_filenames",
"occurrence": "first"
}
}
}, {
"algorithm": "output_image_to_client",
"parameters": {
"image_path": {
"source": "chain_ledger",
"source_algorithm": "stitch_tiles",
"key": "image_path",
"occurrence": "first"
},
"image_bounds": {
"source": "chain_ledger",
"source_algorithm": "stitch_tiles",
"key": "image_bounds",
"occurrence": "first"
}
}
}
]
}
return chain_def
def get_updated_chain():
original_chain = get_chains()
new_chain = {
"do_some_math": [
{
"algorithm": "add_numbers",
"parameter_source": "user"
}, {
"algorithm": "subtract_numbers",
"parameters": {
"starting_value": {
"source": "chain_ledger",
"occurrence": "first",
"key": "result",
"source_algorithm": "add_numbers"
},
"number_to_subtract": {
"source": "user"
}
}
}, {
"algorithm": "multiply_numbers",
"parameters": {
"number_to_multiply": {
"source": "user"
},
"starting_value": {
"source": "chain_ledger",
"occurrence": "first",
"key": "result",
"source_algorithm": "subtract_numbers"
}
}
}, {
"algorithm": "divide_numbers",
"parameters": {
"number_to_divide": {
"source": "user"
},
"starting_value": {
"source": "chain_ledger",
"occurrence": "first",
"key": "result",
"source_algorithm": "multiply_numbers"
}
}
}, {
"algorithm": "output_text",
"parameters": {
"result": {
"source": "chain_ledger",
"occurrence": "first",
"key": "result",
"source_algorithm": "divide_numbers"
}
}
}
]
}
new_chain.update(original_chain)
return new_chain
def get_algorithms():
algs = [
{
"name": "getmaptiles_roi",
"display_name": "Get Map Tiles In ROI",
"description":
"This algorithm will gather up map tiles at a given zoom "
"level that intersect with the provided polygon. The source "
"is the national map provided by USGS. All tiles will be "
"written out to disk at a specified location. This "
"location is also saved onto the chain ledger.",
"version": "0.0.1",
"license": "MIT",
"private": False,
"homepage": "https://tiledriver.com/developer",
"required_parameters": [
{
"name": "roi",
"description":
"Polygon WKT to obtain tiles that intersect.",
"display_name": "Polygon WKT",
"data_type": "string",
"field_type": "text",
"help_text":
"Enter well known text (WKT) for the polygon region "
"to obtain intersecting tiles.",
"min_value": None,
"max_value": None,
"default_value":
"POLYGON((-77.0419692993164 38.9933585922412,"
"-77.17311859130861 38.891887936025896,"
"-77.03853607177736 38.790272111428706,"
"-76.91013336181642 38.891887936025896,"
"-77.0419692993164 38.9933585922412))",
"custom_validation": None,
"parameter_choices": [],
"sort_order": 0
},
{
"name": "zoom",
"description": "Zoom level",
"display_name": "Zoom level",
"data_type": "integer",
"field_type": "number",
"help_text":
"Enter an integer corresponding to the zoom "
"level, 16 is max value supported.",
"min_value": 5,
"max_value": 16,
"default_value": 14,
"custom_validation": None,
"parameter_choices": [],
"sort_order": 1
}
],
"optional_parameters": [],
"outputs": [
{
"name": "image_filenames",
"description":
"Absolute paths to each image collected separated "
"by commas.",
"display_name": "Names of images collected",
"data_type": "string",
"sort_order": 0
}
]
}, {
"name": "stitch_tiles",
"display_name": "Stitch together tiles",
"description":
"This algorithm stitches a group of map tiles saved in a "
"directory together. The resulting image is saved in png "
"format with the image path and bounds saved onto the "
"chain ledger.",
"version": "0.0.1",
"license": "MIT",
"private": False,
"homepage": "https://tiledriver.com/developer",
"required_parameters": [
{
"name": "image_filenames",
"description":
"List of image filenames to stitch together",
"display_name": "Images to Stitch Together",
"data_type": "string",
"field_type": "text",
"help_text": "Comma separated list.",
"min_value": None,
"max_value": None,
"default_value": None,
"custom_validation": None,
"parameter_choices": [],
"sort_order": 0
}
],
"optional_parameters": [],
"outputs": [
{
"name": "image_path",
"description": "Path to image that was stitched together",
"display_name": "Stitched Image Path",
"data_type": "string",
"sort_order": 0
},
{
"name": "image_bounds",
"description":
"Array containing the leaflet bounds of stiched "
"image extent",
"display_name": "Stitched Image Bounds",
"data_type": "string",
"sort_order": 1
}
]
}, {
"name": "output_image_to_client",
"display_name": "Output Image To Web",
"description":
"This algorithm will pull the path to an image (RGB png "
"currently supported) and image bounds (as defined by "
"leaflet) from the chain ledger and re-enter this "
"information with the correct keys needed by the Algorithm "
"Toolkit to send an image back to a chain endpoint request "
"for placement onto a map.",
"version": "0.0.1",
"license": "MIT",
"private": False,
"homepage": "https://tiledriver.com/developer",
"required_parameters": [
{
"name": "image_path",
"description": "Path of image to send to client",
"display_name": "Image Path",
"data_type": "string",
"field_type": "text",
"help_text": "Absolute path to image",
"min_value": None,
"max_value": None,
"default_value": None,
"custom_validation": None,
"parameter_choices": [],
"sort_order": 0
},
{
"name": "image_bounds",
"description":
"Array containing the leaflet bounds of image extent",
"display_name": "Image Bounds",
"data_type": "string",
"field_type": "text",
"help_text":
"String array containing image bounds numbers.",
"min_value": None,
"max_value": None,
"default_value": None,
"custom_validation": None,
"parameter_choices": [],
"sort_order": 1
}],
"optional_parameters": [],
"outputs": [
{
"name": "image_url",
"description": "Path of image to send to client",
"display_name": "Image Path",
"data_type": "string",
"sort_order": 0
},
{
"name": "image_extent",
"description":
"Array containing the leaflet bounds of image extent",
"display_name": "Image Bounds",
"data_type": "string",
"sort_order": 1
}
]
}
]
return algs
def get_chain_algs():
algs = get_algorithms()
chain_algs = []
for alg in algs:
for p in alg['required_parameters']:
if alg['name'] == 'getmaptiles_roi':
p['source'] = 'user'
else:
p['source'] = 'chain_ledger'
chain_algs.append(alg)
return chain_algs
def get_test_run_chain():
chain = {
"algorithms": [
{
"name": "getmaptiles_roi",
"parameters": {
"roi":
"POLYGON((-77.0419692993164 38.9933585922412,"
"-77.17311859130861 38.891887936025896,"
"-77.03853607177736 38.790272111428706,"
"-76.91013336181642 38.891887936025896,"
"-77.0419692993164 38.9933585922412))",
"zoom": "14"
}
}, {
"name": "stitch_tiles",
"parameters": {}
}, {
"name": "output_image_to_client",
"parameters": {}
}
],
"chain_name": "map_tiles"
}
return chain
def test_algorithm_form_data():
alg = {
"description": "Add two numbers together to get a result.",
"display_name": "Add two numbers",
"homepage": "google.com",
"license": "Proprietary",
"name": "add_numbers",
"parameters": [
{
"custom_validation": "",
"data_type": "integer",
"default_value": "",
"description": "The starting value for the add operation.",
"display_name": "Starting Value",
"field_type": "number",
"help_text": "Enter a starting value",
"max_value": "0",
"min_value": "100",
"name": "starting_value",
"original_name": "starting_value",
"parameter_choices": "",
"required": True,
"sort_order": 0
},
{
"custom_validation": "",
"data_type": "integer",
"default_value": "",
"description": "The number to add to the starting value.",
"display_name": "Number to Add",
"field_type": "number",
"help_text": "Enter a number to add to the starting value",
"max_value": "1",
"min_value": "500",
"name": "number_to_add",
"original_name": "number_to_add",
"parameter_choices": "",
"required": True,
"sort_order": 1
},
{
"custom_validation": "",
"data_type": "string",
"default_value": "",
"description": "Random string to display.",
"display_name": "Random String",
"field_type": "text",
"help_text": "Enter a random string",
"max_value": "",
"min_value": "",
"name": "random_string",
"original_name": "random_string",
"parameter_choices": "",
"required": False,
"sort_order": 0
}
],
"outputs": [
{
"data_type": "integer",
"description": "The result of the add operation.",
"display_name": "Result",
"name": "result",
"original_name": "result",
"sort_order": 0
}
],
'deleted_parameters': '',
'deleted_outputs': '',
"private": True,
"version": "0.0.1"
}
return alg
def get_chain_builder_block_list():
block_list = [
u'<category name="Get Map Tiles In ROI" colour="222">\n '
'<block type="getmaptiles_roi"></block>\n '
'<block type="getmaptiles_roi__image_filenames"></block>\n</category>',
u'<category name="Output Image To Web" colour="222">\n '
'<block type="output_image_to_client"></block>\n '
'<block type="output_image_to_client__image_url"></block>\n '
'<block type="output_image_to_client__image_extent"></block>\n'
'</category>',
u'<category name="Stitch together tiles" colour="222">\n '
'<block type="stitch_tiles"></block>\n '
'<block type="stitch_tiles__image_path"></block>\n '
'<block type="stitch_tiles__image_bounds"></block>\n</category>'
]
return block_list
def get_chain_builder_additional_block():
original_blocks = get_chain_builder_block_list()
new_block = [
u'<category name="Add two numbers" colour="222">\n '
'<block type="beamio/add_numbers"></block>\n '
'<block type="beamio/add_numbers__result"></block>\n</category>'
]
return new_block + original_blocks
def get_chain_builder_block_scripts():
block_scripts = [
u'Blockly.Blocks["output_image_to_client"] = {init: function() '
'{this.appendDummyInput().setAlign(Blockly.ALIGN_CENTRE)'
'.appendField(new Blockly.FieldLabel("ALGORITHM", "font-weight-bold"'
'));this.appendDummyInput().appendField("Output Image To Web");'
'this.appendDummyInput().setAlign(Blockly.ALIGN_RIGHT)'
'.appendField(new Blockly.FieldLabel("Required Input Fields:", '
'"font-weight-bold"));this.appendValueInput("image_path")'
'.setCheck("string").setAlign(Blockly.ALIGN_RIGHT)'
'.appendField("image_path");this.appendValueInput("image_bounds")'
'.setCheck("string").setAlign(Blockly.ALIGN_RIGHT)'
'.appendField("image_bounds");this.setInputsInline(false);'
'this.setPreviousStatement(true, null);this.setNextStatement('
'true, null);this.setColour(222);this.setTooltip("Output Image '
'To Web");this.data = "algorithm";} };\nBlockly'
'.Blocks["output_image_to_client__image_url"] = {init: function() '
'{this.appendDummyInput().setAlign(Blockly.ALIGN_CENTRE)'
'.appendField(new Blockly.FieldLabel("OUTPUT FIELD", '
'"font-weight-bold"));this.appendDummyInput().appendField("Output '
'Image To Web").appendField(new Blockly.FieldDropdown(['
'["first","first"],["second","second"],["third","third"],'
'["fourth","fourth"],["fifth","fifth"],["sixth","sixth"],]), '
'"occurrence");this.appendDummyInput().appendField("image_url");'
'this.setOutput(true, "string");this.setColour(222);this.data = '
'"input";} };\nBlockly.Blocks["output_image_to_client__image_extent"]'
' = {init: function() {this.appendDummyInput().setAlign(Blockly'
'.ALIGN_CENTRE).appendField(new Blockly.FieldLabel("OUTPUT FIELD", '
'"font-weight-bold"));this.appendDummyInput().appendField("Output '
'Image To Web").appendField(new Blockly.FieldDropdown([["first",'
'"first"],["second","second"],["third","third"],["fourth","fourth"],'
'["fifth","fifth"],["sixth","sixth"],]), "occurrence");this'
'.appendDummyInput().appendField("image_extent");this.setOutput(true, '
'"string");this.setColour(222);this.data = "input";} };\n',
u'Blockly'
'.Blocks["getmaptiles_roi"] = {init: function() {this'
'.appendDummyInput().setAlign(Blockly.ALIGN_CENTRE).appendField(new '
'Blockly.FieldLabel("ALGORITHM", "font-weight-bold"));this'
'.appendDummyInput().appendField("Get Map Tiles In ROI");this'
'.appendDummyInput().setAlign(Blockly.ALIGN_RIGHT).appendField(new '
'Blockly.FieldLabel("Required Input Fields:", "font-weight-bold"));'
'this.appendValueInput("roi").setCheck("string").setAlign(Blockly'
'.ALIGN_RIGHT).appendField("roi");this.appendValueInput("zoom")'
'.setCheck("integer").setAlign(Blockly.ALIGN_RIGHT)'
'.appendField("zoom");this.setInputsInline(false);this'
'.setPreviousStatement(true, null);this.setNextStatement(true, null);'
'this.setColour(222);this.setTooltip("Get Map Tiles In ROI");'
'this.data = "algorithm";} };\nBlockly.Blocks['
'"getmaptiles_roi__image_filenames"] = {init: function() {this'
'.appendDummyInput().setAlign(Blockly.ALIGN_CENTRE).appendField(new '
'Blockly.FieldLabel("OUTPUT FIELD", "font-weight-bold"));this'
'.appendDummyInput().appendField("Get Map Tiles In ROI").appendField('
'new Blockly.FieldDropdown([["first","first"],["second","second"],'
'["third","third"],["fourth","fourth"],["fifth","fifth"],["sixth",'
'"sixth"],]), "occurrence");this.appendDummyInput().appendField('
'"image_filenames");this.setOutput(true, "string");this.setColour(222)'
';this.data = "input";} };\n',
u'Blockly.Blocks["stitch_tiles"] = {'
'init: function() {this.appendDummyInput().setAlign(Blockly'
'.ALIGN_CENTRE).appendField(new Blockly.FieldLabel("ALGORITHM", '
'"font-weight-bold"));this.appendDummyInput().appendField("Stitch '
'together tiles");this.appendDummyInput().setAlign(Blockly'
'.ALIGN_RIGHT).appendField(new Blockly.FieldLabel("Required Input '
'Fields:", "font-weight-bold"));this.appendValueInput('
'"image_filenames").setCheck("string").setAlign(Blockly.ALIGN_RIGHT)'
'.appendField("image_filenames");this.setInputsInline(false);this'
'.setPreviousStatement(true, null);this.setNextStatement(true, null);'
'this.setColour(222);this.setTooltip("Stitch together tiles");'
'this.data = "algorithm";} };\nBlockly.Blocks['
'"stitch_tiles__image_path"] = {init: function() {this'
'.appendDummyInput().setAlign(Blockly.ALIGN_CENTRE).appendField(new '
'Blockly.FieldLabel("OUTPUT FIELD", "font-weight-bold"));this'
'.appendDummyInput().appendField("Stitch together tiles").appendField('
'new Blockly.FieldDropdown([["first","first"],["second","second"],'
'["third","third"],["fourth","fourth"],["fifth","fifth"],'
'["sixth","sixth"],]), "occurrence");this.appendDummyInput()'
'.appendField("image_path");this.setOutput(true, "string");this'
'.setColour(222);this.data = "input";} };\nBlockly.Blocks['
'"stitch_tiles__image_bounds"] = {init: function() {this'
'.appendDummyInput().setAlign(Blockly.ALIGN_CENTRE).appendField(new '
'Blockly.FieldLabel("OUTPUT FIELD", "font-weight-bold"));this'
'.appendDummyInput().appendField("Stitch together tiles")'
'.appendField(new Blockly.FieldDropdown([["first","first"],'
'["second","second"],["third","third"],["fourth","fourth"],["fifth",'
'"fifth"],["sixth","sixth"],]), "occurrence");this.appendDummyInput()'
'.appendField("image_bounds");this.setOutput(true, "string");this'
'.setColour(222);this.data = "input";} };\n'
]
return block_scripts
def get_chain_builder_additional_script():
original_scripts = get_chain_builder_block_scripts()
new_script = [
u'Blockly.Blocks["beamio/add_numbers"] = {init: function() {this'
'.appendDummyInput().setAlign(Blockly.ALIGN_CENTRE).appendField'
'(new Blockly.FieldLabel("ALGORITHM", "font-weight-bold"));this'
'.appendDummyInput().appendField("Add two numbers");this'
'.appendDummyInput().setAlign(Blockly.ALIGN_RIGHT).appendField'
'(new Blockly.FieldLabel("Required Input Fields:", '
'"font-weight-bold"));this.appendValueInput("starting_value")'
'.setCheck("integer").setAlign(Blockly.ALIGN_RIGHT).appendField'
'("starting_value");this.appendValueInput("number_to_add")'
'.setCheck("integer").setAlign(Blockly.ALIGN_RIGHT).appendField'
'("number_to_add");this.appendDummyInput().setAlign(Blockly'
'.ALIGN_RIGHT).appendField(new Blockly.FieldLabel("Optional '
'Input Fields:", "font-weight-bold"));this.appendValueInput'
'("random_string").setCheck("string").setAlign(Blockly.ALIGN_RIGHT)'
'.appendField("random_string");this.setInputsInline(false);this'
'.setPreviousStatement(true, null);this.setNextStatement(true, null);'
'this.setColour(222);this.setTooltip("Add two numbers");'
'this.data = "algorithm";} };\nBlockly'
'.Blocks["beamio/add_numbers__result"] = {init: function() {this'
'.appendDummyInput().setAlign(Blockly.ALIGN_CENTRE).appendField'
'(new Blockly.FieldLabel("OUTPUT FIELD", "font-weight-bold"));'
'this.appendDummyInput().appendField("Add two numbers").appendField'
'(new Blockly.FieldDropdown([["first","first"],["second","second"],'
'["third","third"],["fourth","fourth"],["fifth","fifth"],'
'["sixth","sixth"],]), "occurrence");this.appendDummyInput()'
'.appendField("result");this.setOutput(true, "integer");this'
'.setColour(222);this.data = "input";} };\n'
]
return original_scripts[:3] + new_script + original_scripts[3:]
def get_chain_builder_chain_blocks():
chain_blocks = (
'<xml xmlns="http://www.w3.org/1999/xhtml" '
'id="workspaceBlocks" style="display:none">\n'
'<variables></variables>\n'
'<block type="getmaptiles_roi" id="testid" '
'x="10" y="10">\n'
'<data>algorithm</data>\n'
'<value name="roi">\n'
'<block type="user_input" id="testid">\n'
'<data>input</data>\n'
'</block>\n'
'</value>\n'
'<value name="zoom">\n'
'<block type="user_input" id="testid">\n'
'<data>input</data>\n'
'</block>\n'
'</value>\n'
'<next>\n'
'<block type="stitch_tiles" id="testid">\n'
'<data>algorithm</data>\n'
'<value name="image_filenames">\n'
'<block type="getmaptiles_roi__image_filenames" id="testid">\n'
'<field name="occurrence">first</field><data>input</data>\n'
'</block>\n'
'</value>\n'
'<next>\n'
'<block type="output_image_to_client" id="testid">\n'
'<data>algorithm</data>\n'
'<value name="image_path">\n'
'<block type="stitch_tiles__image_path" id="testid">\n'
'<field name="occurrence">first</field><data>input</data>\n'
'</block>\n'
'</value>\n'
'<value name="image_bounds">\n'
'<block type="stitch_tiles__image_bounds" id="testid">\n'
'<field name="occurrence">first</field><data>input</data>\n'
'</block>\n'
'</value>\n'
'</block>\n'
'</next>\n'
'</block>\n'
'</next>\n'
'</block>\n'
'</xml>\n'
)
return chain_blocks
def get_chain_builder_chain_blocks_math():
chain_blocks = (
'<xml xmlns="http://www.w3.org/1999/xhtml" '
'id="workspaceBlocks" style="display:none">\n'
'<variables></variables>\n'
'<block type="add_numbers" id="testid" x="10" y="10">\n'
'<data>algorithm</data>\n'
'<next>\n'
'<block type="subtract_numbers" id="testid">\n'
'<data>algorithm</data>\n'
'<next>\n'
'<block type="multiply_numbers" id="testid">\n'
'<data>algorithm</data>\n'
'<next>\n'
'<block type="divide_numbers" id="testid">\n'
'<data>algorithm</data>\n'
'<next>\n'
'<block type="output_text" id="testid">\n'
'<data>algorithm</data>\n'
'</block>\n'
'</next>\n'
'</block>\n'
'</next>\n'
'</block>\n'
'</next>\n'
'</block>\n'
'</next>\n'
'</block>\n'
'</xml>\n'
)
return chain_blocks