def test_delegated_predicates(predicate_name): feature_1 = GeoFeature.from_shape(Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])) feature_2 = GeoFeature.from_shape( Polygon([(0.5, 0), (1.5, 0), (1.5, 1), (0.5, 1)])) assert (getattr(feature_1, predicate_name)(feature_2) == getattr( feature_1.geometry, predicate_name)(feature_2.geometry))
def test_rasterization_of_line_has_correct_pixel_width(resolution): xmax, ymax = 11, 5 pixels_width = 1 line = GeoFeature.from_shape( LineString([(xmax / 2, 0), (xmax / 2, ymax * 4 / 5)])) roi = GeoVector.from_bounds(xmin=0, ymin=0, xmax=xmax, ymax=ymax, crs=DEFAULT_CRS) fc = FeatureCollection([line]) expected_image = np.zeros( (int(ymax // resolution), int(xmax // resolution)), dtype=np.uint8) expected_image[int(1 // resolution):, expected_image.shape[1] // 2] = 1 expected_affine = Affine(resolution, 0.0, 0.0, 0.0, -resolution, 5.0) expected_crs = DEFAULT_CRS expected_result = GeoRaster2(expected_image, expected_affine, expected_crs, nodata=0) result = fc.rasterize(resolution, polygonize_width=pixels_width, crs=DEFAULT_CRS, bounds=roi) assert result == expected_result
def test_rasterization_of_line_simple(): resolution = 1 pixels_width = 1 line = GeoFeature.from_shape(LineString([(2.5, 0), (2.5, 3)])) roi = GeoVector.from_bounds(xmin=0, ymin=0, xmax=5, ymax=5, crs=DEFAULT_CRS) fc = FeatureCollection([line]) expected_image = np.zeros((5, 5), dtype=np.uint8) expected_image[2:, 2] = 1 expected_affine = Affine(1.0, 0.0, 0.0, 0.0, -1.0, 5.0) expected_crs = DEFAULT_CRS expected_result = GeoRaster2(expected_image, expected_affine, expected_crs, nodata=0) result = fc.rasterize(resolution, polygonize_width=pixels_width, crs=DEFAULT_CRS, bounds=roi) assert result == expected_result
def fc_generator(num_features): gen_features = ( GeoFeature.from_shape( Polygon([(0 + d_x, 0), (0 + d_x, 1), (1 + d_x, 1), (1 + d_x, 0)]) ) for d_x in range(num_features) ) return FeatureCollection(gen_features)
def test_feature_collection_to_record_from_record(): num_features = 3 gen_features = (GeoFeature.from_shape( Polygon([(0 + d_x, 0), (0 + d_x, 1), (1 + d_x, 1), (1 + d_x, 0)])) for d_x in range(num_features)) fc = FeatureCollection(gen_features) assert mapping(fc) == mapping( FeatureCollection.from_record(fc.to_record(WGS84_CRS), WGS84_CRS))
def test_feature_collection_filter_returns_proper_elements(): num_features = 3 gen_features = (GeoFeature.from_shape( Polygon([(0 + d_x, 0), (0 + d_x, 1), (1 + d_x, 1), (1 + d_x, 0)])) for d_x in range(num_features)) filter_gv = GeoVector(Point(1.5, 0.5)) fcol = FeatureCollection(gen_features) res = fcol.filter(intersects=filter_gv) assert len(list(res)) == 1
def test_convex_hull_raises_warning_with_invalid_shape(): # Invalid coordinates coords = [(0, 0), (0, 2), (1, 1), (2, 2), (2, 0), (1, 1), (0, 0)] shape = Polygon(coords) gv = GeoFeature.from_shape(shape) assert not shape.is_valid fcol = FeatureCollection([gv]) with pytest.warns(UserWarning) as record: fcol.convex_hull assert len(record) == 1 assert record[0].message.args[0] == "Some invalid shapes found, discarding them."
def test_feature_collection_geo_interface(): num_features = 3 gen_features = (GeoFeature.from_shape(Point(0.0, 0.0)) for _ in range(num_features)) fcol = FeatureCollection(gen_features) expected_geo_interface = { 'type': 'FeatureCollection', 'features': [feature.__geo_interface__ for feature in fcol] } geo_interface = mapping(fcol) assert geo_interface.keys() == expected_geo_interface.keys() assert geo_interface['type'] == expected_geo_interface['type'] assert geo_interface['features'] == expected_geo_interface['features']
def test_polygonize_is_equivalent_to_geovector(): line = LineString([(1, 1), (0, 0)]) feature = GeoFeature.from_shape(line) assert feature.polygonize(1).geometry == feature.geometry.polygonize(1) assert feature.polygonize(1).attributes == feature.attributes