示例#1
0
def test_new_york_bounding_box_in_km():
    # se_lat, nw_lon, nw_lat, se_lon
    radius = 1000
    expected = (31.729247, -85.893746, 49.695552, -62.118053)
    actual   = distance.bounding_box(NEW_YORK[1], NEW_YORK[0], radius, False)
    nw_apart = distance.haversine(expected[1], expected[2], actual[1], actual[2])
    se_apart = distance.haversine(expected[3], expected[0], actual[3], actual[0])
    tools.assert_true(nw_apart/radius < 0.01)
    tools.assert_true(se_apart/radius < 0.01)
示例#2
0
def test_chicago_bounding_box_in_mi():
    # se_lat, nw_lon, nw_lat, se_lon
    radius = 25
    expected = (41.475476, -88.169804, 42.198323, -87.199595)
    actual   = distance.bounding_box(CHICAGO[1], CHICAGO[0], radius, True)
    nw_apart = distance.haversine(expected[1], expected[2], actual[1], actual[2])
    se_apart = distance.haversine(expected[3], expected[0], actual[3], actual[0])
    tools.assert_true(nw_apart/radius < 0.01)
    tools.assert_true(se_apart/radius < 0.01)
示例#3
0
def test_new_york_bounding_box_in_km():
    # se_lat, nw_lon, nw_lat, se_lon
    radius = 1000
    expected = (31.729247, -85.893746, 49.695552, -62.118053)
    actual = distance.bounding_box(NEW_YORK[1], NEW_YORK[0], radius, False)
    nw_apart = distance.haversine(expected[1], expected[2], actual[1],
                                  actual[2])
    se_apart = distance.haversine(expected[3], expected[0], actual[3],
                                  actual[0])
    tools.assert_true(nw_apart / radius < 0.01)
    tools.assert_true(se_apart / radius < 0.01)
示例#4
0
def test_chicago_bounding_box_in_mi():
    # se_lat, nw_lon, nw_lat, se_lon
    radius = 25
    expected = (41.475476, -88.169804, 42.198323, -87.199595)
    actual = distance.bounding_box(CHICAGO[1], CHICAGO[0], radius, True)
    nw_apart = distance.haversine(expected[1], expected[2], actual[1],
                                  actual[2])
    se_apart = distance.haversine(expected[3], expected[0], actual[3],
                                  actual[0])
    tools.assert_true(nw_apart / radius < 0.01)
    tools.assert_true(se_apart / radius < 0.01)
示例#5
0
def solve_using_set(radius, in_miles=True):
    zip_codes = set(zip_code.load())
    results = []
    while len(zip_codes) > 0:
        z = zip_codes.pop()
        d = lambda x: distance.haversine(z.lon, z.lat, x.lon, x.lat, in_miles) <= radius
        zip_codes.difference_update(filter(d, zip_codes))
        results.append(z)
    return results
示例#6
0
def solve_using_set(radius, in_miles=True):
    zip_codes = set(zip_code.load())
    results = []
    while len(zip_codes) > 0:
        z = zip_codes.pop()
        d = lambda x: distance.haversine(z.lon, z.lat, x.lon, x.lat, in_miles) <= radius
        zip_codes.difference_update(filter(d, zip_codes))
        results.append(z)
    return results
示例#7
0
    def search(self, zip_code, radius, in_miles=True, rectangle=None):
        """Search the quad-tree for US postal codes within a given
        radius of the argument US postal code.

        Args:
            zip_code (ZipCode): the center of the search
            radius (numeric): the maximum distance from zip_code
            in_miles (bool): whether the radius is in miles (True)
                otherwise kilometers (False)
            rectangle (Rectangle): the bounding box of the search
                represented as a circle on the surface of a sphere.
                internal use only.

        Returns:
            list (ZipCode): all US postal codes within radius of the
                argument US postal code

        """
        if rectangle is None:
            se_lat, nw_lon, nw_lat, se_lon = distance.bounding_box(
                zip_code.lon, zip_code.lat, radius, in_miles)
            rectangle = Rectangle(nw=Point(lat=nw_lat, lon=nw_lon),
                                  se=Point(lat=se_lat, lon=se_lon))
        results = [
            z for z in self.zip_codes if distance.haversine(
                zip_code.lon, zip_code.lat, z.lon, z.lat, in_miles) <= radius
        ]
        if self.nw is not None:
            if intersect(self.nw.rectangle, rectangle):
                results.extend(
                    self.nw.search(zip_code, radius, in_miles, rectangle))
        if self.ne is not None:
            if intersect(self.ne.rectangle, rectangle):
                results.extend(
                    self.ne.search(zip_code, radius, in_miles, rectangle))
        if self.se is not None:
            if intersect(self.se.rectangle, rectangle):
                results.extend(
                    self.se.search(zip_code, radius, in_miles, rectangle))
        if self.sw is not None:
            if intersect(self.sw.rectangle, rectangle):
                results.extend(
                    self.sw.search(zip_code, radius, in_miles, rectangle))
        return results
示例#8
0
    def search(self, zip_code, radius, in_miles=True, rectangle=None):
        """Search the quad-tree for US postal codes within a given
        radius of the argument US postal code.

        Args:
            zip_code (ZipCode): the center of the search
            radius (numeric): the maximum distance from zip_code
            in_miles (bool): whether the radius is in miles (True)
                otherwise kilometers (False)
            rectangle (Rectangle): the bounding box of the search
                represented as a circle on the surface of a sphere.
                internal use only.

        Returns:
            list (ZipCode): all US postal codes within radius of the
                argument US postal code

        """
        if rectangle is None:
            se_lat, nw_lon, nw_lat, se_lon = distance.bounding_box(zip_code.lon, zip_code.lat, radius, in_miles)
            rectangle = Rectangle(
                nw=Point(lat=nw_lat, lon=nw_lon),
                se=Point(lat=se_lat, lon=se_lon))
        results = [z for z in self.zip_codes
                   if distance.haversine(zip_code.lon, zip_code.lat, z.lon, z.lat, in_miles) <= radius]
        if self.nw is not None:
            if intersect(self.nw.rectangle, rectangle):
                results.extend(self.nw.search(zip_code, radius, in_miles, rectangle))
        if self.ne is not None:
            if intersect(self.ne.rectangle, rectangle):
                results.extend(self.ne.search(zip_code, radius, in_miles, rectangle))
        if self.se is not None:
            if intersect(self.se.rectangle, rectangle):
                results.extend(self.se.search(zip_code, radius, in_miles, rectangle))
        if self.sw is not None:
            if intersect(self.sw.rectangle, rectangle):
                results.extend(self.sw.search(zip_code, radius, in_miles, rectangle))
        return results
示例#9
0
def test_chicago_to_new_york_in_km():
    expected = 1148
    actual = distance.haversine(CHICAGO[1], CHICAGO[0], NEW_YORK[1],
                                NEW_YORK[0], False)
    tools.assert_equal(int(actual), expected)
示例#10
0
def test_new_york_to_chicago_in_mi():
    expected = 713
    actual = distance.haversine(NEW_YORK[1], NEW_YORK[0], CHICAGO[1],
                                CHICAGO[0], True)
    tools.assert_equal(int(actual), expected)
示例#11
0
def test_chicago_to_new_york_in_km():
    expected = 1148
    actual = distance.haversine(CHICAGO[1], CHICAGO[0], NEW_YORK[1], NEW_YORK[0], False)
    tools.assert_equal(int(actual), expected)
示例#12
0
def test_new_york_to_chicago_in_mi():
    expected = 713
    actual = distance.haversine(NEW_YORK[1], NEW_YORK[0], CHICAGO[1], CHICAGO[0], True)
    tools.assert_equal(int(actual), expected)