Example #1
0
def test_geokeys():
    "Test geo hash keys returned for various coordinates."

    init_geohash(_GHKEYLENGTH, _SCALEFACTOR)
    expected = [
        (0.0, 0.0, 's0000'),
        (89, 0.0, 'upb42'),
        (89.999999999999992, 0.0, 'upbpb'), # Max lat supported.
        (-90, 0.0, 'h0000'),
        (-90, -180, '00000'),
        (-90, +180, '00000'),
        (-90, +90, 'n0000'),
        (-90, -90, '40000'),
        (-45, -45, '70000'),
        (-45, 45, 'm0000'),
        (45, 45, 'v0000'),
        (45, -45, 'g0000')
        ]
        
    for (lat, lon, ghkey) in expected:
        elem = new_osm_element(C.NODE, '0')
        elem[C.LAT] = lat * _SCALEFACTOR
        elem[C.LON] = lon * _SCALEFACTOR
        res = geohash_key_for_element(elem)

        assert res == ghkey
Example #2
0
    def add(self, elem):
        '''Add information about a node 'elem' to the geo table.

        Usage:
        >>> gt = GeoGroupTable()
        >>> gt = gt.add(elem)

        The node 'elem' should have a 'lat' and 'lon' fields that
        encode its latitude and longitude respectively.  The 'id'
        field specifies the node's "id".
        '''

        assert elem.namespace == C.NODE, "elem is not a node: %s" % str(elem)

        # Determine the geo-key for the node.
        ghkey = geohash_key_for_element(elem)
        # Retrieve the partition covering this location.
        ghdoc = self.geodb[ghkey]

        elemid = elem.id
        if elemid not in ghdoc:
            ghdoc.add(elem)
            self.lru[ghkey] = ghdoc