Skip to content

Commit 981d2fd

Browse files
authored
Merge pull request #8 from hotosm/feat/plain-geojson
Add function to request plain geojson output
2 parents 8c610d1 + 665e898 commit 981d2fd

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

osm_data_client/client.py

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,60 @@ async def request_snapshot(
9393
log.error("Unexpected error in API request: %s", str(ex))
9494
raise APIRequestError(0, {}, str(ex)) from ex
9595

96+
async def request_plain_geojson_snapshot(
97+
self, geometry: GeometryInput, params: RequestParams
98+
) -> dict[str, Any]:
99+
"""
100+
Request a snapshot of OSM geojson data.
101+
102+
Args:
103+
geometry: Validated GeoJSON geometry object
104+
params: Validated request parameters
105+
106+
Returns:
107+
Plain geojson of osm features
108+
109+
Raises:
110+
APIRequestError: If the API request fails
111+
"""
112+
payload = {
113+
**params.to_api_params(),
114+
"geometry": geometry.to_dict(),
115+
}
116+
117+
log.debug("Requesting snapshot with params: %s", json.dumps(payload))
118+
119+
async with ClientSession() as session:
120+
try:
121+
async with session.post(
122+
f"{self.config.base_api_url}/snapshot/plain/",
123+
json=payload,
124+
headers=self.headers,
125+
) as response:
126+
response_data = await response.json()
127+
if response.status >= 400:
128+
log.error(
129+
"API request failed with status %d: %s",
130+
response.status,
131+
response_data,
132+
)
133+
raise APIRequestError(response.status, response_data)
134+
135+
# Log queue information if available
136+
if "queue" in response_data:
137+
queue_position = response_data.get("queue", 0)
138+
if queue_position > 0:
139+
log.info("Request queued at position %d", queue_position)
140+
141+
log.debug("Snapshot request successful: %s", response_data)
142+
return response_data
143+
except ClientResponseError as ex:
144+
log.error("API client error: %s", str(ex))
145+
raise APIRequestError(ex.status, {}, str(ex)) from ex
146+
except Exception as ex:
147+
log.error("Unexpected error in API request: %s", str(ex))
148+
raise APIRequestError(0, {}, str(ex)) from ex
149+
96150
async def poll_task_status(
97151
self, task_link: str, polling_interval: int = 2
98152
) -> dict[str, Any]:
@@ -250,7 +304,7 @@ async def get_osm_data(
250304
geometry: dict[str, Any] | str,
251305
output_options: RawDataOutputOptions = RawDataOutputOptions.default(),
252306
**kwargs,
253-
) -> RawDataResult:
307+
) -> RawDataResult | dict:
254308
"""
255309
Get OSM data for a specified area.
256310
@@ -285,6 +339,14 @@ async def get_osm_data(
285339
geometry_input = GeometryInput.from_input(geometry)
286340
params = RequestParams.from_kwargs(**kwargs)
287341

342+
if (
343+
params.output_type == "geojson"
344+
and params.bind_zip
345+
and not output_options.download_file
346+
):
347+
log.info("Requesting OSM geojson data snapshot")
348+
return await self.api.request_plain_geojson_snapshot(geometry_input, params)
349+
288350
# Request snapshot
289351
log.info("Requesting OSM data snapshot for %s", params.file_name)
290352
task_response = await self.api.request_snapshot(geometry_input, params)

0 commit comments

Comments
 (0)