-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaw_voronoi.py
61 lines (56 loc) · 2.41 KB
/
aw_voronoi.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
import sys, os, arcpy, math
sys.path.append('.')
import common
TMP_CIRCPTS = 'tmp_circ'
TMP_ALLPOLY = 'tmp_voroall'
with common.runtool(7) as parameters:
common.progress('parsing attributes')
## GET AND PREPARE THE ATTRIBUTES
# obtained from the tool input
points, ptsIDFld, weightFld, normStr, transferFldsStr, tolerStr, outPath = parameters
location, outName = os.path.split(outPath)
normalization = math.sqrt(common.toFloat(normStr, 'normalization value') / math.pi)
tolerance = common.toFloat(tolerStr, 'positional tolerance value')
transferFlds = common.parseFields(transferFldsStr)
common.progress('creating weighting layer')
common.overwrite(True)
circLayer = common.createFeatureClass(os.path.join(location, TMP_CIRCPTS), crs=points)
inShpFld = arcpy.Describe(points).ShapeFieldName
circShapeFld = arcpy.Describe(circLayer).ShapeFieldName
arcpy.AddField_management(circLayer, ptsIDFld, common.outTypeOfField(points, ptsIDFld))
inCount = common.count(points)
common.progress('opening weighting layer')
inCur = arcpy.SearchCursor(points)
outCur = arcpy.InsertCursor(circLayer)
prog = common.progressor('weighting points', inCount)
pi2 = 2 * math.pi
for inRow in inCur:
# load input geometry
pt = inRow.getValue(inShpFld).getPart(0)
id = inRow.getValue(ptsIDFld)
coor = (pt.X, pt.Y)
# load radius
radius = math.sqrt(inRow.getValue(weightFld)) * normalization
print inRow.getValue(weightFld), radius, normalization
ptCount = max(int(pi2 * radius / tolerance), 3)
delta = pi2 / ptCount
angle = 0
while angle < pi2:
outRow = outCur.newRow()
outRow.setValue(circShapeFld, arcpy.Point(coor[0] + radius * math.cos(angle), coor[1] + radius * math.sin(angle)))
outRow.setValue(ptsIDFld, id)
outCur.insertRow(outRow)
angle += delta
prog.move()
del inCur, outCur, inRow, outRow
prog.end()
common.progress('building voronoi polygons')
singlePolys = common.featurePath(location, TMP_ALLPOLY)
mergedPolys = common.featurePath(location, outName)
arcpy.CreateThiessenPolygons_analysis(circLayer, singlePolys, 'ALL')
common.progress('dissolving to weighted polygons')
arcpy.Dissolve_management(singlePolys, mergedPolys, ptsIDFld)
common.progress('joining transfer fields')
arcpy.JoinField_management(mergedPolys, ptsIDFld, points, ptsIDFld, transferFlds)
common.progress('deleting temporary files')
common.delete(singlePolys)