def test_matcher_colon(self): m = Matcher.from_sql("addr:housenumber = 1") self.assertTrue(m.matches({'addr:housenumber':'1'})) m = Matcher.from_sql("building != 'yes'") self.assertFalse(m.matches({'building':'yes'})) self.assertTrue(m.matches({'building':'no'}))
def test_matcher_binop(self): m = Matcher.from_sql("building = 'yes'") self.assertTrue(m.matches({'building':'yes'})) self.assertFalse(m.matches({'building':'no'})) m = Matcher.from_sql("building != 'yes'") self.assertFalse(m.matches({'building':'yes'})) self.assertTrue(m.matches({'building':'no'}))
def test_matcher_doublequote(self): m = Matcher.from_sql("\"addr:housenumber\" = 1") self.assertTrue(m.matches({'addr:housenumber':'1'})) m = Matcher.from_sql("\"addr:housenumber\" IN ('foo')") self.assertTrue(m.matches({'addr:housenumber':'foo'})) m = Matcher.from_sql("\"addr:housenumber\" IS NOT NULL") self.assertTrue(m.matches({'addr:housenumber':'foo'}))
class Theme: def __init__(self, name, d): self.name = name # set geometry types. self.points = False self.lines = False self.polygons = False if 'types' not in d: self.points = True self.lines = True self.polygons = True else: if 'points' in d['types']: self.points = True if 'lines' in d['types']: self.lines = True if 'polygons' in d['types']: self.polygons = True self.keys = d['select'] self.matcher = None if 'where' in d: self.matcher = Matcher(d['where'][0]) def matches(self, geom_type, tags): if geom_type == GeomType.POINT and not self.points: return False if geom_type == GeomType.LINE and not self.lines: return False if geom_type == GeomType.POLYGON and not self.polygons: return False return self.matcher.matches(tags)
def test_to_sql(self): sql = "building = 'yes'" self.assertEqual(Matcher.from_sql(sql).to_sql(),sql) sql = "building IS NOT NULL" self.assertEqual(Matcher.from_sql(sql).to_sql(),sql) sql = "building IN ('one','two')" self.assertEqual(Matcher.from_sql(sql).to_sql(),sql) sql = "building != 'yes'" self.assertEqual(Matcher.from_sql(sql).to_sql(),sql) sql = "building >= 0" self.assertEqual(Matcher.from_sql(sql).to_sql(),sql) sql = "building <= 0" self.assertEqual(Matcher.from_sql(sql).to_sql(),sql) sql = "building > 0" self.assertEqual(Matcher.from_sql(sql).to_sql(),sql) sql = "building < 0" self.assertEqual(Matcher.from_sql(sql).to_sql(),sql) sql = "building > 0 AND building < 5" self.assertEqual(Matcher.from_sql(sql).to_sql(),sql) sql = "building > 0 OR building < 5" self.assertEqual(Matcher.from_sql(sql).to_sql(),sql)
def __init__(self, name, d): self.name = name # set geometry types. self.points = False self.lines = False self.polygons = False if 'types' not in d: self.points = True self.lines = True self.polygons = True else: if 'points' in d['types']: self.points = True if 'lines' in d['types']: self.lines = True if 'polygons' in d['types']: self.polygons = True self.keys = d['select'] self.matcher = None if 'where' in d: self.matcher = Matcher(d['where'][0])
def __init__(self, name, d, default_osm_id): self.name = name # set geometry types. self.points = False self.lines = False self.polygons = False if not isinstance(d, dict): if isinstance(d, list): raise InvalidMapping( 'theme {0} must be YAML dict (types: , select:) , not list (- types, - select)' .format(name)) raise InvalidMapping('Theme value must be dict') if 'types' not in d: self.points = True self.lines = True self.polygons = True else: for t in d['types']: if t not in ['points', 'lines', 'polygons']: raise InvalidMapping( 'types: for theme {0} must be list containing one or more of: points, lines, polygons' .format(name)) if 'points' in d['types']: self.points = True if 'lines' in d['types']: self.lines = True if 'polygons' in d['types']: self.polygons = True if 'select' not in d: raise InvalidMapping('missing select: for theme {0}'.format(name)) self.keys = set(d['select']) self.osm_id = default_osm_id if 'osm_id' in self.keys: self.osm_id = True self.keys.remove('osm_id') if 'where' in d: try: if not d['where']: raise InvalidMapping( 'where: for theme {0} is invalid'.format(name)) if isinstance(d['where'], list): self.matcher = Matcher.null() for w in d['where']: self.matcher = self.matcher.union(Matcher.from_sql(w)) else: self.matcher = Matcher.from_sql(d['where']) except pyparsing.ParseException: raise InvalidMapping('Invalid SQL: {0}'.format(d['where'])) else: self.matcher = Matcher.null() for key in self.keys: self.matcher = self.matcher.union(Matcher.any(key)) extra = d.copy() if 'where' in extra: del extra['where'] if 'select' in d: del extra['select'] if 'types' in d: del extra['types'] self.extra = extra
def test_in(self): m = Matcher("building IN ('one','two')") self.assertTrue(m.matches({'building':'one'})) self.assertTrue(m.matches({'building':'two'})) self.assertFalse(m.matches({})) self.assertFalse(m.matches({'building':'three'}))
def test_matcher_is_not_null(self): m = Matcher("building IS NOT NULL") self.assertTrue(m.matches({'building':'one'})) self.assertTrue(m.matches({'building':'two'})) self.assertFalse(m.matches({}))
def test_matcher_and(self): m = Matcher("building = 'yes' AND amenity = 'bank'") self.assertFalse(m.matches({'building':'yes'})) self.assertFalse(m.matches({'amenity':'bank'}))
def test_matcher_or(self): m = Matcher("building = 'yes' OR amenity = 'bank'") self.assertTrue(m.matches({'building':'yes'})) self.assertTrue(m.matches({'amenity':'bank'})) self.assertFalse(m.matches({}))
def test_null(self): m = Matcher.null() self.assertFalse(m.matches({'building':'one'}))
def test_union(self): m = Matcher.any("building").union(Matcher.any("parking")) self.assertTrue(m.matches({'building':'one'})) self.assertTrue(m.matches({'parking':'one'}))
def test_any(self): m = Matcher.any("building"); self.assertTrue(m.matches({'building':'one'}))