This repository has been archived by the owner on May 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
conform.py
1473 lines (1215 loc) · 55.6 KB
/
conform.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
# coding=ascii
from __future__ import absolute_import, division, print_function
import logging; _L = logging.getLogger('openaddr.conform')
import os
import errno
import tempfile
import mimetypes
import json
import copy
import csv
import re
from zipfile import ZipFile
from locale import getpreferredencoding
from os.path import splitext
from hashlib import sha1
from uuid import uuid4
from .sample import sample_geojson, stream_geojson
from osgeo import ogr, osr, gdal
ogr.UseExceptions()
def gdal_error_handler(err_class, err_num, err_msg):
errtype = {
gdal.CE_None:'None',
gdal.CE_Debug:'Debug',
gdal.CE_Warning:'Warning',
gdal.CE_Failure:'Failure',
gdal.CE_Fatal:'Fatal'
}
err_msg = err_msg.replace('\n',' ')
err_class = errtype.get(err_class, 'None')
_L.error("GDAL gave %s %s: %s", err_class, err_num, err_msg)
gdal.PushErrorHandler(gdal_error_handler)
# The canonical output schema for conform
OPENADDR_CSV_SCHEMA = ['LON', 'LAT', 'NUMBER', 'STREET', 'UNIT', 'CITY',
'DISTRICT', 'REGION', 'POSTCODE', 'ID', 'HASH']
# Field names for use in cached CSV files.
# We add columns to the extracted CSV with our own data with these names.
GEOM_FIELDNAME = 'OA:geom'
X_FIELDNAME, Y_FIELDNAME = 'OA:x', 'OA:y'
attrib_types = {
'street': 'OA:street',
'number': 'OA:number',
'unit': 'OA:unit',
'city': 'OA:city',
'postcode': 'OA:postcode',
'district': 'OA:district',
'region': 'OA:region',
'id': 'OA:id'
}
var_types = attrib_types.copy()
UNZIPPED_DIRNAME = 'unzipped'
geometry_types = {
ogr.wkbPoint: 'Point',
ogr.wkbPoint25D: 'Point 2.5D',
ogr.wkbLineString: 'LineString',
ogr.wkbLineString25D: 'LineString 2.5D',
ogr.wkbLinearRing: 'LinearRing',
ogr.wkbPolygon: 'Polygon',
ogr.wkbPolygon25D: 'Polygon 2.5D',
ogr.wkbMultiPoint: 'MultiPoint',
ogr.wkbMultiPoint25D: 'MultiPoint 2.5D',
ogr.wkbMultiLineString: 'MultiLineString',
ogr.wkbMultiLineString25D: 'MultiLineString 2.5D',
ogr.wkbMultiPolygon: 'MultiPolygon',
ogr.wkbMultiPolygon25D: 'MultiPolygon 2.5D',
ogr.wkbGeometryCollection: 'GeometryCollection',
ogr.wkbGeometryCollection25D: 'GeometryCollection 2.5D',
ogr.wkbUnknown: 'Unknown'
}
# extracts:
# - '123' from '123 Main St'
# - '123 1/2' from '123 1/2 Main St'
# - '123-1/2' from '123-1/2 Main St'
# - '123-1' from '123-1 Main St'
# - '123a' from '123a Main St'
# - '123-a' from '123-a Main St'
# - '' from '3rd St' (the 3 belongs to the street, it's not a house number)
#
# this regex can be optimized but number scenarios are much cleaner this way:
# - just digits with optional fractional
# - two groups of digits separated by a hyphen (for queens-style addresses, eg - 69-15 51st Ave)
# - digits and a letter, optionally separated by a hyphen
prefixed_number_pattern = re.compile("^\s*(\d+(?:[ -]\d/\d)?|\d+-\d+|\d+-?[A-Z])\s+", re.IGNORECASE)
# extracts:
# - 'Main St' from '123 Main St'
# - 'Main St' from '123 1/2 Main St'
# - 'Main St' from '123-1/2 Main St'
# - 'Main St' from '123-1 Main St'
# - 'Main St' from '123a Main St'
# - 'Main St' from '123-a Main St'
# - 'Main St' from 'Main St'
#
# like prefixed_number_pattern, this regex can be optimized but this is cleaner
postfixed_street_pattern = re.compile("^(?:\s*(?:\d+(?:[ -]\d/\d)?|\d+-\d+|\d+-?[A-Z])\s+)?(.*)", re.IGNORECASE)
# extracts:
# - 'Main Street' from '123 Main Street Unit 3'
# - 'Main Street' from '123 Main Street Apartment 3'
# - 'Main Street' from '123 Main Street Apt 3'
# - 'Main Street' from '123 Main Street Apt. 3'
# - 'Main Street' from '123 Main Street Suite 3'
# - 'Main Street' from '123 Main Street Ste 3'
# - 'Main Street' from '123 Main Street Ste. 3'
# - 'Main Street' from '123 Main Street Building 3'
# - 'Main Street' from '123 Main Street Bldg 3'
# - 'Main Street' from '123 Main Street Bldg. 3'
# - 'Main Street' from '123 Main Street Lot 3'
# - 'Main Street' from '123 Main Street #3'
# - 'Main Street' from '123 Main Street # 3'
# This regex contains 3 groups: optional house number, street, optional unit
# only street is a matching group, house number and unit are non-matching
postfixed_street_with_units_pattern = re.compile("^(?:\s*(?:\d+(?:[ -]\d/\d)?|\d+-\d+|\d+-?[A-Z])\s+)?(.+?)(?:\s+(?:(?:UNIT|APARTMENT|APT\.?|SUITE|STE\.?|BUILDING|BLDG\.?|LOT)\s+|#).+)?$", re.IGNORECASE)
# extracts:
# - 'Unit 3' from 'Main Street Unit 3'
# - 'Apartment 3' from 'Main Street Apartment 3'
# - 'Apt 3' from 'Main Street Apt 3'
# - 'Apt. 3' from 'Main Street Apt. 3'
# - 'Suite 3' from 'Main Street Suite 3'
# - 'Ste 3' from 'Main Street Ste 3'
# - 'Ste. 3' from 'Main Street Ste. 3'
# - 'Building 3' from 'Main Street Building 3'
# - 'Bldg 3' from 'Main Street Bldg 3'
# - 'Bldg. 3' from 'Main Street Bldg. 3'
# - 'Lot 3' from 'Main Street Lot 3'
# - '#3' from 'Main Street #3'
# - '# 3' from 'Main Street # 3'
postfixed_unit_pattern = re.compile("\s((?:(?:UNIT|APARTMENT|APT\.?|SUITE|STE\.?|BUILDING|BLDG\.?|LOT)\s+|#).+)$", re.IGNORECASE)
def mkdirsp(path):
try:
os.makedirs(path)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
class ConformResult:
processed = None
sample = None
license = None
geometry_type = None
address_count = None
path = None
elapsed = None
sharealike_flag = None
attribution_flag = None
attribution_name = None
def __init__(self, processed, sample, website, license, geometry_type,
address_count, path, elapsed, sharealike_flag,
attribution_flag, attribution_name):
self.processed = processed
self.sample = sample
self.website = website
self.license = license
self.geometry_type = geometry_type
self.address_count = address_count
self.path = path
self.elapsed = elapsed
self.sharealike_flag = sharealike_flag
self.attribution_flag = attribution_flag
self.attribution_name = attribution_name
@staticmethod
def empty():
return ConformResult(None, None, None, None, None, None, None, None, None, None, None)
def todict(self):
return dict(processed=self.processed, sample=self.sample)
class DecompressionError(Exception):
pass
class DecompressionTask(object):
@classmethod
def from_format_string(clz, format_string):
if format_string == None:
return GuessDecompressTask()
elif format_string.lower() == 'zip':
return ZipDecompressTask()
else:
raise KeyError("I don't know how to decompress for format {}".format(format_string))
def decompress(self, source_paths):
raise NotImplementedError()
class GuessDecompressTask(DecompressionTask):
''' Decompression task that tries to guess compression from file names.
'''
def decompress(self, source_paths, workdir, filenames):
types = {type for (type, _) in map(mimetypes.guess_type, source_paths)}
if types == {'application/zip'}:
substitute_task = ZipDecompressTask()
_L.info('Guessing zip compression based on file names')
return substitute_task.decompress(source_paths, workdir, filenames)
_L.warning('Could not guess a single compression from file names')
return source_paths
def is_in(path, names):
'''
'''
if path.lower() in names:
# Found it!
return True
for name in names:
# Maybe one of the names is an enclosing directory?
if not os.path.relpath(path.lower(), name).startswith('..'):
# Yes, that's it.
return True
return False
class ZipDecompressTask(DecompressionTask):
def decompress(self, source_paths, workdir, filenames):
output_files = []
expand_path = os.path.join(workdir, UNZIPPED_DIRNAME)
mkdirsp(expand_path)
# Extract contents of zip file into expand_path directory.
for source_path in source_paths:
with ZipFile(source_path, 'r') as z:
for name in z.namelist():
if len(filenames) and not is_in(name, filenames):
# Download only the named file, if any.
_L.debug("Skipped file {}".format(name))
continue
z.extract(name, expand_path)
# Collect names of directories and files in expand_path directory.
for (dirpath, dirnames, filenames) in os.walk(expand_path):
for dirname in dirnames:
if os.path.splitext(dirname)[-1].lower() == '.gdb':
output_files.append(os.path.join(dirpath, dirname))
_L.debug("Expanded directory {}".format(output_files[-1]))
for filename in filenames:
output_files.append(os.path.join(dirpath, filename))
_L.debug("Expanded file {}".format(output_files[-1]))
return output_files
class ExcerptDataTask(object):
''' Task for sampling three rows of data from datasource.
'''
known_types = ('.shp', '.json', '.geojson', '.csv', '.kml', '.gml', '.gdb')
def excerpt(self, source_paths, workdir, conform):
'''
Tested version from openaddr.excerpt() on master branch:
if ext == '.zip':
_L.debug('Downloading all of {cache}'.format(**extras))
with open(cachefile, 'w') as file:
for chunk in got.iter_content(1024**2):
file.write(chunk)
zf = ZipFile(cachefile, 'r')
for name in zf.namelist():
_, ext = splitext(name)
if ext in ('.shp', '.shx', '.dbf'):
with open(join(workdir, 'cache'+ext), 'w') as file:
file.write(zf.read(name))
if exists(join(workdir, 'cache.shp')):
ds = ogr.Open(join(workdir, 'cache.shp'))
else:
ds = None
elif ext == '.json':
_L.debug('Downloading part of {cache}'.format(**extras))
scheme, host, path, query, _, _ = urlparse(got.url)
if scheme in ('http', 'https'):
conn = HTTPConnection(host, 80)
conn.request('GET', path + ('?' if query else '') + query)
resp = conn.getresponse()
elif scheme == 'file':
with open(path) as rawfile:
resp = StringIO(rawfile.read(1024*1024))
else:
raise RuntimeError('Unsure what to do with {}'.format(got.url))
with open(cachefile, 'w') as file:
file.write(sample_geojson(resp, 10))
ds = ogr.Open(cachefile)
else:
ds = None
'''
encoding = conform.get('encoding')
csvsplit = conform.get('csvsplit', ',')
known_paths = ExcerptDataTask._get_known_paths(source_paths, workdir, conform, self.known_types)
if not known_paths:
# we know nothing.
return None, None
data_path = known_paths[0]
_, data_ext = os.path.splitext(data_path.lower())
# Sample a few GeoJSON features to save on memory for large datasets.
if data_ext in ('.geojson', '.json'):
data_path = ExcerptDataTask._sample_geojson_file(data_path)
format_string = conform.get('format')
# GDAL has issues with weird input CSV data, so use Python instead.
if format_string == 'csv':
return ExcerptDataTask._excerpt_csv_file(data_path, encoding, csvsplit)
ogr_data_path = normalize_ogr_filename_case(data_path)
datasource = ogr.Open(ogr_data_path, 0)
layer = datasource.GetLayer()
if not encoding:
encoding = guess_source_encoding(datasource, layer)
# GDAL has issues with non-UTF8 input CSV data, so use Python instead.
if data_ext == '.csv' and encoding not in ('utf8', 'utf-8'):
return ExcerptDataTask._excerpt_csv_file(data_path, encoding, csvsplit)
layer_defn = layer.GetLayerDefn()
fieldcount = layer_defn.GetFieldCount()
fieldnames = [layer_defn.GetFieldDefn(i).GetName() for i in range(fieldcount)]
fieldnames = [f.decode(encoding) if hasattr(f, 'decode') else f for f in fieldnames]
data_sample = [fieldnames]
for (feature, _) in zip(layer, range(5)):
row = [feature.GetField(i) for i in range(fieldcount)]
row = [v.decode(encoding) if hasattr(v, 'decode') else v for v in row]
data_sample.append(row)
if len(data_sample) < 2:
raise ValueError('Not enough rows in data source')
# Determine geometry_type from layer, sample, or give up.
if layer_defn.GetGeomType() in geometry_types:
geometry_type = geometry_types.get(layer_defn.GetGeomType(), None)
elif fieldnames[-3:] == [X_FIELDNAME, Y_FIELDNAME, GEOM_FIELDNAME]:
geometry = ogr.CreateGeometryFromWkt(data_sample[1][-1])
geometry_type = geometry_types.get(geometry.GetGeometryType(), None)
else:
geometry_type = None
return data_sample, geometry_type
@staticmethod
def _get_known_paths(source_paths, workdir, conform, known_types):
format_string = conform.get('format')
if format_string != 'csv' or 'file' not in conform:
paths = [source_path for source_path in source_paths
if os.path.splitext(source_path)[1].lower() in known_types]
# If nothing was found or named but we expect a CSV, return first file.
if not paths and format_string == 'csv' and 'file' not in conform:
return source_paths[:1]
return paths
unzipped_base = os.path.join(workdir, UNZIPPED_DIRNAME)
unzipped_paths = dict([(os.path.relpath(source_path, unzipped_base), source_path)
for source_path in source_paths])
if conform['file'] not in unzipped_paths:
return []
csv_path = ExcerptDataTask._make_csv_path(unzipped_paths.get(conform['file']))
return [csv_path]
@staticmethod
def _make_csv_path(csv_path):
_, csv_ext = os.path.splitext(csv_path.lower())
if csv_ext != '.csv':
# Convince OGR it's looking at a CSV file.
new_path = csv_path + '.csv'
os.link(csv_path, new_path)
csv_path = new_path
return csv_path
@staticmethod
def _sample_geojson_file(data_path):
# Sample a few GeoJSON features to save on memory for large datasets.
with open(data_path, 'r') as complete_layer:
temp_dir = os.path.dirname(data_path)
_, temp_path = tempfile.mkstemp(dir=temp_dir, suffix='.json')
with open(temp_path, 'w') as temp_file:
temp_file.write(sample_geojson(complete_layer, 10))
return temp_path
@staticmethod
def _excerpt_csv_file(data_path, encoding, csvsplit):
with open(data_path, 'r', encoding=encoding) as file:
input = csv.reader(file, delimiter=csvsplit)
data_sample = [row for (row, _) in zip(input, range(6))]
if len(data_sample) >= 2 and GEOM_FIELDNAME in data_sample[0]:
geom_index = data_sample[0].index(GEOM_FIELDNAME)
geometry = ogr.CreateGeometryFromWkt(data_sample[1][geom_index])
geometry_type = geometry_types.get(geometry.GetGeometryType(), None)
else:
geometry_type = None
return data_sample, geometry_type
def elaborate_filenames(filename):
''' Return a list of filenames for a single name from conform file tag.
Used to expand example.shp with example.shx, example.dbf, and example.prj.
'''
if filename is None:
return []
filename = filename.lower()
base, original_ext = splitext(filename)
if original_ext == '.shp':
return [base + ext for ext in (original_ext, '.shx', '.dbf', '.prj')]
return [filename]
def guess_source_encoding(datasource, layer):
''' Guess at a string encoding using hints from OGR and locale().
Duplicate the process used in Fiona, described and implemented here:
https://github.com/openaddresses/machine/issues/42#issuecomment-69693143
https://github.com/Toblerity/Fiona/blob/53df35dc70fb/docs/encoding.txt
https://github.com/Toblerity/Fiona/blob/53df35dc70fb/fiona/ogrext.pyx#L386
'''
ogr_recoding = layer.TestCapability(ogr.OLCStringsAsUTF8)
is_shapefile = datasource.GetDriver().GetName() == 'ESRI Shapefile'
return (ogr_recoding and 'UTF-8') \
or (is_shapefile and 'ISO-8859-1') \
or getpreferredencoding()
def find_source_path(source_definition, source_paths):
''' Figure out which of the possible paths is the actual source
'''
try:
conform = source_definition["conform"]
except KeyError:
_L.warning('Source is missing a conform object')
raise
format_string = conform.get('format')
protocol_string = source_definition.get('protocol')
if format_string in ("shapefile", "shapefile-polygon"):
# TODO this code is too complicated; see XML variant below for simpler option
# Shapefiles are named *.shp
candidates = []
for fn in source_paths:
basename, ext = os.path.splitext(fn)
if ext.lower() == ".shp":
candidates.append(fn)
if len(candidates) == 0:
_L.warning("No shapefiles found in %s", source_paths)
return None
elif len(candidates) == 1:
_L.debug("Selected %s for source", candidates[0])
return candidates[0]
else:
# Multiple candidates; look for the one named by the file attribute
if "file" not in conform:
_L.warning("Multiple shapefiles found, but source has no file attribute.")
return None
source_file_name = conform["file"]
for c in candidates:
if source_file_name == os.path.basename(c):
return c
_L.warning("Source names file %s but could not find it", source_file_name)
return None
elif format_string == "geojson" and protocol_string != "ESRI":
candidates = []
for fn in source_paths:
basename, ext = os.path.splitext(fn)
if ext.lower() in (".json", ".geojson"):
candidates.append(fn)
if len(candidates) == 0:
_L.warning("No JSON found in %s", source_paths)
return None
elif len(candidates) == 1:
_L.debug("Selected %s for source", candidates[0])
return candidates[0]
else:
_L.warning("Found more than one JSON file in source, can't pick one")
# geojson spec currently doesn't include a file attribute. Maybe it should?
return None
elif format_string == "geojson" and protocol_string == "ESRI":
# Old style ESRI conform: ESRI downloader should only give us a single cache.csv file
return source_paths[0]
elif format_string == "csv":
# Return file if it's specified, else return the first file we find
if "file" in conform:
for fn in source_paths:
# Consider it a match if the basename matches; directory names are a mess
if os.path.basename(conform["file"]) == os.path.basename(fn):
return fn
_L.warning("Conform named %s as file but we could not find it." % conform["file"])
return None
# See if a file has a CSV extension
for fn in source_paths:
if os.path.splitext(fn)[1].lower() == '.csv':
return fn
# Nothing else worked so just return the first one.
return source_paths[0]
elif format_string == "gdb":
candidates = []
for fn in source_paths:
fn = re.sub('\.gdb.*', '.gdb', fn)
basename, ext = os.path.splitext(fn)
if ext.lower() == ".gdb" and fn not in candidates:
candidates.append(fn)
if len(candidates) == 0:
_L.warning("No GDB found in %s", source_paths)
return None
elif len(candidates) == 1:
_L.debug("Selected %s for source", candidates[0])
return candidates[0]
else:
# Multiple candidates; look for the one named by the file attribute
if "file" not in conform:
_L.warning("Multiple GDBs found, but source has no file attribute.")
return None
source_file_name = conform["file"]
for c in candidates:
if source_file_name == os.path.basename(c):
return c
_L.warning("Source names file %s but could not find it", source_file_name)
return None
elif format_string == "xml":
# Return file if it's specified, else return the first .gml file we find
if "file" in conform:
for fn in source_paths:
# Consider it a match if the basename matches; directory names are a mess
if os.path.basename(conform["file"]) == os.path.basename(fn):
return fn
_L.warning("Conform named %s as file but we could not find it." % conform["file"])
return None
else:
for fn in source_paths:
_, ext = os.path.splitext(fn)
if ext == ".gml":
return fn
_L.warning("Could not find a .gml file")
return None
else:
_L.warning("Unknown source conform format %s", format_string)
return None
class ConvertToCsvTask(object):
known_types = ('.shp', '.json', '.csv', '.kml', '.gdb')
def convert(self, source_definition, source_paths, workdir):
"Convert a list of source_paths and write results in workdir"
_L.debug("Converting to %s", workdir)
# Create a subdirectory "converted" to hold results
output_file = None
convert_path = os.path.join(workdir, 'converted')
mkdirsp(convert_path)
# Find the source and convert it
source_path = find_source_path(source_definition, source_paths)
if source_path is not None:
basename, ext = os.path.splitext(os.path.basename(source_path))
dest_path = os.path.join(convert_path, basename + ".csv")
rc = conform_cli(source_definition, source_path, dest_path)
if rc == 0:
with open(dest_path) as file:
addr_count = sum(1 for line in file) - 1
# Success! Return the path of the output CSV
return dest_path, addr_count
# Conversion must have failed
return None, 0
def convert_regexp_replace(replace):
''' Convert regular expression replace string from $ syntax to slash-syntax.
Replace one kind of replacement, then call self recursively to find others.
'''
if re.search(r'\$\d+\b', replace):
# $dd* back-reference followed by a word break.
return convert_regexp_replace(re.sub(r'\$(\d+)\b', r'\\\g<1>', replace))
if re.search(r'\$\d+\D', replace):
# $dd* back-reference followed by an non-digit character.
return convert_regexp_replace(re.sub(r'\$(\d+)(\D)', r'\\\g<1>\g<2>', replace))
if re.search(r'\$\{\d+\}', replace):
# ${dd*} back-reference.
return convert_regexp_replace(re.sub(r'\$\{(\d+)\}', r'\\g<\g<1>>', replace))
return replace
def normalize_ogr_filename_case(source_path):
'''
'''
base, ext = splitext(source_path)
if ext == ext.lower():
# Extension is already lowercase, no need to do anything.
return source_path
normal_path = base + ext.lower()
if os.path.exists(normal_path):
# We appear to be on a case-insensitive filesystem.
return normal_path
os.link(source_path, normal_path)
# May need to deal with some additional files.
extras = {'.Shp': ('.Shx', '.Dbf', '.Prj'), '.SHP': ('.SHX', '.DBF', '.PRJ')}
if ext in extras:
for other_ext in extras[ext]:
if os.path.exists(base + other_ext):
os.link(base + other_ext, base + other_ext.lower())
return normal_path
def ogr_source_to_csv(source_definition, source_path, dest_path):
''' Convert a single shapefile or GeoJSON in source_path and put it in dest_path
'''
in_datasource = ogr.Open(source_path, 0)
layer_id = source_definition['conform'].get('layer', 0)
if isinstance(layer_id, int):
in_layer = in_datasource.GetLayerByIndex(layer_id)
_L.info("Converting layer %s (%s) to CSV", layer_id, repr(in_layer.GetName()))
else:
in_layer = in_datasource.GetLayerByName(layer_id)
_L.info("Converting layer %s to CSV", repr(in_layer.GetName()))
# Determine the appropriate SRS
inSpatialRef = in_layer.GetSpatialRef()
srs = source_definition["conform"].get("srs", None)
if srs is not None:
# OGR may have a projection, but use the explicit SRS instead
if srs.startswith(u"EPSG:"):
_L.debug("SRS tag found specifying %s", srs)
inSpatialRef = osr.SpatialReference()
inSpatialRef.ImportFromEPSG(int(srs[5:]))
else:
# OGR is capable of doing more than EPSG, but so far we don't need it.
raise Exception("Bad SRS. Can only handle EPSG, the SRS tag is %s", srs)
elif inSpatialRef is None:
raise Exception("No projection found for source {}".format(source_path))
# Determine the appropriate text encoding. This is complicated in OGR, see
# https://github.com/openaddresses/machine/issues/42
if in_layer.TestCapability(ogr.OLCStringsAsUTF8):
# OGR turned this to UTF 8 for us
shp_encoding = 'utf-8'
elif "encoding" in source_definition["conform"]:
shp_encoding = source_definition["conform"]["encoding"]
else:
_L.warning("No encoding given and OGR couldn't guess. Trying ISO-8859-1, YOLO!")
shp_encoding = "iso-8859-1"
_L.debug("Assuming shapefile data is encoded %s", shp_encoding)
# Get the input schema, create an output schema
in_layer_defn = in_layer.GetLayerDefn()
out_fieldnames = []
for i in range(0, in_layer_defn.GetFieldCount()):
field_defn = in_layer_defn.GetFieldDefn(i)
out_fieldnames.append(field_defn.GetName())
out_fieldnames.append(X_FIELDNAME)
out_fieldnames.append(Y_FIELDNAME)
# Set up a transformation from the source SRS to EPSG:4326
outSpatialRef = osr.SpatialReference()
outSpatialRef.ImportFromEPSG(4326)
coordTransform = osr.CoordinateTransformation(inSpatialRef, outSpatialRef)
# Write a CSV file with one row per feature in the OGR source
with open(dest_path, 'w', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=out_fieldnames)
writer.writeheader()
in_feature = in_layer.GetNextFeature()
while in_feature:
row = dict()
for i in range(0, in_layer_defn.GetFieldCount()):
field_defn = in_layer_defn.GetFieldDefn(i)
field_value = in_feature.GetField(i)
if field_defn.type is ogr.OFTString:
# Convert OGR's byte sequence strings to Python Unicode strings
field_value = in_feature.GetFieldAsBinary(i).decode(shp_encoding)
row[field_defn.GetNameRef()] = field_value
geom = in_feature.GetGeometryRef()
if geom is not None:
geom.Transform(coordTransform)
# Calculate the centroid of the geometry and write it as X and Y columns
try:
centroid = geom.Centroid()
except RuntimeError as e:
if 'Invalid number of points in LinearRing found' not in str(e):
raise
xmin, xmax, ymin, ymax = geom.GetEnvelope()
row[X_FIELDNAME] = xmin/2 + xmax/2
row[Y_FIELDNAME] = ymin/2 + ymax/2
else:
row[X_FIELDNAME] = centroid.GetX()
row[Y_FIELDNAME] = centroid.GetY()
else:
row[X_FIELDNAME] = None
row[Y_FIELDNAME] = None
writer.writerow(row)
in_feature.Destroy()
in_feature = in_layer.GetNextFeature()
in_datasource.Destroy()
def csv_source_to_csv(source_definition, source_path, dest_path):
"Convert a source CSV file to an intermediate form, coerced to UTF-8 and EPSG:4326"
_L.info("Converting source CSV %s", source_path)
# Encoding processing tag
enc = source_definition["conform"].get("encoding", "utf-8")
# csvsplit processing tag
delim = source_definition["conform"].get("csvsplit", ",")
# Extract the source CSV, applying conversions to deal with oddball CSV formats
# Also convert encoding to utf-8 and reproject to EPSG:4326 in X and Y columns
with open(source_path, 'r', encoding=enc) as source_fp:
in_fieldnames = None # in most cases, we let the csv module figure these out
# headers processing tag
if "headers" in source_definition["conform"]:
headers = source_definition["conform"]["headers"]
if (headers == -1):
# Read a row off the file to see how many columns it has
temp_reader = csv.reader(source_fp, delimiter=str(delim))
first_row = next(temp_reader)
num_columns = len(first_row)
source_fp.seek(0)
in_fieldnames = ["COLUMN%d" % n for n in range(1, num_columns+1)]
_L.debug("Synthesized header %s", in_fieldnames)
else:
# partial implementation of headers and skiplines,
# matches the sources in our collection as of January 2015
# this code handles the case for Korean inputs where there are
# two lines of headers and we want to skip the first one
assert "skiplines" in source_definition["conform"]
assert source_definition["conform"]["skiplines"] == headers
# Skip N lines to get to the real header. headers=2 means we skip one line
for n in range(1, headers):
next(source_fp)
else:
# check the source doesn't specify skiplines without headers
assert "skiplines" not in source_definition["conform"]
reader = csv.DictReader(source_fp, delimiter=delim, fieldnames=in_fieldnames)
num_fields = len(reader.fieldnames)
protocol_string = source_definition['protocol']
# Construct headers for the extracted CSV file
if protocol_string == "ESRI":
# ESRI sources: just copy what the downloader gave us. (Already has OA:x and OA:y)
out_fieldnames = list(reader.fieldnames)
else:
# CSV sources: replace the source's lat/lon columns with OA:x and OA:y
old_latlon = [source_definition["conform"]["lat"], source_definition["conform"]["lon"]]
old_latlon.extend([s.upper() for s in old_latlon])
out_fieldnames = [fn for fn in reader.fieldnames if fn not in old_latlon]
out_fieldnames.append(X_FIELDNAME)
out_fieldnames.append(Y_FIELDNAME)
# Write the extracted CSV file
with open(dest_path, 'w', encoding='utf-8') as dest_fp:
writer = csv.DictWriter(dest_fp, out_fieldnames)
writer.writeheader()
# For every row in the source CSV
row_number = 0
for source_row in reader:
row_number += 1
if len(source_row) != num_fields:
_L.debug("Skipping row. Got %d columns, expected %d", len(source_row), num_fields)
continue
try:
out_row = row_extract_and_reproject(source_definition, source_row)
except Exception as e:
_L.error('Error in row {}: {}'.format(row_number, e))
raise
else:
writer.writerow(out_row)
def geojson_source_to_csv(source_path, dest_path):
'''
'''
# For every row in the source GeoJSON
with open(source_path) as file:
# Write the extracted CSV file
with open(dest_path, 'w', encoding='utf-8') as dest_fp:
writer = None
for (row_number, feature) in enumerate(stream_geojson(file)):
if writer is None:
out_fieldnames = list(feature['properties'].keys())
out_fieldnames.extend((X_FIELDNAME, Y_FIELDNAME))
writer = csv.DictWriter(dest_fp, out_fieldnames)
writer.writeheader()
try:
row = feature['properties']
geom = ogr.CreateGeometryFromJson(json.dumps(feature['geometry']))
if not geom:
continue
center = geom.Centroid()
except Exception as e:
_L.error('Error in row {}: {}'.format(row_number, e))
raise
else:
row.update({X_FIELDNAME: center.GetX(), Y_FIELDNAME: center.GetY()})
writer.writerow(row)
_transform_cache = {}
def _transform_to_4326(srs):
"Given a string like EPSG:2913, return an OGR transform object to turn it in to EPSG:4326"
if srs not in _transform_cache:
epsg_id = int(srs[5:]) if srs.startswith("EPSG:") else int(srs)
# Manufacture a transform object if it's not in the cache
in_spatial_ref = osr.SpatialReference()
in_spatial_ref.ImportFromEPSG(epsg_id)
out_spatial_ref = osr.SpatialReference()
out_spatial_ref.ImportFromEPSG(4326)
_transform_cache[srs] = osr.CoordinateTransformation(in_spatial_ref, out_spatial_ref)
return _transform_cache[srs]
def row_extract_and_reproject(source_definition, source_row):
''' Find lat/lon in source CSV data and store it in ESPG:4326 in X/Y in the row
'''
format_string = source_definition["conform"].get('format')
protocol_string = source_definition['protocol']
# Ignore any lat/lon names for natively geographic sources.
ignore_conform_names = bool(format_string != 'csv')
# ESRI-derived source CSV is synthetic; we should ignore any lat/lon names.
ignore_conform_names |= bool(protocol_string == 'ESRI')
# Set local variables lon_name, source_x, lat_name, source_y
if ignore_conform_names:
# Use our own X_FIELDNAME convention
lat_name = Y_FIELDNAME
lon_name = X_FIELDNAME
source_x = source_row[lon_name]
source_y = source_row[lat_name]
else:
# Conforms can name the lat/lon columns from the original source data
lat_name = source_definition["conform"]["lat"]
lon_name = source_definition["conform"]["lon"]
if lon_name in source_row:
source_x = source_row[lon_name]
else:
source_x = source_row[lon_name.upper()]
if lat_name in source_row:
source_y = source_row[lat_name]
else:
source_y = source_row[lat_name.upper()]
# Prepare an output row with the source lat and lon columns deleted
out_row = copy.deepcopy(source_row)
for n in lon_name, lon_name.upper(), lat_name, lat_name.upper():
if n in out_row: del out_row[n]
# Convert commas to periods for decimal numbers. (Not using locale.)
try:
source_x = source_x.replace(',', '.')
source_y = source_y.replace(',', '.')
except AttributeError:
# Add blank data to the output CSV and get out
out_row[X_FIELDNAME] = None
out_row[Y_FIELDNAME] = None
return out_row
# Reproject the coordinates if necessary
if "srs" not in source_definition["conform"]:
out_x = source_x
out_y = source_y
else:
try:
srs = source_definition["conform"]["srs"]
source_x = float(source_x)
source_y = float(source_y)
point = ogr.Geometry(ogr.wkbPoint)
point.AddPoint_2D(float(source_x), float(source_y))
point.Transform(_transform_to_4326(srs))
out_x = "%.7f" % point.GetX()
out_y = "%.7f" % point.GetY()
except (TypeError, ValueError) as e:
if not (source_x == "" or source_y == ""):
_L.debug("Could not reproject %s %s in SRS %s", source_x, source_y, srs)
out_x = ""
out_y = ""
# Add the reprojected data to the output CSV
out_row[X_FIELDNAME] = out_x
out_row[Y_FIELDNAME] = out_y
return out_row
def row_function(sd, row, key, fxn):
function = fxn["function"]
if function == "join":
row = row_fxn_join(sd, row, key, fxn)
elif function == "regexp":
row = row_fxn_regexp(sd, row, key, fxn)
elif function == "format":
row = row_fxn_format(sd, row, key, fxn)
elif function == "prefixed_number":
row = row_fxn_prefixed_number(sd, row, key, fxn)
elif function == "postfixed_street":
row = row_fxn_postfixed_street(sd, row, key, fxn)
elif function == "postfixed_unit":
row = row_fxn_postfixed_unit(sd, row, key, fxn)
elif function == "remove_prefix":
row = row_fxn_remove_prefix(sd, row, key, fxn)
elif function == "remove_postfix":
row = row_fxn_remove_postfix(sd, row, key, fxn)
elif function == "chain":
row = row_fxn_chain(sd, row, key, fxn)
elif function == "first_non_empty":
row = row_fxn_first_non_empty(sd, row, key, fxn)
elif function == "get":
row = row_fxn_get_from_array_string(sd, row, key, fxn)
return row
def row_transform_and_convert(sd, row):
"Apply the full conform transform and extract operations to a row"
# Some conform specs have fields named with a case different from the source
row = row_smash_case(sd, row)
c = sd["conform"]
"Attribute tags can utilize processing fxns"
for k, v in c.items():
if k in attrib_types and type(v) is list:
"Lists are a concat shortcut to concat fields with spaces"
row = row_merge(sd, row, k)
if k in attrib_types and type(v) is dict:
"Dicts are custom processing functions"
row = row_function(sd, row, k, v)
if "advanced_merge" in c:
raise ValueError('Found unsupported "advanced_merge" option in conform')
if "split" in c:
raise ValueError('Found unsupported "split" option in conform')
# Make up a random fingerprint if none exists
cache_fingerprint = sd.get('fingerprint', str(uuid4()))
row2 = row_convert_to_out(sd, row)