Skip to content

Commit 8f8202b

Browse files
committed
Fix python code example #880
1 parent d785daa commit 8f8202b

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

web-client/src/utils/reqParmsConverters.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export function jsonToPythonDict(data: unknown, indent: number = 0): string {
6464
}
6565

6666
/**
67-
* Maps web client endpoints to SlideRule run() API names
67+
* Maps web client endpoints to SlideRule run() API names (for non-GEDI endpoints)
6868
*/
6969
const endpointToRunName: Record<string, string> = {
7070
atl06: 'atl06',
@@ -78,10 +78,16 @@ const endpointToRunName: Record<string, string> = {
7878
atl08: 'atl08',
7979
atl08p: 'atl08',
8080
atl24x: 'atl24x',
81-
atl13x: 'atl13x',
82-
gedi01bp: 'gedi01b',
83-
gedi02ap: 'gedi02a',
84-
gedi04ap: 'gedi04a'
81+
atl13x: 'atl13x'
82+
}
83+
84+
/**
85+
* Maps GEDI endpoints to their gedi module function names
86+
*/
87+
const gediEndpointToFunc: Record<string, string> = {
88+
gedi01bp: 'gedi01bp',
89+
gedi02ap: 'gedi02ap',
90+
gedi04ap: 'gedi04ap'
8591
}
8692

8793
/**
@@ -107,15 +113,26 @@ function getOutputFormat(jsonData: unknown): string | null {
107113
*/
108114
export function generatePythonClientCode(jsonData: unknown, endpoint: string): string {
109115
const normalizedEndpoint = endpoint.toLowerCase().trim()
110-
const runName = endpointToRunName[normalizedEndpoint] || normalizedEndpoint
111-
112116
const parmsDict = jsonToPythonDict(jsonData, 0)
113117

118+
// Check if this is a GEDI endpoint
119+
const gediFunc = gediEndpointToFunc[normalizedEndpoint]
120+
const isGedi = !!gediFunc
121+
114122
// Check if output format is specified (returns file path instead of GeoDataFrame)
115123
const outputFormat = getOutputFormat(jsonData)
116124
const hasFileOutput =
117125
outputFormat === 'parquet' || outputFormat === 'geoparquet' || outputFormat === 'csv'
118126

127+
// Determine import and execute statements based on endpoint type
128+
const importStatement = isGedi
129+
? 'from sliderule import sliderule, gedi'
130+
: 'from sliderule import sliderule'
131+
132+
const executeStatement = isGedi
133+
? `gedi.${gediFunc}(parms)`
134+
: `sliderule.run("${endpointToRunName[normalizedEndpoint] || normalizedEndpoint}", parms)`
135+
119136
if (hasFileOutput) {
120137
// When output format is specified, the API returns a file path
121138
const geopandasImport =
@@ -128,7 +145,7 @@ SlideRule request generated from web client
128145
Endpoint: ${endpoint}
129146
"""
130147
131-
from sliderule import sliderule
148+
${importStatement}
132149
${geopandasImport}
133150
134151
# Initialize SlideRule client
@@ -138,7 +155,7 @@ sliderule.init("slideruleearth.io")
138155
parms = ${parmsDict}
139156
140157
# Execute request (returns file path when output format is specified)
141-
output_file = sliderule.run("${runName}", parms)
158+
output_file = ${executeStatement}
142159
print(f"Data saved to: {output_file}")
143160
144161
# Read the output file into a GeoDataFrame
@@ -156,7 +173,7 @@ SlideRule request generated from web client
156173
Endpoint: ${endpoint}
157174
"""
158175
159-
from sliderule import sliderule
176+
${importStatement}
160177
161178
# Initialize SlideRule client
162179
sliderule.init("slideruleearth.io")
@@ -165,7 +182,7 @@ sliderule.init("slideruleearth.io")
165182
parms = ${parmsDict}
166183
167184
# Execute request
168-
gdf = sliderule.run("${runName}", parms)
185+
gdf = ${executeStatement}
169186
170187
# Display results
171188
print(f"Retrieved {len(gdf)} records")

0 commit comments

Comments
 (0)