-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeoJsonCountryGridCreater.py
More file actions
104 lines (85 loc) · 5 KB
/
GeoJsonCountryGridCreater.py
File metadata and controls
104 lines (85 loc) · 5 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
import os
import re
import GeoJsonDistrictGridCreater
import Screen
def print_matrix(matrix):
for row in matrix:
print row
def print_image(image):
print image_string(image, add_newline=True)
def image_string(image, add_newline=False):
string = ''
for row in image:
string += (''.join(map(str, row))).replace('0', ' ')
if add_newline:
string += '\n'
return string.rstrip()
class GeoJsonCountryGridCreater(object):
def __init__(self, directory_pattern, start_directory='.', matrix_border_offset=3, debug=True):
self.directories = self.get_directories(start_directory, directory_pattern)
self.matrix_border_offset = matrix_border_offset
self.debug = debug
def get_directories(self, start_directory, directory_pattern):
all_directories = os.listdir(start_directory)
relevant_directories = [os.path.join(start_directory, directory) for directory in all_directories if re.match(directory_pattern, os.path.basename(directory.strip('/'))) and 'TN-3' not in directory]
return relevant_directories
def print_if_debug(self, message):
if self.debug:
print message
def get_screen_info(self, coordinates_list):
xcor_temp_list = [pair[0] for pair in coordinates_list]
ycor_temp_list = [pair[1] for pair in coordinates_list]
min_xcor = min(xcor_temp_list)
max_xcor = max(xcor_temp_list)
min_ycor = min(ycor_temp_list)
max_ycor = max(ycor_temp_list)
x_range = int(max_xcor - min_xcor) + 1
y_range = int(max_ycor - min_ycor) + 1
return min_xcor, min_ycor, x_range, y_range
def get_country_grid(self):
country_grid_coordinates = self.get_country_grid_coordinates()
min_xcor, min_ycor, x_range, y_range = self.get_screen_info(country_grid_coordinates)
grid = Screen.Screen(x_range + 2 * self.matrix_border_offset, y_range + 2 * self.matrix_border_offset)
self.print_if_debug('Building state screen...')
for coordinate in country_grid_coordinates:
xcor = int(coordinate[0] - min_xcor) + self.matrix_border_offset
ycor = y_range + self.matrix_border_offset - int(coordinate[1] - min_ycor)
grid.plot(xcor, ycor)
return grid
def get_country_grid_coordinates(self):
country_grid_coordinates = []
for index, directory in enumerate(self.directories):
file_path = os.path.join(directory, 'preprocessed.txt')
self.print_if_debug('Processing district {}/{}'.format(index + 1, len(self.directories)))
district_grid_creater = GeoJsonDistrictGridCreater.GeoJsonDistrictGridCreater(file_path)
country_grid_coordinates.extend(district_grid_creater.get_district_grid_coordinates())
return country_grid_coordinates
def get_country_grid_with_more_info(self):
country_grid_coordinates = self.get_country_grid_coordinates()
min_xcor, min_ycor, x_range, y_range = self.get_screen_info(country_grid_coordinates)
grid = Screen.Screen(x_range + 2 * self.matrix_border_offset, y_range + 2 * self.matrix_border_offset)
for index, directory in enumerate(self.directories):
file_path = os.path.join(directory, 'preprocessed.txt')
self.print_if_debug('Processing district {}/{}'.format(index + 1, len(self.directories)))
district_grid_creater = GeoJsonDistrictGridCreater.GeoJsonDistrictGridCreater(file_path)
for coordinate in district_grid_creater.get_district_grid_coordinates():
xcor = int(coordinate[0] - min_xcor) + self.matrix_border_offset
ycor = y_range + self.matrix_border_offset - int(coordinate[1] - min_ycor)
grid.plot(xcor, ycor, new_value=district_grid_creater.district_name.strip('0123456789-'))
return grid
def get_state_array_more_info(self):
state_grid_coordinates = self.get_state_grid_coordinates()
min_xcor, min_ycor, x_range, y_range = self.get_screen_info(state_grid_coordinates)
grid = Screen.Screen(x_range + 2 * self.matrix_border_offset, y_range + 2 * self.matrix_border_offset)
state_grid_coordinates_dict = {}
for index, directory in enumerate(self.directories):
file_path = os.path.join(directory, 'preprocessed.txt')
self.print_if_debug('Processing district {}/{}'.format(index + 1, len(self.directories)))
district_grid_creater = GeoJsonDistrictGridCreater.GeoJsonDistrictGridCreater(file_path)
state_grid_coordinates_dict[os.path.basename(directory.strip('/'))] = district_grid_creater.get_district_grid_coordinates()
for name, coordinates_list in state_grid_coordinates_dict.items():
for coordinate in coordinates_list:
xcor = int(coordinate[0] - min_xcor) + self.matrix_border_offset
ycor = y_range + self.matrix_border_offset - int(coordinate[1] - min_ycor)
state_array_more_info[ycor][xcor] = name
return state_array_more_info