11
2- from geoengine .auth import Session , initialize_session
3- from typing import Any
2+ from logging import debug
3+ from geoengine .types import Bbox
4+ from geoengine .error import GeoEngineException
5+ from geoengine .auth import Session , get_session
6+ from typing import Any , Dict
47import geopandas as gpd
58import requests as req
69from owslib .wfs import WebFeatureService
@@ -15,9 +18,77 @@ def get_features(self, workflow_id: Any, bbox: Any) -> gpd.GeoDataFrame:
1518 pass
1619
1720
18- def initialize ( server_url : str ) -> Accessor :
19- '''
20- Initialize communication between this library and a Geo Engine instance
21- '''
21+ class WorkflowId :
22+ def __init__ ( self , response : Dict [ str , str ]) -> None :
23+ if not 'id' in response :
24+ raise GeoEngineException ( response )
2225
23- Accessor (initialize_session (server_url ))
26+ self .__id = response ['id' ]
27+
28+ def __str__ (self ) -> str :
29+ return self .__id
30+
31+
32+ def register_workflow (workflow : str ) -> WorkflowId :
33+ session = get_session ()
34+
35+ workflow_response = req .post (
36+ f'{ session .server_url ()} /workflow' ,
37+ json = workflow ,
38+ headers = session .auth_headers ()
39+ ).json ()
40+
41+ return WorkflowId (workflow_response )
42+
43+
44+ def geopandas_by_workflow_id (workflow_id : WorkflowId , bbox : Bbox ) -> gpd .GeoDataFrame :
45+ session = get_session ()
46+
47+ # wfs = WebFeatureService(url=f'{session.server_url()}/wfs', version='2.0.0')
48+
49+ # TODO: add resolution
50+ # TODO: customize SRS
51+ params = dict (
52+ service = 'WFS' ,
53+ version = "2.0.0" ,
54+ request = 'GetFeature' ,
55+ outputFormat = 'application/json' ,
56+ typeNames = f'registry:{ workflow_id } ' ,
57+ bbox = bbox .bbox_str (),
58+ time = bbox .time_str (),
59+ srsName = 'EPSG:4326' ,
60+ )
61+
62+ wfs_url = req .Request (
63+ 'GET' , url = f'{ session .server_url ()} /wfs' , params = params ).prepare ().url
64+
65+ debug (f'WFS URL:\n { wfs_url } ' )
66+ print (f'WFS URL:\n { wfs_url } ' )
67+
68+ data_response = req .get (wfs_url , headers = session .auth_headers ())
69+
70+ def geo_json_with_time_to_geopandas (data_response ):
71+ '''
72+ GeoJson has no standard for time, so we parse the when field
73+ separately and attach it to the data frame as columns `start`
74+ and `end`.
75+ '''
76+
77+ data = gpd .read_file (StringIO (data_response .text ))
78+
79+ geo_json = data_response .json ()
80+ start = [f ['when' ]['start' ] for f in geo_json ['features' ]]
81+ end = [f ['when' ]['end' ] for f in geo_json ['features' ]]
82+
83+ data ['start' ] = gpd .pd .to_datetime (start )
84+ data ['end' ] = gpd .pd .to_datetime (end )
85+
86+ return data
87+
88+ return geo_json_with_time_to_geopandas (data_response )
89+
90+
91+ def geopandas_by_workflow (workflow : str , bbox : Bbox ) -> gpd .GeoDataFrame :
92+ workflow_id = register_workflow (workflow )
93+
94+ return geopandas_by_workflow_id (workflow_id , bbox )
0 commit comments