-
Notifications
You must be signed in to change notification settings - Fork 2
/
Boundaries.cs
226 lines (185 loc) · 7.48 KB
/
Boundaries.cs
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
using System;
using System.IO;
using Newtonsoft.Json;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
namespace City_of_Calgary_2018
{
class Point {
public double x { get; set; }
public double y { get; set; }
public Point(double x, double y) {
this.x = x;
this.y = y;
}
}
class DataLayer
{
public string communityName = "None";
public string communitySector = "None";
public string communityType = "None";
bool foundCommunity = false;
public void getData(String fileName, double lonInput, double latInput)
{
using (StreamReader r = new StreamReader(fileName))
{
// Fetch JSON data
string json = r.ReadToEnd();
dynamic data = JsonConvert.DeserializeObject(json);
// Scan the communities
foreach (dynamic community in data.data)
{
dynamic currentCommunity = community;
dynamic type = community[9];
if (type == "Residential" ||
type == "Industrial" ||
type == "Major Park") {
// communityName = community[12];
// communitySector = community[13];
// Clean-up polygon data
string polygon = community[8];
polygon = polygon.Substring(0, polygon.Length - 2); // Get rid of the last 2 chars
polygon = polygon.Substring(10); // Get rid of the first 10 chars
// Split polygon string into lon and lat pairs
string[] polygonCoord = polygon.Split(',');
Point[] polygonShape = new Point[polygonCoord.Length];
int count = 0;
foreach (string coordList in polygonCoord) {
// Console.WriteLine(coordList);
// coordList = " -114.070768950013 50.853339905893"
string coordListNew = "";
if (coordList[0].Equals(' ')) {
// Console.WriteLine("here");
coordListNew = coordList.Substring(1);
} else {
coordListNew = coordList;
}
// Console.WriteLine(coordListNew);
string[] coord = coordListNew.Split(' ');
// Console.WriteLine(coord[0]);
// Console.WriteLine(coord[1]);
double lon = Double.Parse(coord[0]); // -114.070768950013
double lat = Double.Parse(coord[1]); // 50.853339905893
polygonShape[count++] = new Point(lon, lat);
}
Point statueLocation = new Point(lonInput, latInput);
Intersect intersect = new Intersect();
bool result = intersect.isInside(polygonShape, polygonCoord.Length, statueLocation);
if (result) {
foundCommunity = true;
communityName = currentCommunity[12];
communityType = currentCommunity[13];
communitySector = currentCommunity[9];
return;
}
}
}
}
}
}
class Intersect
{
public int infinity = int.MaxValue;
// Given three colinear points p, q, r, the function checks if
// point q lies on line segment 'pr'
bool onSegment(Point p, Point q, Point r)
{
if (q.x <= Math.Max(p.x, r.x) && q.x >= Math.Min(p.x, r.x) &&
q.y <= Math.Max(p.y, r.y) && q.y >= Math.Min(p.y, r.y))
{
return true;
}
return false;
}
// To find orientation of ordered triplet (p, q, r).
// The function returns following values
// 0 --> p, q and r are colinear
// 1 --> Clockwise
// 2 --> Counterclockwise
int orientation(Point p, Point q, Point r)
{
double val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
// colinear
if (val == 0)
{
return 0;
}
// clock or counterclock wise
if (val > 0)
{
return 1;
}
else
{
return 2;
}
}
// The function that returns true if line segment 'p1q1'
// and 'p2q2' intersect.
bool doIntersect(Point p1, Point q1, Point p2, Point q2)
{
// Find the four orientations needed for general and
// special cases
int o1 = orientation(p1, q1, p2);
int o2 = orientation(p1, q1, q2);
int o3 = orientation(p2, q2, p1);
int o4 = orientation(p2, q2, q1);
// General case
if (o1 != o2 && o3 != o4)
{
return true;
}
// Special Cases
// p1, q1 and p2 are colinear and p2 lies on segment p1q1
if (o1 == 0 && onSegment(p1, p2, q1))
{
return true;
}
// p1, q1 and p2 are colinear and q2 lies on segment p1q1
if (o2 == 0 && onSegment(p1, q2, q1))
{
return true;
}
// p2, q2 and p1 are colinear and p1 lies on segment p2q2
if (o3 == 0 && onSegment(p2, p1, q2))
{
return true;
}
// p2, q2 and q1 are colinear and q1 lies on segment p2q2
if (o4 == 0 && onSegment(p2, q1, q2))
{
return true;
}
return false; // Doesn't fall in any of the above cases
}
// Returns true if the point p lies inside the polygon[] with n vertices
public bool isInside(Point[] polygon, int n, Point p)
{
// There must be at least 3 vertices in polygon[]
if (n < 3) return false;
// Create a point for line segment from p to infinite
Point extreme = new Point(infinity, p.y);
// Count intersections of the above line with sides of polygon
int count = 0, i = 0;
do
{
int next = (i + 1) % n;
// Check if the line segment from 'p' to 'extreme' intersects
// with the line segment from 'polygon[i]' to 'polygon[next]'
if (doIntersect(polygon[i], polygon[next], p, extreme))
{
// If the point 'p' is colinear with line segment 'i-next',
// then check if it lies on segment. If it lies, return true,
// otherwise false
if (orientation(polygon[i], p, polygon[next]) == 0)
return onSegment(polygon[i], p, polygon[next]);
count++;
}
i = next;
} while (i != 0);
// Return true if count is odd, false otherwise
return (count % 2 == 1);
}
}
}