def test_file_collection_open_save_geojson(): fcol = FileCollection.open("tests/data/vector/bsas_barrios_lla.geojson") with tempfile.NamedTemporaryFile(suffix=".json") as fp: fcol.save(fp.name) fcol_res = FileCollection.open(fp.name) assert fcol_res.crs == WGS84_CRS assert fcol == fcol_res
def test_file_collection_open_save_shapefile(): fcol = FileCollection.open("tests/data/vector/creaf/42111/42111.shp")[:10] with tempfile.NamedTemporaryFile(suffix=".json") as fp: fcol.save(fp.name) fcol_res = FileCollection.open(fp.name) import time time.sleep(5) assert fcol_res.crs == WGS84_CRS assert fcol == fcol_res.reproject(fcol.crs)
def test_feature_collection_with_dates_serializes_correctly(): # "For Shapefiles, however, the only possible field type is 'date' as 'datetime' and 'time' are not available." # https://github.com/Toblerity/Fiona/pull/130 # See also: https://github.com/Toblerity/Fiona/issues/572 schema = { 'geometry': 'Point', 'properties': OrderedDict([ ('prop_date', 'date'), ]), } expected_attributes = { 'prop_date': date(2018, 4, 23), } feature = GeoFeature(GeoVector(Point(0, 0)), expected_attributes) with tempfile.TemporaryDirectory() as path: file_path = os.path.join(path, "test_dates.shp") with fiona.open(file_path, mode='w', driver="ESRI Shapefile", schema=schema, crs=feature.crs) as sink: sink.write(mapping(feature)) fc = FileCollection.open(file_path) assert fc.schema == schema assert fc[0].geometry == feature.geometry assert fc[0].attributes == expected_attributes
def test_feature_collection_with_invalid_schema(): fc = FileCollection.open("tests/data/vector/bsas_barrios_lla.geojson") schema = fc.schema.copy() schema["properties"] = {} with pytest.raises(ValueError) as exception: fc2 = FeatureCollection(list(fc), schema=schema) assert exception.message.startswith("Record does not match collection schema")
def test_feature_collection_apply_preseves_the_order_just_changes_type(): fc = FileCollection.open("tests/data/vector/bsas_barrios_lla.geojson") new_fc = fc.apply(COMUNA=None) expected_properties_schema = OrderedDict( [('BARRIO', "str"), ('COMUNA', 'str'), ('PERIMETRO', 'float'), ('AREA', 'float')]) assert new_fc.schema["properties"] == expected_properties_schema
def test_feature_collection_schema_of_empty_set(): fc = FeatureCollection([]) assert fc.schema == {"geometry": None, "properties": {}} with tempfile.NamedTemporaryFile(suffix=".json") as target: fc.save(target.name) fc2 = FileCollection.open(target.name) assert fc == fc2
def test_feature_collection_apply_appends_new_keys_to_the_end(): fc = FileCollection.open("tests/data/vector/bsas_barrios_lla.geojson") fc_expepcted_schema = fc.schema.copy() new_fc = fc.apply(prop1=3) new_expepcted_schema = fc.schema.copy() new_expepcted_schema["properties"]["prop1"] = "int" assert new_fc.schema == new_expepcted_schema assert fc.schema == fc_expepcted_schema
def test_file_collection_open_save_respects_projection(): fcol = fc_generator(4).reproject(WEB_MERCATOR_CRS) with tempfile.NamedTemporaryFile(suffix=".json") as fp: fcol.save(fp.name) fcol_res = FileCollection.open(fp.name) assert fcol_res.crs == WGS84_CRS assert fcol == fcol_res.reproject(fcol.crs)
def test_file_collection_open(): expected_len = 53 expected_attribute_names = ['BARRIO', 'COMUNA', 'PERIMETRO', 'AREA'] fcol = FileCollection.open("tests/data/vector/bsas_barrios_lla.geojson") assert len(fcol) == expected_len assert fcol.attribute_names == expected_attribute_names assert fcol.crs == WGS84_CRS
def test_file_save_geojson_twice(): fcol = FileCollection.open("tests/data/vector/bsas_barrios_lla.geojson") with tempfile.NamedTemporaryFile(suffix=".json") as fp: fcol.save(fp.name) fcol.save(fp.name)
def test_featurecollection_save_has_no_side_effects(): fc = FeatureCollection([ GeoFeature(GeoVector(Point(0, 0)), {'attr1': 1}), GeoFeature(GeoVector(Point(0, 0)), {'attr2': 1}) ]) with tempfile.NamedTemporaryFile(suffix=".json") as fp: fc.save(fp.name) assert fc[0].attributes == {'attr1': 1} assert fc[1].attributes == {'attr2': 1} @pytest.mark.parametrize("fc", [ fc_generator(num_features=5), FileCollection.open("tests/data/vector/bsas_barrios_lla.geojson"), ]) def test_collection_slicing(fc): features = list(fc) # Single item assert fc[0] == features[0] assert fc[-1] == features[-1] # Positive step slicing assert fc[1:3] == FeatureCollection(features[1:3]) assert fc[:2] == FeatureCollection(features[:2]) assert fc[-2:] == FeatureCollection(features[-2:]) assert fc[:-1] == FeatureCollection(features[:-1]) assert fc[::2] == FeatureCollection(features[::2])
def test_feature_collection_groupby_preserves_schema(): fc = FileCollection.open("tests/data/vector/bsas_barrios_lla.geojson") schema = fc.schema.copy() for group, collection in fc.groupby('COMUNA'): assert schema == collection.schema
def test_feature_collection_with_valid_schema(): fc = FileCollection.open("tests/data/vector/bsas_barrios_lla.geojson") fc2 = FeatureCollection(list(fc), schema=fc.schema) assert fc.schema == fc2.schema
def test_file_collection_open_shapefile_with_no_proj(): fcol = FileCollection.open("tests/data/vector/creaf/42112_noprj/42112.shp", crs=WGS84_CRS)[:10] assert fcol.crs == WGS84_CRS for feature in fcol: assert feature.crs == WGS84_CRS
def test_file_collection_filelike(): with open("tests/data/vector/bsas_barrios_lla.geojson", "rb") as f: fcol = FileCollection.open(f) assert [feature for feature in fcol]
def test_file_collection_bytesio(): with open("tests/data/vector/bsas_barrios_lla.geojson", "rb") as f: data = io.BytesIO(f.read()) fcol = FileCollection.open(data) assert [feature for feature in fcol]