-
Notifications
You must be signed in to change notification settings - Fork 0
/
HexStatistics.py
336 lines (319 loc) · 16.5 KB
/
HexStatistics.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
#######################################################################
# Filename: HexStatistics.py #
# Author: Karolina Mamczarz #
# Institution: AGH University of Science and Technology in Cracow, #
# Poland #
# Faculty: Mining Surveying and Environmental Engineering #
# Department: Integrated Geodesy and Cartography #
# Last update: 2017-09-14 #
# Version: 1.0.0 #
# Description: Statistics used to test quality of line simplification #
# Class: HexStatistics #
# Methods: __init__, comparison_grid_1_sqkm, length, read_geom, #
# length_difference, point_count, std_point_per_area, #
# density_per_length, surface_in_between, #
# surface_in_between_per_length_area, hausdorff_distance #
# Result: Report in a text file #
# Required: ArcGIS for Desktop Advanced #
#######################################################################
from sys import exit
import arcpy
from math import sqrt
class HexStatistics(object):
def __init__(self, original, simplified, report_file_path):
self.original = original
self.simplified = simplified
self.report_file_path = report_file_path
self.report = open(self.report_file_path + '.txt', 'w')
self.grid = self.comparison_grid_1_sqkm()
self.calculate_statistics()
def comparison_grid_1_sqkm(self):
temp_merge = "in_memory\\merge"
arcpy.Merge_management([self.original, self.simplified], temp_merge)
temp_grid = "in_memory\\grid"
arcpy.GridIndexFeatures_cartography(
temp_grid, temp_merge, "INTERSECTFEATURE", "NO_USEPAGEUNIT", "#",
"1 Kilometers", "1 Kilometers")
arcpy.Delete_management(temp_merge)
arcpy.AddMessage("Comparison grid (1 sq km) prepared.")
return temp_grid
@staticmethod
def length(line):
with arcpy.da.SearchCursor(line, ["SHAPE@"]) as cursor:
for i_line in cursor:
length = i_line[0].getLength("PLANAR", "METERS")
return length
@staticmethod
def read_geom(shape):
with arcpy.da.SearchCursor(shape, ["SHAPE@"]) as cursor:
shape_coords = []
for row in cursor:
for part in row[0]:
for pnt in part:
shape_coords.append([pnt.X, pnt.Y])
return shape_coords
def length_difference(self):
arcpy.AddMessage("Started length difference statistics...")
self.report.write("\n---Difference in length---\n")
percentage = (self.length(self.simplified) /
self.length(self.original)) * 100
self.report.write("Percentage change in length of lines: "
+ "{:0.2f}%\n".format(percentage))
arcpy.AddMessage("Length difference statistics - done.")
return
def point_count(self):
arcpy.AddMessage("Started point count statistics...")
self.report.write("\n---Point count---\n")
original_pntnum = float(len(self.read_geom(self.original)))
self.report.write("Number of points before simplification: {:0.0f}\n"
.format(original_pntnum))
simplified_pntnum = float(len(self.read_geom(self.simplified)))
self.report.write("Number of points after simplification: {:0.0f}\n"
.format(simplified_pntnum))
percentage = (simplified_pntnum / original_pntnum) * 100
self.report.write("Percentage change in number of coordinates: "
+ "{:0.2f}%\n".format(percentage))
arcpy.AddMessage("Point count statistics - done.")
return
def std_point_per_area(self):
arcpy.AddMessage("Started point count standard deviation "
+ "statistics...")
self.report.write("\n---Percentage change of standard deviation of "
+ "point count per area---\n")
with arcpy.da.SearchCursor(self.grid, ["PageNumber"]) as cursor:
count_cells = 0
for row in cursor:
count_cells += 1
self.report.write("Number of cells in grid: {0}\n"
.format(count_cells))
temp_points_original = "in_memory\\points_original"
temp_points_simplified = "in_memory\\points_simplified"
arcpy.FeatureVerticesToPoints_management(self.original,
temp_points_original)
arcpy.FeatureVerticesToPoints_management(self.simplified,
temp_points_simplified)
temp_intersect_original = "in_memory\\intersect_original"
temp_intersect_simplified = "in_memory\\intersect_simplified"
arcpy.Intersect_analysis([temp_points_original, self.grid],
temp_intersect_original, "ALL", "#", "POINT")
arcpy.Intersect_analysis([temp_points_simplified, self.grid],
temp_intersect_simplified, "ALL", "#",
"POINT")
arcpy.Delete_management(temp_points_original)
arcpy.Delete_management(temp_points_simplified)
temp_stats_original = "in_memory\\stats_original"
temp_stats_simplified = "in_memory\\stats_simplified"
arcpy.Statistics_analysis(temp_intersect_original, temp_stats_original,
"FID SUM", "PageNumber")
arcpy.Statistics_analysis(temp_intersect_simplified,
temp_stats_simplified,
"FID SUM", "PageNumber")
arcpy.Delete_management(temp_intersect_original)
arcpy.Delete_management(temp_intersect_simplified)
with arcpy.da.SearchCursor(temp_stats_original,
["FREQUENCY"]) as cursor:
points_original_in_cells = []
for row_fr1 in cursor:
points_original_in_cells.append(row_fr1[0])
mean_original = sum(points_original_in_cells) \
/ float(len(points_original_in_cells))
points_original_in_cells[:] = [(x - mean_original)**2
for x in points_original_in_cells]
std_original = sqrt(float(sum(points_original_in_cells)) \
/ float(len(points_original_in_cells) - 1))
self.report.write("Number of cells in grid intersected by original "
+ "points: {0}\n"
.format(len(points_original_in_cells)))
self.report.write("Standard deviation of original points: "
+ "{:0.2f}\n".format(std_original))
with arcpy.da.SearchCursor(temp_stats_simplified,
["FREQUENCY"]) as cursor:
points_simplified_in_cells = []
for row_fr2 in cursor:
points_simplified_in_cells.append(row_fr2[0])
mean_simplified = sum(points_simplified_in_cells) \
/ float(len(points_simplified_in_cells))
points_simplified_in_cells[:] = [(x - mean_simplified)**2
for x in points_simplified_in_cells]
std_simplified = sqrt(float(sum(points_simplified_in_cells)) \
/ float(len(points_simplified_in_cells) - 1))
self.report.write("Number of cells in grid intersected by simplified "
+ "points: "
+ "{0}\n".format(len(points_simplified_in_cells)))
self.report.write("Standard deviation of simplified points: "
+ "{:0.2f}\n".format(std_simplified))
percentage = (std_simplified / std_original)*100
self.report.write("Percentage change in the standard deviation of the "
+ "number of coordinates: "
+ "{:0.2f}%\n".format(percentage))
arcpy.Delete_management(temp_stats_original)
arcpy.Delete_management(temp_stats_simplified)
arcpy.AddMessage("Point count standard deviation statistics"
+ " - done.")
return
def density_per_length(self):
arcpy.AddMessage("Started point density statistics...")
self.report.write("\n---Average point density per length---\n")
original_pntnum = len(self.read_geom(self.original))
original_length = self.length(self.original)
density_per_length_original = original_pntnum / original_length
self.report.write("Density of original points per meter:"
+ " {:0.6f} points/m\n".format(
density_per_length_original))
simplified_pntnum = len(self.read_geom(self.simplified))
simplified_length = self.length(self.simplified)
density_per_length_simplified = simplified_pntnum / simplified_length
self.report.write("Density of simplified points per meter: "
+ "{:0.6f} points/m\n".format(
density_per_length_simplified))
average_density = density_per_length_original \
- density_per_length_simplified
self.report.write("Difference in average density: "
+ "{:0.10f} points/m\n".format(average_density))
arcpy.AddMessage("Point density statistics - done.")
return
def surface_in_between(self):
arcpy.AddMessage("Started differential surface statistics... ")
self.report.write("\n---Differential surface---\n")
temp_merge = "in_memory\\merge"
arcpy.Merge_management([self.original, self.simplified], temp_merge)
temp_poly = "in_memory\\poly"
arcpy.FeatureToPolygon_management(temp_merge, temp_poly)
arcpy.Delete_management(temp_merge)
arcpy.AddGeometryAttributes_management(temp_poly, "AREA", "#",
"SQUARE_METERS")
with arcpy.da.SearchCursor(temp_poly, ["POLY_AREA"]) as cursor:
summarize = 0
parts = 0
for row in cursor:
summarize += row[0]
parts += 1
mean = summarize / parts
arcpy.Delete_management(temp_poly)
self.report.write("Sum area: {:0.0f} sq m\n".format(summarize))
self.report.write("Mean area: {:0.0f} sq m\n".format(mean))
arcpy.AddMessage("Differential surface statistics - done.")
return
def surface_in_between_per_length_area(self):
arcpy.AddMessage("Started differential surface per area"
+ " statistics... ")
self.report.write("\n---Differential surface per length and area---\n")
temp_merge = "in_memory\\merge"
arcpy.Merge_management([self.original, self.simplified], temp_merge)
temp_poly = "in_memory\\poly"
arcpy.FeatureToPolygon_management(temp_merge, temp_poly)
arcpy.Delete_management(temp_merge)
temp_intersect_poly = "in_memory\\intersect_poly"
arcpy.AddGeometryAttributes_management(temp_poly, "AREA", "#",
"SQUARE_METERS")
with arcpy.da.SearchCursor(temp_poly, ["POLY_AREA"]) as cursor:
summarize = 0
for row in cursor:
summarize += row[0]
arcpy.Intersect_analysis([temp_poly, self.grid],
temp_intersect_poly, "ALL", "#", "INPUT")
arcpy.AddGeometryAttributes_management(temp_intersect_poly, "AREA",
"#", "SQUARE_METERS")
temp_poly_points = "in_memory\\poly_points"
arcpy.FeatureToPoint_management(temp_intersect_poly, temp_poly_points,
"INSIDE")
with arcpy.da.SearchCursor(temp_poly_points, ["SHAPE@", "POLY_AREA"]) \
as cursor:
poly_points_array = []
poly_point_area_array = []
for point in cursor:
poly_points_array.append(point[0])
poly_point_area_array.append(point[1])
with arcpy.da.SearchCursor(self.original, ["SHAPE@"]) as cursor:
side_array = []
for polyline in cursor:
for i_p in poly_points_array:
line = polyline[0]
check_point = line.queryPointAndDistance(i_p)
side_array.append(check_point[3])
sum_right = 0
sum_left = 0
iter = 0
for elem in side_array:
if elem is True:
sum_right += poly_point_area_array[iter]
iter += 1
elif elem is False:
sum_left += poly_point_area_array[iter]
iter += 1
arcpy.Delete_management(temp_poly)
arcpy.Delete_management(temp_intersect_poly)
arcpy.Delete_management(temp_poly_points)
self.report.write("Sum of positive areal displacement: {:0.0f} sq m\n"
.format(sum_right))
self.report.write("Sum of negative areal displacement: {:0.0f} sq m\n"
.format(sum_left))
average_differential = summarize / self.length(self.original)
self.report.write("The total areal difference per length: "
+ "{:0.0f} sq m/m\n".format(average_differential))
differential_sqkm = float(len(set(poly_point_area_array)))
difference_per_sqkm = summarize / differential_sqkm
self.report.write("Average areal difference per 1 sq km: "
+ "{:0.0f} sq m/sq km\n".format(difference_per_sqkm))
arcpy.AddMessage("Differential surface per area statistics - done.")
return
def hausdorff_distance(self):
arcpy.AddMessage("Started Hausdorff distance statistics...")
self.report.write("\n---Hausdorff distance---\n")
original_points = self.read_geom(self.original)
simplified_points = self.read_geom(self.simplified)
orig_size = len(original_points)
simpl_size = len(simplified_points)
from_orig_to_simpl = 0.0
i = 0
while i < orig_size:
cmin = 1000000000000000.0
j = 0
while j < simpl_size:
d = sqrt((simplified_points[j][0] - original_points[i][0])**2
+ (simplified_points[j][1] - original_points[i][1])**2)
if d <= cmin:
cmin = d
j += 1
if cmin >= from_orig_to_simpl:
from_orig_to_simpl = cmin
i += 1
from_simpl_to_orig = 0.0
i = 0
while i < simpl_size:
cmin = 1000000000000000.0
j = 0
while j < orig_size:
d = sqrt((original_points[j][0] - simplified_points[i][0])**2
+ (original_points[j][1] - simplified_points[i][1])**2)
if d <= cmin:
cmin = d
j += 1
if cmin >= from_simpl_to_orig:
from_simpl_to_orig = cmin
i += 1
haus_dist = max([from_orig_to_simpl, from_simpl_to_orig])
self.report.write('Hausdorff distance: {:0.3f} m\n'.format(haus_dist))
self.report.write('Hausdorff distance from original to simplified: '
+ '{:0.3f} m\n'.format(from_orig_to_simpl))
self.report.write('Hausdorff distance from simplified to original: '
+ '{:0.3f} m\n'.format(from_simpl_to_orig))
arcpy.AddMessage("Hausdorff distance statistics - done.")
return
def calculate_statistics(self):
self.report.write("---------Simplification statistics---------\n")
arcpy.AddMessage("Started calculating statistics...")
self.length_difference()
self.point_count()
self.std_point_per_area()
self.density_per_length()
self.surface_in_between()
self.surface_in_between_per_length_area()
self.hausdorff_distance()
arcpy.AddMessage("Finished calculating statistics...")
self.report.close()
if __name__ == '__main__':
calculating_statistics = HexStatistics(arcpy.GetParameterAsText(0),
arcpy.GetParameterAsText(1),
arcpy.GetParameterAsText(2))
exit(calculating_statistics)