Skip to content

Commit

Permalink
Merge pull request #1362 from tilezen/case-logic
Browse files Browse the repository at this point in the history
Move logic from jinja templates -> yaml
  • Loading branch information
rmarianski authored Aug 7, 2017
2 parents e2748cb + 6b150e2 commit 4eb1fbd
Show file tree
Hide file tree
Showing 17 changed files with 256 additions and 32 deletions.
7 changes: 3 additions & 4 deletions queries/ne.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,7 @@ SELECT
NULL::jsonb AS __earth_properties__,

jsonb_build_object(
'min_zoom', 4,
'area', way_area::bigint,
'area', way_area,
'kind', 'urban_area'
) AS __landuse_properties__,

Expand Down Expand Up @@ -247,7 +246,7 @@ SELECT
NULL::jsonb AS __places_properties__,

jsonb_build_object(
'min_zoom', GREATEST(scalerank, 5),
'min_zoom', scalerank,
'scalerank', scalerank,
'featurecla', featurecla,
'type', type,
Expand Down Expand Up @@ -319,7 +318,7 @@ SELECT
'boundary', true,
{% endif %}
{% if has_area %}
'area', way_area::bigint,
'area', way_area,
{% endif %}
'min_zoom', mz_water_min_zoom,
'featurecla', featurecla
Expand Down
2 changes: 1 addition & 1 deletion queries/osmdata.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ SELECT
{% filter geometry %}mz_label_placement{% endfilter %} AS __label__,
jsonb_build_object(
'source', 'openstreetmapdata.com',
'area', way_area::bigint,
'area', way_area,
'min_zoom', {{ min_zoom }}
) AS __properties__,

Expand Down
3 changes: 1 addition & 2 deletions queries/planet_osm_line.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ SELECT

CASE WHEN mz_landuse_min_zoom IS NOT NULL
THEN jsonb_build_object(
'min_zoom', mz_landuse_min_zoom,
'min_zoom', mz_landuse_min_zoom
-- NOTE: the name logic between pois and buildings exists only for polygons
'mz_is_building', CASE WHEN mz_calculate_is_building_or_part(tags->'building', tags->'building:part') THEN TRUE END
)
END AS __landuse_properties__,

Expand Down
2 changes: 0 additions & 2 deletions queries/planet_osm_point.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ SELECT

CASE WHEN mz_places_min_zoom IS NOT NULL
THEN jsonb_build_object(
'country_capital', CASE WHEN tags->'capital' = 'yes' THEN TRUE END,
'region_capital', CASE WHEN tags->'state_capital' = 'yes' THEN TRUE END,
'min_zoom', mz_places_min_zoom
)
END AS __places_properties__,
Expand Down
3 changes: 1 addition & 2 deletions queries/planet_osm_polygon.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ SELECT
CASE WHEN mz_landuse_min_zoom IS NOT NULL
THEN jsonb_build_object(
'min_zoom', mz_landuse_min_zoom,
'name', CASE WHEN mz_poi_min_zoom IS NULL THEN tags->'name' ELSE NULL END,
'mz_is_building', CASE WHEN mz_calculate_is_building_or_part(tags->'building', tags->'building:part') THEN TRUE END
'name', CASE WHEN mz_poi_min_zoom IS NULL THEN tags->'name' ELSE NULL END
)
END AS __landuse_properties__

Expand Down
13 changes: 8 additions & 5 deletions queries/wof.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
SELECT
wof_id AS __id__,
{% filter geometry %}label_position{% endfilter %} AS __geometry__,
COALESCE(to_jsonb(l10n_name), '{}'::jsonb) AS __properties__,

COALESCE(to_jsonb(l10n_name), '{}'::jsonb) ||
jsonb_build_object(
'name', name,
'source', 'whosonfirst.mapzen.com',
'mz_n_photos', n_photos,
'area', area::bigint,
'area', area,
'is_landuse_aoi', is_landuse_aoi,
'placetype', wof_np.placetype_string
) AS __properties__,

jsonb_build_object(
'min_zoom', min_zoom,
'max_zoom', max_zoom,
'is_landuse_aoi', CASE WHEN is_landuse_aoi = TRUE THEN TRUE END,
'kind', wof_np.placetype_string
'max_zoom', max_zoom
) AS __places_properties__

FROM wof_neighbourhood wof_n
Expand Down
164 changes: 163 additions & 1 deletion test/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ def test_building_kind_detail(self):
self.assertEquals('beach_hut', out_props.get('kind_detail'))
self.assertEquals('passageway', out_props.get('building_part'))

def test_area(self):
import shapely.geometry
shape = shapely.geometry.Polygon([(0, 0), (1, 1), (1, 0)])
props = dict(building='yes', area=3.14159)
meta = make_test_metadata()
out_props = self.buildings.fn(shape, props, None, meta)
area = out_props.get('area')
self.assertIsNotNone(area)
self.assertTrue(isinstance(area, int))
self.assertEquals(3, area)


class BoundariesTest(unittest.TestCase):

Expand Down Expand Up @@ -174,6 +185,16 @@ def test_ne(self):
out_props = self.earth.fn(None, props, None, meta)
self.assertEquals('earth', out_props.get('kind'))

def test_osmdata_area(self):
from tilequeue.process import make_metadata
meta = make_metadata('shp')
props = dict(area=3.14159)
out_props = self.earth.fn(None, props, None, meta)
area = out_props.get('area')
self.assertIsNotNone(area)
self.assertTrue(isinstance(area, int))
self.assertEquals(3, area)


class LanduseTest(unittest.TestCase):

Expand All @@ -190,6 +211,61 @@ def test_osm(self):
out_props = self.landuse.fn(None, props, None, meta)
self.assertEquals('scree', out_props.get('kind'))

def test_mz_is_building(self):
meta = make_test_metadata()

props = {
'leisure': 'park',
'building': 'yes'
}
out_props = self.landuse.fn(None, props, None, meta)
self.assertTrue(out_props.get('mz_is_building'))

props = {
'leisure': 'park',
'building:part': 'yes'
}
out_props = self.landuse.fn(None, props, None, meta)
self.assertTrue(out_props.get('mz_is_building'))

props = {
'leisure': 'park',
'building': 'office'
}
out_props = self.landuse.fn(None, props, None, meta)
self.assertTrue(out_props.get('mz_is_building'))

props = {
'leisure': 'park',
'building': 'no'
}
out_props = self.landuse.fn(None, props, None, meta)
self.assertIsNone(out_props.get('mz_is_building'))

props = {
'leisure': 'park',
'building:part': 'no'
}
out_props = self.landuse.fn(None, props, None, meta)
self.assertIsNone(out_props.get('mz_is_building'))

def test_ne_area(self):
from tilequeue.process import make_metadata
meta = make_metadata('ne')
props = dict(area=3.14159)
out_props = self.landuse.fn(None, props, None, meta)
area = out_props.get('area')
self.assertIsNotNone(area)
self.assertTrue(isinstance(area, int))
self.assertEquals(3, area)

def test_ne_min_zoom(self):
from tilequeue.process import make_metadata
meta = make_metadata('ne')
props = dict(featurecla='Urban area')
out_props = self.landuse.fn(None, props, None, meta)
self.assertEquals(4, out_props.get('min_zoom'))


class PlacesTest(unittest.TestCase):

Expand Down Expand Up @@ -218,6 +294,62 @@ def test_ne(self):
self.assertEquals('locality', out_props.get('kind'))
self.assertEquals('scientific_station', out_props.get('kind_detail'))

def test_wof_is_landuse_aoi(self):
from tilequeue.process import make_metadata
meta = make_metadata('wof')

props = dict(is_landuse_aoi=True)
out_props = self.places.fn(None, props, None, meta)
self.assertTrue(out_props.get('is_landuse_aoi'))

props = dict(is_landuse_aoi=False)
out_props = self.places.fn(None, props, None, meta)
self.assertIsNone(out_props.get('is_landuse_aoi'))

props = dict(is_landuse_aoi=None)
out_props = self.places.fn(None, props, None, meta)
self.assertIsNone(out_props.get('is_landuse_aoi'))

props = dict()
out_props = self.places.fn(None, props, None, meta)
self.assertIsNone(out_props.get('is_landuse_aoi'))

def test_wof_area(self):
from tilequeue.process import make_metadata
meta = make_metadata('wof')

props = dict(area=3.14159)
out_props = self.places.fn(None, props, None, meta)
area = out_props.get('area')
self.assertIsNotNone(area)
self.assertTrue(isinstance(area, int))
self.assertEquals(3, area)

props = dict(area=None)
out_props = self.places.fn(None, props, None, meta)
self.assertIsNone(out_props.get('area'))

def test_wof_kind(self):
from tilequeue.process import make_metadata
meta = make_metadata('wof')

props = dict(placetype='neighbourhood')
out_props = self.places.fn(None, props, None, meta)
self.assertEquals('neighbourhood', out_props.get('kind'))

def test_capital(self):
meta = make_test_metadata()

props = dict(place='country', name='foo',
capital='yes', state_capital='yes')
out_props = self.places.fn(None, props, None, meta)
self.assertTrue(out_props.get('country_capital'))
self.assertTrue(out_props.get('region_capital'))

props = dict(place='state', name='foo', state_capital='no')
out_props = self.places.fn(None, props, None, meta)
self.assertIsNone(out_props.get('region_capital'))


class PoisTest(unittest.TestCase):

Expand Down Expand Up @@ -248,6 +380,15 @@ def test_no_name_none(self):
out_props = self.pois.fn(None, props, None, meta)
self.assertIsNone(out_props.get('kind'))

def test_area(self):
props = dict(name='foo', leisure='park', area=3.14159)
meta = make_test_metadata()
out_props = self.pois.fn(None, props, None, meta)
area = out_props.get('area')
self.assertIsNotNone(area)
self.assertTrue(isinstance(area, int))
self.assertEquals(3, area)


class RoadsTest(unittest.TestCase):

Expand All @@ -270,12 +411,13 @@ def test_ne(self):
props = {
'featurecla': 'Road',
'type': 'Road',
'scalerank': 42,
'scalerank': 2,
}
meta = make_test_metadata()
out_props = self.roads.fn(None, props, None, meta)
self.assertEquals('major_road', out_props.get('kind'))
self.assertEquals('secondary', out_props.get('kind_detail'))
self.assertEquals(5, out_props.get('min_zoom'))


class TransitTest(unittest.TestCase):
Expand Down Expand Up @@ -318,6 +460,26 @@ def test_ne(self):
out_props = self.water.fn(None, props, None, meta)
self.assertEquals('lake', out_props.get('kind'))

def test_ne_area(self):
from tilequeue.process import make_metadata
meta = make_metadata('ne')
props = dict(featurecla='Lake', area=3.14159)
out_props = self.water.fn(None, props, None, meta)
area = out_props.get('area')
self.assertIsNotNone(area)
self.assertTrue(isinstance(area, int))
self.assertEquals(3, area)

def test_osmdata_area(self):
from tilequeue.process import make_metadata
meta = make_metadata('shp')
props = dict(area=3.14159)
out_props = self.water.fn(None, props, None, meta)
area = out_props.get('area')
self.assertIsNotNone(area)
self.assertTrue(isinstance(area, int))
self.assertEquals(3, area)


class LanduseMinZoomTest(unittest.TestCase):

Expand Down
4 changes: 1 addition & 3 deletions vectordatasource/meta/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ def parse_lookup(ast_state, l):
def parse_func(ast_state, name, m):
assert isinstance(m, list)

args = []
for arg in m:
args.append(ast_value(ast_state, arg))
args = [ast_value(ast_state, x) for x in m]

return ast.Call(
ast.Name(name, ast.Load()),
Expand Down
4 changes: 4 additions & 0 deletions vectordatasource/meta/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@
'mz_get_min_zoom_highway_level_gate': 'mz_get_min_zoom_highway_level_gate',
'util.calculate_path_major_route': 'mz_calculate_path_major_route',
'mz_calculate_ferry_level': 'mz_calculate_ferry_level',
'util.is_building': 'mz_calculate_is_building_or_part',
'util.tag_str_to_bool': 'notimplemented',
'util.safe_int': 'notimplemented',
'util.true_or_none': 'notimplemented',
}


Expand Down
24 changes: 24 additions & 0 deletions vectordatasource/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,27 @@ def calculate_path_major_route(fid):
def cycling_network(tags, fid):
# TODO: implement me! current implementation is a stub.
return None


def tag_str_to_bool(str_or_none):
return True if str_or_none in ('yes', 'true') else None


def true_or_none(x):
return True if x is True else None


def tag_set_and_not_no(x):
return x is not None and x != 'no'


def is_building(building_tag, building_part_tag):
result = (tag_set_and_not_no(building_tag) or
tag_set_and_not_no(building_part_tag))
return true_or_none(result)


def safe_int(x):
if x is None:
return None
return int(x)
5 changes: 4 additions & 1 deletion yaml/buildings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
# content of globals is ignored, unless included in later parts of the file.
globals:
- &output_properties
area: {col: area}
area:
call:
func: util.safe_int
args: [ { col: area } ]
label_placement: {col: label_placement}
min_zoom: {col: min_zoom}
mz_label_placement: {col: mz_label_placement}
Expand Down
5 changes: 4 additions & 1 deletion yaml/earth.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
globals:
- &output_properties
name: {col: name}
area: {col: area}
area:
call:
func: util.safe_int
args: [ { col: area } ]
source: {col: source}
min_zoom: {col: min_zoom}
mz_label_placement: {col: mz_label_placement}
Expand Down
Loading

0 comments on commit 4eb1fbd

Please sign in to comment.