diff --git a/telluric/features.py b/telluric/features.py index 5c88d537..a9c7aae0 100644 --- a/telluric/features.py +++ b/telluric/features.py @@ -1,4 +1,6 @@ import warnings +import geojson + from collections import Mapping from dateutil.parser import parse as parse_date @@ -12,6 +14,7 @@ ) from telluric.plotting import NotebookPlottingMixin from telluric import GeoRaster2 +from rasterio.crs import CRS def transform_properties(properties, schema): @@ -114,7 +117,10 @@ def attributes(self): @property def __geo_interface__(self): - return self.to_record(WGS84_CRS) + return_val = self.to_record(WGS84_CRS) + if self.crs.is_epsg_code: + return_val["crs"] = {"type": "name", "properties": {"name": "EPSG:%s" % (self.crs.to_epsg())}} + return return_val def to_record(self, crs): ret_val = { @@ -124,6 +130,21 @@ def to_record(self, crs): } return ret_val + def save(self, filename): + """ This will save the feature as a geojson + """ + geojson.dump(self, open(filename, 'w')) + + @classmethod + def open(cls, filename, crs=None): + record = geojson.load(open(filename)) + crs = crs or CRS.from_string(record["crs"]["properties"]["name"]) + feature = cls.from_record(record, crs=WGS84_CRS ) + if crs: + feature = feature.reproject(new_crs=crs) + return feature + + @staticmethod def _get_class_from_record(record): if "raster" in record: diff --git a/tests/test_features.py b/tests/test_features.py index e8501acc..5354352e 100644 --- a/tests/test_features.py +++ b/tests/test_features.py @@ -248,3 +248,9 @@ def test_transform_properties(): ('prop3', '2018-05-19T15:00:00'), ('prop4', None) ]), schema) == expected_properties + +def test_save_open_feature(): + feature = GeoFeature(GeoVector.from_xyz(12,123,12), properties={"bla":"bla"}) + feature.save("feature.geojson") + feature2 = GeoFeature.open("feature.geojson") + assert feature == feature2