-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathosmapformatmp.pas
More file actions
590 lines (553 loc) · 15.4 KB
/
osmapformatmp.pas
File metadata and controls
590 lines (553 loc) · 15.4 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
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
unit OsMapFormatMp;
{$ifdef FPC}
{$mode objfpc}{$H+}
{$else}
{$ZEROBASEDSTRINGS OFF}
{$endif}
interface
uses
{$ifdef MSWINDOWS}Windows, {$else}Types, {$endif}
Classes, SysUtils, strutils,
{$ifdef FPC}
streamex,
{$endif}
OsMapManager, OsMapObjects, OsMapGeometry,
OsMapTypes;
type
{ TMpFileImporter }
TMpFileImporter = object
private
IsWay, IsPoint, IsArea, IsTypeParsed: Boolean;
FManager: TMapManager;
LineNo: Integer;
AreaCircleNo: Integer;
TmpArea: TMapArea;
TmpWay: TMapWay;
TmpData: array of string; // for data before item type
procedure ParseLine(AText: string);
procedure ParseKeyValue(const AKey, AValue: string);
procedure ParseAreaType(const TypeId: Integer);
procedure ParseWayType(const TypeId: Integer);
procedure ParseLabel(AValue: string);
procedure ParseData0(AValue: string);
public
procedure Init();
function Import(AManager: TMapManager; AFileName: string): Boolean;
end;
function ImportFromMpFile(AManager: TMapManager; AFileName: string): Boolean;
implementation
{$ifdef FPC}
uses Math, LazUTF8;
{$endif}
function ImportFromMpFile(AManager: TMapManager; AFileName: string): Boolean;
var
Importer: TMpFileImporter;
begin
Result := Importer.Import(AManager, AFileName);
end;
{ TMpFileImporter }
procedure TMpFileImporter.Init();
begin
IsArea := False;
IsPoint := False;
IsWay := False;
IsTypeParsed := False;
TmpArea := nil;
TmpWay := nil;
end;
procedure TMpFileImporter.ParseLine(AText: string);
var
s, sKey, sValue: string;
n, i: Integer;
begin
s := Copy(AText, 1, 1);
if s = '[' then
begin
if AText = '[POI]' then
begin
IsPoint := True;
end
else
if AText = '[POLYLINE]' then
begin
IsWay := True;
end
else
if AText = '[POLYGON]' then
begin
IsArea := True;
AreaCircleNo := 0;
end
else
if AText = '[END]' then
begin
for i:=0 to Length(TmpData)-1 do
begin
if IsTypeParsed then
ParseLine(TmpData[i]);
end;
TmpData := [];
IsTypeParsed := False;
if IsPoint then
begin
IsPoint := False;
end;
if IsWay then
begin
IsWay := False;
if Assigned(TmpWay) then
begin
TmpWay.FileOffset := LineNo;
FManager.MapData.WayList.Add(TmpWay);
TmpWay := nil;
end;
end;
if IsArea then
begin
IsArea := False;
if Assigned(TmpArea) then
begin
TmpArea.FileOffset := LineNo;
if Length(TmpArea.Rings[0].Nodes) > 2 then
FManager.MapData.AreaList.Add(TmpArea)
else
TmpArea.Free();
TmpArea := nil;
end;
end;
end;
end
else
if (s = ';') or (s = '') then
begin
// comment, ignore
end
else
begin
// key=value
n := Pos('=', AText);
sKey := Copy(AText, 1, n-1);
if IsTypeParsed or (sKey = 'Type') then
begin
sValue := Copy(AText, n+1, MaxInt);
ParseKeyValue(sKey, sValue);
end
else if IsPoint or IsWay or IsArea then
begin
// parse later
n := Length(TmpData);
SetLength(TmpData, n+1);
TmpData[n] := AText;
end;
end;
end;
procedure TMpFileImporter.ParseKeyValue(const AKey, AValue: string);
var
TypeId: Integer;
begin
if AKey = 'Type' then
begin
TypeId := StrToInt(AValue);
if IsArea then
begin
ParseAreaType(TypeId);
end;
if IsWay then
begin
ParseWayType(TypeId);
end;
IsTypeParsed := True;
end
else
if (AKey = 'Data0') or (AKey = 'Data1') or (AKey = 'Data2') then
begin
ParseData0(AValue);
end
else
if AKey = 'Label' then
begin
ParseLabel(AValue);
end
else
if AKey = 'HouseNumber' then
begin
if IsArea and Assigned(TmpArea) then
begin
// номер дома
TmpArea.Rings[0].FeatureValueBuffer.SetFeatureValue(ftName, AValue);
end
end
else
if AKey = 'StreetDesc' then
begin
if IsArea and Assigned(TmpArea) then
begin
// улица
TmpArea.Rings[0].FeatureValueBuffer.SetFeatureValue(ftAddress, AValue);
end
end
else
if AKey = 'EndLevel' then
begin
if IsArea and Assigned(TmpArea) then
begin
// уровень, ZOrder
TmpArea.Rings[0].FeatureValueBuffer.SetFeatureValue(ftLayer, AValue);
end
end;
end;
procedure TMpFileImporter.ParseAreaType(const TypeId: Integer);
begin
case TypeId of
$01, // place = city
$02: // place = town
begin
TmpArea := TMapArea.Create();
TmpArea.Rings[0].SetType(FManager.MapTypeConfig.GetTypeInfo('city'));
end;
$03, // landuse = residential
$10f02: // landuse = residential
begin
TmpArea := TMapArea.Create();
TmpArea.Rings[0].SetType(FManager.MapTypeConfig.GetTypeInfo('landuse_residential'));
end;
$08, // landuse = retail
$0A, // amenity = school
$0B: // amenity = hospital
begin
TmpArea := TMapArea.Create();
TmpArea.Rings[0].SetType(FManager.MapTypeConfig.GetTypeInfo('landuse_social'));
end;
$04, // landuse = military
$0C: // amenity = industrial
begin
TmpArea := TMapArea.Create();
TmpArea.Rings[0].SetType(FManager.MapTypeConfig.GetTypeInfo('landuse_industrial'));
end;
$05: // landuse = garages; amenity = parking
begin
TmpArea := TMapArea.Create();
TmpArea.Rings[0].SetType(FManager.MapTypeConfig.GetTypeInfo('landuse_parking'));
end;
$06: // building = garages
begin
TmpArea := TMapArea.Create();
TmpArea.Rings[0].SetType(FManager.MapTypeConfig.GetTypeInfo('building_industrial'));
end;
$07, // building = yes, aeroway = terminal
$09: // building = yes, leisure = marina
begin
TmpArea := TMapArea.Create();
TmpArea.Rings[0].SetType(FManager.MapTypeConfig.GetTypeInfo('building_industrial'));
end;
$6F: // building = industrial
begin
TmpArea := TMapArea.Create();
TmpArea.Rings[0].SetType(FManager.MapTypeConfig.GetTypeInfo('building_industrial'));
end;
$13: // building
begin
TmpArea := TMapArea.Create();
TmpArea.Rings[0].SetType(FManager.MapTypeConfig.GetTypeInfo('building'));
end;
$17: // leisure = park
begin
TmpArea := TMapArea.Create();
TmpArea.Rings[0].SetType(FManager.MapTypeConfig.GetTypeInfo('forest'));
end;
$18, // leisure = pitch, sport = soccer
$19, // leisure = pitch, sports
$4D: // sport = ice_hockey
begin
//TmpArea := TMapArea.Create();
//TmpArea.Rings[0].SetType(FManager.MapTypeConfig.GetTypeInfo('sports'));
end;
$40, // natural = water
$41, // natural = water
$81, // natural = wetland
$45, // leisure = swimming_pool
$47: // waterway = riverbank
begin
TmpArea := TMapArea.Create();
TmpArea.Rings[0].SetType(FManager.MapTypeConfig.GetTypeInfo('water'));
end;
$4E, // landuse = orchard
$4F, // natural = scrub
$50, // landuse = forest
$51: // natural = wood
begin
TmpArea := TMapArea.Create();
TmpArea.Rings[0].SetType(FManager.MapTypeConfig.GetTypeInfo('forest'));
end;
$6C, // building = apartments
$6D, // building = administrative
$6E: // building = hospital
begin
TmpArea := TMapArea.Create();
TmpArea.Rings[0].SetType(FManager.MapTypeConfig.GetTypeInfo('building_apartments'));
end;
$88, // landuse = farmland
$89, // natural = beach
$11006: // landuse = farmland
begin
TmpArea := TMapArea.Create();
TmpArea.Rings[0].SetType(FManager.MapTypeConfig.GetTypeInfo('farmland'));
end;
$95, // natural = grassland
$98, // landuse = grass
$11001: // landuse = meadow
begin
TmpArea := TMapArea.Create();
TmpArea.Rings[0].SetType(FManager.MapTypeConfig.GetTypeInfo('grass'));
end;
$1A, // barrier = wall, landuse = cemetery
$6A, // area = yes, highway = footway, highway = pedestrian
$8A: // natural = scree
begin
// area = yes, highway = footway, highway = pedestrian
end
else
begin
//TmpArea := TMapArea.Create();
//TmpArea.Rings[0].SetType(FManager.MapTypeConfig.GetTypeInfo('grass'));
end;
end;
end;
procedure TMpFileImporter.ParseWayType(const TypeId: Integer);
begin
case TypeId of
$00: // highway = construction
begin
TmpWay := TMapWay.Create();
TmpWay.SetType(FManager.MapTypeConfig.GetTypeInfo('highway_construction'));
end;
$01: // highway = trunk (магистраль)
begin
TmpWay := TMapWay.Create();
TmpWay.SetType(FManager.MapTypeConfig.GetTypeInfo('highway_trunk'));
end;
$04: // highway = primary (главная дорога)
begin
TmpWay := TMapWay.Create();
TmpWay.SetType(FManager.MapTypeConfig.GetTypeInfo('highway_primary'));
end;
$02, // highway = secondary (второстепенная дорога)
$03, // highway = tertiary
$05: // highway = secondary, tertiary
begin
TmpWay := TMapWay.Create();
TmpWay.SetType(FManager.MapTypeConfig.GetTypeInfo('highway_secondary'));
end;
$06: // highway = residential (проезд к домам)
begin
TmpWay := TMapWay.Create();
TmpWay.SetType(FManager.MapTypeConfig.GetTypeInfo('highway_residential'));
end;
$07: // highway = service (служебный проезд)
begin
TmpWay := TMapWay.Create();
TmpWay.SetType(FManager.MapTypeConfig.GetTypeInfo('highway_service'));
end;
$08: // highway = tertiary_link (выезд на второстепенную)
begin
TmpWay := TMapWay.Create();
TmpWay.SetType(FManager.MapTypeConfig.GetTypeInfo('highway_secondary'));
end;
$09: // highway = trunk_link (выезд на магистраль)
begin
TmpWay := TMapWay.Create();
TmpWay.SetType(FManager.MapTypeConfig.GetTypeInfo('highway_trunk'));
end;
$0A: // highway = track (грунтовка)
begin
//TmpWay := TMapWay.Create();
//TmpWay.SetType(FManager.MapTypeConfig.GetTypeInfo('highway_primary'));
end;
$0C: // junction = roundabout, highway = primary, secondary (круговое движение)
begin
TmpWay := TMapWay.Create();
TmpWay.SetType(FManager.MapTypeConfig.GetTypeInfo('highway_primary'));
end;
$14: // railway = rail (железная дорога)
begin
TmpWay := TMapWay.Create();
TmpWay.SetType(FManager.MapTypeConfig.GetTypeInfo('railway_rail'));
end;
$15: // natural = coastline
begin
TmpWay := TMapWay.Create();
TmpWay.SetType(FManager.MapTypeConfig.GetTypeInfo('coastline'));
end;
$18, // waterway = stream
$1F: // waterway = river
begin
//TmpWay := TMapWay.Create();
//TmpWay.SetType(FManager.MapTypeConfig.GetTypeInfo('water_way'));
end;
$3F: // railway = tram (трамвайные рельсы)
begin
TmpWay := TMapWay.Create();
TmpWay.SetType(FManager.MapTypeConfig.GetTypeInfo('railway_tram'));
end;
$16, // highway = path (тропинка)
$1C, // boundary = administrative
$22, // area = yes, natural = cliff
$26, // waterway = ditch
$27, // aeroway = runway
$28, // man_made = pipeline
$29, // power = line
$42, // highway = track, tracktype = grade1 (метро?)
$43, // highway = residential, tunnel = yes
$46, // barrier = fence
$48: // man_made = cutline
begin
//TmpWay := TMapWay.Create();
//TmpWay.SetType(FManager.MapTypeConfig.GetTypeInfo('highway_path'));
end;
else
{begin
TmpWay := TMapWay.Create();
TmpWay.SetType(FManager.MapTypeConfig.GetTypeInfo('highway_road'));
end;}
end;
end;
procedure TMpFileImporter.ParseLabel(AValue: string);
var
n: Integer;
begin
if Pos('~[', AValue) > 0 then
begin
// remove ~[0x05]11
n := Pos(' ', AValue);
if n > 0 then
AValue := Copy(AValue, n+1, MaxInt)
else
Exit;
end;
if IsArea and Assigned(TmpArea) then
begin
// номер дома, название участка
//TmpArea.Rings[0].FeatureValueBuffer.SetFeatureValue(ftName, UTF8ToSys(AValue));
TmpArea.Rings[0].FeatureValueBuffer.SetFeatureValue(ftName, AValue);
end
else
if IsWay and Assigned(TmpWay) then
begin
// название дороги / реки
//TmpArea.Rings[0].FeatureValueBuffer.SetFeatureValue(ftName, UTF8ToSys(AValue));
TmpWay.FeatureValueBuffer.SetFeatureValue(ftName, AValue);
end;
end;
procedure TMpFileImporter.ParseData0(AValue: string);
function _ExtractCoord(var AOffs: Integer; out ACoord: TGeoPoint): Boolean;
var
iBeg, iEnd: Integer;
begin
iBeg := PosEx('(', AValue, AOffs);
if iBeg > 0 then
begin
iEnd := PosEx(')', AValue, AOffs);
AOffs := iEnd + 1;
Result := ACoord.Parse(Copy(AValue, iBeg+1, iEnd-iBeg-1));
end
else
Result := False;
end;
var
Offs: Integer;
//Coord: TGeoPoint;
GeoPoint: TGeoPoint;
n: Integer;
begin
if Assigned(TmpWay) then
begin
Offs := 1;
n := 0;
while _ExtractCoord(Offs, GeoPoint) do
begin
//GeoPoint.Serial := Byte(n);
AppendGeoPointToArray(TmpWay.Nodes, GeoPoint);
Inc(n);
end;
end;
if Assigned(TmpArea) then
begin
if AreaCircleNo > 0 then
begin
SetLength(TmpArea.Rings, AreaCircleNo+1);
TmpArea.Rings[AreaCircleNo].Ring := 2;
TmpArea.Rings[AreaCircleNo].SetType(TmpArea.Rings[0].GetType());
end;
Offs := 1;
n := 0;
while _ExtractCoord(Offs, GeoPoint) do
begin
//GeoPoint.Serial := Byte(n);
AppendGeoPointToArray(TmpArea.Rings[AreaCircleNo].Nodes, GeoPoint);
Inc(n);
end;
Inc(AreaCircleNo);
end;
end;
function TMpFileImporter.Import(AManager: TMapManager; AFileName: string): Boolean;
var
//txt: TextFile;
ss: string;
ResStream: TStream;
sr: TStreamReader;
begin
Result := False;
Assert(Assigned(AManager));
if Pos('_', AFileName) = 1 then
begin
// from resource
ResStream := TResourceStream.Create(HInstance, AFileName, Windows.RT_RCDATA);
end
else
begin
if not FileExists(AFileName) then
Exit;
ResStream := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyNone);
//ResStream := TFileStream.Create(AFileName, fmOpenRead);
end;
sr := TStreamReader.Create(ResStream);
Init();
FManager := AManager;
LineNo := 0;
{$ifdef FPC}
while not sr.Eof do
{$else}
while not sr.EndOfStream do
{$endif}
begin
Inc(LineNo);
ss := sr.ReadLine;
ParseLine(ss);
end;
Result := True;
sr.Free();
ResStream.Free();
{end
else
begin
if not FileExists(AFileName) then
Exit;
Init();
FManager := AManager;
LineNo := 0;
AssignFile(txt, AFileName);
try
Reset(txt);
while not EOF(txt) do
begin
Inc(LineNo);
ReadLn(txt, ss);
ParseLine(ss);
end;
Result := True;
finally
CloseFile(txt);
end;
end; }
end;
end.