Repository navigation
Expand file tree
/
Copy pathFixFilteringEngine.py
More file actions
127 lines (96 loc) · 3.79 KB
/
Copy pathFixFilteringEngine.py
File metadata and controls
127 lines (96 loc) · 3.79 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
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
import xml.etree.ElementTree as ET
import pandas as pd
from matplotlib import pyplot as plt
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon
from IMCObjects import GPoint
from Projector import EquirectangularProjector
class CsvTool:
@staticmethod
def DumpToCsv(data, fileName):
df = pd.DataFrame(data)
df.to_csv(fileName)
class GoogleEarthTool:
@staticmethod
def ParseToCsvPointsFromKML(kmlFileName='Data\\Border\\Polygon.kml', csvFileName='Data\\Border\\DumpPolygon.csv'):
PolygonEndPoints = GoogleEarthTool.KMLPointsParser(kmlFileName)
CsvTool.DumpToCsv(GoogleEarthTool.GenerateDataToDump(PolygonEndPoints), csvFileName)
return PolygonEndPoints
''' Return List of type GPoint'''
@staticmethod
def KMLPointsParser(kmlFileName):
PointList = list()
tree = ET.parse(kmlFileName)
root = tree.getroot()
points = root.findall(".//Placemark/Point/coordinates")
i = 0
for point in points:
i += 1
temp = str(point.text).split(',')
PointList.append(GPoint(float(temp[1]), float(temp[0])))
print("{} {}".format(i, temp))
return PointList
@staticmethod
def GenerateDataToDump(endPoints):
dataToDump = []
for p in endPoints:
assert isinstance(p, GPoint)
dataToDump.append({"lat": p.getLat(), "long": p.getLong()})
return dataToDump
class OuterWallsFiltering:
def __init__(self, gPointsList, projector=None):
self.GEndPointsList = gPointsList
self.Projector = None
self.Polygon = None
assert isinstance(gPointsList, list)
assert len(gPointsList) > 0
if projector is None:
self.InitProjector()
else:
assert isinstance(projector, EquirectangularProjector)
self.Projector = projector
self.GeneratePolygon()
def InitProjector(self):
print("Generate projector for OuterWallsFiltering")
minLat = min(fix.getLat() for fix in self.GEndPointsList)
maxLat = max(fix.getLat() for fix in self.GEndPointsList)
minLong = min(fix.getLong() for fix in self.GEndPointsList)
maxLong = max(fix.getLong() for fix in self.GEndPointsList)
self.Projector = EquirectangularProjector(None, False, (minLat, minLong), (maxLat, maxLong))
def GeneratePolygon(self):
""" Close the polygon"""
self.GEndPointsList.append(self.GEndPointsList[0])
Coords = []
for p in self.GEndPointsList:
assert isinstance(p, GPoint)
x, y = self.Projector.latLongToXY(p.getLat(), p.getLong())
Coords.append((x, y))
self.Polygon = Polygon(Coords)
def IsInPolygon(self, point):
assert isinstance(point, Point)
return self.Polygon.contains(point)
def Plot(self, axs):
axs.plot(*self.Polygon.exterior.xy, color='#6699cc', alpha=0.7, linewidth=3, solid_capstyle='round',
zorder=2)
class FilteringFactoryGenerator:
@staticmethod
def GenerateFilter(projector):
assert projector is not None
assert isinstance(projector, EquirectangularProjector)
EndPoints = GoogleEarthTool.ParseToCsvPointsFromKML()
return OuterWallsFiltering(EndPoints, projector)
@staticmethod
def PureGenerateFilter():
EndPoints = GoogleEarthTool.ParseToCsvPointsFromKML()
return OuterWallsFiltering(EndPoints)
def main():
filterEngine = FilteringFactoryGenerator.PureGenerateFilter()
fig, axs = plt.subplots(1, 1)
point = Point(0.5, 0.5)
print(filterEngine.IsInPolygon(point))
point = Point(40, 25)
print(filterEngine.IsInPolygon(point))
filterEngine.Plot(axs)
plt.show()
if __name__ == "__main__":
main()