def test_write_read_geojson_with_props(gjson, props): byte_stream = io.BytesIO() writer = geostream.writer(byte_stream, props) writer.write_feature(gjson) byte_stream.seek(0) header1, header2, prop_len = struct.unpack( 'Bii', byte_stream.read(struct.calcsize('Bii'))) assert header1 in GEOSTREAM_SCHEMA_VERSIONS assert header2 == GEOJSON_EPSG_SRID assert prop_len > 0 p = byte_stream.read(prop_len) header_props = cbor2.loads(p) assert header_props["unit"] == "something" length = struct.unpack('i', byte_stream.read(struct.calcsize('i')))[0] zipped_data = byte_stream.read(length) trailing_length = struct.unpack('i', byte_stream.read(struct.calcsize('i')))[0] assert len(zipped_data) == length assert length == trailing_length byte_stream.seek(0) reader = geostream.reader(byte_stream) assert reader.srid == GEOJSON_EPSG_SRID assert reader.schema_version in GEOSTREAM_SCHEMA_VERSIONS assert reader.properties == props read_features = [f for f in reader] assert len(read_features) == 1 assert gjson == read_features[0] byte_stream.seek(0) rev_reader = geostream.reader(byte_stream, reverse=True) assert rev_reader.srid == GEOJSON_EPSG_SRID assert rev_reader.schema_version in GEOSTREAM_SCHEMA_VERSIONS assert rev_reader.properties == props rev_features = [f for f in rev_reader] assert len(rev_features) == 1 assert gjson == read_features[0]
def test_read_invalid_schema_from_stream_raises_exception(feat_collection_2): with pytest.raises(ValueError): collection = feat_collection_2 collection_props = {"unit": "something", "key": "uuid"} byte_stream = io.BytesIO() writer = geostream.writer(byte_stream, collection_props) writer.write_feature_collection(collection) byte_stream.seek(0) byte_stream.write(struct.pack("B", 0)) byte_stream.seek(0) geostream.reader(byte_stream)
def test_read_truncated_after_feature_length(feat_collection_2): collection = feat_collection_2 collection_props = {} byte_stream = io.BytesIO() writer = geostream.writer(byte_stream, collection_props) writer.write_feature_collection(collection) byte_stream.write(struct.pack( "i", 2)) # append the length of a second feature, but don't append data byte_stream.seek(0) reader = geostream.reader(byte_stream) read_features = [f for f in reader] assert len(read_features) == 1
def test_read_truncated_feature_data(feat_collection_2): collection = feat_collection_2 byte_stream = io.BytesIO() writer = geostream.writer(byte_stream) writer.write_feature_collection(collection) byte_stream.seek(0) good_data = byte_stream.read() bad_byte_stream = io.BytesIO() bad_byte_stream.write( good_data[:-5]) # truncate trailing length and 1 byte of feature data bad_byte_stream.seek(0) reader = geostream.reader(bad_byte_stream) read_features = [f for f in reader] assert len(read_features) == 0
def test_reverse_read_gjson_features_from_longer_stream(feat_collection_3): collection = feat_collection_3 collection_props = {"unit": "something", "key": "uuid"} byte_stream = io.BytesIO() writer = geostream.writer(byte_stream, collection_props) for i in range(0, 100): writer.write_feature_collection(collection) byte_stream.seek(0) reader = geostream.reader(byte_stream, reverse=True) assert reader.srid == GEOJSON_EPSG_SRID assert reader.schema_version in GEOSTREAM_SCHEMA_VERSIONS assert reader.properties == collection_props read_features = [f for f in reader] assert len(read_features) == 300 feature_list = feat_collection_3["features"] assert feature_list[0] == read_features[2] assert feature_list[1] == read_features[1] assert feature_list[2] == read_features[0]
def test_write_geojson_feature_collection_diff_srid(feat_collection_1): collection = feat_collection_1 output_stream = io.BytesIO() writer = geostream.writer(output_stream, srid=1234) writer.write_feature_collection(collection) output_stream.seek(0) header1, header2, props_len = struct.unpack( 'Bii', output_stream.read(struct.calcsize('Bii'))) assert header1 in GEOSTREAM_SCHEMA_VERSIONS assert header2 == 1234 assert props_len == 0 length = struct.unpack('i', output_stream.read(struct.calcsize('i')))[0] zipped_data = output_stream.read(length) trailing_length = struct.unpack('i', output_stream.read( struct.calcsize('i')))[0] assert len(zipped_data) == length assert length == trailing_length