示例#1
0
    def find(self, document):
        if isinstance(document, basestring):
            document = ElementTree.fromstring(document)
        elif not ElementTree.iselement(document):
            document = ElementTree.parse(document)

        point_qname = self._get_qname(self.POINT_CLASS)
        lat_qname = self._get_qname(self.LATITUDE_PROPERTY)
        long_qname = self._get_qname(self.LONGITUDE_PROPERTY)
        alt_qname = self._get_qname(self.ALTITUDE_PROPERTY)

        queue = [document]
        while queue:
            element = queue.pop()
            if not self.point_class or element.tag == point_qname:
                lat_el = element.find(lat_qname)
                long_el = element.find(long_qname)
                alt_el = element.find(alt_qname)
                if lat_el is not None and long_el is not None:
                    latitude = lat_el.text
                    longitude = long_el.text
                    altitude = alt_el and alt_el.text
                    try:
                        point = Point((latitude, longitude, altitude))
                    except (TypeError, ValueError):
                        if not self.ignore_invalid:
                            raise
                    else:
                        yield Location(None, point)

                queue.extend(reversed(element))
示例#2
0
 def find(self, document):
     strainer = SoupStrainer(attrs={'class': self.GEO_CLASS})
     if not isinstance(document, BeautifulSoup):
         elements = BeautifulSoup(document, parseOnlyThese=strainer)
     else:
         elements = document.findAll(strainer)
     
     for element in elements:
         preformatted = element.name == 'pre'
         lat_element = element.find(attrs={'class': self.LATITUDE_CLASS})
         long_element = element.find(attrs={'class': self.LONGITUDE_CLASS})
         latitude = None
         longitude = None
         if lat_element and long_element:
             latitude = self._get_value(lat_element, preformatted)
             longitude = self._get_value(long_element, preformatted)
         elif self.shorthand:
             lat_long = re.split(self.SEP, self._get_value(element), 1)
             if len(lat_long) == 2:
                 latitude, longitude = lat_long
         if latitude and longitude:
             lat_match = FLOAT_RE.match(unescape(latitude))
             long_match = FLOAT_RE.match(unescape(longitude))
             if lat_match and long_match:
                 latitude = float(lat_match.group(1))
                 longitude = float(long_match.group(1))
                 text = unescape(self._get_text(element).strip())
                 name = re.sub('\s+', ' ', text)
                 yield Location(name, (latitude, longitude))
示例#3
0
    def test_should_get_data_when_location_has_multiple_polygons(
            self, mocked_geocode):
        data = Data()

        bbox = (0.0, 1.0, 2.0, 3.0)
        location = Location(
            raw={
                'boundingbox': bbox,
                'geojson': {
                    'coordinates':
                    [[[[-3.0938758, 55.9348096], [-3.0938096, 55.9346675],
                       [-3.0934263, 55.9347236], [-3.0934925, 55.9348656],
                       [-3.0938758, 55.9348096]]],
                     [[[9.6240234375, 52.429222277955134],
                       [12.3486328125, 52.429222277955134],
                       [12.3486328125, 53.85252660044951],
                       [9.6240234375, 53.85252660044951],
                       [9.6240234375, 52.429222277955134]]]],
                    'type':
                    'MultiPolygon'
                }
            })
        mocked_geocode.return_value = location

        output1, output2 = data.get('roads', 'location')

        self.assertIsInstance(output1, types.GeneratorType)
        self.assertIsInstance(output2, types.GeneratorType)
示例#4
0
文件: osm.py 项目: kendhia/SahanaEden
 def parse_result(result):
     location = result.get('display_name')
     latitude = result.get('lat')
     longitude = result.get('lon')
     if latitude and longitude:
         point = Point(latitude, longitude)
     else:
         point = None
     return Location(location, point, result)
示例#5
0
 def _get_location(self, attrs):
     position = attrs.pop('position')
     name = attrs.pop('placename')
     if position is not None:
         if position or not self.ignore_invalid:
             try:
                 point = Point(position)
             except (TypeError, ValueError):
                 if not self.ignore_invalid:
                     raise
             else:
                 return Location(name, point, attrs)
示例#6
0
    def test_should_retrieve_bbox_from_location_name_for_point(
            self, mocked_geocode):
        bbox = (0.0, 1.0, 2.0, 3.0)
        location = Location(raw={
            'boundingbox': bbox,
            'geojson':
            {
                'coordinates': [-79.6371123, 39.2138905],
                'type': 'Point'
            }
        })
        mocked_geocode.return_value = location

        output = coords_for('test')

        self.assertEqual(output, (bbox[0], bbox[2], bbox[1], bbox[3]))
示例#7
0
def get_address_from_cache(*args):
    if len(args) == 1:
        address = args[0]
    else:
        address = args[1]

    # deepcopy here for cases were we fiddle with the data
    # eg in TestMember::test_set_address_bad_response
    # Maybe shallow copy will do, but better be sure …
    address = deepcopy(ADDRESSES.get(address))

    if address is None:
        return None
    if not isinstance(address['point'], Point):
        address['point'] = Point(**address['point'])
    return Location(**address)
示例#8
0
 def find(self, document):
     strainer = SoupStrainer('meta', attrs={'name': self.META_NAME})
     if not isinstance(document, BeautifulSoup):
         elements = BeautifulSoup(document, parseOnlyThese=strainer)
     else:
         elements = document.findAll(strainer)
     
     for element in elements:
         lat_long = element.get('content')
         if lat_long or not self.ignore_invalid:
             try:
                 point = Point(unescape(lat_long))
             except (TypeError, ValueError):
                 if not self.ignore_invalid:
                     raise
             else:
                 yield Location(None, point)
示例#9
0
文件: test_api.py 项目: eirki/trek
def test_version():
    with patch("backend.api.location_query") as mock:
        mock.return_value = [
            Location(
                address="test_adress",
                point=Point(latitude=41.5, longitude=-81),
                raw="abc",
            )
        ]
        response = client.get("/api/locations", params={"query": "testing"})
        assert response.json() == {
            "result": [{
                "address": "test_adress",
                "latitude": 41.5,
                "longitude": -81.0,
                "altitude": 0.0,
            }]
        }
示例#10
0
    def test_should_get_data_when_location_defined_on_function_call(
            self, mocked_process_osm_output, mocked_geocode):
        test_data = ([1, 2], ['a', 'b'])
        bbox = (0.0, 1.0, 2.0, 3.0)
        location = Location(
            raw={
                'boundingbox': bbox,
                'geojson': {
                    'coordinates': [-79.6371123, 39.2138905],
                    'type': 'Point'
                }
            })

        mocked_geocode.return_value = location
        mocked_process_osm_output.return_value = test_data
        data = Data()

        output = data.get('roads', 'location')

        self.assertEqual(test_data, output)
示例#11
0
    def test_should_retrieve_bbox_from_location_name_for_polygon(
            self, mocked_geocode):
        bbox = (0.0, 1.0, 2.0, 3.0)
        location = Location(raw={
            'boundingbox': bbox,
            'geojson':
            {
                'coordinates': [
                    [
                        [
                            -3.0938758,
                            55.9348096
                        ],
                        [
                            -3.0938096,
                            55.9346675
                        ],
                        [
                            -3.0934263,
                            55.9347236
                        ],
                        [
                            -3.0934925,
                            55.9348656
                        ],
                        [
                            -3.0938758,
                            55.9348096
                        ]
                    ]
                ],
                'type': 'Polygon'
            }
        })
        mocked_geocode.return_value = location

        output = coords_for('test')

        self.assertIsInstance(output, BoundingBox)
示例#12
0
    def location(self):
        """
        Get the location, but as a geopy location object

        Returns
        -------
        Location
        """
        # if the input was a string, we do a google lookup
        if isinstance(self._location, str):
            location = GoogleV3().geocode(self._location)

        # if the input was an iterable, it is latitude and longitude
        elif hasattr(self._location, '__iter__'):
            lat, long = self._location
            gepoint = Point(latitude=lat, longitude=long)
            location = Location(point=gepoint)

        else:
            raise ValueError('Invalid location')

        return location
示例#13
0
 def parse_result(result):
     strip = ", \n"
     address = util.get_first_text(result, 'Address', strip)
     city = util.get_first_text(result, 'City', strip)
     state = util.get_first_text(result, 'State', strip)
     zip = util.get_first_text(result, 'Zip', strip)
     country = util.get_first_text(result, 'Country', strip)
     city_state = util.join_filter(", ", [city, state])
     place = util.join_filter(" ", [city_state, zip])
     location = util.join_filter(", ", [address, place, country])
     latitude = util.get_first_text(result, 'Latitude') or None
     longitude = util.get_first_text(result, 'Longitude') or None
     if latitude and longitude:
         point = Point(latitude, longitude)
     else:
         point = Non
     return Location(location, point, {
         'Address': address,
         'City': city,
         'State': state,
         'Zip': zip,
         'Country': country
     })
示例#14
0
    def test_should_retrieve_bbox_from_location_name_for_multipolygon(
            self, mocked_geocode):
        bbox = (0.0, 1.0, 2.0, 3.0)
        location = Location(raw={
            'boundingbox': bbox,
            'geojson':
            {
                'coordinates': [
                    [[
                        [
                            -3.0938758,
                            55.9348096
                        ],
                        [
                            -3.0938096,
                            55.9346675
                        ],
                        [
                            -3.0934263,
                            55.9347236
                        ],
                        [
                            -3.0934925,
                            55.9348656
                        ],
                        [
                            -3.0938758,
                            55.9348096
                        ]
                    ]],
                    [[
                        [
                            9.6240234375,
                            52.429222277955134
                        ],
                        [
                            12.3486328125,
                            52.429222277955134
                        ],
                        [
                            12.3486328125,
                            53.85252660044951
                        ],
                        [
                            9.6240234375,
                            53.85252660044951
                        ],
                        [
                            9.6240234375,
                            52.429222277955134
                        ]
                    ]]
                ],
                'type': 'MultiPolygon'
            }
        })
        mocked_geocode.return_value = location

        output = list(coords_for('test'))

        self.assertIsInstance(output[0], BoundingBox)
        self.assertIsInstance(output[1], BoundingBox)
        self.assertEqual(len(output), 2)