Пример #1
0
    def test_polygon(test):
        g = {
            "type": "Polygon",
            "coordinates": [[[-100.0, 34.0], [-101.0, 35.4], [-101.5, 34.4]]]
        }
        expected = [-100.83333333333333, 34.6]
        assert expected == geojson.get_centroid(g)

        # should get same results if polygon has same last and first coordinates
        g['coordinates'][0].append([-100.0, 34.0])
        assert expected == geojson.get_centroid(g)
Пример #2
0
    def _get_central_lat_lng(self):
        if len(self._input_data['fires']) > 1:
            # input data could possibly specifu multiple fires at
            # the same location, but we won't bother trying to accept that
            self._handle_error(400, ErrorMessages.SINGLE_FIRE_ONLY)

        fire = fires.Fire(self._input_data['fires'][0])

        try:
            locations = fire.locations
        except ValueError as e:
            # fire.locations includes validation of the location data
            self._handle_error(400, ErrorMessages.INVALID_FIRE_LOCATION_INFO)

        if not locations:
            self._handle_error(400, ErrorMessages.NO_ACTIVITY_INFO)

        centroids = []
        for loc in locations:
            try:
                latlng = locationutils.LatLng(loc)
                centroids.append((latlng.latitude, latlng.longitude))
            except Exception as e:
                self._handle_error(400,
                                   ErrorMessages.INVALID_FIRE_LOCATION_INFO)

        if len(centroids) > 1:
            multi_point = {
                "type": 'MultiPoint',
                "coordinates": [[e[1], e[0]] for e in centroids]
            }
            coords = get_centroid(multi_point)
            return (coords[1], coords[0])
        else:
            return centroids[0]
Пример #3
0
 def test_multi_line_string(test):
     g = {
         "type": "MultiLineString",
         "coordinates": [[[-100.0, 34.0], [-101.0, 35.4]], [[-101.5, 34.4]]]
     }
     expected = [-100.83333333333333, 34.6]
     assert expected == geojson.get_centroid(g)
Пример #4
0
 def test_line_string(test):
     g = {
         "type": "LineString",
         "coordinates": [[-100.0, 34.0], [-101.0, 35.4]]
     }
     expected = [-100.5, 34.7]
     assert expected == geojson.get_centroid(g)
Пример #5
0
 def test_multi_point(test):
     g = {
         "type": "MultiPoint",
         "coordinates": [[-100.0, 34.0], [-101.0, 35.4]]
     }
     expected = [-100.5, 34.7]
     assert expected == geojson.get_centroid(g)
Пример #6
0
 def test_polygon_with_hole(test):
     g = {
         "type":
         "Polygon",
         "coordinates": [
             [[-100.0, 34.0], [-101.0, 35.4], [-101.5, 34.4]],
             # the hole is ignored in centroid computation
             [[-100.2, 35.2], [-100.3, 34.4], [-100.1, 34.3]]
         ]
     }
     expected = [-100.83333333333333, 34.6]
     assert expected == geojson.get_centroid(g)
Пример #7
0
    def _compute_from_geo_data(self, geo_type, coordinates):
        try:
            geo_data = {"type": geo_type, 'coordinates': coordinates}
            coordinate = get_centroid(geo_data)
            self._latitude = coordinate[1]
            self._longitude = coordinate[0]

        except:
            error_msg = (MISSING_OR_INVALID_COORDINATES_FOR_PERIMETER
                         if geo_type == 'Polygon' else
                         MISSING_OR_INVALID_LAT_LNG_FOR_SPECIFIED_POINT)
            raise ValueError(error_msg)
Пример #8
0
 def _compute(self):
     if 'latitude' in self._location and 'longitude' in self._location:
         self._latitude = self._location['latitude']
         self._longitude = self._location['longitude']
     elif 'geojson' in self._location:
         coordinate = get_centroid(self._location['geojson'])
         self._latitude = coordinate[1]
         self._longitude = coordinate[0]
     elif 'shape_file' in self._location:
         raise NotImplementedError(
             "Importing of shape data from file not implemented")
     else:
         raise ValueError("Insufficient location data required for "
                          "determining single lat/lng for location")
Пример #9
0
    def test_multi_polygon(test):
        g = {
            "type":
            "MultiPolygon",
            "coordinates": [
                [
                    # centroid is [-100.83333333333333, 34.6]
                    [[-100.0, 34.0], [-101.0, 35.4], [-101.5, 34.4]],
                    # the hole is ignored in centroid computation
                    [[-100.2, 35.2], [-100.3, 34.4], [-100.1, 34.3]]
                ],
                [
                    # centroid is [-102.96666666666665, 34.19333333333333]
                    [[-104.1, 34.12], [-102.4, 33.23], [-102.4, 35.23]]
                ]
            ]
        }
        expected = [-101.89999999999999, 34.39666666666666]
        assert expected == geojson.get_centroid(g)

        # should get same results if polygons have same last and first coordinates
        g['coordinates'][0][0].append([-100.0, 34.0])
        g['coordinates'][1][0].insert(0, [-102.4, 35.23])
        assert expected == geojson.get_centroid(g)
Пример #10
0
 def test_point(test):
     g = {"type": "Point", "coordinates": [-102.0, 39.5]}
     expected = [-102.0, 39.5]
     assert expected == geojson.get_centroid(g)