Skip to content

Commit

Permalink
Merge pull request brazil-data-cube#36 from fabianazioti/b-0.6
Browse files Browse the repository at this point in the history
WIP update base on lccs spec 0.6 working on brazil-data-cube#35
  • Loading branch information
gqueiroz authored Feb 12, 2021
2 parents 0b7d9d6 + 658e6b9 commit 4a9f65a
Show file tree
Hide file tree
Showing 10 changed files with 346 additions and 260 deletions.
2 changes: 1 addition & 1 deletion docs/sphinx/classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Classes
-------


.. autoclass:: lccs.classes::ClassificationSystemClasses
.. autoclass:: lccs.classes::ClassificationSystemClass
:members:
:special-members: __init__
:member-order: bysource
20 changes: 9 additions & 11 deletions docs/sphinx/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ If you want to know the LCCS version, use the option ``--version`` as in::

Output::

lccs, version 0.4.0.post0
lccs, version 0.6.0


To list the available classification systems in a service, use the ``classification-systems`` command and provides a URL to the ``--url`` option::
Expand All @@ -32,7 +32,7 @@ The above command will return a list of classification system names as::

To get more information about a specific classification system, use the ``classification-systems-describe`` command::

lccs --url 'http://brazildatacube.dpi.inpe.br/dev/lccs/' classification-systems-describe --system_id 'PRODES'
lccs --url 'http://brazildatacube.dpi.inpe.br/dev/lccs/' classification-systems-describe --system_name 'PRODES-1.0'

Output::

Expand All @@ -46,7 +46,7 @@ Output::

List the available classes of a classification system, use the ``classes`` command::

lccs --url 'http://brazildatacube.dpi.inpe.br/dev/lccs/' classes --system_id 'PRODES'
lccs --url 'http://brazildatacube.dpi.inpe.br/dev/lccs/' classes --system_name 'PRODES-1.0'

The above command will return a list of classes of PRODES as::

Expand All @@ -59,23 +59,21 @@ The above command will return a list of classes of PRODES as::

To get more information about a specific class, use the ``class-describe`` command::

lccs --url 'http://brazildatacube.dpi.inpe.br/dev/lccs/' class-describe --system_id 'PRODES' --class_id 'Desmatamento'
lccs --url 'http://brazildatacube.dpi.inpe.br/dev/lccs/' class-describe --system_name 'PRODES-1.0' --class_name 'Desflorestamento'

The above command will return a::

- class_parent: None
- classification_system_id: 4
- classification_system_name: PRODES
- code: DESMATAMENTO
- classification_system_id: 1
- code: DESFLORESTAMENTO
- description:
- id: 1
- links: [[{'href': 'http://brazildatacube.dpi.inpe.br/dev/lccs/classification_system/PRODES/classes/Desmatamento', 'rel': 'self', 'title': 'Link to this document', 'type': 'application/json'},...]
- name: Desmatamento
- links: [[{'href': 'http://brazildatacube.dpi.inpe.br/dev/lccs/classification_system/1/classes/1', 'rel': 'self', 'title': 'Link to this document', 'type': 'application/json'},...]
- name: Desflorestamento


Retrieve all available classification system mappings, use the ``available-mappings`` command::

lccs --url 'http://brazildatacube.dpi.inpe.br/dev/lccs/' available-mappings --system_id_source 'TerraClass_AMZ'
lccs --url 'http://brazildatacube.dpi.inpe.br/dev/lccs/' available-mappings --system_name_source 'TerraClass_AMZ'

The above command will return a list of classification systems as::

Expand Down
51 changes: 28 additions & 23 deletions examples/ex-01.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,48 @@

print(lccs.__version__)

url = os.environ.get('LCCS_SERVER_URL', 'http://brazildatacube.dpi.inpe.br/dev/lccs/')
url = os.environ.get('LCCS_SERVER_URL', 'https://brazildatacube.dpi.inpe.br/dev/lccs/')

service = lccs.LCCS(url)
service = lccs.LCCS('http://127.0.0.1:5000/')

print("Classificatom System Avaliable on Service:")
print("Return all classificaton systems available in service")
print(service.classification_systems)

# The examples presented in this code vary depending on the database used. Check the parameters informed.

print("\nInformations about TerraClass_AMZ Classification System:")
class_system = service.classification_system(system_id='TerraClass_AMZ')
# Return a specific classification system
class_system = service.classification_system('PRODES-1.0')
print(class_system.description)
print(class_system.name)

print("\nTerraClass_AMZ Classes: \n")
classes = class_system.classes()
print(classes)
# Return a classes of a classification system
classes = class_system.classes
for i in classes:
print(i.name)

print("\nTerraClass_AMZ Desflorestamento informations: \n")
class_desflorestamento = class_system.classes(class_id='Desflorestamento')
print(class_desflorestamento)
# Return a specific class of a classification system
prodes_desflorestamento = class_system.get_class('Desflorestamento')
print(prodes_desflorestamento.name)

all_mapping = service.available_mappings(system_id_source='TerraClass_AMZ')
# Get all available mappings for a specific classification system
all_mapping = service.available_mappings(system_source_name='PRODES-1.0')
print(all_mapping)

mapping = service.mappings(system_id_source='TerraClass_AMZ', system_id_target='PRODES')
# Get mapping
mapping = service.mappings(system_name_source='PRODES-1.0', system_name_target='TerraClass_AMZ-1.0')

print("\nMapping {} to {}: \n".format(mapping.source_classification_system, mapping.target_classification_system))
print(f"\nMapping PRODES-1.0 to TerraClass_AMZ-1.0: \n")

for mp in mapping.mapping:
print("Source Class: {} | Target Class: {}".format(mp.source_class, mp.target_class))
for mp in mapping:
print(mp)

# Get all styles avaliable for MapBiomas4
styles = service.styles(system_id='MapBiomas5')
# Get all styles available for a specific classification system
style_formats = service.style_formats(system_source_name='PRODES-1.0')
for style_f in style_formats:
print(style_f.name)

print(styles)

# Save Style File
service.get_styles(system_id='MapBiomas5', format_id='QGIS')
# Save a style file of a specific classification system and style format
service.get_style(system_name='PRODES-1.0', format_name='QGIS')

# Save Style File passing the path directory
service.get_styles(system_id='MapBiomas5', format_id='QGIS', path='/home/user/Downloads/')
service.get_style(system_name='PRODES-1.0', format_name='QGIS', path='/home/user/Downloads/')
37 changes: 29 additions & 8 deletions lccs/class_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#
"""Python Client Library for the LCCS Web Service."""

from .classes import ClassificationSystemClass, ClassificationSystemClasses
from .classes import ClassificationSystemClass
from .link import Link
from .utils import Utils

Expand All @@ -23,6 +23,7 @@ def __init__(self, data, validate=False):
"""
self._validate = validate
super(ClassificationSystem, self).__init__(data or {})
self._get_classes()

@property
def links(self):
Expand Down Expand Up @@ -54,18 +55,38 @@ def authority_name(self):
""":return: authority_name of classification system."""
return self['authority_name']

def classes(self, class_id=None, filter=None):
""":return: Classes from the class system."""
@property
def classes(self):
""":return: classes of the classification system."""
return self['classes']

def get_class(self, class_name):
""":return: a specif class of of the classification system."""
for i in self.classes:
if i.name == class_name:
return ClassificationSystemClass(i, self._validate)

def _get_classes(self, filter=None):
""":return: get classes of the classification system.."""
classes = list()
for link in self['links']:
if link['rel'] == 'classes':
if class_id is not None:
data = Utils._get('{}/{}'.format(link["href"], class_id))
return ClassificationSystemClass(data, self._validate)
data = Utils._get(link['href'], params=filter)
return ClassificationSystemClasses(data).get_class
return ClassificationSystemClasses({})
[classes.append(ClassificationSystemClass(Utils._get(i['href'], self._validate), self._validate)) for i in data if i['rel'] == 'child']
self['classes'] = classes
return ClassificationSystemClass({})

def _repr_html_(self):
"""HTML repr."""
return Utils.render_html('classification_system.html', classification_system=self)

def __repr__(self):
"""Return the string representation of a classification system object."""
text = f'{self.id}:{self.name}- Version {self.version}'
return text

def __str__(self):
"""Return the string representation of a classification system object."""
return f'<Classification System [{self.id}:{self.name}- Version {self.version}]>'


31 changes: 8 additions & 23 deletions lccs/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
# under the terms of the MIT License; see LICENSE file for more details.
#
"""Python Client Library for the LCCS Web Service."""
from .utils import Utils


class ClassificationSystemClass(dict):
"""Class."""
"""Class of the classification system."""

def __init__(self, data, validate=False):
"""Initialize instance with dictionary data.
Expand All @@ -24,39 +23,25 @@ def __init__(self, data, validate=False):

@property
def id(self):
""":return: the Class id."""
""":return: the class id."""
return self['id']

@property
def description(self):
""":return: the Class description."""
""":return: the class description."""
return self['description']

@property
def name(self):
""":return: the Class identifier (name)."""
""":return: the class name."""
return self['name']

@property
def code(self):
""":return: the Class code."""
""":return: the class code."""
return self['code']


class ClassificationSystemClasses(dict):
"""Classifications System Classes."""

def __init__(self, data, validate=False):
"""Initialize instance with dictionary data.
:param data: Dict with Classes metadata.
:param validate: true if the Classes should be validate using its jsonschema. Default is False.
"""
self._validate = validate
super(ClassificationSystemClasses, self).__init__(data or {})

@property
def get_class(self):
""":return: list of classes."""
return [ClassificationSystemClass(Utils._get(i['href'], self._validate), self._validate) for i in self['links'] if (i['rel'] == 'child')]
def class_parent_id(self):
""":return: the class code."""
return self['class_parent_id']
Loading

0 comments on commit 4a9f65a

Please sign in to comment.