forked from TileStache/TileStache
-
Notifications
You must be signed in to change notification settings - Fork 11
/
transform.py
772 lines (629 loc) · 24.2 KB
/
transform.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
# transformation functions to apply to features
from numbers import Number
from StreetNames import short_street_name
from collections import defaultdict
from shapely.strtree import STRtree
from shapely.geometry.base import BaseMultipartGeometry
import re
# attempts to convert x to a floating point value,
# first removing some common punctuation. returns
# None if conversion failed.
def to_float(x):
if x is None:
return None
# normalize punctuation
x = x.replace(';', '.').replace(',', '.')
try:
return float(x)
except ValueError:
return None
feet_pattern = re.compile('([+-]?[0-9.]+)\'(?: *([+-]?[0-9.]+)")?')
number_pattern = re.compile('([+-]?[0-9.]+)')
def _to_float_meters(x):
if x is None:
return None
as_float = to_float(x)
if as_float is not None:
return as_float
# trim whitespace to simplify further matching
x = x.strip()
# try explicit meters suffix
if x.endswith(' m'):
meters_as_float = to_float(x[:-2])
if meters_as_float is not None:
return meters_as_float
# try if it looks like an expression in feet via ' "
feet_match = feet_pattern.match(x)
if feet_match is not None:
feet = feet_match.group(1)
inches = feet_match.group(2)
feet_as_float = to_float(feet)
inches_as_float = to_float(inches)
total_inches = 0.0
parsed_feet_or_inches = False
if feet_as_float is not None:
total_inches = feet_as_float * 12.0
parsed_feet_or_inches = True
if inches_as_float is not None:
total_inches += inches_as_float
parsed_feet_or_inches = True
if parsed_feet_or_inches:
meters = total_inches * 0.02544
return meters
# try and match the first number that can be parsed
for number_match in number_pattern.finditer(x):
potential_number = number_match.group(1)
as_float = to_float(potential_number)
if as_float is not None:
return as_float
return None
def _coalesce(properties, *property_names):
for prop in property_names:
val = properties.get(prop)
if val:
return val
return None
def _remove_properties(properties, *property_names):
for prop in property_names:
properties.pop(prop, None)
return properties
def _building_calc_levels(levels):
levels = max(levels, 1)
levels = (levels * 3) + 2
return levels
def _building_calc_min_levels(min_levels):
min_levels = max(min_levels, 0)
min_levels = min_levels * 3
return min_levels
def _building_calc_height(height_val, levels_val, levels_calc_fn):
height = _to_float_meters(height_val)
if height is not None:
return height
levels = _to_float_meters(levels_val)
if levels is None:
return None
levels = levels_calc_fn(levels)
return levels
road_kind_highway = set(('motorway', 'motorway_link'))
road_kind_major_road = set(('trunk', 'trunk_link', 'primary', 'primary_link',
'secondary', 'secondary_link',
'tertiary', 'tertiary_link'))
road_kind_path = set(('footpath', 'track', 'footway', 'steps', 'pedestrian',
'path', 'cycleway'))
road_kind_rail = set(('rail', 'tram', 'light_rail', 'narrow_gauge',
'monorail', 'subway'))
def _road_kind(properties):
highway = properties.get('highway')
if highway in road_kind_highway:
return 'highway'
if highway in road_kind_major_road:
return 'major_road'
if highway in road_kind_path:
return 'path'
railway = properties.get('railway')
if railway in road_kind_rail:
return 'rail'
return 'minor_road'
def add_id_to_properties(shape, properties, fid, zoom):
properties['id'] = fid
return shape, properties, fid
def detect_osm_relation(shape, properties, fid, zoom):
# Assume all negative ids indicate the data was a relation. At the
# moment, this is true because only osm contains negative
# identifiers. Should this change, this logic would need to become
# more robust
if isinstance(fid, Number) and fid < 0:
properties['osm_relation'] = True
return shape, properties, fid
def remove_feature_id(shape, properties, fid, zoom):
return shape, properties, None
def building_kind(shape, properties, fid, zoom):
building = _coalesce(properties, 'building:part', 'building')
if building and building != 'yes':
kind = building
else:
kind = _coalesce(properties, 'amenity', 'shop', 'tourism')
if kind:
properties['kind'] = kind
return shape, properties, fid
def building_height(shape, properties, fid, zoom):
height = _building_calc_height(
properties.get('height'), properties.get('building:levels'),
_building_calc_levels)
if height is not None:
properties['height'] = height
else:
properties.pop('height', None)
return shape, properties, fid
def building_min_height(shape, properties, fid, zoom):
min_height = _building_calc_height(
properties.get('min_height'), properties.get('building:min_levels'),
_building_calc_min_levels)
if min_height is not None:
properties['min_height'] = min_height
else:
properties.pop('min_height', None)
return shape, properties, fid
def building_trim_properties(shape, properties, fid, zoom):
properties = _remove_properties(
properties,
'amenity', 'shop', 'tourism',
'building', 'building:part',
'building:levels', 'building:min_levels')
return shape, properties, fid
def road_kind(shape, properties, fid, zoom):
source = properties.get('source')
assert source, 'Missing source in road query'
if source == 'naturalearthdata.com':
return shape, properties, fid
properties['kind'] = _road_kind(properties)
return shape, properties, fid
def road_classifier(shape, properties, fid, zoom):
source = properties.get('source')
assert source, 'Missing source in road query'
if source == 'naturalearthdata.com':
return shape, properties, fid
highway = properties.get('highway')
tunnel = properties.get('tunnel')
bridge = properties.get('bridge')
is_link = 'yes' if highway and highway.endswith('_link') else 'no'
is_tunnel = 'yes' if tunnel and tunnel in ('yes', 'true') else 'no'
is_bridge = 'yes' if bridge and bridge in ('yes', 'true') else 'no'
properties['is_link'] = is_link
properties['is_tunnel'] = is_tunnel
properties['is_bridge'] = is_bridge
return shape, properties, fid
def road_sort_key(shape, properties, fid, zoom):
# Calculated sort value is in the range 0 to 39
sort_val = 0
# Base layer range is 15 to 24
highway = properties.get('highway', '')
railway = properties.get('railway', '')
aeroway = properties.get('aeroway', '')
if highway == 'motorway':
sort_val += 24
elif railway in ('rail', 'tram', 'light_rail', 'narrow_guage', 'monorail'):
sort_val += 23
elif highway == 'trunk':
sort_val += 22
elif highway == 'primary':
sort_val += 21
elif highway == 'secondary' or aeroway == 'runway':
sort_val += 20
elif highway == 'tertiary' or aeroway == 'taxiway':
sort_val += 19
elif highway.endswith('_link'):
sort_val += 18
elif highway in ('residential', 'unclassified', 'road', 'living_street'):
sort_val += 17
elif highway in ('unclassified', 'service', 'minor'):
sort_val += 16
else:
sort_val += 15
if zoom >= 15:
# Bridges and tunnels add +/- 10
bridge = properties.get('bridge')
tunnel = properties.get('tunnel')
if bridge in ('yes', 'true'):
sort_val += 10
elif (tunnel in ('yes', 'true') or
(railway == 'subway' and tunnel not in ('no', 'false'))):
sort_val -= 10
# Explicit layer is clipped to [-5, 5] range
layer = properties.get('layer')
if layer:
layer_float = to_float(layer)
if layer_float is not None:
layer_float = max(min(layer_float, 5), -5)
# The range of values from above is [5, 34]
# For positive layer values, we want the range to be:
# [34, 39]
if layer_float > 0:
sort_val = int(layer_float + 34)
# For negative layer values, [0, 5]
elif layer_float < 0:
sort_val = int(layer_float + 5)
properties['sort_key'] = sort_val
return shape, properties, fid
def road_trim_properties(shape, properties, fid, zoom):
properties = _remove_properties(properties, 'bridge', 'layer', 'tunnel')
return shape, properties, fid
def _reverse_line_direction(shape):
if shape.type != 'LineString':
return False
shape.coords = shape.coords[::-1]
return True
def road_oneway(shape, properties, fid, zoom):
oneway = properties.get('oneway')
if oneway in ('-1', 'reverse'):
did_reverse = _reverse_line_direction(shape)
if did_reverse:
properties['oneway'] = 'yes'
elif oneway in ('true', '1'):
properties['oneway'] = 'yes'
elif oneway in ('false', '0'):
properties['oneway'] = 'no'
return shape, properties, fid
def road_abbreviate_name(shape, properties, fid, zoom):
name = properties.get('name', None)
if not name:
return shape, properties, fid
short_name = short_street_name(name)
properties['name'] = short_name
return shape, properties, fid
def route_name(shape, properties, fid, zoom):
route_name = properties.get('route_name', '')
if route_name:
name = properties.get('name', '')
if route_name == name:
del properties['route_name']
return shape, properties, fid
def place_ne_capital(shape, properties, fid, zoom):
source = properties.get('source', '')
if source == 'naturalearthdata.com':
kind = properties.get('kind', '')
if kind == 'Admin-0 capital':
properties['capital'] = 'yes'
elif kind == 'Admin-1 capital':
properties['state_capital'] = 'yes'
return shape, properties, fid
def tags_create_dict(shape, properties, fid, zoom):
tags_hstore = properties.get('tags')
if tags_hstore:
tags = dict(tags_hstore)
properties['tags'] = tags
return shape, properties, fid
def tags_remove(shape, properties, fid, zoom):
properties.pop('tags', None)
return shape, properties, fid
tag_name_alternates = (
'int_name',
'loc_name',
'nat_name',
'official_name',
'old_name',
'reg_name',
'short_name',
)
def tags_name_i18n(shape, properties, fid, zoom):
tags = properties.get('tags')
if not tags:
return shape, properties, fid
name = properties.get('name')
if not name:
return shape, properties, fid
for k, v in tags.items():
if (k.startswith('name:') and v != name or
k.startswith('alt_name:') and v != name or
k.startswith('alt_name_') and v != name or
k.startswith('old_name:') and v != name):
properties[k] = v
for alt_tag_name_candidate in tag_name_alternates:
alt_tag_name_value = tags.get(alt_tag_name_candidate)
if alt_tag_name_value and alt_tag_name_value != name:
properties[alt_tag_name_candidate] = alt_tag_name_value
return shape, properties, fid
# creates a list of indexes, each one for a different cut
# attribute value, in priority order.
#
# STRtree stores geometries and returns these from the query,
# but doesn't appear to allow any other attributes to be
# stored along with the geometries. this means we have to
# separate the index out into several "layers", each having
# the same attribute value. which isn't all that much of a
# pain, as we need to cut the shapes in a certain order to
# ensure priority anyway.
class _Cutter:
def __init__(self, features, attrs, attribute,
target_attribute, keep_geom_type):
group = defaultdict(list)
for feature in features:
shape, props, fid = feature
attr = props.get(attribute)
group[attr].append(shape)
# if the user didn't supply any options for controlling
# the cutting priority, then just make some up based on
# the attributes which are present in the dataset.
if attrs is None:
all_attrs = set()
for feature in features:
all_attrs.add(feature[1].get(attribute))
attrs = list(all_attrs)
cut_idxs = list()
for attr in attrs:
if attr in group:
cut_idxs.append((attr, STRtree(group[attr])))
self.attribute = attribute
self.target_attribute = target_attribute
self.cut_idxs = cut_idxs
self.keep_geom_type = keep_geom_type
self.new_features = []
# cut up the argument shape, projecting the configured
# attribute to the properties of the intersecting parts
# of the shape. adds all the cut up bits to the
# new_features list.
def cut(self, shape, props, fid):
original_geom_type = type(shape)
for cutting_attr, cut_idx in self.cut_idxs:
cutting_shapes = cut_idx.query(shape)
for cutting_shape in cutting_shapes:
if cutting_shape.intersects(shape):
shape = self._intersect(
shape, props, fid, cutting_shape,
cutting_attr, original_geom_type)
# if there's no geometry left outside the
# shape, then we can exit the loop early, as
# nothing else will intersect.
if shape.is_empty:
break
# if there's still geometry left outside, then it
# keeps the old, unaltered properties.
self._add(shape, props, fid, original_geom_type)
# only keep geometries where either the type is the
# same as the original, or we're not trying to keep the
# same type.
def _add(self, shape, props, fid, original_geom_type):
if (not shape.is_empty and
(not self.keep_geom_type or
isinstance(shape, original_geom_type))):
self.new_features.append((shape, props, fid))
# if it's a multi-geometry, then split it up so
# that we can compare the types of the leaves.
# note that we compare the type first, just in
# case the original was a multi*.
elif isinstance(shape, BaseMultipartGeometry):
for geom in shape.geoms:
self._add(geom, props, fid,
original_geom_type)
# intersects the shape with the cutting shape and
# handles attribute projection. anything "inside" is
# kept as it must have intersected the highest
# priority cutting shape already. the remainder is
# returned.
def _intersect(self, shape, props, fid, cutting_shape,
cutting_attr, original_geom_type):
inside = shape.intersection(cutting_shape)
outside = shape.difference(cutting_shape)
if cutting_attr is not None:
inside_props = props.copy()
inside_props[self.target_attribute] = cutting_attr
else:
inside_props = props
self._add(inside, inside_props, fid,
original_geom_type)
return outside
# intercut takes features from a base layer and cuts each
# of them against a cutting layer, splitting any base
# feature which intersects into separate inside and outside
# parts.
#
# the parts of each base feature which are outside any
# cutting feature are left unchanged. the parts which are
# inside have their property with the key given by the
# 'target_attribute' parameter set to the same value as the
# property from the cutting feature with the key given by
# the 'attribute' parameter.
#
# the intended use of this is to project attributes from one
# layer to another so that they can be styled appropriately.
#
# - feature_layers: list of layers containing both the base
# and cutting layer.
# - base_layer: str name of the base layer.
# - cutting_layer: str name of the cutting layer.
# - attribute: optional str name of the property / attribute
# to take from the cutting layer.
# - target_attribute: optional str name of the property /
# attribute to assign on the base layer. defaults to the
# same as the 'attribute' parameter.
# - cutting_attrs: list of str, the priority of the values
# to be used in the cutting operation. this ensures that
# items at the beginning of the list get cut first and
# those values have priority (won't be overridden by any
# other shape cutting).
# - keep_geom_type: if truthy, then filter the output to be
# the same type as the input. defaults to True, because
# this seems like an eminently sensible behaviour.
#
# returns a feature layer which is the base layer cut by the
# cutting layer.
def intercut(feature_layers, base_layer, cutting_layer,
attribute, target_attribute=None,
cutting_attrs=None,
keep_geom_type=True):
base = None
cutting = None
# the target attribute can default to the attribute if
# they are distinct. but often they aren't, and that's
# why target_attribute is a separate parameter.
if target_attribute is None:
target_attribute = attribute
# search through all the layers and extract the ones
# which have the names of the base and cutting layer.
# it would seem to be better to use a dict() for
# layers, and this will give odd results if names are
# allowed to be duplicated.
for feature_layer in feature_layers:
layer_datum = feature_layer['layer_datum']
layer_name = layer_datum['name']
if layer_name == base_layer:
base = feature_layer
elif layer_name == cutting_layer:
cutting = feature_layer
# base or cutting layer not available. this could happen
# because of a config problem, in which case you'd want
# it to be reported. but also can happen when the client
# selects a subset of layers which don't include either
# the base or the cutting layer. then it's not an error.
# the interesting case is when they select the base but
# not the cutting layer...
if base is None or cutting is None:
return None
# sanity check on the availability of the cutting
# attribute.
assert attribute is not None, \
'Parameter attribute to intercut was None, but ' + \
'should have been an attribute name. Perhaps check ' + \
'your configuration file and queries.'
base_features = base['features']
cutting_features = cutting['features']
# make a cutter object to help out
cutter = _Cutter(cutting_features, cutting_attrs,
attribute, target_attribute,
keep_geom_type)
for base_feature in base_features:
# we use shape to track the current remainder of the
# shape after subtracting bits which are inside cuts.
shape, props, fid = base_feature
cutter.cut(shape, props, fid)
base['features'] = cutter.new_features
return base
# explicit order for some kinds of landuse
_landuse_sort_order = {
'aerodrome': 2,
'apron': 3,
'cemetery': 2,
'commercial': 2,
'conservation': 1,
'farm': 1,
'farmland': 1,
'forest': 1,
'golf_course': 2,
'hospital': 2,
'nature_reserve': 1,
'park': 1,
'parking': 2,
'pedestrian': 2,
'place_of_worship': 2,
'playground': 2,
'railway': 2,
'recreation_ground': 1,
'residential': 1,
'retail': 2,
'runway': 3,
'rural': 1,
'school': 2,
'stadium': 1,
'university': 2,
'urban': 1,
'zoo': 2
}
# sets a key "order" on anything with a landuse kind
# specified in the landuse sort order above. this is
# to help with maintaining a consistent order across
# post-processing steps in the server and drawing
# steps on the client.
def landuse_sort_key(shape, properties, fid, zoom):
kind = properties.get('kind')
if kind is not None:
key = _landuse_sort_order.get(kind)
if key is not None:
properties['order'] = key
return shape, properties, fid
# place kinds, as used by OSM, mapped to their rough
# scale_ranks so that we can provide a defaulted,
# non-curated scale_rank / min_zoom value.
_default_scalerank_for_place_kind = {
'locality': 13,
'isolated_dwelling': 13,
'farm': 13,
'hamlet': 12,
'neighbourhood': 12,
'village': 11,
'suburb': 10,
'quarter': 10,
'borough': 10,
'town': 8,
'city': 8,
'province': 4,
'state': 4,
'sea': 3,
'country': 0,
'ocean': 0,
'continent': 0
}
# if the feature does not have a scale_rank attribute already,
# which would have come from a curated source, then calculate
# a default one based on the kind of place it is.
def calculate_default_place_scalerank(shape, properties, fid, zoom):
# don't override an existing attribute
scalerank = properties.get('scalerank')
if scalerank is not None:
return shape, properties, fid
# base calculation off kind
kind = properties.get('kind')
if kind is None:
return shape, properties, fid
scalerank = _default_scalerank_for_place_kind.get(kind)
if scalerank is None:
return shape, properties, fid
# adjust scalerank for state / country capitals
if kind in ('city', 'town'):
if properties.get('state_capital') == 'yes':
scalerank -= 1
elif properties.get('capital') == 'yes':
scalerank -= 2
properties['scalerank'] = scalerank
return shape, properties, fid
# create a new layer from the boundaries of polygons in the
# base layer, subtracting any sections of the boundary which
# intersect other polygons.
#
# the purpose of this is to provide us a shoreline / river
# bank layer from the water layer without having any of the
# shoreline / river bank draw over the top of any of the base
# polygons.
#
# properties on the lines returned are the same as the
# polygon feature they came from.
#
# any features in feature_layers[layer] which aren't
# polygons will be ignored.
def exterior_boundaries(feature_layers, base_layer,
new_layer_name):
layer = None
# search through all the layers and extract the one
# which has the name of the base layer we were given
# as a parameter.
for feature_layer in feature_layers:
layer_datum = feature_layer['layer_datum']
layer_name = layer_datum['name']
if layer_name == base_layer:
layer = feature_layer
break
# if we failed to find the base layer then it's
# possible the user just didn't ask for it, so return
# an empty result.
if layer is None:
return None
features = layer['features']
# create an index so that we can efficiently find the
# polygons intersecting the 'current' one.
index = STRtree([f[0] for f in features])
new_features = list()
# loop through all the polygons, taking the boundary
# of each and subtracting any parts which are within
# other polygons. what remains (if anything) is the
# new feature.
for feature in features:
shape, props, fid = feature
if shape.geom_type in ('Polygon', 'MultiPolygon'):
boundary = shape.boundary
cutting_shapes = index.query(boundary)
for cutting_shape in cutting_shapes:
if cutting_shape is not shape:
boundary = boundary.difference(cutting_shape)
if not boundary.is_empty:
new_features.append((boundary, props.copy(), fid))
# make a copy of the old layer's information - it
# shouldn't matter about most of the settings, as
# post-processing is one of the last operations.
# but we need to override the name to ensure we get
# some output.
new_layer_datum = layer['layer_datum'].copy()
new_layer_datum['name'] = new_layer_name
new_layer = layer.copy()
new_layer['layer_datum'] = new_layer_datum
new_layer['features'] = new_features
new_layer['name'] = new_layer_name
return new_layer