-
Notifications
You must be signed in to change notification settings - Fork 1
/
intersectingValidation.py
161 lines (134 loc) · 6.11 KB
/
intersectingValidation.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
import json
import os
import pandas as pd
import pyprog
from shapely.geometry import LineString, Point, Polygon, MultiPoint
def readJsonFile(path):
'''
To read Jsonfile with the given path
Parameters
----------
path : TYPE
path of the json file.
Returns
-------
dataDict : TYPE
Json file.
'''
with open(path, 'r') as data_json:
dataDict = json.load(data_json)
return dataDict
def geometryFormat(geometryDatarow):
if (geometryDatarow[0]["type"] == "LineString"):
try:
return LineString(geometryDatarow[0]["coordinates"])
except:
return "invalid"
if (geometryDatarow[0]["type"] == "Point"):
try:
return Point(geometryDatarow[0]["coordinates"])
except:
return "invalid"
if (geometryDatarow[0]["type"] == "Polygon"):
return "invalid"
def indexInvalidGeometryType(geometryData):
dataInvalidGeoJsonFormat = geometryData.apply(geometryFormat, axis=1)
ind_drop = dataInvalidGeoJsonFormat[dataInvalidGeoJsonFormat.apply(lambda row: row == 'invalid')].index
return ind_drop
def brunnelcheck(x, skipTag):
if (skipTag in x[0].keys()):
if (x[0]["brunnel"] == None):
return False
else:
return True
else:
return False
def geojsonWrite(path, invalidWays, originalGeoJsonFile, fileName):
'''
To write the given invalid ways in the GeoJson format
Parameters
----------
path : TYPE
path to write the json file.
invalidWays : TYPE
list of invalid paths with its attributes.
originalGeoJsonFile : TYPE
actutal json file given for validation.
Returns
-------
None.
'''
invalidPaths = originalGeoJsonFile.copy()
invalidPaths['features'] = invalidWays
path = os.path.join(path, fileName)
with open(path + '.geojson', 'w') as fp:
json.dump(invalidPaths, fp, indent=4)
def intersectLineStringInValidFormat(geoJSONdata, skipTag, cf, fileName):
featuresData = pd.DataFrame(geoJSONdata["features"])
geometryData = pd.DataFrame(featuresData["geometry"])
propertyData = pd.DataFrame(featuresData["properties"])
invalidWayGeoJSONFormat = []
intersectingNodeGeoJSON = []
violatingWayFeatures = []
print("number of ways in the currrent file : ", len(geoJSONdata["features"]))
print("--" * 30)
print("Checking whether there are any Ways without an intersecting node")
print("--" * 30)
brunnelValid = pd.DataFrame(propertyData.apply(brunnelcheck, args=(skipTag,), axis=1))
invalidGeometryIndex = indexInvalidGeometryType(geometryData).values.tolist()
for counter in range(len(geoJSONdata["features"])):
if counter in invalidGeometryIndex:
invalidWayGeoJSONFormat.append(geoJSONdata["features"][counter])
brunnelExist = brunnelValid[brunnelValid[0].apply(lambda row: row == True)].index
geometryDataFormat = geometryData.apply(geometryFormat, axis=1)
geometryDataFormatCopy = geometryDataFormat.copy()
prog = pyprog.ProgressBar(" ", " ", total=len(geometryDataFormat), bar_length=26, complete_symbol="=",
not_complete_symbol=" ",
wrap_bar_prefix=" [", wrap_bar_suffix="] ", progress_explain="",
progress_loc=pyprog.ProgressBar.PROGRESS_LOC_END)
prog.update()
for rowIdI, wayI in geometryDataFormat.iteritems():
prog.set_stat(rowIdI + 1)
# Update Progress Bar again
prog.update()
if (wayI == "invalid" or rowIdI in brunnelExist):
continue
for rowIdJ, wayJ in geometryDataFormatCopy.iteritems():
if (rowIdI >= rowIdJ or wayJ == "invalid" or rowIdJ in brunnelExist):
continue
if (wayI.intersects(wayJ) and wayI.touches(wayJ) != True):
intersection = wayI.intersection(wayJ)
if (type(intersection) == type(LineString())):
continue
elif type(intersection) == type(Point()):
roundedIntersection = tuple(round(dimension, 7) for dimension in intersection.coords[0])
if (roundedIntersection in wayI.coords[:]) or (roundedIntersection in wayJ.coords[:]):
continue
intersectingNodeGeoJSON.append(
{"type": "Feature", "geometry": {"type": "Point", "coordinates": roundedIntersection}})
elif (type(intersection) == type(MultiPoint())):
appendPoints = []
for point in intersection:
roundedIntersection = tuple(round(dimension, 7) for dimension in point.coords[0])
if (roundedIntersection in wayI.coords[:]) or (roundedIntersection in wayJ.coords[:]):
continue
appendPoints.append(roundedIntersection)
if (len(appendPoints) > 1):
intersectingNodeGeoJSON.append(
{"type": "Feature", "geometry": {"type": "MultiPoint", "coordinates": appendPoints}})
elif (len(appendPoints) == 0):
continue
else:
intersectingNodeGeoJSON.append(
{"type": "Feature", "geometry": {"type": "Point", "coordinates": appendPoints[0]}})
if (len(violatingWayFeatures) == 0):
violatingWayFeatures = [geoJSONdata["features"][rowIdI]]
violatingWayFeatures.append(geoJSONdata["features"][rowIdJ])
else:
violatingWayFeatures.append(geoJSONdata["features"][rowIdI])
violatingWayFeatures.append(geoJSONdata["features"][rowIdJ])
fileName = fileName.split('.')[0]
geojsonWrite(cf.writePath, violatingWayFeatures, geoJSONdata, fileName + "_Missing_Intersection")
geojsonWrite(cf.writePath, intersectingNodeGeoJSON, geoJSONdata, fileName + "_recommended_Intersections")
prog.end()
# return intersectingNodeGeoJSON, invalidWayGeoJSONFormat, violatingWayFeatures