-class NominatimID:
- """ Splits a unique identifier for places into its components.
- As place_ids cannot be used for testing, we use a unique
- identifier instead that is of the form <osmtype><osmid>[:<class>].
- """
-
- id_regex = re.compile(r"(?P<tp>[NRW])(?P<id>\d+)(:(?P<cls>\w+))?")
-
- def __init__(self, oid):
- self.typ = self.oid = self.cls = None
-
- if oid is not None:
- m = self.id_regex.fullmatch(oid)
- assert m is not None, "ID '%s' not of form <osmtype><osmid>[:<class>]" % oid
-
- self.typ = m.group('tp')
- self.oid = m.group('id')
- self.cls = m.group('cls')
-
- def __str__(self):
- if self.cls is None:
- return self.typ + self.oid
-
- return '%s%d:%s' % (self.typ, self.oid, self.cls)
-
- def table_select(self):
- """ Return where clause and parameter list to select the object
- from a Nominatim table.
- """
- where = 'osm_type = %s and osm_id = %s'
- params = [self.typ, self. oid]
-
- if self.cls is not None:
- where += ' and class = %s'
- params.append(self.cls)
-
- return where, params
-
- def get_place_id(self, cur):
- where, params = self.table_select()
- cur.execute("SELECT place_id FROM placex WHERE %s" % where, params)
- assert cur.rowcount == 1, \
- "Expected exactly 1 entry in placex for %s found %s" % (str(self), cur.rowcount)
-
- return cur.fetchone()[0]
-
-
-def assert_db_column(row, column, value, context):
- if column == 'object':
- return
-
- if column.startswith('centroid'):
- if value == 'in geometry':
- query = """SELECT ST_Within(ST_SetSRID(ST_Point({}, {}), 4326),
- ST_SetSRID('{}'::geometry, 4326))""".format(
- row['cx'], row['cy'], row['geomtxt'])
- cur = context.db.cursor()
- cur.execute(query)
- assert cur.fetchone()[0], "(Row %s failed: %s)" % (column, query)
- else:
- fac = float(column[9:]) if column.startswith('centroid*') else 1.0
- x, y = value.split(' ')
- assert Almost(float(x) * fac) == row['cx'], "Bad x coordinate"
- assert Almost(float(y) * fac) == row['cy'], "Bad y coordinate"
- elif column == 'geometry':
- geom = context.osm.parse_geometry(value, context.scene)
- cur = context.db.cursor()
- query = "SELECT ST_Equals(ST_SnapToGrid(%s, 0.00001, 0.00001), ST_SnapToGrid(ST_SetSRID('%s'::geometry, 4326), 0.00001, 0.00001))" % (
- geom, row['geomtxt'],)
- cur.execute(query)
- assert cur.fetchone()[0], "(Row %s failed: %s)" % (column, query)
- elif value == '-':
- assert row[column] is None, "Row %s" % column
- else:
- assert value == str(row[column]), \
- "Row '%s': expected: %s, got: %s" % (column, value, str(row[column]))
-
-
-################################ STEPS ##################################
-
-@given(u'the scene (?P<scene>.+)')
-def set_default_scene(context, scene):
- context.scene = scene