-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeighborFinder.py
More file actions
executable file
·71 lines (63 loc) · 2.1 KB
/
NeighborFinder.py
File metadata and controls
executable file
·71 lines (63 loc) · 2.1 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
import json
import decimal
from shapely.geometry import Polygon, MultiPolygon
with open('countriesData_small.js') as f:
content = f.readlines();
countries = {};
for line in content:
if not line.startswith('{"'):
continue
line = line.strip()
line = line[0:-1]
j = json.loads(line, parse_float=decimal.Decimal)
countries[j['name']] = j['border']
for country, borders in countries.iteritems():
print(country + ' has ' + str(len(borders)) + ' borders')
polygons = [];
for border in borders:
#print(len(border))
if len(border) < 3:
continue
p = Polygon(border)
if p.area < 5:
continue
print("current border's area: " + str(p.area))
polygons.append(p)
if len(polygons) > 1:
areas = [p.area for p in polygons]
countries[country] = polygons[areas.index(max(areas))]
elif len(polygons) == 1:
countries[country] = polygons[0]
else:
raise('Country must contain at least one Polygon')
for this_country, this_polygon in countries.iteritems():
#print(this_country)
#print(this_polygon)
try:
print('representative point of ' + this_country + ': ' + str(this_polygon.representative_point()))
except ValueError:
print('representative point of ' + this_country + ': ' + str(this_polygon.convex_hull.representative_point()))
for other_country, other_polygon in countries.iteritems():
if this_country == other_country:
continue
if this_polygon.intersects(other_polygon):
print(this_country + ' has a border with ' + other_country)
ger = countries['Germany']
pol = countries['Poland']
spa = countries['Spain']
fra = countries['France']
print('Germany')
#print(ger)
print('---------------------')
print('Poland')
#print(pol)
print('---------------------')
print('Spain')
#print(spa)
print('---------------------')
print(ger.area)
print(pol.area)
print(spa.area)
print(fra.area)
print('---------------------')
#print(ger.representative_point())