This repository was archived by the owner on Jul 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpymif.py
416 lines (377 loc) · 14.3 KB
/
pymif.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# -*- coding: utf-8 -*-
"""
Python script for .mif .mid files parsing
"""
"""
EXAMPLE
---
>>> from pymif import Mif
>>> mif = Mif(mif_string)
>>> mif.getVersion()
300
>>> mif.getColumns()
[{'type': 'CHAR(256)', 'name': 'id'}, {'type': 'CHAR(1000)', 'name': 'help'}]
>>> mif.getLineStarted("DATA")
[{'line_text': 'Data', 'attrs': '', 'line_num': 12}]
>>> mif.getLineStarted("MULTIPOINT")
[{'line_text': 'MULTIPOINT 4', 'attrs': '4', 'line_num': 11}]
>>> mid_string = open('etetet.mid', 'r').read()
>>> from pymif import Mid
>>> mid = Mid(mid_string, mif)
>>> mid.data()
[...]
>>> from pymif import CoordSys
>>> cs = mif.getCoordSys()
>>> coordSys = CoordSys(cs)
>>> coordSys.epsg
4326
"""
class Mif:
"""
Getting data from the .mif file
Parameters
----------
file_mif : <String>
Mif file text
Methods
----------
.getVersion(); \n
.getColumns(); \n
.getDescription(); \n
.getCoordSys(); \n
.getDelimiter(); \n
.getLineStarted(); \n
.getGeometry(); \n
"""
def __init__(self, file_mif):
self.file_mif = file_mif
self.lines = file_mif.split('\n')
for l in xrange(len(self.lines)):
if l >= len(self.lines):
break
line = self.lines[l]
if not line:
del self.lines[l]
def getVersion(self):
"""
Returns file version
For example:
----
<Integer>: 300
"""
info = self.getLineStarted('VERSION')
if not info:
return None
return int(info[0]['attrs'])
def getColumns(self):
"""
Returns COLUMNS field list
For example:
----
<list>: [..., { "name": %name%, "type" %type% }, ...]
"""
columns = []
info = self.getLineStarted("COLUMNS")
if not info:
return []
info = info[0]
start = info['line_num'] + 1
finish = start + int(info['attrs'])
for i in range(start, finish):
line = self.lines[i]
if line[0] in ['\t', ' ']:
line = line[1:]
line = line.replace('\t', '').split(' ')
if line[0] in ['\t', ' ', '']:
line = line[1:]
col_name = line[0]
col_type = ''.join(line[1:])
columns.append({"name": col_name, "type": col_type})
return columns
def getDescription(self):
"""
Returns DESCRIPTION field list
For example:
----
<list>: [..., { "name": %name%, "description" %description% }, ...]
"""
columns = []
info = self.getLineStarted("DESCRIPTION")
if not info:
return []
info = info[0]
start = info['line_num'] + 1
finish = start + int(info['attrs'])
for i in range(start, finish):
line = self.lines[i]
if line[0] != '\t':
raise ValueError("Строка в поле description должна начинаться с табуляции")
line = line.replace("\t", "").split(' ')
col_name = line[0]
col_description = ''.join(line[1:])
if col_description[0] == '"' or col_description[0] == "'":
col_description = col_description[1:-1]
columns.append({"name": col_name, "description": col_description})
return columns
def getCoordSys(self):
"""
Returns string coord. system
For example:
---
<string>: "Earth Projection 8, 104, 'm', 115.03333333333, 0, 1, 7250000, -5411057.63"
"""
info = self.getLineStarted("CoordSys")
if not info:
return None
return info[0]['attrs']
def getDelimiter(self):
"""
Returns current delimiter if it exists else returns standart delimiter "\\t"
For example:
---
<String>: "\\t"
"""
info = self.getLineStarted("DELIMITER")
if not info:
return "\t"
delimiter = info[0]['attrs']
if delimiter[0] == delimiter[-1] and delimiter[0] in ['\'', '\"']:
delimiter = delimiter[1:-1]
return delimiter
def getLineStarted(self, string):
"""
Returns all lines that starts with incoming argument
Parameters
---
string: <String>
for example: "VERISON" or "COLUMNS"
Returns
---
<List>: [ ..., {
"line_num": <Integer> %LineNumber%,
"attrs": <String> %LineAttributes%,
"line_text": <String> %CurrentLineString%
}, ... ]
"""
string = str(string)
l = len(string)
return_lines = []
for i in xrange(len(self.lines)):
line = self.lines[i]
if line[:l].lower() == string.lower():
attrs = line[l:]
if attrs and attrs[0] == ' ':
attrs = attrs[1:]
return_lines.append({"line_num": int(i), "attrs": attrs, "line_text": self.lines[i]})
return return_lines
def getGeometry(self):
"""
Returns geometry's list
Returns
---
<List>: [ ..., {
"geom": <List> %PointList%,
"type": <String> %GeometryType%
}, ... ]
"""
info = self.getLineStarted("DATA")
if not info:
return None
info = info[0]
l = info['line_num'] + 1
m = len(self.lines)
geom_objects = []
types = ("point", "line", "pline", "region", "arc", "text", "rect", "roundrect", "ellipse", "multipoint", "collection", "none")
# Собираются все линии, с которых начинается геометрия
for line_num in range(l, m):
line = self.lines[line_num]
if line.lower().startswith(types):
geom_objects.append(line_num)
geom = []
for l in geom_objects:
line = self.lines[l].lower().split(' ')
parsers = {
"point": self.__parsePoint,
"line": self.__parseLine,
"pline": self.__parsePline,
"region": self.__parseRegion,
"arc": self.__parseArc,
"text": self.__parseText,
"rect": self.__parseRect,
"roundrect": self.__parseRoundrect,
"ellipse": self.__parseEllipse,
"multipoint": self.__parseMultipoint,
"collection": self.__parseCollection,
"none": self.__parseNone,
}
try:
func = parsers[line[0]](l)
except:
raise ValueError("Uknown data format %s at line %s" % (line[0], l))
geom.append(func)
return geom
def __parsePoint(self, line):
"""Parse point"""
line_str = self.lines[line].lower().replace('\t', '').split(' ')
geometry = line_str[1:]
return { "type": "point", "geom": geometry }
def __parseLine(self, line):
"""Parse line"""
line_str = self.lines[line].lower().replace('\t', '').split(' ')
geometry = [line_str[1:3], line_str[3:5]]
return { "type": "line", "geom": geometry }
def __parsePline(self, line):
"""Parse poly line"""
line_str = self.lines[line].lower().replace('\t', '').split(' ')
geom_len = 2
geometry = [[]]
if len(line_str) > 1 and line_str[1]:
geom_len = int(line_str[1])
line += 1
for l in range(line, line + geom_len):
point = self.lines[l].replace('\t', '').split(' ')
geometry[0].append(point)
return { "type": "pline", "geom": geometry }
def __parseRegion(self, line):
"""Parse region"""
line_str = self.lines[line].lower().replace('\t', '').split(' ')
reg_count = int(line_str[1])
reg_len = None
geometry = []
for _ in xrange(reg_count):
for l in range(line, len(self.lines)):
try:
reg_len = int(self.lines[l])
except:
pass
else:
line = l + 1
break
if not reg_len:
return None
geometry.append([])
for l in range(line, line + reg_len):
point = self.lines[l].replace('\t', '').split(' ')
geometry[-1].append(point)
return { "type": "region", "geom": geometry, "reg_count": reg_count }
def __parseArc(self, line): # TODO
pass; return { "type": "arc", "geom": None }
def __parseText(self, line): # TODO
pass; return { "type": "text", "geom": None }
def __parseRect(self, line): # TODO
pass; return { "type": "rect", "geom": None }
def __parseRoundrect(self, line): # TODO
pass; return { "type": "roundrect", "geom": None }
def __parseEllipse(self, line): # TODO
pass; return { "type": "ellipse", "geom": None }
def __parseMultipoint(self, line):
"""Parse multipoint"""
line_str = self.lines[line].lower().replace('\t', '').split(' ')
point_count = int(line_str[1])
line += 1
geometry = []
for i in range(line, line + point_count):
point = self.lines[i].replace('\t', '').split(' ')
geometry.append(point)
return { "type": "multipoint", "geom": geometry }
def __parseCollection(self, line): # TODO
"""Parse geometry collection"""
line_str = self.lines[line].lower().replace('\t', '').split(' ')
collection_arr = []
collection_len = int(line_str[1])
line += 1
types = ("point", "line", "pline", "region", "arc", "text", "rect", "roundrect", "ellipse", "multipoint", "collection")
for l in range(line, len(self.lines)):
line_str = self.lines[l].lower().replace('\t', '')
if line_str.startswith(types):
collection_arr.append(l)
if len(collection_arr) >= collection_len:
break
parsers = {
"point": self.__parsePoint,
"line": self.__parseLine,
"pline": self.__parsePline,
"region": self.__parseRegion,
"arc": self.__parseArc,
"text": self.__parseText,
"rect": self.__parseRect,
"roundrect": self.__parseRoundrect,
"ellipse": self.__parseEllipse,
"multipoint": self.__parseMultipoint,
"collection": self.__parseCollection,
"none": self.__parseNone,
}
geom_return = []
for obj in collection_arr:
geom_type = self.lines[obj].lower().replace('\t', '').split(' ')[0]
try:
func = parsers[geom_type]
except:
raise ValueError("Uknown data format %s at line %s" % (line[0], l))
geom_return.append(func(obj))
return { "type": "collection", "geom": geom_return }
def __parseNone(self, line): # TODO
"""returns None"""
return { "type": "None", "geom": None }
class Mid:
# Функция инициализации класса
# Принимает строку содержимого mid файла в file_mid
def __init__(self, file_mid, file_mif):
self.file_mid = file_mid
self.lines = file_mid.split('\n')
self.mif = file_mif
if not isinstance(file_mif, Mif):
raise ValueError("'file_mif' argument must be a Mif() instance")
def data(self, soft = False):
columns = self.mif.getDescription() if self.mif.getDescription() else self.mif.getColumns()
key = 'description' if self.mif.getDescription() else 'name'
delimiter = self.mif.getDelimiter()
data = {"count": 0, "info": []}
for line in self.lines:
if not line:
continue
data['info'].append([])
if line[-1] == '\t':
line = line[:-1]
els = line.split(delimiter)
if len(els) != len(columns) and not soft:
raise ValueError("columns length is not equal to mid file data.\ncall this function with 'soft=True' argument")
for i in xrange(len(els)):
attr_name = columns[ i % len(els) ]
data['info'][-1].append( { "name": attr_name[key], "value": els[i] } )
data['count'] = len(data['info'])
return data
class CoordSys:
"""
Перевод MapInfo системы координат в EPSG:
Variables
---
.epsg : Integer \n
.name : String
Parameters
---
String : coordSys - Система координат из файла
"""
def __init__(self, coordSys):
self.__loadProjs()
try:
info = self.projs[coordSys]
except:
print coordSys
raise ValueError("Uknown coordsys")
else:
self.epsg = info['epsg']
self.name = info['name']
def __loadProjs(self):
self.projs = {
"Earth Projection 1, 104": {"epsg": 4326, "name": "WGS84 / Long/Lat"},
"Earth 1, 104": {"epsg": 4326, "name": "WGS84"},
"Earth Projection 8, 104, \"m\", 97.03333333333, 0, 1, 1250000, -5411057.63": {"epsg": 911001, "name": "MSK-38 - zone 1"},
"Earth Projection 8, 104, \"m\", 100.03333333333, 0, 1, 2250000, -5411057.63": {"epsg": 911002, "name": "MSK-38 - zone 2"},
"Earth Projection 8, 104, \"m\", 103.03333333333, 0, 1, 3250000, -5411057.63": {"epsg": 911003, "name": "MSK-38 - zone 3"},
"Earth Projection 8, 104, \"m\", 106.03333333333, 0, 1, 4250000, -5411057.63": {"epsg": 911004, "name": "MSK-38 - zone 4"},
"Earth Projection 8, 104, \"m\", 109.03333333333, 0, 1, 5250000, -5411057.63": {"epsg": 911005, "name": "MSK-38 - zone 5"},
"Earth Projection 8, 104, \"m\", 112.03333333333, 0, 1, 6250000, -5411057.63": {"epsg": 911006, "name": "MSK-38 - zone 6"},
"Earth Projection 8, 104, \"m\", 115.03333333333, 0, 1, 7250000, -5411057.63": {"epsg": 911007, "name": "MSK-38 - zone 7"},
"Earth Projection 8, 104, \"m\", 118.03333333333, 0, 1, 8250000, -5411057.63": {"epsg": 911008, "name": "MSK-38 - zone 8"},
}