4
4
"""Class for ohsome API response"""
5
5
6
6
import json
7
- from typing import Optional
7
+ from typing import Optional , Union
8
8
9
9
import geopandas as gpd
10
10
import pandas as pd
@@ -25,7 +25,7 @@ def __init__(self, response=None, url=None, params=None):
25
25
26
26
def as_dataframe (
27
27
self , multi_index : Optional [bool ] = True , explode_tags : Optional [tuple ] = ()
28
- ):
28
+ ) -> Union [ pd . DataFrame , gpd . GeoDataFrame ] :
29
29
"""
30
30
Converts the ohsome response to a pandas.DataFrame or a geopandas.GeoDataFrame if the
31
31
response contains geometries
@@ -40,13 +40,7 @@ def as_dataframe(
40
40
else :
41
41
return self ._as_geodataframe (multi_index , explode_tags )
42
42
43
- def _as_dataframe (self , multi_index = True ):
44
- """
45
- Converts the ohsome response to a pandas.DataFrame
46
- :param multi_index: If true returns the dataframe with a multi index
47
- :return: pandas.DataFrame
48
- """
49
-
43
+ def _as_dataframe (self , multi_index = True ) -> pd .DataFrame :
50
44
groupby_names = []
51
45
if "result" in self .data .keys ():
52
46
result_df = pd .DataFrame ().from_records (self .data ["result" ])
@@ -79,15 +73,17 @@ def _as_dataframe(self, multi_index=True):
79
73
80
74
def _as_geodataframe (
81
75
self , multi_index : Optional [bool ] = True , explode_tags : Optional [tuple ] = ()
82
- ):
83
- """
84
- Converts the ohsome response to a geopandas.GeoDataFrame
85
- :param multi_index: If true returns the dataframe with a multi index
86
- :return: geopandas.GeoDataFrame
87
- """
88
-
76
+ ) -> gpd .GeoDataFrame :
89
77
if len (self .data ["features" ]) == 0 :
90
- return gpd .GeoDataFrame (crs = "epsg:4326" , columns = ["@osmId" , "geometry" ])
78
+ return gpd .GeoDataFrame (
79
+ crs = "epsg:4326" ,
80
+ columns = ["@osmId" , "geometry" ]
81
+ + (
82
+ list (explode_tags ) + ["@other_tags" ]
83
+ if explode_tags is not None
84
+ else []
85
+ ),
86
+ )
91
87
92
88
try :
93
89
if explode_tags is not None :
@@ -128,7 +124,7 @@ def _as_geodataframe(
128
124
129
125
return features .sort_index ()
130
126
131
- def to_json (self , outfile ):
127
+ def to_json (self , outfile ) -> None :
132
128
"""
133
129
Write response to json file
134
130
:return:
@@ -137,7 +133,7 @@ def to_json(self, outfile):
137
133
with open (outfile , "w" , encoding = "utf-8" ) as dst :
138
134
json .dump (self .data , dst , indent = 2 , ensure_ascii = False )
139
135
140
- def _set_index (self , result_df , groupby_names ):
136
+ def _set_index (self , result_df , groupby_names ) -> None :
141
137
"""
142
138
Set multi-index based on groupby names and time
143
139
:param result_df:
0 commit comments