def test_placekey_distance(self): """ Test distance computation between two Placekeys """ self.assertEqual( pk.placekey_distance(pk.geo_to_placekey(0.0, 0.0), pk.geo_to_placekey(0.0, 0.0)), 0.0, "identical points have distance 0") for i, sample in enumerate(self.distance_samples): difference = abs( pk.placekey_distance(sample['placekey_1'], sample['placekey_2']) - (sample['distance'] * 1000)) self.assertLessEqual(difference, 100, "distances too far apart ({})".format(i))
def test_geo_to_placekey(self): """ Test geo to Placekey conversion """ for row in self.sample: self.assertEqual( pk.geo_to_placekey(row['lat'], row['long']), row['placekey'], "converted geo ({}, {}) did not match placekey ({})".format( row['lat'], row['long'], row['placekey']))
def test_polygon_to_placekeys(self): """ Test generation of placekeys that intersect a polygon """ # Polygon is identical to the boundary of a single Placekey geo = (51.509865, -0.118092) # London poly = pk.placekey_to_polygon(pk.geo_to_placekey(*geo)) keys_no_touching = pk.polygon_to_placekeys(poly, include_touching=False) keys_touching = pk.polygon_to_placekeys(poly, include_touching=True) self.assertCountEqual(keys_no_touching['interior'], ['@4hh-zvh-66k'], "interior plackeys don't match") self.assertCountEqual(keys_no_touching['boundary'], (), "boundary plackeys don't match (no touching)") self.assertCountEqual(keys_touching['boundary'], ('@4hh-zvh-649', '@4hh-zvh-ffz', '@4hh-zvh-fs5', '@4hh-zvh-6hq', '@4hh-zvh-gx5', '@4hh-zvh-6c5'), "boundary plackeys don't match (touching)") self.assertCountEqual(keys_no_touching['interior'], keys_touching['interior'], "allow_touching flag doesn't impact interior") # Polygon contains no Placekey hexagons (it is a shrunk and translated # resolution 10 hexagon) poly = wkt_loads('POLYGON ((40.74001974100619 -73.9349274413473, ' '40.73989965751763 -73.93565632113229, ' '40.73939022508482 -73.93588138346178, ' '40.73900088156213 -73.93537758083224, ' '40.73912096147669 -73.93464871779295, ' '40.73963038848786 -73.93442364063776, ' '40.74001974100619 -73.9349274413473))') keys = pk.polygon_to_placekeys(poly) self.assertEqual(keys['interior'], (), "no interior placekeys") self.assertCountEqual( keys['boundary'], ('@627-s8p-vmk', '@627-s8p-xyv', '@627-s8p-xt9', '@627-s8p-vfz'), "boundary placekey sets not equal") # This tests the WKT and GeoJSON wrappers geo = (41.2565, 95.9345) poly = pk.placekey_to_polygon(pk.geo_to_placekey(*geo)).buffer( 0.01) # (lat, long)-tuples poly_keys = pk.polygon_to_placekeys(poly, include_touching=False, geo_json=False) wkt_keys = pk.wkt_to_placekeys(poly.wkt, include_touching=False, geo_json=False) # GeoJSON uses (long, lat) tuples. We'll test doing it both ways with the geo_json parameter. conformant_geojson_keys = pk.geojson_to_placekeys( transform(lambda lat, long: (long, lat), poly), include_touching=False, geo_json=True) non_conformant_geojson_keys = pk.geojson_to_placekeys( poly, include_touching=False, geo_json=False) self.assertCountEqual( poly_keys['interior'], wkt_keys['interior'], "poly and wkt conversions' interiors don't match") self.assertCountEqual( poly_keys['interior'], conformant_geojson_keys['interior'], "poly and conformant geojson conversions' interiors don't match") self.assertCountEqual( poly_keys['interior'], non_conformant_geojson_keys['interior'], "poly and non-conformant geojson conversions' interiors don't match" ) self.assertCountEqual( poly_keys['boundary'], wkt_keys['boundary'], "poly and wkt conversions' interiors don't match") self.assertCountEqual( poly_keys['boundary'], conformant_geojson_keys['boundary'], "poly and conformant geojson conversions' interiors don't match") self.assertCountEqual( poly_keys['boundary'], non_conformant_geojson_keys['boundary'], "poly and non-conformant geojson conversions' interiors don't match" )