forked from OliverHennhoefer/arcpy-supercluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupercluster.py
86 lines (70 loc) · 2.62 KB
/
supercluster.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
# -*- coding: cp1252 -*-
'''
Geomatics Masters Course: Web Feature Service
Assignment II
Authors:
Implementation a Point Clustering Algorithm
'''
import arcpy
import os
from math import *
import random
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def shapefileToList(inputFeature):
pointsList = []
with arcpy.da.SearchCursor(inputFeature, "SHAPE@XY") as Reader:
for feature in Reader:
x = feature[0][0]
y = feature[0][1]
point = Point(x, y)
pointsList.append(point)
return pointsList
def deletePointsInCircle(centerPoint, pointsList, radius):
deletionList = []
for point in pointsList:
diff_x = point.x - centerPoint.x
diff_y = point.y - centerPoint.y
distance = 100000* (sqrt((diff_x)**2 + (diff_y)**2))
arcpy.AddMessage(distance)
#distance = sqrt((point.x-centerPoint.x)**2+(point.y-centerPoint.y)**2)
if distance < radius:
deletionList.append(pointsList.index(point))
for index in sorted(deletionList, reverse=True):
del pointsList[index]
return pointsList
###########################################################
###########################################################
inputFeaturePoints = arcpy.GetParameter(0)
outputFeaturePoints = arcpy.GetParameterAsText(2)
radius = arcpy.GetParameterAsText(1)
arcpy.env.overWriteOutput = True
sR = arcpy.Describe(inputFeaturePoints).spatialReference
arcpy.env.outputCoordinateSystem = sR
arcpy.env.workspace = os.path.dirname(outputFeaturePoints)
unclusteredPointsList = shapefileToList(inputFeaturePoints)
radius = int(radius)
clusterPointsList = []
createClusters = True
while createClusters == True:
randomCenterPoint = random.choice(unclusteredPointsList)
clusterPointsList.append(randomCenterPoint)
del unclusteredPointsList[unclusteredPointsList.index(randomCenterPoint)]
unclusteredPointsList = deletePointsInCircle(randomCenterPoint, unclusteredPointsList, radius)
if len(unclusteredPointsList) == 0:
createClusters = False
pointArray = arcpy.Array()
for element in clusterPointsList:
arcPoint = arcpy.Point(element.x, element.y)
pointArray.add(arcPoint)
arcpy.CreateFeatureclass_management(os.path.dirname(outputFeaturePoints),\
os.path.basename(outputFeaturePoints),"POINT",\
template=inputFeaturePoints,\
spatial_reference=sR)
cursor = arcpy.da.InsertCursor(outputFeaturePoints, ['SHAPE@'])
for points in pointArray:
cursor.insertRow([points])
del cursor
arcpy.SetParameter(1, outputFeaturePoints)