forked from osm-fr/osmose-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Analyser_Merge.py
1192 lines (1078 loc) · 45.9 KB
/
Analyser_Merge.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
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
#-*- coding: utf-8 -*-
###########################################################################
## ##
## Copyrights Frédéric Rodrigo 2012 ##
## ##
## This program is free software: you can redistribute it and/or modify ##
## it under the terms of the GNU General Public License as published by ##
## the Free Software Foundation, either version 3 of the License, or ##
## (at your option) any later version. ##
## ##
## This program is distributed in the hope that it will be useful, ##
## but WITHOUT ANY WARRANTY; without even the implied warranty of ##
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ##
## GNU General Public License for more details. ##
## ##
## You should have received a copy of the GNU General Public License ##
## along with this program. If not, see <http://www.gnu.org/licenses/>. ##
## ##
###########################################################################
import io
import bz2
import datetime
import gzip
from backports import csv # In python3 only just "import csv"
import inspect
import psycopg2.extras
import psycopg2.extensions
import os
import os.path
import time
import zipfile
import tempfile
import json
import re
from collections import defaultdict
from .Analyser_Osmosis import Analyser_Osmosis
from modules.Stablehash import stablehash64, hexastablehash
from modules import downloader
from modules import PointInPolygon
from modules import SourceVersion
try:
from pyproj import Transformer
except ImportError:
# No available in py2
Transformer = None
GENERATE_DELETE_TAG = u"DELETE TAG aechohve0Eire4ooyeyaey1gieme0xoo"
sql_schema = """
DO language 'plpgsql' $$
BEGIN
IF NOT EXISTS (SELECT * FROM information_schema.schemata WHERE schema_name = '%(schema)s' ) THEN
CREATE SCHEMA %(schema)s;
END IF;
END $$
"""
sql00 = """
CREATE TEMP TABLE %(official)s_temp (
ref varchar(65534),
tags hstore,
tags1 hstore,
fields hstore,
geom geography
)
"""
sql01_ref = """
SELECT
%(distinct)s
%(x)s AS _x,
%(y)s AS _y,
*
FROM
%(table)s
WHERE
%(where)s
%(order_by)s
"""
sql01_geo = """
SELECT
%(distinct)s
%(x)s AS _x,
%(y)s AS _y,
*
FROM
%(table)s
WHERE
%(x)s IS NOT NULL AND
%(y)s IS NOT NULL AND
%(x)s::varchar NOT IN ('', 'null') AND
%(y)s::varchar NOT IN ('', 'null') AND
%(where)s
%(order_by)s
"""
sql02 = """
INSERT INTO
%(official)s_temp
VALUES (
%(ref)s,
%(tags)s,
%(tags1)s,
%(fields)s,
ST_SetSRID(ST_MakePoint(%(lon)s, %(lat)s), 4326)::geography
)
"""
sql02b = """
DROP TABLE IF EXISTS %(official)s CASCADE;
CREATE UNLOGGED TABLE %(official)s AS
SELECT
ref,
tags,
tags1,
fields,
geom
FROM
%(official)s_temp
GROUP BY
ref,
tags,
tags1,
fields,
geom
"""
sql03a = """
CREATE INDEX ir_%(official)s ON %(official)s(ref)
"""
sql03b = """
CREATE INDEX ig_%(official)s ON %(official)s USING GIST(geom)
"""
sql10 = """
CREATE TEMP TABLE missing_official AS
SELECT
official.ref,
ST_AsText(official.geom),
official.tags,
official.fields,
official.geom
FROM
%(official)s AS official
LEFT JOIN osm_item ON
%(joinClause)s
WHERE
osm_item.id IS NULL
"""
sql11 = """
CREATE INDEX missing_official_index_ref ON missing_official(ref);
CREATE INDEX missing_official_index_geom ON missing_official USING GIST(geom);
"""
sql12 = """
SELECT * FROM missing_official;
"""
sql20 = """
CREATE TEMP TABLE missing_osm AS
SELECT
osm_item.id,
osm_item.type,
CASE
WHEN osm_item.geom IS NOT NULL THEN ST_AsText(osm_item.geom)
ELSE ST_AsText(any_locate(osm_item.type, osm_item.id))
END,
osm_item.tags,
osm_item.geom,
osm_item.shape
FROM
osm_item
LEFT JOIN %(official)s AS official ON
%(joinClause)s
WHERE
osm_item.ref IS NULL AND
official.ref IS NULL
"""
sql21 = """
CREATE INDEX missing_osm_index_shape ON missing_osm USING GIST(shape)
"""
sql22 = """
SELECT * FROM missing_osm
"""
sql23 = """
SELECT
osm_item.id,
osm_item.type,
CASE
WHEN osm_item.geom IS NOT NULL THEN ST_AsText(osm_item.geom)
ELSE ST_AsText(any_locate(osm_item.type, osm_item.id))
END,
osm_item.tags,
osm_item.geom,
osm_item.ref
FROM
osm_item
LEFT JOIN %(official)s AS official ON
%(joinClause)s
WHERE
osm_item.ref IS NOT NULL AND
official.ref IS NULL
"""
sql30 = """
SELECT
DISTINCT ON (id)
missing_osm.id,
missing_osm.type,
CASE
WHEN missing_osm.geom IS NOT NULL THEN ST_AsText(missing_osm.geom)
ELSE ST_AsText(any_locate(missing_osm.type, missing_osm.id))
END,
missing_official.tags AS official_tags,
missing_official.fields AS official_fields,
missing_osm.tags AS osm_tags
FROM
missing_official
JOIN missing_osm ON
%(joinClause)s
ORDER BY
missing_osm.id
%(orderBy)s
"""
sql40 = """
CREATE TEMP TABLE match AS
SELECT
osm_item.id,
osm_item.type,
osm_item.tags,
osm_item.geom
FROM
osm_item
JOIN %(official)s AS official ON
%(joinClause)s
"""
sql41 = """
(
SELECT
id::bigint AS osm_id,
type::varchar AS osm_type,
tags::hstore,
ST_X(geom::geometry)::float AS lon,
ST_Y(geom::geometry)::float AS lat
FROM
match
) UNION ALL (
SELECT
NULL::bigint AS osm_id,
NULL::varchar AS osm_type,
tags::hstore,
ST_X(geom::geometry)::float AS lon,
ST_Y(geom::geometry)::float AS lat
FROM
missing_official
) UNION ALL (
SELECT
id::bigint AS osm_id,
type::varchar AS osm_type,
tags::hstore,
ST_X(geom::geometry)::float AS lon,
ST_Y(geom::geometry)::float AS lat
FROM
missing_osm
)
"""
sql50 = """
SELECT
osm_item.id,
ST_AsText(osm_item.geom),
ST_AsText(official.geom)
FROM
%(official)s AS official
JOIN osm_item ON
%(joinClause)s AND
NOT official.geom && osm_item.geom
"""
sql60 = """
SELECT
osm_item.id,
osm_item.type,
ST_AsText(osm_item.geom),
official.tags,
osm_item.tags,
official.fields AS official_fields
FROM
%(official)s AS official
JOIN osm_item ON
%(joinClause)s
WHERE
official.tags1 - (SELECT coalesce(array_agg(key), array[]::text[]) FROM each(official.tags1) WHERE NOT osm_item.tags?key AND value = '""" + GENERATE_DELETE_TAG + """') - osm_item.tags - 'source'::text != ''::hstore
"""
class Source:
def __init__(self, attribution = None, millesime = None, encoding = "utf-8", file = None, fileUrl = None, fileUrlCache = 30, zip = None, gzip = False, filter = None):
"""
Describe the source file.
@param encoding: file charset encoding
@param file: file name in storage
@param urlFile: remote URL of source file
@param fileUrlCache: days for file in cache
@param zip: extract file from zip
@param gzip: uncompress as gzip
@param filter: lambda expression applied on text file before loading
"""
self.attribution = attribution
self.millesime = millesime
self.encoding = encoding
self.file = file
self.fileUrl = fileUrl
self.fileUrlCache = fileUrlCache
self.zip = zip
self.gzip = gzip
self.filter = filter
if self.file:
if not os.path.isabs(self.file):
self.file = "merge_data/" + self.file
if self.attribution and "%s" in self.attribution:
self.attribution_re = re.compile(self.attribution.replace("%s", ".*"))
def time(self):
if self.file:
return int(os.path.getmtime(self.file)+.5)
elif self.fileUrl:
if self.zip:
f = downloader.urlopen(self.fileUrl, self.fileUrlCache, mode='rb')
date_time = zipfile.ZipFile(f, 'r').getinfo(self.zip).date_time
return int(time.mktime(date_time + (0, 0, -1))+.5)
else:
return int(downloader.urlmtime(self.fileUrl, self.fileUrlCache)+.5)
def path(self):
if self.file:
return self.file
elif self.fileUrl:
# Do nothing about ZIP
return downloader.path(self.fileUrl, self.fileUrlCache)
def open(self):
if self.file:
f = bz2.BZ2File(self.file)
elif self.fileUrl:
f = downloader.urlopen(self.fileUrl, self.fileUrlCache, mode='rb')
if self.zip:
z = zipfile.ZipFile(f, 'r').open(self.zip)
f = io.BytesIO(z.read())
f.seek(0)
elif self.gzip:
d = gzip.open(downloader.path(self.fileUrl, self.fileUrlCache), mode='r')
f = io.BytesIO(d.read())
f.seek(0)
f = io.StringIO(f.read().decode(self.encoding, 'ignore'))
f.seek(0)
if self.filter:
f = io.StringIO(self.filter(f.read()))
f.seek(0)
return f
def as_tag_value(self):
if "%s" in self.attribution:
return self.attribution % self.millesime
else:
return " - ".join(filter(lambda x: x is not None, [self.attribution, self.millesime]))
def match_attribution(self, s):
if "%s" not in self.attribution:
return self.attribution in s
else:
return self.attribution_re.match(s)
class Parser:
def header(self):
pass
def import_(self, table, srid, osmosis):
pass
def close(self):
pass
class CSV(Parser):
def __init__(self, source, separator = u',', null = u'', header = True, quote = u'"', csv = True, skip_first_lines = 0):
"""
Describe the CSV file format, mainly for postgres COPY command in order to load data, but also for other thing, like load header.
Setting param as None disable parameter into the COPY command.
@param source: source file reader
@param separator: one char separator
@param null: string loaded à NULL
@param header: CSV have header row
@param quote: one char string delimiter
@param csv: load file as CSV on COPY command
@param skip_first_lines: skip lines before reading CSV content
"""
self.source = source
self.separator = separator
self.null = null
self.have_header = header
self.quote = quote
self.csv = csv
self.skip_first_lines = skip_first_lines
self.f = None
def header(self):
self.f = self.source.open()
for _ in range(self.skip_first_lines):
self.f.__next__()
if self.have_header:
return csv.reader(self.f, delimiter=self.separator, quotechar=self.quote).next()
def import_(self, table, srid, osmosis):
self.f = self.f or self.source.open()
for _ in range(self.skip_first_lines):
self.f.__next__()
copy = "COPY %s FROM STDIN WITH %s %s %s %s %s" % (
table,
("DELIMITER AS '%s'" % self.separator) if self.separator is not None else "",
("NULL AS '%s'" % self.null) if self.null is not None else "",
"CSV" if self.csv else "",
"HEADER" if self.csv and self.header else "",
("QUOTE '%s'" % self.quote) if self.csv and self.quote else "")
osmosis.giscurs.copy_expert(copy, self.f)
def close(self):
self.f.close()
def flattenjson(b):
"""
Converts multi-level JSON properties into single level
Columns names are separated by a "."
Based on Alec McGail implementation, see https://stackoverflow.com/a/28246154
@param b: source JSON object
@return flattened JSON object
"""
val = {}
for i in b.keys():
if isinstance( b[i], dict ):
get = flattenjson(b[i])
for j in get.keys():
val[ i + "." + j ] = get[j]
elif isinstance( b[i], list):
val[i] = "[" + ",".join(map(str, b[i])) + "]"
else:
val[i] = b[i]
return val
def removequotesjson(s):
"""
Removes trailing and leading char " if any in given string
@param s : source string
@return same string without trailing/leading " char
"""
try:
return s.strip('"')
except AttributeError:
return s
class JSON(Parser):
def __init__(self, source, extractor = lambda json: json):
"""
Load JSON file data.
@param source: source file reader
@param extractor: lambda returning an interable
"""
self.source = source
self.extractor = extractor
self.json = None
def header(self):
self.json = list(map(flattenjson, self.extractor(json.loads(self.source.open().read()))))
return self.json[0].keys()
def import_(self, table, srid, osmosis):
self.json = self.json or map(flattenjson, self.extractor(json.loads(self.source.open().read())))
for row in self.json:
osmosis.giscurs.execute(u"insert into \"%s\" (\"%s\") values (%s)" %
(table, u'", "'.join(row.keys()), (u'%s, ' * len(row.keys()))[:-2]),
list(map(removequotesjson, row.values())))
class GeoJSON(Parser):
def __init__(self, source, extractor = lambda json: json):
"""
Load GeoJSON file data.
@param source: source file reader
@param extractor: lambda returning an interable
"""
self.source = source
self.extractor = extractor
self.json = None
def header(self):
self.json = self.extractor(json.loads(self.source.open().read()))
columns = set()
# Read all entries because structure can vary
for feature in self.json['features']:
columns = columns.union(list(flattenjson(feature['properties']).keys()))
columns = list(columns)
columns.append(u"geom_x")
columns.append(u"geom_y")
return columns
def import_(self, table, srid, osmosis):
self.json = self.json or self.extractor(json.loads(self.source.open().read()))
insert_statement = u"insert into %s (%%s) values %%s" % table
for row in self.json['features']:
row['properties'] = flattenjson(row['properties'])
columns = list(row['properties'].keys())
values = list(map(removequotesjson, map(lambda column: row['properties'][column], columns)))
columns.append(u"geom_x")
columns.append(u"geom_y")
values.append(row['geometry']['coordinates'][0])
values.append(row['geometry']['coordinates'][1])
osmosis.giscurs.execute(u"insert into \"%s\" (\"%s\") values (%s)" %
(table, u'", "'.join(columns), (u'%s, ' * len(columns))[:-2]),
values)
class SHP(Parser):
def __init__(self, source, edit = lambda s: s):
"""
Load Shape file data.
@param source: source file reader
@param edit: edit the SQL code produced by shp2pgsql
"""
self.source = source
self.edit = edit
def header(self):
return True
def import_(self, table, srid, osmosis):
tmp_file = tempfile.NamedTemporaryFile(delete = False)
tmp_file.close()
unzip = "unzip -o -d %s_ %s" % (tmp_file.name, self.source.path())
if os.system(unzip):
raise Exception("unzip error")
shp2pgsql = "shp2pgsql -e -k -W \"%s\" -s \"%s\" \"%s_/%s\" \"%s\" > \"%s\"" % (
self.source.encoding,
srid,
tmp_file.name,
self.source.zip,
table,
tmp_file.name
)
if os.system(shp2pgsql):
raise Exception("shp2pgsql error")
sql = self.edit(open(tmp_file.name, 'r').read()).split(";\n")
for s in sql:
if s != "":
osmosis.giscurs.execute(s)
os.remove(tmp_file.name)
class GTFS(CSV):
def __init__(self, source):
"""
Load GTFS file data.
@param source: source file reader
"""
source.zip = "stops.txt"
CSV.__init__(self, source)
class Load(object):
def __init__(self, x = ("NULL",), y = ("NULL",), srid = 4326, table_name = None, create = None,
select = {}, uniq = None, where = lambda res: True, map = lambda i: i, xFunction = lambda i: i, yFunction = lambda i: i):
"""
Describ the conversion of data set loaded with COPY into the database into an other table more usable for processing.
@param x: the name of x column, as or converted to longitude, can be a SQL expression formatted as ("SQL CODE",)
@param y: the name of y column, as or converted to latitude, can be a SQL expression formatted as ("SQL CODE",)
@param srid: the projection of x and y coordinate
@param table_name: override the default table name
@param create: the data base table description, generated by default from file header et format
@param select: dict reformatted as SQL to filter row import before conversion, prefer this as the where param
@param uniq: select distinct by column list
@param where: lambda expression taking row as dict and returning boolean to determine whether or not inserting the row into the table
@param map: lambda return a replace record
@param xFunction: lambda expression for convert x content column before reprojection, identity by default
@param yFunction: lambda expression for convert y content column before reprojection, identity by default
"""
self.x = x
self.y = y
self.srid = srid
self.table_name = table_name
self.create = create
self.select = select
self.uniq = uniq
self.where = where
self.map = map
self.xFunction = xFunction
self.yFunction = yFunction
def formatCSVSelect(self):
where = []
for k, v in self.select.items():
if isinstance(v, list):
cond = "\"%s\" IN ('%s')" % (k, "','".join(map(lambda i: i.replace("'", "''"), filter(lambda i: i is not None, v))))
if None in v:
cond = "(" + cond + " OR \"%s\" IS NULL)" % k
where.append(cond)
elif v is None or v == False:
where.append("\"%s\" IS NULL" % k)
elif v == True:
where.append("\"%s\" IS NOT NULL" % k)
elif '%' in v:
where.append("\"%s\" LIKE '%s'" % (k, v.replace("'", "''")))
else:
where.append("\"%s\" = '%s'" % (k, v.replace("'", "''")))
if where == []:
return "1=1"
else:
return " AND ".join(where)
def run(self, osmosis, parser, mapping, db_schema, default_table_base_name, time):
"""
@return if data loaded in data base
"""
table_base_name = self.table_name or default_table_base_name
if len(table_base_name) <= 63: # 63 max postgres relation name
table = table_base_name
else:
table = table_base_name[-(63-10):]+hexastablehash(table_base_name)[-10:]
osmosis.run("CREATE UNLOGGED TABLE IF NOT EXISTS meta (name character varying(255) NOT NULL, update integer, bbox character varying(1024) )")
self.data = False
def setDataTrue():
self.data=True
osmosis.run0("SELECT * FROM meta WHERE name='%s' AND update=%s" % (table, time), lambda res: setDataTrue())
if not self.data:
osmosis.logger.log(u"Load source into database")
osmosis.run("DROP TABLE IF EXISTS %s" % table)
if not self.create:
header = parser.header()
if header:
if header != True:
self.create = ",".join(map(lambda c: "\"%s\" VARCHAR(65534)" % c[0:50], header))
else:
raise AssertionError("No table schema provided")
osmosis.run(sql_schema % {"schema": db_schema})
if self.create:
osmosis.run("CREATE UNLOGGED TABLE %s (%s)" % (table, self.create))
parser.import_(table, self.srid, osmosis)
osmosis.run("DELETE FROM meta WHERE name = '%s'" % table)
osmosis.run("INSERT INTO meta VALUES ('%s', %s, NULL)" % (table, time))
osmosis.run0("COMMIT")
osmosis.run0("BEGIN")
parser.close()
# Convert
country_hash = osmosis.config.db_schema.split('_')[-1][0:10] + hexastablehash(osmosis.config.db_schema)[-4:]
if len(default_table_base_name + '_' + country_hash) <= 63-2-3: # 63 max postgres relation name, 3 is index name prefix
tableOfficial = default_table_base_name + '_' + country_hash + "_o"
else:
tableOfficial = (default_table_base_name + '_' + country_hash)[-(63-2-3-4):] + '_o' + hexastablehash(default_table_base_name)[-4:]
self.data = False
def setData(res):
self.data=res
osmosis.run0("SELECT bbox FROM meta WHERE name='%s' AND bbox IS NOT NULL AND update IS NOT NULL AND update=%s" % (tableOfficial, time), lambda res: setData(res))
if not self.data:
self.pip = PointInPolygon.PointInPolygon(self.polygon_id) if self.srid and self.polygon_id else None
if self.pip:
if Transformer:
transformer = Transformer.from_crs(self.srid, 4326)
else: # py2 conditional
transformer = None #
osmosis.logger.log(u"Convert data to tags")
osmosis.run(sql_schema % {"schema": db_schema})
osmosis.run(sql00 % {"official": tableOfficial})
giscurs = osmosis.gisconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
giscurs_getpoint = osmosis.gisconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
mult_space = re.compile(r'\s+')
def insertOfficial(res):
if not self.where(res):
return
res = self.map(res)
x = self.xFunction(res['_x'])
y = self.yFunction(res['_y'])
if not self.pip or (x and y):
is_pip = False
if self.pip:
if transformer:
lonLat = transformer.transform(x, y)
else:
giscurs_getpoint.execute("SELECT ST_AsText(ST_Transform(ST_SetSRID(ST_MakePoint(%(x)s, %(y)s), %(SRID)s), 4326))" % {"x": x, "y": y, "SRID": self.srid})
lonLat = self.osmosis.get_points(giscurs_getpoint.fetchone()[0])[0]
lonLat = [float(lonLat["lon"]), float(lonLat["lat"])]
is_pip = self.pip.point_inside_polygon(lonLat[0], lonLat[1])
if not self.pip or is_pip:
for k in res.keys():
try:
res[k] = mult_space.sub(' ', res[k].strip()) # Strip and remove duplicate space
except AttributeError:
pass
tags = mapping.generate.tagFactory(res)
tags[1].update(tags[0])
giscurs.execute(sql02.replace("%(official)s", tableOfficial), {
"ref": tags[1].get(mapping.osmRef) if mapping.osmRef != "NULL" else None,
"tags": tags[1],
"tags1": tags[0],
"fields": dict(zip(dict(res).keys(), map(lambda s: (s is None and None) or u'{0}'.format(s), dict(res).values()))),
"lon": lonLat[0] if is_pip else None, "lat": lonLat[1] if is_pip else None
})
if isinstance(self.x, tuple):
self.x = self.x[0]
else:
self.x = "\"%s\"" % self.x
if isinstance(self.y, tuple):
self.y = self.y[0]
else:
self.y = "\"%s\"" % self.y
if self.uniq:
l = ','.join(map(lambda v: '"%s"' % v, self.uniq))
distinct = "DISTINCT ON (%s)" % l
order_by = "ORDER BY %s" % l
else:
distinct = order_by = ""
osmosis.run0((sql01_ref if mapping.osmRef != "NULL" else sql01_geo) % {"table":table, "x":self.x, "y":self.y, "where":self.formatCSVSelect(), "distinct": distinct, "order_by": order_by}, insertOfficial)
osmosis.run(sql02b.replace("%(official)s", tableOfficial))
if self.srid:
giscurs.execute("SELECT ST_AsText(ST_Envelope(ST_Extent(geom::geometry))::geography) FROM %s" % tableOfficial)
self.bbox = giscurs.fetchone()[0]
else:
self.bbox = None
osmosis.run(sql03a % {"official": tableOfficial})
osmosis.run(sql03b % {"official": tableOfficial})
giscurs_getpoint.close()
giscurs.close()
osmosis.run("DELETE FROM meta WHERE name='%s'" % tableOfficial)
if self.bbox is not None:
osmosis.run("INSERT INTO meta VALUES ('%s', %s, '%s')" % (tableOfficial, time, self.bbox))
osmosis.run0("COMMIT")
osmosis.run0("BEGIN")
else:
self.bbox = self.data[0]
if not(self.srid and not self.bbox): # Abort condition
return tableOfficial
class Select:
def __init__(self, types = [], tags = {}):
"""
On witch OSM we try to join data set.
@param types: object types, array of "relations", "ways" and "nodes"
@param tags: dict of tags or array of dicts, array mean "OR"
"""
self.types = types
self.tags = tags
class Generate:
def __init__(self, missing_official_fix = True, static1 = {}, static2 = {}, mapping1 = {}, mapping2 = {}, tag_keep_multiple_values = [], text = lambda tags, fields: {}):
"""
How result error file is build.
@param missing_official_fix: boolean to generate or not new object with quickfix
@param static1: dict of primary tags apply as is
@param static2: dict of secondary tags apply as is, not checked on update process
@param mapping1: dict of primary tags, if value is string then data set column value is take, else lambda
@param mapping2: dict of secondary tags, if value is string then data set column value is take, else lambda, not checked on update process
@parem tag_keep_multiple_values: if tags already have value or multiple values just append the new one
@param text: lambda return string, describe this error
"""
self.missing_official_fix = missing_official_fix
self.static1 = static1
self.static2 = static2
self.mapping1 = mapping1
self.mapping2 = mapping2
self.tag_keep_multiple_values = tag_keep_multiple_values
self.text = text
def eval_staticGroup(self, static, analyser):
for tag, colomn in static.items():
if inspect.isfunction(colomn) or inspect.ismethod(colomn):
r = colomn(analyser)
if r:
static[tag] = r
def eval_static(self, analyser):
self.eval_staticGroup(self.static1, analyser)
self.eval_staticGroup(self.static2, analyser)
delete_tag = GENERATE_DELETE_TAG
def tagFactoryGroup(self, res, static, mapping):
tags = dict(static)
for tag, colomn in mapping.items():
if inspect.isfunction(colomn) or inspect.ismethod(colomn):
r = colomn(res)
if r:
tags[tag] = r
elif colomn and res[colomn]:
tags[tag] = res[colomn]
return dict(map(lambda kv: [kv[0], kv[1] is not None and u'{0}'.format(kv[1]) or None], tags.items()))
def tagFactory(self, res):
tags = self.tagFactoryGroup(res, self.static1, self.mapping1)
tags_secondary = self.tagFactoryGroup(res, self.static2, self.mapping2)
return [tags, tags_secondary]
class Mapping:
def __init__(self, select = Select(), osmRef = "NULL", conflationDistance = None, extraJoin = None, generate = Generate()):
"""
How data is mapped with OSM data.
@param select: fetch OSM data, see Select
@param osmRef: the osm key for join data on reference
@param conflationDistance: if no osmRef, do do conflation, use this threshold
@param extraJoin: additional key condition to join on
@param generate: build the result, see Generate
"""
self.select = select
self.osmRef = osmRef
self.conflationDistance = conflationDistance
self.extraJoin = extraJoin
self.generate = generate
class Analyser_Merge(Analyser_Osmosis):
def __init__(self, config, logger):
Analyser_Osmosis.__init__(self, config, logger)
def init(self, url, name, parser, load = Load(), mapping = Mapping()):
"""
@param url: remote URL of data source, webpage
@param name: official name of the data set
"""
self.url = url
self.name = name
self.parser = parser
self.load = load
self.mapping = mapping
if hasattr(self, 'missing_official'):
self.classs[self.missing_official['id']] = self.missing_official
else:
self.missing_official = None
if hasattr(self, 'missing_osm'):
self.classs[self.missing_osm['id']] = self.missing_osm
else:
self.missing_osm = None
if hasattr(self, 'possible_merge'):
self.classs[self.possible_merge['id']] = self.possible_merge
else:
self.possible_merge = None
if hasattr(self, 'moved_official'):
self.classs[self.moved_official['id']] = self.moved_official
else:
self.moved_official = None
if hasattr(self, 'update_official'):
self.classs[self.update_official['id']] = self.update_official
else:
self.update_official = None
for (id, c) in self.classs.items():
c['resource'] = url
if not isinstance(self.mapping.select.tags, list):
self.mapping.select.tags = [self.mapping.select.tags]
self.mapping.generate.eval_static(self)
self.load.osmosis = self
self.load.polygon_id = self.config.polygon_id
def float_comma(self, val):
if val is not None:
return float(val.replace(',', '.'))
def degree(self, val):
if val is not None and u'°' in val:
# 01°13'23,8 -> 1,334388
return reduce(lambda sum, i: sum * 60 + i, map(lambda i: float(i.replace(u',', u'.')), filter(lambda i: i != '', val.replace(u'°', u"'").split(u"'"))), 0) / 3600
else:
return val
def date_format(self, date_string, format='%d/%m/%Y'):
try:
dt = datetime.datetime.strptime(date_string, format)
return str(dt.date())
except ValueError:
return None
def source(self, a):
return a.parser.source.as_tag_value()
def analyser_version(self):
return SourceVersion.version(self.parser.source.time(), self.__class__)
def analyser_osmosis_common(self):
self.run("SET search_path TO %s" % (self.config.db_schema_path or ','.join([self.config.db_user, self.config.db_schema, 'public']),));
table = self.load.run(self, self.parser, self.mapping, self.config.db_user, self.__class__.__name__.lower()[15:], self.analyser_version())
if not table:
self.logger.log(u"Empty bbox, abort")
return
# Extract OSM objects
if self.load.srid:
typeSelect = {'N': 'geom', 'W': 'linestring', 'R': 'relation_locate(id)'}
typeGeom = {'N': 'geom', 'W': 'way_locate(linestring)', 'R': 'relation_locate(id)'}
if self.mapping.osmRef == "NULL" or self.possible_merge:
typeShape = {'N': 'geom', 'W': 'ST_Envelope(linestring)', 'R': 'relation_shape(id)'}
else:
typeShape = {'N': 'NULL', 'W': 'NULL', 'R': 'NULL'}
else:
typeSelect = {'N': 'NULL', 'W': 'NULL', 'R': 'NULL'}
typeGeom = {'N': 'NULL', 'W': 'NULL', 'R': 'NULL'}
typeShape = {'N': 'NULL', 'W': 'NULL', 'R': 'NULL'}
self.logger.log(u"Retrive OSM item")
where = "((" + (") OR (".join(map(lambda x: self.where(x), self.mapping.select.tags))) + "))"
self.run("CREATE TEMP TABLE osm_item AS " +
("UNION ALL".join(
map(lambda type:
("""(
SELECT
'%(type)s'::char(1) AS type,
id,
trim(both from ref) AS ref,
%(geom)s::geography AS geom,
%(shape)s::geography AS shape,
tags
FROM
%(from)s
LEFT JOIN LATERAL regexp_split_to_table(tags->'%(ref)s', ';') a(ref) ON true
WHERE""" + ("""
%(geomSelect)s IS NOT NULL AND""" if self.load.srid else "") + ("""
ST_SetSRID(ST_Expand(ST_GeomFromText('%(bbox)s'), %(distance)s), 4326) && %(geomSelect)s AND""" if self.load.bbox and self.load.srid else "") + """
tags != ''::hstore AND
%(where)s)""") % {"type":type[0].upper(), "ref":self.mapping.osmRef, "geomSelect":typeSelect[type[0].upper()], "geom":typeGeom[type[0].upper()], "shape":typeShape[type[0].upper()], "from":type, "bbox":self.load.bbox, "distance": self.mapping.conflationDistance or 0, "where":where},
self.mapping.select.types
)
))
)
if self.mapping.osmRef != "NULL":
self.run("CREATE INDEX osm_item_index_ref ON osm_item(ref)")
self.run("CREATE INDEX osm_item_index_shape ON osm_item USING GIST(shape)")
joinClause = []
if self.mapping.osmRef != "NULL":
joinClause.append("official.ref = osm_item.ref")
elif self.load.srid:
joinClause.append("ST_DWithin(official.geom, osm_item.shape, %s)" % self.mapping.conflationDistance)
if self.mapping.extraJoin:
joinClause.append("official.tags->'%(tag)s' = osm_item.tags->'%(tag)s'" % {"tag": self.mapping.extraJoin})
joinClause = " AND\n".join(joinClause) + "\n"
# Missing official
self.run(sql10 % {"official": table, "joinClause": joinClause})
self.run(sql11)
if self.missing_official:
self.run(sql12, lambda res: {
"class": self.missing_official['id'],
"subclass": str(stablehash64("%s%s%s"%(res[0],res[1],sorted(res[3].items())))),
"self": lambda r: [0]+r[1:],
"data": [self.node_new, self.positionAsText],
"text": self.mapping.generate.text(defaultdict(lambda:None,res[2]), defaultdict(lambda:None,res[3])),
"fix": self.passTags(res[2]) if self.mapping.generate.missing_official_fix and res[2] != {} else None,
} )
if self.mapping.osmRef != "NULL":
self.run(sql20 % {"official": table, "joinClause": joinClause})
self.run(sql21)
if self.missing_osm:
# Missing OSM
self.run(sql22, lambda res: {
"class": self.missing_osm['id'],
"data": [self.typeMapping[res[1]], None, self.positionAsText]
} )
# Invalid OSM
self.run(sql23 % {"official": table, "joinClause": joinClause}, lambda res: {
"class": self.missing_osm['id'],
"subclass": str(stablehash64(res[5])) if self.mapping.osmRef != "NULL" else None,
"data": [self.typeMapping[res[1]], None, self.positionAsText]
} )
# Possible merge
if self.possible_merge:
possible_merge_joinClause = []
possible_merge_orderBy = ""
if self.load.srid:
possible_merge_joinClause.append("ST_DWithin(missing_official.geom, missing_osm.shape, %s)" % self.mapping.conflationDistance)
possible_merge_orderBy = ", ST_Distance(missing_official.geom, missing_osm.shape) ASC"
if self.mapping.extraJoin:
possible_merge_joinClause.append("missing_official.tags->'%(tag)s' = missing_osm.tags->'%(tag)s'" % {"tag": self.mapping.extraJoin})
possible_merge_joinClause = " AND\n".join(possible_merge_joinClause) + "\n"