Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding open and save method to feature #157

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion telluric/features.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import warnings
import geojson

from collections import Mapping

from dateutil.parser import parse as parse_date
Expand All @@ -12,6 +14,7 @@
)
from telluric.plotting import NotebookPlottingMixin
from telluric import GeoRaster2
from rasterio.crs import CRS


def transform_properties(properties, schema):
Expand Down Expand Up @@ -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 = {
Expand All @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -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