Example #1
0
    def test_df_rows_order(self, mock_request):

        geocoding_result = Regions([
            self.foo.set_query('').build_geocoded(),
            self.bar.set_query('').build_geocoded(),
            self.baz.set_query('').build_geocoded(),
        ])

        mock_request.return_value = make_success_response() \
            .set_geocoded_features(
            [
                self.foo.set_query(self.bar_id).set_centroid(GeoPoint(0, 1)).build_geocoded(),
                self.bar.set_query(self.foo_id).set_centroid(GeoPoint(0, 1)).build_geocoded(),
                self.baz.set_query(self.baz_id).set_centroid(GeoPoint(0, 1)).build_geocoded(),
            ]
        ).build()

        df = geocoding_result.centroids()

        mock_request.assert_called_with(
            RequestBuilder() \
                .set_request_kind(RequestKind.explicit) \
                .set_ids([self.foo.id, self.bar.id, self.baz.id]) \
                .set_requested_payload([PayloadKind.centroids]) \
                .build()
        )

        assert [self.foo.name, self.bar.name,
                self.baz.name] == df[DF_REQUEST].tolist()
Example #2
0
    def test_df_rows_duplication_should_be_processed_correctly(
            self, mock_request):
        foo_id = '123'
        foo_name = 'foo'

        bar_id = '234'
        bar_name = 'bar'

        geocoding_result = Regions([
            self.foo.set_query('').build_geocoded(),
            self.bar.set_query('').build_geocoded(),
            self.foo.set_query('').build_geocoded()
        ])

        mock_request.return_value = make_success_response() \
            .set_geocoded_features(
            [
                self.foo.set_query(foo_id).set_centroid(GeoPoint(0, 1)).build_geocoded(),
                self.bar.set_query(bar_id).set_centroid(GeoPoint(0, 1)).build_geocoded()
            ]
        ).build()

        df = geocoding_result.centroids()

        mock_request.assert_called_with(
            RequestBuilder() \
                .set_request_kind(RequestKind.explicit) \
                .set_ids([self.foo.id, self.bar.id]) \
                .set_requested_payload([PayloadKind.centroids]) \
                .build()
        )

        assert [self.foo.name, self.bar.name,
                self.foo.name] == df[DF_REQUEST].tolist()
Example #3
0
    def test_plot_should_have_geometries_when_regions_in_map_parameter(self, mock_request):

        mock_request.return_value = make_success_response() \
            .set_geocoded_features(
            [
                FeatureBuilder() \
                    .set_query(USA_REQUEST) \
                    .set_id(USA_ID) \
                    .set_name(USA_NAME) \
                    .set_boundary(GeoPoint(0, 1))
                    .build_geocoded(),
                FeatureBuilder() \
                    .set_query(RUSSIA_REQUEST) \
                    .set_id(RUSSIA_ID) \
                    .set_name(RUSSIA_NAME) \
                    .set_boundary(GeoPoint(0, 1))
                    .build_geocoded()

            ]
        ).build()

        plotSpec = ggplot() + geom_polygon(map=self.make_regions())

        # previous behaviour
        # expected_map_data_meta = {
        #    'georeference': {}
        # }

        expected_map_data_meta = {
            'geodataframe': {'geometry': 'geometry'}
        }

        assert expected_map_data_meta == get_map_data_meta(plotSpec, 0)
Example #4
0
    def test_direct_answers_take_request_from_query(self, mock_request):

        geocoding_result = Geocodes(
            level_kind=LevelKind.city,
            queries=[
                RegionQuery(request='fooo'),
                RegionQuery(request='barr'),
                RegionQuery(request='bazz'),
            ],
            answers=[
                Answer([self.foo.set_query('').build_geocoded()]),
                Answer([self.bar.set_query('').build_geocoded()]),
                Answer([self.baz.set_query('').build_geocoded()]),
            ])

        mock_request.return_value = make_success_response() \
            .set_geocoded_features(
            [
                self.foo.set_query(self.bar_id).set_centroid(GeoPoint(0, 1)).build_geocoded(),
                self.bar.set_query(self.foo_id).set_centroid(GeoPoint(0, 1)).build_geocoded(),
                self.baz.set_query(self.baz_id).set_centroid(GeoPoint(0, 1)).build_geocoded(),
            ]
        ).build()

        df = geocoding_result.centroids()

        mock_request.assert_called_with(
            RequestBuilder() \
                .set_request_kind(RequestKind.explicit) \
                .set_ids([self.foo.id, self.bar.id, self.baz.id]) \
                .set_requested_payload([PayloadKind.centroids]) \
                .build()
        )

        assert_row(df, names=['fooo', 'barr', 'bazz'])
Example #5
0
def test_near_to_region(mock_request):
    mock_request.return_value = make_success_response() \
        .set_geocoded_features(
        [
            feature(BAZ).set_centroid(GeoPoint(1, 2)).build_geocoded()
        ]
    ).build()

    actual = \
        regions_builder(request=names(FOO, BAR)) \
            .where(names(BAR), near=single_region(BAZ)) \
            ._get_queries()

    expected = [query(FOO.name), query(BAR.name, near=GeoPoint(1, 2))]

    assert expected == actual
Example #6
0
    def test_df_request_when_query_is_empty_should_be_taken_from_found_name_column(self, mock_request):
        foo_id = '123'
        foo_name = 'foo'
        geocoding_result = Regions(
            LevelKind.city,
            [
                FeatureBuilder().set_id(foo_id).set_query('').set_name(foo_name).build_geocoded()
            ]
        )

        mock_request.return_value = make_success_response() \
            .set_geocoded_features(
            [
                FeatureBuilder().set_id(foo_id).set_query(foo_id).set_name(foo_name).set_centroid(
                    GeoPoint(0, 1)).build_geocoded()
            ]
        ).build()

        df = geocoding_result.centroids()

        mock_request.assert_called_with(
            RequestBuilder() \
                .set_request_kind(RequestKind.explicit)
                .set_ids([foo_id]) \
                .set_requested_payload([PayloadKind.centroids]) \
                .build()
        )

        assert foo_name == df[DF_REQUEST][0]
Example #7
0
def test_near_shapely_point():
    actual = \
        regions_builder(request=names(FOO, BAR)) \
            .where(names(BAR), near=ShapelyPoint(1., 2.)) \
            ._get_queries()

    expected = [query(FOO.name), query(BAR.name, near=GeoPoint(1., 2.))]

    assert expected == actual
Example #8
0
def test_near_with_default_ignoring_strategy(mock_request):
    mock_request.return_value = make_success_response() \
        .set_geocoded_features(
        [
            feature(BAZ).set_centroid(GeoPoint(1., 2.)).build_geocoded()
        ]
    ).build()

    actual = \
        regions_builder(request=names(FOO, BAR)) \
            .allow_ambiguous() \
            .where(names(BAR), near=single_region(BAZ)) \
            ._get_queries()

    expected = [
        query(FOO.name, ignoring_strategy=IgnoringStrategyKind.take_namesakes),
        query(BAR.name, near=GeoPoint(1., 2.), ignoring_strategy=None)
    ]

    assert expected == actual
Example #9
0
    def test_exploding_answers_to_data_frame_take_request_from_feature_name(
            self, mock_request):
        foo_id = '123'
        foo_name = 'foo'

        bar_id = '456'
        bar_name = 'bar'
        geocoding_result = Geocodes(
            level_kind=LevelKind.city,
            queries=[RegionQuery(request=None)],
            answers=[
                Answer([
                    FeatureBuilder().set_id(foo_id).set_name(
                        foo_name).build_geocoded(),
                    FeatureBuilder().set_id(bar_id).set_name(
                        bar_name).build_geocoded()
                ])
            ])

        mock_request.return_value = make_success_response() \
            .set_geocoded_features(
            [
                FeatureBuilder().set_id(foo_id).set_query(foo_id).set_name(foo_name).set_centroid(
                    GeoPoint(0, 1)).build_geocoded(),
                FeatureBuilder().set_id(bar_id).set_query(bar_id).set_name(bar_name).set_centroid(
                    GeoPoint(2, 3)).build_geocoded(),
            ]
        ).build()

        df = geocoding_result.centroids()

        mock_request.assert_called_with(
            RequestBuilder() \
                .set_request_kind(RequestKind.explicit)
                .set_ids([foo_id, bar_id]) \
                .set_requested_payload([PayloadKind.centroids]) \
                .build()
        )

        assert_request_and_found_name_are_equal(df)
Example #10
0
    def test_df_rows_duplication_should_be_processed_correctly(
            self, mock_request):
        foo_id = '123'
        foo_name = 'foo'

        bar_id = '234'
        bar_name = 'bar'

        geocoding_result = Geocodes(level_kind=LevelKind.city,
                                    queries=[
                                        RegionQuery('foo'),
                                        RegionQuery('bar'),
                                        RegionQuery('foo')
                                    ],
                                    answers=[
                                        Answer([self.foo.build_geocoded()]),
                                        Answer([self.bar.build_geocoded()]),
                                        Answer([self.foo.build_geocoded()])
                                    ])

        mock_request.return_value = make_success_response() \
            .set_answers(
            [
                Answer([self.foo.set_query(foo_id).set_centroid(GeoPoint(0, 1)).build_geocoded()]),
                Answer([self.bar.set_query(bar_id).set_centroid(GeoPoint(0, 1)).build_geocoded()])
            ]
        ).build()

        df = geocoding_result.centroids()

        mock_request.assert_called_with(
            RequestBuilder() \
                .set_request_kind(RequestKind.explicit) \
                .set_ids([self.foo.id, self.bar.id]) \
                .set_requested_payload([PayloadKind.centroids]) \
                .build()
        )

        assert_row(df, names=['foo', 'bar', 'foo'])
Example #11
0
def make_point_boundary(lon: float, lat: float) -> Multipolygon:
    return Multipolygon(
        [
            Polygon(
                [
                    Ring(
                        [
                            GeoPoint(lon, lat)
                        ]
                    )
                ]
            )
        ]
    )
Example #12
0
def test_reorder_for_centroids_should_happen(mock_request):
    new_york = FeatureBuilder().set_id('2').set_query('New York').set_name('New York').set_centroid(GeoPoint(0, 0)).build_geocoded()
    las_vegas = FeatureBuilder().set_id('3').set_query('Las Vegas').set_name('Las Vegas').set_centroid(GeoPoint(0, 0)).build_geocoded()

    los_angeles = FeatureBuilder().set_id('1').set_query('Los Angeles').set_name('Los Angeles').set_centroid(
        GeoPoint(0, 0)).build_geocoded()
    mock_request.return_value = make_success_response().set_geocoded_features([new_york, las_vegas, los_angeles]).build()

    df = Geocodes(
        level_kind=LevelKind.city,
        queries=features_to_queries([los_angeles, new_york, las_vegas, los_angeles]),
        answers=features_to_answers([los_angeles, new_york, las_vegas, los_angeles])
    ).centroids()

    assert ['Los Angeles', 'New York', 'Las Vegas', 'Los Angeles'] == df[DF_COLUMN_FOUND_NAME].tolist()
Example #13
0
def make_ring_bondary(ring: GJRing) -> Ring:
    return Ring([GeoPoint(lon(p), lat(p)) for p in ring])
Example #14
0
def make_single_point_boundary(lon: float = 0, lat: float = 0) -> GeoPoint:
    return GeoPoint(lon, lat)
Example #15
0
def make_centroid_point() -> GeoPoint:
    return GeoPoint(CENTROID_LON, CENTROID_LAT)
Example #16
0
def test_reorder_for_centroids_should_happen(mock_request):
    mock_request.return_value = make_success_response() \
        .set_geocoded_features(
        [
            FeatureBuilder().set_id('2').set_query('New York').set_name('New York').set_centroid(GeoPoint(0, 0)).build_geocoded(),
            FeatureBuilder().set_id('3').set_query('Las Vegas').set_name('Las Vegas').set_centroid(GeoPoint(0, 0)).build_geocoded(),
            FeatureBuilder().set_id('1').set_query('Los Angeles').set_name('Los Angeles').set_centroid(GeoPoint(0, 0)).build_geocoded()
        ]
    ).build()

    df = Regions([
        make_region('Los Angeles', 'Los Angeles', '1', []),
        make_region('New York', 'New York', '2', []),
        make_region('Las Vegas', 'Las Vegas', '3', []),
        make_region('Los Angeles', 'Los Angeles', '1', []),
    ]).centroids()

    assert ['Los Angeles', 'New York', 'Las Vegas',
            'Los Angeles'] == df[DF_FOUND_NAME].tolist()