kartaview
KartaView related functionality.
KartaViewClient
Client for fetching KartaView image metadata via bounding boxes.
Records are validated against KartaViewImage and malformed ones are skipped
individually. No authentication is required, but authenticated requests get a
higher rate limit (1000 rather than 100 requests per hour).
Usage example
from streetscapes.sources.kartaview import KartaViewClient
client = KartaViewClient() # or KartaViewClient(token) to authenticate
bbox: (west, south, east, north)
bbox = (4.899, 52.372, 4.901, 52.374)
Fetch as pandas DataFrame
df = client.fetch_metadata_bbox(bbox)
fetch directly as GeoDataFrame
gdf = client.fetch_metadata_bbox_gpd(bbox)
__init__(token=None, retries=3, timeout=60)
Instantiate the client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str, optional KartaView access token. Not required, but raises the rate limit. |
required | |
retries
|
int, optional Number of request retries on failure (default is 3). |
required | |
timeout
|
int, optional Seconds to wait for a response (default is 60). A request normally takes about a second, but the API regularly stalls for much longer than that. |
required |
db_fields
property
Get schema's fields.
download_image(url, output_dir, image_id, uid=None, skip_existing=True)
Download image from a URL to output_path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The download URL. |
required |
output_dir
|
str | Path
|
Destination directory. |
required |
image_id
|
int | str | None
|
KartaView image ID. |
required |
uid
|
UUID | None
|
Image UUID (from the SHA-256 hash). |
None
|
skip_existing
|
bool
|
Don't re-download existing images. |
True
|
Returns:
| Type | Description |
|---|---|
ImageMeta
|
Image metadata. |
fetch_image_url(image_id)
Fetch the image URL from the KartaView API by image ID.
fetch_metadata_bbox(bbox, limit=1000, pano_only=False, daytime_only=False)
Fetch metadata for a bounding box and convert to a pandas DataFrame.
Every record is validated against KartaViewImage before being included;
records that fail validation are skipped and reported.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
tuple[float, float, float, float] Bounding box as (west, south, east, north). |
required | |
limit
|
int Maximum number of images to fetch (default 1000, 0 for no limit). |
required | |
pano_only
|
bool Only fetch panoramic images (default False). |
required | |
daytime_only
|
bool Only fetch images captured with the sun at least 2 degrees high (default False). |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame DataFrame with KartaView metadata. |
fetch_metadata_bbox_gpd(bbox, limit=1000, pano_only=False, daytime_only=False)
Fetch metadata for a bounding box and convert to a GeoDataFrame.
Geometry columns are parsed from WKT and the CRS is set to EPSG:4326.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
tuple[float, float, float, float] Bounding box as (west, south, east, north). |
required | |
limit
|
int Maximum number of images to fetch (default 1000, 0 for no limit). |
required | |
pano_only
|
bool Only fetch panoramic images (default False). |
required | |
daytime_only
|
bool Only fetch images captured in daylight (default False). |
required |
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
gpd.GeoDataFrame GeoDataFrame with KartaView metadata and geometry columns. |
KartaViewError
Bases: RuntimeError
Raised when the KartaView API cannot be reached.
KartaViewImage
Bases: BaseModel
KartaView image record, validated against the database schema.
KartaView returns every value as a string, spells its fields in camelCase and
splits positions over separate lat/lng fields, so raw API records are
normalised (see _from_api) before the fields below are validated.
Field names and types mirror the kartaview table schema, except for image
which is only known after the image itself has been downloaded, and
altitude, which KartaView does not report at all.
to_row()
Get the record as a row for the kartaview table.
filter_listed(images, pano_only=False, daytime_only=False)
Filter listed images on what the listing endpoint already reports.
Doing so before the full records are fetched saves requests, and lets a limit count only the images that are kept.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
list[dict]
|
Image records as returned by the listing endpoint. |
required |
pano_only
|
bool
|
Only keep panoramic images. |
False
|
daytime_only
|
bool
|
Only keep images captured in daylight (see
|
False
|
Returns:
| Type | Description |
|---|---|
list[dict]
|
The images that were kept. |
validate_records(records)
Validate raw API records, dropping (and reporting) the invalid ones.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
records
|
list[dict]
|
Raw image records as returned by the KartaView API. |
required |
Returns:
| Type | Description |
|---|---|
list[KartaViewImage]
|
The records that passed validation. |