Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repos:

# Can run individually with `flynt [file]` or `flynt [source]`
- repo: https://github.com/ikamensh/flynt
rev: '1.0.1'
rev: '1.0.6'
hooks:
- id: flynt
args: ["--fail-on-change", "--verbose"]
Expand Down
92 changes: 65 additions & 27 deletions geometric_features/geometric_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
from importlib.resources import files as imp_res_files

from geometric_features.download import download_files
from geometric_features.feature_collection import (FeatureCollection,
read_feature_collection)
from geometric_features.feature_collection import (
FeatureCollection,
read_feature_collection,
)
from geometric_features.version import __version__


Expand All @@ -25,6 +27,7 @@ class GeometricFeatures(object):
from if files are missing from the local cache

"""

# Authors
# -------
# Xylar Asay-Davis
Expand All @@ -47,24 +50,28 @@ def __init__(self, cacheLocation=None, remoteBranchOrTag=None):
"""

if cacheLocation is None:
if 'GEOMETRIC_DATA_DIR' in os.environ:
self.cacheLocation = os.environ['GEOMETRIC_DATA_DIR']
else:
self.cacheLocation = './geometric_data'
self.cacheLocation = _get_default_cache_location()
else:
self.cacheLocation = cacheLocation
if remoteBranchOrTag is None:
self.remoteBranch = __version__
else:
self.remoteBranch = remoteBranchOrTag

features_file = (imp_res_files('geometric_features') /
'features_and_tags.json')
features_file = (
imp_res_files('geometric_features') / 'features_and_tags.json'
)
with features_file.open('r') as file:
self.allFeaturesAndTags = json.load(file)

def read(self, componentName, objectType, featureNames=None, tags=None,
allTags=True):
def read(
self,
componentName,
objectType,
featureNames=None,
tags=None,
allTags=True,
):
"""
Read one or more features from the cached collection of geometric
features. If any of the requested features have not been cached, they
Expand Down Expand Up @@ -102,10 +109,12 @@ def read(self, componentName, objectType, featureNames=None, tags=None,
# -------
# Xylar Asay-Davis

featureNames = self._get_feature_names(componentName, objectType,
featureNames, tags, allTags)
fileList = self._download_geometric_features(componentName, objectType,
featureNames)
featureNames = self._get_feature_names(
componentName, objectType, featureNames, tags, allTags
)
fileList = self._download_geometric_features(
componentName, objectType, featureNames
)

fc = FeatureCollection()
for fileName in fileList:
Expand Down Expand Up @@ -144,8 +153,9 @@ def split(self, fc, destinationDir=None):
componentName = feature['properties']['component']
objectType = feature['properties']['object']

relativePath = _get_file_name(componentName, objectType,
featureName)
relativePath = _get_file_name(
componentName, objectType, featureName
)
fullPath = os.path.join(destinationDir, relativePath)

path, file = os.path.split(fullPath)
Expand All @@ -160,8 +170,9 @@ def split(self, fc, destinationDir=None):

singleFC.to_geojson(fullPath, stripHistory=True)

def _download_geometric_features(self, componentName, objectType,
featureNames):
def _download_geometric_features(
self, componentName, objectType, featureNames
):
"""
Determine a list of requested files and download the any that are
missing from the repo
Expand Down Expand Up @@ -197,23 +208,28 @@ def _download_geometric_features(self, componentName, objectType,
fileList = []
filesToDownload = []
for featureName in featureNames:
relativePath = _get_file_name(componentName, objectType,
featureName)
relativePath = _get_file_name(
componentName, objectType, featureName
)
fullPath = os.path.join(self.cacheLocation, relativePath)
fileList.append(fullPath)
if not os.path.exists(fullPath):
filesToDownload.append(relativePath)

if len(filesToDownload) > 0:
baseURL = 'https://raw.githubusercontent.com/MPAS-Dev/' \
baseURL = (
'https://raw.githubusercontent.com/MPAS-Dev/'
'geometric_features/{}/geometric_data'.format(
self.remoteBranch)
self.remoteBranch
)
)
download_files(filesToDownload, baseURL, self.cacheLocation)

return fileList

def _get_feature_names(self, componentName, objectType, featureNames,
tags, allTags):
def _get_feature_names(
self, componentName, objectType, featureNames, tags, allTags
):
"""
Find features by name or tags, reporting errors in the process

Expand Down Expand Up @@ -260,7 +276,8 @@ def _get_feature_names(self, componentName, objectType, featureNames,

if objectType not in component:
raise KeyError(
f'invalid object {objectType} in component {componentName}')
f'invalid object {objectType} in component {componentName}'
)

availableFeaturesAndTags = component[objectType]

Expand Down Expand Up @@ -318,7 +335,28 @@ def _get_file_name(componentName, objectType, featureName):
featureDir = featureName.strip().replace(' ', '_').strip('.')
for char in badCharacters:
featureDir = featureDir.replace(char, '')
fileName = os.path.join(componentName, objectType, featureDir,
f'{objectType}.geojson')
fileName = os.path.join(
componentName, objectType, featureDir, f'{objectType}.geojson'
)

return fileName


def _get_default_cache_location():
"""
Get the default location of the local geometric features cache.

Returns
-------
cache_location : str
The default cache location
"""
if 'GEOMETRIC_DATA_DIR' in os.environ:
return os.environ['GEOMETRIC_DATA_DIR']

package_dir = os.path.dirname(os.path.abspath(__file__))
repo_cache = os.path.join(os.path.dirname(package_dir), 'geometric_data')
if os.path.isdir(repo_cache):
return repo_cache

return './geometric_data'
Loading
Loading