Spain (NAP)¶
NAPDownloader ¶
Bases: BaseGTFSDownloader
Client for searching and downloading feeds from Spain's NAP.
Requires a NAP API key, available after registering at https://nap.transportes.gob.es.
Initialize the client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_key
|
Optional[str]
|
NAP API key. If not provided, it is read from the
|
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If no API key is available. |
Source code in pyGTFSHandler/downloaders/spain/nap.py
get_region_id ¶
Resolve a region name to its NAP region id via fuzzy matching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Region name to look up (e.g. a municipality name). |
required |
region_type
|
Union[int, str]
|
Which kind of region to search among: a NAP category name/alias (e.g. "municipio", "province") or its integer index (0=Provincia, 1=ComunidadAutonoma, 2=AreaUrbana, 3=Municipio). |
3
|
Returns:
| Type | Description |
|---|---|
Optional[int]
|
The matched region's NAP id, or |
Optional[int]
|
type was found or none matched closely enough. |
Source code in pyGTFSHandler/downloaders/spain/nap.py
get_transport_type_id ¶
Resolve a transport mode name to its NAP transport type id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transport_name
|
str
|
Transport mode name or alias (e.g. "bus", "train", "ferry"). |
required |
Returns:
| Type | Description |
|---|---|
Optional[int]
|
The matched transport type's NAP id, or |
Source code in pyGTFSHandler/downloaders/spain/nap.py
get_file_type_id ¶
Resolve a file format name (e.g. "GTFS") to its NAP file type id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_type
|
str
|
File format name, e.g. "GTFS" or "NeTEx". |
'GTFS'
|
Returns:
| Type | Description |
|---|---|
Optional[int]
|
The matched file type's NAP id, or |
Source code in pyGTFSHandler/downloaders/spain/nap.py
get_organization_id ¶
Resolve an organization/operator name to its NAP organization id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
organization_name
|
str
|
Organization name to look up. |
required |
Returns:
| Type | Description |
|---|---|
Optional[int]
|
The matched organization's NAP id, or |
Source code in pyGTFSHandler/downloaders/spain/nap.py
find_datasets ¶
find_datasets(region: Union[int, str, List[Union[int, str]]] = (), transport_type: Union[int, str, List[Union[int, str]]] = (), organization: Union[int, str, List[Union[int, str]]] = (), file_type: Union[int, str] = 'GTFS', region_type: Union[int, str, List[Union[int, str]]] = 3, start_date: Optional[Union[str, datetime]] = None, end_date: Optional[Union[str, datetime]] = None, file_description: Union[str, List[str]] = (), keep: str = 'newest') -> List[Dict[str, Any]]
Find NAP datasets ("conjuntos de datos") matching filters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
region
|
Union[int, str, List[Union[int, str]]]
|
Region name(s) or NAP region id(s) to filter by. |
()
|
transport_type
|
Union[int, str, List[Union[int, str]]]
|
Transport mode name(s) or NAP transport type id(s) to filter by. |
()
|
organization
|
Union[int, str, List[Union[int, str]]]
|
Organization name(s) or NAP organization id(s) to filter by. |
()
|
file_type
|
Union[int, str]
|
File format name or NAP file type id. Defaults to "GTFS". |
'GTFS'
|
region_type
|
Union[int, str, List[Union[int, str]]]
|
Region category name(s)/id(s) that |
3
|
start_date
|
Optional[Union[str, datetime]]
|
If set (with |
None
|
end_date
|
Optional[Union[str, datetime]]
|
End of the validity range; see |
None
|
file_description
|
Union[str, List[str]]
|
Substring(s) to match against each dataset's name/description. |
()
|
keep
|
str
|
When filtering by date and a dataset has multiple matching files, which to keep: "newest", "oldest", or "all". |
'newest'
|
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
A list of raw NAP dataset dictionaries (each with a |
List[Dict[str, Any]]
|
|
Source code in pyGTFSHandler/downloaders/spain/nap.py
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | |
search_feeds ¶
search_feeds(region: Union[int, str, List[Union[int, str]]] = (), transport_type: Union[int, str, List[Union[int, str]]] = (), organization: Union[int, str, List[Union[int, str]]] = (), file_type: Union[int, str] = 'GTFS', region_type: Union[int, str, List[Union[int, str]]] = 3, start_date: Optional[Union[str, datetime]] = None, end_date: Optional[Union[str, datetime]] = None, file_description: Union[str, List[str]] = (), keep: str = 'newest') -> List[GTFSFeedMetadata]
Search NAP for GTFS feeds matching filters.
See find_datasets for the filter arguments. Each NAP dataset can
bundle several downloadable files; this returns one
GTFSFeedMetadata per file.
Returns:
| Type | Description |
|---|---|
List[GTFSFeedMetadata]
|
A list of |
Source code in pyGTFSHandler/downloaders/spain/nap.py
download_feeds ¶
download_feeds(feeds: List[GTFSFeedMetadata], download_folder: str, overwrite: bool = False, unzip: bool = True) -> List[str]
Download previously found NAP feeds to a local folder.
Overrides the base implementation to send the ApiKey header
required by NAP's download endpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feeds
|
List[GTFSFeedMetadata]
|
Feeds to download, as returned by |
required |
download_folder
|
str
|
Directory to store (and, if |
required |
overwrite
|
bool
|
If True, re-download and replace files that already exist on disk. |
False
|
unzip
|
bool
|
If True, extract each ZIP after downloading and delete the ZIP file. |
True
|
Returns:
| Type | Description |
|---|---|
List[str]
|
Absolute paths to the downloaded feeds. |
Source code in pyGTFSHandler/downloaders/spain/nap.py
download_historic_stack ¶
download_historic_stack(output_path: str, datasets: Union[Dict[str, Any], List[Dict[str, Any]]], start_date: Union[str, datetime], end_date: Union[str, datetime], day_separation: int = 1, overwrite: bool = False, aoi: Optional[AOIType] = None) -> List[str]
Download and stitch a dataset's publication history into one feed.
For each dataset, downloads every historic publication whose date
falls within [start_date, end_date] (plus the publication
immediately preceding it, to cover the start of the range), trims
each publication's calendar.txt/calendar_dates.txt so
consecutive publications don't overlap, and merges them via
pyGTFSHandler.utils.stack_gtfs.historic_stack.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_path
|
str
|
Directory to assemble the stitched feed(s) in. |
required |
datasets
|
Union[Dict[str, Any], List[Dict[str, Any]]]
|
One or more raw NAP dataset dictionaries, as
returned by |
required |
start_date
|
Union[str, datetime]
|
Start of the date range to cover. Accepts
"today", |
required |
end_date
|
Union[str, datetime]
|
End of the date range to cover. |
required |
day_separation
|
int
|
Minimum number of days a publication is assumed to stay valid for, if service periods don't force it shorter. |
1
|
overwrite
|
bool
|
If True, redo datasets whose stitched output already exists. |
False
|
aoi
|
Optional[AOIType]
|
Optional AOI passed through to
|
None
|
Returns:
| Type | Description |
|---|---|
List[str]
|
Paths to each dataset's stitched |
List[str]
|
|
Source code in pyGTFSHandler/downloaders/spain/nap.py
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 | |
utils ¶
Helpers specific to the Spanish NAP downloader (downloaders.spain.nap).
These functions are not reused by any other downloader (unlike
downloaders.utils, which holds source-agnostic helpers), which is why
they live under downloaders/spain/ instead: they deal with NAP-specific
concerns -- parsing the flexible date formats accepted by NAP's
find_files, and stitching together consecutive historic NAP GTFS
publications (calendar.txt/calendar_dates.txt trimming) ahead of
pyGTFSHandler.utils.stack_gtfs.historic_stack.
input_date ¶
input_date(start_date: Optional[Union[str, date, datetime]], end_date: Optional[Union[str, date, datetime]]) -> Tuple[Optional[datetime], Optional[datetime]]
Normalize NAP's flexible start/end date inputs into datetimes.
Accepts None, the literal string "today", date/datetime
objects, or strings in %d%m%Y or %d-%m-%Y format. If only one of
start_date/end_date is given, the other is filled in with the same
value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_date
|
Optional[Union[str, date, datetime]]
|
Start date, in any of the accepted forms, or |
required |
end_date
|
Optional[Union[str, date, datetime]]
|
End date, in any of the accepted forms, or |
required |
Returns:
| Type | Description |
|---|---|
Optional[datetime]
|
A tuple |
Optional[datetime]
|
|
Source code in pyGTFSHandler/downloaders/spain/utils.py
resolve_publication_start_date ¶
resolve_publication_start_date(calendar_path: Optional[str], calendar_dates_path: Optional[str], candidate_date: datetime) -> datetime
Determine a historic publication's true service start date.
NAP's /Fichero/historico only exposes a publication/upload date
(fecha); that is not necessarily when the feed's own service
actually starts (publications are sometimes uploaded ahead of or
behind their own calendar.txt/calendar_dates.txt service period).
_trim_calendars needs a start date that is actually inside the
feed's service period, since it's used as the lower bound when
trimming the previous publication's end_date in process_calendar/
process_calendar_dates.
If candidate_date (the publication date) falls inside the feed's own
combined calendar.txt/calendar_dates.txt service date range, it is
used as-is. Otherwise, it isn't a meaningful anchor for this feed, so
the feed's own earliest service date is used instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
calendar_path
|
Optional[str]
|
Path to the feed's |
required |
calendar_dates_path
|
Optional[str]
|
Path to the feed's |
required |
candidate_date
|
datetime
|
The publication date to validate (NAP's |
required |
Returns:
| Type | Description |
|---|---|
datetime
|
|
datetime
|
range, otherwise the feed's earliest service date. Falls back to |
datetime
|
|
datetime
|
either file. |
Source code in pyGTFSHandler/downloaders/spain/utils.py
process_calendar ¶
process_calendar(path: str, file_date: datetime, possible_dates: List[datetime], day_separation: int, calendar_path: str) -> Tuple[Optional[int], datetime]
Trim a historic calendar.txt so it stops before the next publication.
Used while stitching together successive NAP GTFS publications: caps
end_date at whichever comes first among file_date + day_separation
days, the file's own minimum end_date, or the next candidate
publication date in possible_dates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the source |
required |
file_date
|
datetime
|
Publication date of this GTFS file. |
required |
possible_dates
|
List[datetime]
|
Candidate dates of subsequent publications, in chronological order. |
required |
day_separation
|
int
|
Minimum number of days a publication is assumed to stay valid for, if service periods don't force it shorter. |
required |
calendar_path
|
str
|
Path to write the trimmed |
required |
Returns:
| Type | Description |
|---|---|
Optional[int]
|
A tuple |
datetime
|
into |
Tuple[Optional[int], datetime]
|
one (or |
Tuple[Optional[int], datetime]
|
computed cutoff date. |
Source code in pyGTFSHandler/downloaders/spain/utils.py
process_calendar_dates ¶
process_calendar_dates(path: str, file_date: datetime, possible_dates: List[datetime], day_separation: int, calendar_dates_path: str, next_index: Optional[int] = None, has_calendar: bool = True) -> Tuple[Optional[int], Optional[datetime]]
Trim a historic calendar_dates.txt to match the stitched period.
Companion to process_calendar, used for GTFS publications that rely
on (or supplement with) calendar_dates.txt exceptions rather than a
plain weekly calendar.txt pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the source |
required |
file_date
|
datetime
|
Publication date of this GTFS file. |
required |
possible_dates
|
List[datetime]
|
Candidate dates of subsequent publications, in chronological order. |
required |
day_separation
|
int
|
Minimum number of days a publication is assumed to stay valid for, if service exceptions don't force it shorter. |
required |
calendar_dates_path
|
str
|
Path to write the trimmed
|
required |
next_index
|
Optional[int]
|
Index into |
None
|
has_calendar
|
bool
|
Whether this publication also has a |
True
|
Returns:
| Type | Description |
|---|---|
Optional[int]
|
A tuple |
Optional[datetime]
|
|
Source code in pyGTFSHandler/downloaders/spain/utils.py
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | |