From 7736f738f72a78349ac9f3857437901141bfe8a5 Mon Sep 17 00:00:00 2001 From: Robert Marianski Date: Thu, 8 Nov 2018 11:14:01 -0800 Subject: [PATCH] Merge water lines --- integration-test/1135-water-lines-merge.py | 116 +++++++++++++++++++++ integration-test/__init__.py | 24 +++++ queries.yaml | 7 ++ 3 files changed, 147 insertions(+) create mode 100644 integration-test/1135-water-lines-merge.py diff --git a/integration-test/1135-water-lines-merge.py b/integration-test/1135-water-lines-merge.py new file mode 100644 index 000000000..da9197fd2 --- /dev/null +++ b/integration-test/1135-water-lines-merge.py @@ -0,0 +1,116 @@ +from . import FixtureTest + + +class WaterLinesMergeTest(FixtureTest): + + def test_successful_merge(self): + from ModestMaps.Core import Coordinate + from shapely.geometry import LineString + from tilequeue.tile import coord_to_bounds + import dsl + + z, x, y = 9, 145, 201 + + bounds = coord_to_bounds(Coordinate(zoom=z, column=x, row=y)) + mid_x = (bounds[2] - bounds[0]) / 2.0 + bounds[0] + mid_y = (bounds[3] - bounds[1]) / 2.0 + bounds[1] + ls1 = LineString([(mid_x-0.01, mid_y-0.01), (mid_x, mid_y)]) + ls2 = LineString([(mid_x, mid_y), (mid_x+0.01, mid_y+0.01)]) + props = dict(waterway=u'river', name=u'foo') + self.generate_fixtures( + dsl.way(1, ls1, props), + dsl.way(2, ls2, props), + ) + + self.assert_n_matching_features( + z, x, y, 'water', { + 'name': 'foo', + 'kind': 'river', + 'label_placement': type(None), + }, 1) + + with self.features_in_tile_layer(z, x, y, 'water') as features: + for f in features: + if 'label_placement' in f['properties']: + continue + assert f['geometry']['type'] == 'LineString' + assert len(f['geometry']['coordinates']) == 2 + + def test_unsuccessful_merge_same_props(self): + from ModestMaps.Core import Coordinate + from shapely.geometry import LineString + from tilequeue.tile import coord_to_bounds + import dsl + + z, x, y = 9, 145, 201 + + bounds = coord_to_bounds(Coordinate(zoom=z, column=x, row=y)) + mid_x = (bounds[2] - bounds[0]) / 2.0 + bounds[0] + mid_y = (bounds[3] - bounds[1]) / 2.0 + bounds[1] + ls1 = LineString([ + (bounds[0]+0.1, bounds[1]+0.1), + (mid_x-0.1, mid_y-0.1), + ]) + ls2 = LineString([ + (mid_x+0.1, mid_y+0.1), + (bounds[2]-0.1, bounds[2]-0.1), + ]) + props = dict(waterway=u'river', name=u'foo') + self.generate_fixtures( + dsl.way(1, ls1, props), + dsl.way(2, ls2, props), + ) + + self.assert_n_matching_features( + z, x, y, 'water', { + 'name': 'foo', + 'kind': 'river', + 'label_placement': type(None), + }, 1) + + with self.features_in_tile_layer(z, x, y, 'water') as features: + for f in features: + if 'label_placement' in f['properties']: + continue + assert f['geometry']['type'] == 'MultiLineString' + multi_coords = f['geometry']['coordinates'] + assert len(multi_coords) == 2 + assert len(multi_coords[0]) == 2 + assert len(multi_coords[1]) == 2 + + def test_unsuccessful_merge_diff_props(self): + from ModestMaps.Core import Coordinate + from shapely.geometry import LineString + from tilequeue.tile import coord_to_bounds + import dsl + + z, x, y = 9, 145, 201 + + bounds = coord_to_bounds(Coordinate(zoom=z, column=x, row=y)) + mid_x = (bounds[2] - bounds[0]) / 2.0 + bounds[0] + mid_y = (bounds[3] - bounds[1]) / 2.0 + bounds[1] + ls1 = LineString([ + (bounds[0]+0.1, bounds[1]+0.1), + (mid_x-0.1, mid_y-0.1), + ]) + ls2 = LineString([ + (mid_x+0.1, mid_y+0.1), + (bounds[2]-0.1, bounds[2]-0.1), + ]) + self.generate_fixtures( + dsl.way(1, ls1, dict(waterway=u'river', name=u'foo')), + dsl.way(2, ls2, dict(waterway=u'river', name=u'bar')), + ) + + self.assert_n_matching_features( + z, x, y, 'water', { + 'kind': 'river', + 'label_placement': type(None), + }, 2) + + with self.features_in_tile_layer(z, x, y, 'water') as features: + for f in features: + if 'label_placement' in f['properties']: + continue + assert f['geometry']['type'] == 'LineString' + assert len(f['geometry']['coordinates']) == 2 diff --git a/integration-test/__init__.py b/integration-test/__init__.py index f9f34cf2b..d76768602 100644 --- a/integration-test/__init__.py +++ b/integration-test/__init__.py @@ -1194,6 +1194,18 @@ def assert_no_matching_feature(self, z, x, y, layer, properties): "layer %r, but was supposed to find none. For example: " "%r" % (properties, layer, feature['properties'])) + def assert_n_matching_features(self, z, x, y, layer, properties, n): + with self.ff.features_in_tile_layer(z, x, y, layer) as features: + num_features, num_matching = count_matching( + features, properties) + + if num_matching != n: + self.test.fail( + "Found %d features matching properties %r in " + "layer %r, but was supposed to find %d. " + "Found %d total features." % + (num_matching, properties, layer, n, num_features)) + def assert_feature_geom_type(self, z, x, y, layer, feature_id, exp_geom_type): with self.ff.features_in_tile_layer(z, x, y, layer) as features: @@ -1295,6 +1307,9 @@ def assert_has_feature(self, z, x, y, layer, props): def assert_no_matching_feature(self, z, x, y, layer, props): self.assertions.assert_no_matching_feature(z, x, y, layer, props) + def assert_n_matching_features(self, z, x, y, layer, props, n): + self.assertions.assert_n_matching_features(z, x, y, layer, props, n) + def assert_feature_geom_type(self, z, x, y, layer, feature_id, exp_geom_type): self.assertions.assert_feature_geom_type( @@ -1342,6 +1357,9 @@ def assert_has_feature(self, z, x, y, layer, props): def assert_no_matching_feature(self, z, x, y, layer, props): pass + def assert_n_matching_features(self, z, x, y, layer, props, n): + pass + def assert_feature_geom_type(self, z, x, y, layer, feature_id, exp_geom_type): pass @@ -1388,6 +1406,9 @@ def assert_has_feature(self, z, x, y, layer, props): def assert_no_matching_feature(self, z, x, y, layer, props): self._add_tile(z, x, y) + def assert_n_matching_features(self, z, x, y, layer, props, n): + self._add_tile(z, x, y) + def assert_feature_geom_type(self, z, x, y, layer, feature_id, exp_geom_type): self._add_tile(z, x, y) @@ -1442,6 +1463,9 @@ def assert_has_feature(self, z, x, y, layer, props): def assert_no_matching_feature(self, z, x, y, layer, props): self.test_instance.assert_no_matching_feature(z, x, y, layer, props) + def assert_n_matching_features(self, z, x, y, layer, props, n): + self.test_instance.assert_n_matching_features(z, x, y, layer, props, n) + def assert_feature_geom_type(self, z, x, y, layer, feature_id, exp_geom_type): self.test_instance.assert_feature_geom_type( diff --git a/queries.yaml b/queries.yaml index 2cdec963c..5039af519 100644 --- a/queries.yaml +++ b/queries.yaml @@ -719,6 +719,13 @@ post_process: params: source_layer: water + # merge line water polygons + - fn: vectordatasource.transform.merge_line_features + params: + source_layer: water + start_zoom: 8 + end_zoom: 15 + - fn: vectordatasource.transform.generate_address_points params: source_layer: buildings