|
2 | 2 | Core functionality for accessing Canadian Census data through the CensusMapper API. |
3 | 3 | """ |
4 | 4 |
|
5 | | -import os |
6 | | -import requests |
| 5 | +import hashlib |
| 6 | +import io |
| 7 | +import json |
| 8 | +import warnings |
| 9 | +from typing import Dict, List, Optional, Union |
| 10 | + |
7 | 11 | import pandas as pd |
8 | 12 | import geopandas as gpd |
9 | | -from typing import Dict, List, Optional, Union |
10 | | -import warnings |
| 13 | +import requests |
11 | 14 |
|
12 | | -from .settings import get_api_key, get_cache_path |
| 15 | +from .settings import get_api_key, get_cache_path, CENSUSMAPPER_API_URL |
13 | 16 | from .cache import get_cached_data, cache_data |
14 | 17 | from .utils import validate_dataset, validate_level, process_regions |
15 | 18 | from .progress import show_request_preview, create_progress_for_request |
@@ -122,10 +125,9 @@ def get_census( |
122 | 125 | return cached_data |
123 | 126 |
|
124 | 127 | # Build API request exactly like the R package |
125 | | - base_url = "https://censusmapper.ca/api/v1/" |
| 128 | + base_url = f"{CENSUSMAPPER_API_URL}/" |
126 | 129 |
|
127 | 130 | # Format parameters exactly like the R package |
128 | | - import json |
129 | 131 |
|
130 | 132 | # Convert regions to JSON format exactly like R package: jsonlite::toJSON(lapply(regions, as.character)) |
131 | 133 | # R package ALWAYS puts region values in arrays - this was the key missing piece! |
@@ -303,8 +305,6 @@ def get_census( |
303 | 305 |
|
304 | 306 | def _generate_cache_key(dataset, regions, vectors, level, geo_format): |
305 | 307 | """Generate a cache key for the given parameters.""" |
306 | | - import hashlib |
307 | | - |
308 | 308 | # Create a string representation of the parameters |
309 | 309 | params_str = f"{dataset}_{regions}_{vectors}_{level}_{geo_format}" |
310 | 310 |
|
@@ -355,15 +355,13 @@ def _extract_vector_metadata(df, vectors, labels): |
355 | 355 |
|
356 | 356 | # Store metadata as dict to avoid pandas attrs comparison bug |
357 | 357 | # Convert DataFrame to list of dicts for storage |
358 | | - df.attrs["census_vectors"] = metadata_df.to_dict(orient='records') |
| 358 | + df.attrs["census_vectors"] = metadata_df.to_dict(orient="records") |
359 | 359 |
|
360 | 360 | return df |
361 | 361 |
|
362 | 362 |
|
363 | 363 | def _process_csv_response(csv_text, vectors, labels): |
364 | 364 | """Process CSV API response into a pandas DataFrame.""" |
365 | | - import io |
366 | | - |
367 | 365 | # Read all columns as strings initially (like R package) |
368 | 366 | df = pd.read_csv(io.StringIO(csv_text), dtype=str, encoding="utf-8") |
369 | 367 |
|
|
0 commit comments