Example #1
0
    def parse(self, data):
        """
        Parse a node containing a point.
        
        :param data: The point node.
        :type data: :py:class:`_ElementTree`
        :return: A Point instance.
        :rtype: :py:class:`Point`
        """
        sr_template = './@{0}'
        point_template = './{0}:{1}/text()'
        point = Point()
        try:
            point.spatial_ref = self._run_xpath(data,
                                                sr_template.format('srsName'))
            position = self._run_xpath(
                data, point_template.format(GML_PREFIX, 'pos'))
            lat, lon = position.split()
            point.latitude = float(lat)
            point.longitude = float(lon)
        except (Exception, TypeError) as ex:
            logger.error('Invalid point input.', ex)
            raise BadRequestException('Invalid point input.')

        return point
    def test_find_service_for_polygon_centroid(self, mock_config, mock_db):
        mock_db.get_urn_table_mappings = MagicMock()
        mock_db.get_urn_table_mappings.return_value = {'urn1': 'service1', 'urn2': 'service2'}

        mock_config.polygon_search_mode_policy = MagicMock()
        mock_config.polygon_search_mode_policy.return_value = \
            lostservice.handling.findservice.PolygonSearchModePolicyEnum.SearchUsingCentroid

        expected = [{'id': 1, 'AREA_RET': 2}, {'id': 2, 'AREA_RET': 10}, {'id': 1, 'AREA_RET': 5}]

        target = lostservice.handling.findservice.FindServiceInner(mock_config, mock_db)
        target.find_service_for_point = MagicMock()
        target.find_service_for_point.return_value = expected

        points = [[0, 0], [2, 0], [2, 2], [0, 2]]
        polygon = Polygon(spatial_ref="something::4326",vertices=points)
        point = Point()
        point.longitude = 1.0
        point.latitude = 1.0
        point.spatial_ref = "something::4326"

        actual = target.find_service_for_polygon('urn1', polygon, False)

        self.assertListEqual(actual, expected)
        mock_config.polygon_search_mode_policy.assert_called_once()
        target.find_service_for_point.assert_called_once()
        # target.find_service_for_point.assert_called_with('urn1', polygon, False)
        self.assertEqual(target.find_service_for_point.call_args[0][1].latitude, point.latitude)
        self.assertEqual(target.find_service_for_point.call_args[0][1].longitude, point.longitude)
        self.assertEqual(target.find_service_for_point.call_args[0][1].spatial_ref, point.spatial_ref)
    def test_find_service_for_point(self, mock_config, mock_db):

        test_data = [{'id': 1, 'AREA_RET': 2}, {'id': 2, 'AREA_RET': 10}, {'id': 1, 'AREA_RET': 5}]

        mock_db.get_urn_table_mappings = MagicMock()
        mock_db.get_urn_table_mappings.return_value = {'urn1': 'service1', 'urn2': 'service2'}
        mock_db.get_containing_boundary_for_point = MagicMock()
        mock_db.get_containing_boundary_for_point.return_value = test_data

        target = lostservice.handling.findservice.FindServiceInner(mock_config, mock_db)
        target._apply_point_multiple_match_policy = MagicMock()
        target._apply_point_multiple_match_policy.return_value = test_data
        target._apply_polygon_multiple_match_policy = MagicMock()
        target._apply_policies = MagicMock()
        target._apply_policies.return_value = test_data

        location = Point()
        location.latitude = 0.0
        location.longitude = 1.1
        location.spatial_ref = 'something::1234'
        actual = target.find_service_for_point('urn1', location, False)
        buffer_dist = mock_config.additional_data_buffer()

        self.assertListEqual(actual, test_data)
        mock_db.get_containing_boundary_for_point.assert_called_with(location, 'service1',
                                                                     add_data_requested=False,
                                                                     buffer_distance = buffer_dist)
        mock_db.get_containing_boundary_for_point.assert_called_once()
        target._apply_point_multiple_match_policy.assert_called_with(test_data)
        target._apply_point_multiple_match_policy.assert_called_once()
        target._apply_polygon_multiple_match_policy.assert_not_called()
        target._apply_policies.assert_called_with(test_data, False)
        target._apply_policies.assert_called_once()
    def list_services_by_location_for_civicaddress(self, civic_request):
        """
        Function to find the service for the civic address

         :param civic_request: civic address request
         :type civic_request: :py:class:`lostservice.model.requests.ListServicesRequest`
         :return: The service mappings for the given civic address.
        """
        rcl_offset_distance = self._list_service_config.get(
            'Policy',
            'offset_distance_from_centerline',
            as_object=False,
            required=False)
        # Now let's create the locator and supply it with the common default strategies.
        locator = self.get_civvy_locator(rcl_offset_distance)
        # Let's get the results for this civic address.
        locator_results = self.run_civic_location_search(
            locator=locator,
            offset_distance=rcl_offset_distance,
            civic_request=civic_request)
        mappings = None
        point = Point()
        if len(locator_results) > 0:
            first_civic_point = None
            for locator_result in locator_results:
                if locator_result.score == 0:
                    first_civic_point = locator_result
                    break
            if first_civic_point:
                civvy_geometry = first_civic_point.geometry
                spatial_reference = civvy_geometry.GetSpatialReference()
                epsg = spatial_reference.GetAttrValue("AUTHORITY", 0)
                srid = spatial_reference.GetAttrValue("AUTHORITY", 1)
                spatial_ref = "{0}::{1}".format(epsg, srid)
                point.latitude = civvy_geometry.GetY()
                point.longitude = civvy_geometry.GetX()
                point.spatial_ref = spatial_ref
                mappings = self.list_services_by_location_for_point(
                    civic_request.service, point)
        return {
            'mappings': mappings,
            'latitude': point.latitude,
            'longitude': point.longitude
        }
    def test_find_service_for_point_expanded(self, mock_config, mock_db):
        test_data = [{'id': 1, 'AREA_RET': 2}, {'id': 2, 'AREA_RET': 10}, {'id': 1, 'AREA_RET': 5}]

        # Set up mock_db to return nothing from point search but return a value for expended circle search.
        mock_db.get_urn_table_mappings = MagicMock()
        mock_db.get_urn_table_mappings.return_value = {'urn1': 'service1', 'urn2': 'service2'}
        mock_db.get_containing_boundary_for_point = MagicMock()
        mock_db.get_containing_boundary_for_point.return_value = []
        mock_db.get_intersecting_boundaries_for_circle = MagicMock()
        mock_db.get_intersecting_boundaries_for_circle.return_value = test_data

        # Set up mock_config to trigger area majority.
        mock_config.do_expanded_search = MagicMock()
        mock_config.do_expanded_search.return_value = True
        mock_config.polygon_multiple_match_policy = MagicMock()
        mock_config.polygon_multiple_match_policy.return_value = \
            lostservice.handling.findservice.PolygonMultipleMatchPolicyEnum.ReturnAreaMajority
        mock_config.expanded_search_buffer = MagicMock()
        mock_config.expanded_search_buffer.return_value = 10
        mock_config.additional_data_uri = MagicMock()
        mock_config.additional_data_uri.return_value = "additional.data.uri"
        mock_config.additional_data_buffer = MagicMock()
        mock_config.additional_data_buffer.return_value = 5.0

        target = lostservice.handling.findservice.FindServiceInner(mock_config, mock_db)
        target._apply_point_multiple_match_policy = MagicMock()
        target._apply_polygon_multiple_match_policy = MagicMock()
        target._apply_polygon_multiple_match_policy.return_value = test_data
        target._apply_policies = MagicMock()
        target._apply_policies.return_value = test_data

        location = Point()
        location.latitude = 0.0
        location.longitude = 1.1
        location.spatial_ref = 'something::1234'

        actual = target.find_service_for_point('urn1',location, False)

        self.assertListEqual(actual, test_data)
        mock_config.do_expanded_search.assert_called_once()
        mock_config.polygon_multiple_match_policy.assert_called_once()
        mock_config.expanded_search_buffer.assert_called_once()
        mock_config.additional_data_uri.assert_called_once()
        mock_db.get_containing_boundary_for_point.assert_called_with(
            location,
            'service1',
            add_data_requested=False,
            buffer_distance = 5.0)
        mock_db.get_containing_boundary_for_point.assert_called_once()



        mock_db.get_intersecting_boundaries_for_circle.assert_called_with(location=location,
                                                                          proximity_buffer=10,
                                                                          boundary_table='service1',
                                                                          proximity_search=True,
                                                                          return_area=True,
                                                                          return_shape=False)
        mock_db.get_intersecting_boundaries_for_circle.assert_called_once()

        target._apply_point_multiple_match_policy.assert_not_called()
        target._apply_polygon_multiple_match_policy.assert_called_with(test_data)
        target._apply_polygon_multiple_match_policy.assert_called_once()
        target._apply_policies.assert_called_with(test_data, False)
        target._apply_policies.assert_called_once()