Skip to content

Instantly share code, notes, and snippets.

@prakashjayy
Created June 15, 2021 05:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save prakashjayy/0dc3bfdbb57fbca266e9dee8118c7b67 to your computer and use it in GitHub Desktop.
Save prakashjayy/0dc3bfdbb57fbca266e9dee8118c7b67 to your computer and use it in GitHub Desktop.
patch read_generic to Feature collection fuction in aeronetlib.
@patch_to(ds.FeatureCollection, cls_method=True)
def read_generic(cls,
fp: str,
dst_crs: Union[str, rasterio.crs.CRS]=None,
):
'''
geojson reader and can also be used to ensure the geojson in mapped to a destined crs
'''
with open(fp, 'r', encoding='utf-8') as f:
collection = json.load(f)
features = []
src_crs = collection['crs']['properties']['name'] if isinstance(collection['crs'], dict) else collection['crs']
print(src_crs)
dst_crs = src_crs if dst_crs is None else dst_crs
for i, feature in enumerate(collection['features']):
try:
new_geometry = transform_geom(
src_crs=src_crs,
dst_crs=dst_crs,
geom=feature["geometry"],
)
feature_ = ds.Feature(
geometry=new_geometry,
properties=feature['properties'],
crs=dst_crs,
)
features.append(feature_)
except (KeyError, IndexError) as e:
message = 'Feature #{} have been removed from collection. Error: {}'.format(i, str(e))
warnings.warn(message, RuntimeWarning)
return cls(features, dst_crs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment