-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_color_map.py
More file actions
63 lines (58 loc) · 2.36 KB
/
create_color_map.py
File metadata and controls
63 lines (58 loc) · 2.36 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
import os, common, colors, loaders, arcpy
BANDS = ('r', 'g', 'b')
class ColorDecomposer(loaders.FunctionUpdater):
requiredInputSlots = ['color']
requiredOutputSlots = BANDS
@staticmethod
def translate(inprow={'color' : 'ffffff'}):
try:
rgb = colors.hexToRGB(inprow['color'])
except colors.ColorCodeError, warn: # if invalid color code
common.warning(warn)
rgb = colors.BLACK_RGB # black
return dict(zip(BANDS, rgb))
with common.runtool(4) as parameters:
zones, colorFld, cellSize, output = parameters
cellSize = float(cellSize)
# calculate color band weights from hex data
common.progress('creating color fields')
bandFields = {}
for band in BANDS:
bandFields[band] = 'TMP_{}0001'.format(band.upper())
common.progress('calculating color fields')
ColorDecomposer(zones, {'color' : colorFld}, bandFields).decompose()
# zoneRows = arcpy.UpdateCursor(zones)
# for row in zoneRows:
# code = row.getValue(colorFld)
# try:
# color = colors.hexToRGB(code)
# except colors.ColorCodeError, warn: # if invalid color code
# common.warning(warn)
# color = colors.BLACK_RGB # black
# for i in range(len(BANDS)):
# row.setValue(bandFields[BANDS[i]], color[i])
# zoneRows.updateRow(row)
# del zoneRows
# convert color band weights to raster
common.progress('converting to raster')
main, ext = os.path.splitext(output)
bandRasters = {}
for band in BANDS:
bandRasters[band] = main + '_' + band + '.img'
arcpy.PolygonToRaster_conversion(zones, bandFields[band], bandRasters[band],
'MAXIMUM_COMBINED_AREA', '', cellSize)
# merge R, G, B bands into one - use IMG format to avoid holes where some of the bands is zero
common.progress('combining color bands')
common.debug(bandRasters)
compRaster = main + '_c.img'
arcpy.CompositeBands_management([bandRasters[band] for band in BANDS], compRaster)
# and copy... this was more bugproof
common.progress('creating result')
arcpy.CopyRaster_management(compRaster, output)
common.progress('defining result projection')
arcpy.DefineProjection_management(output, arcpy.Describe(zones).spatialReference)
# delete temporary mess
common.progress('deleting temporary fields')
arcpy.DeleteField_management(zones, bandFields.values())
common.progress('deleting temporary files')
common.delete(*(list(bandRasters.values()) + [compRaster]))