1
1
import json
2
2
import pathlib
3
- from typing import Union
3
+ from typing import Union , Optional
4
4
import typing
5
5
6
- from openeo .internal .graph_building import PGNode
6
+ from openeo .internal .graph_building import PGNode , _FromNodeMixin
7
7
from openeo .metadata import CollectionMetadata
8
+ from openeo .rest ._datacube import _ProcessGraphAbstraction , THIS
8
9
from openeo .rest .job import RESTJob
9
10
from openeo .util import legacy_alias
10
11
11
-
12
12
if hasattr (typing , 'TYPE_CHECKING' ) and typing .TYPE_CHECKING :
13
13
# Imports for type checking only (circular import issue at runtime). `hasattr` is Python 3.5 workaround #210
14
14
from openeo import Connection
15
15
16
16
17
- class VectorCube :
17
+ class VectorCube ( _ProcessGraphAbstraction ) :
18
18
"""
19
19
A Vector Cube, or 'Vector Collection' is a data structure containing 'Features':
20
20
https://www.w3.org/TR/sdw-bp/#dfn-feature
@@ -24,53 +24,26 @@ class VectorCube:
24
24
"""
25
25
26
26
def __init__ (self , graph : PGNode , connection : 'Connection' , metadata : CollectionMetadata = None ):
27
- super ().__init__ ()
28
- # Process graph
29
- self ._pg = graph
30
- self ._connection = connection
27
+ super ().__init__ (pgnode = graph , connection = connection )
28
+ # TODO: does VectorCube need CollectionMetadata?
31
29
self .metadata = metadata
32
30
33
- def __str__ (self ):
34
- return "DataCube({pg})" .format (pg = self ._pg )
35
-
36
- @property
37
- def graph (self ) -> dict :
38
- """Get the process graph in flat dict representation"""
39
- return self .flat_graph ()
40
-
41
- def flat_graph (self ) -> dict :
42
- """Get the process graph in flat dict representation"""
43
- return self ._pg .flat_graph ()
44
-
45
- flatten = legacy_alias (flat_graph , name = "flatten" )
46
-
47
- def to_json (self , indent = 2 , separators = None ) -> str :
48
- """
49
- Get JSON representation of (flat dict) process graph.
50
- """
51
- pg = {"process_graph" : self .flat_graph ()}
52
- return json .dumps (pg , indent = indent , separators = separators )
53
-
54
- @property
55
- def _api_version (self ):
56
- return self ._connection .capabilities ().api_version_check
57
-
58
- @property
59
- def connection (self ):
60
- return self ._connection
61
-
62
- def process (self , process_id : str , args : dict = None , metadata : CollectionMetadata = None , ** kwargs ) -> 'VectorCube' :
31
+ def process (
32
+ self ,
33
+ process_id : str ,
34
+ arguments : dict = None ,
35
+ metadata : Optional [CollectionMetadata ] = None ,
36
+ namespace : Optional [str ] = None ,
37
+ ** kwargs ) -> 'VectorCube' :
63
38
"""
64
39
Generic helper to create a new DataCube by applying a process.
65
40
66
41
:param process_id: process id of the process.
67
42
:param args: argument dictionary for the process.
68
43
:return: new DataCube instance
69
44
"""
70
- return self .process_with_node (PGNode (
71
- process_id = process_id ,
72
- arguments = args , ** kwargs
73
- ), metadata = metadata )
45
+ pg = self ._build_pgnode (process_id = process_id , arguments = arguments , namespace = namespace , ** kwargs )
46
+ return VectorCube (graph = pg , connection = self ._connection , metadata = metadata or self .metadata )
74
47
75
48
def process_with_node (self , pg : PGNode , metadata : CollectionMetadata = None ) -> 'VectorCube' :
76
49
"""
@@ -80,22 +53,21 @@ def process_with_node(self, pg: PGNode, metadata: CollectionMetadata = None) ->
80
53
:param metadata: (optional) metadata to override original cube metadata (e.g. when reducing dimensions)
81
54
:return: new DataCube instance
82
55
"""
83
- from openeo .rest .datacube import DataCube , THIS
84
56
arguments = pg .arguments
85
57
for k , v in arguments .items ():
86
- if isinstance (v , DataCube ) or isinstance (v , VectorCube ):
87
- arguments [k ] = {"from_node" : v ._pg }
88
- elif v is THIS :
89
- arguments [k ] = {"from_node" : self ._pg }
58
+ # TODO: it's against intended flow to resolve THIS and _FromNodeMixin at this point (should be done before building PGNode)
59
+ if v is THIS :
60
+ v = self
61
+ if isinstance (v , _FromNodeMixin ):
62
+ arguments [k ] = {"from_node" : v .from_node ()}
90
63
# TODO: deep copy `self.metadata` instead of using same instance?
91
- # TODO: cover more cases where metadata has to be altered
92
64
return VectorCube (graph = pg , connection = self ._connection , metadata = metadata or self .metadata )
93
65
94
66
def save_result (self , format : str = "GeoJson" , options : dict = None ):
95
67
return self .process (
96
68
process_id = "save_result" ,
97
- args = {
98
- "data" : { "from_node" : self . _pg } ,
69
+ arguments = {
70
+ "data" : self ,
99
71
"format" : format ,
100
72
"options" : options or {}
101
73
}
0 commit comments