-
Notifications
You must be signed in to change notification settings - Fork 4
feat: enhance project area import with created and skipped counts #616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,13 +25,21 @@ class ProjectAreaImportResult: | |
| fetched: int | ||
| matched: int | ||
| updated: int | ||
| created: int | ||
| skipped: int | ||
| unmatched_locations: tuple[str, ...] | ||
|
|
||
|
|
||
| def _normalize_name(value: str) -> str: | ||
| return value.strip().lower() | ||
|
|
||
|
|
||
| def _geoms_equal(geom1: str, geom2: str) -> bool: | ||
| from shapely import wkt | ||
|
|
||
| return wkt.loads(geom1).equals(wkt.loads(geom2)) | ||
|
|
||
|
|
||
| def _geojson_to_multipolygon_wkt(geometry: dict[str, Any]) -> str: | ||
| geom = shape(geometry) | ||
| if isinstance(geom, Polygon): | ||
|
|
@@ -77,13 +85,16 @@ def _fetch_project_area_features( | |
|
|
||
| def import_project_area_boundaries( | ||
| layer_url: str = PROJECT_AREA_LAYER_URL, | ||
| group_type: str = "Geographic Area", | ||
| ) -> ProjectAreaImportResult: | ||
| with httpx.Client(timeout=60.0) as client: | ||
| features = _fetch_project_area_features(client, layer_url) | ||
|
|
||
| unmatched_locations: list[str] = [] | ||
| matched = 0 | ||
| updated = 0 | ||
| created = 0 | ||
| skipped = 0 | ||
|
|
||
| with session_ctx() as session: | ||
| for feature in features: | ||
|
|
@@ -94,30 +105,53 @@ def import_project_area_boundaries( | |
| if not location_name or geometry is None: | ||
| continue | ||
|
|
||
| normalized_name = _normalize_name(location_name) | ||
| groups = session.scalars( | ||
| select(Group).where( | ||
| func.lower(func.trim(Group.name)) == _normalize_name(location_name) | ||
| func.lower(func.trim(Group.name)) == normalized_name, | ||
| Group.group_type == group_type, | ||
| ) | ||
| ).all() | ||
|
|
||
| if not groups: | ||
| unmatched_locations.append(location_name) | ||
| continue | ||
|
|
||
| matched += len(groups) | ||
| project_area = WKTElement( | ||
| _geojson_to_multipolygon_wkt(geometry), | ||
| srid=4326, | ||
| ) | ||
|
|
||
| if not groups: | ||
| new_group = Group( | ||
| name=location_name, | ||
| group_type=group_type, | ||
| project_area=project_area, | ||
|
Comment on lines
+121
to
+125
|
||
| ) | ||
| session.add(new_group) | ||
| created += 1 | ||
| matched += 1 | ||
| continue | ||
|
|
||
| matched += len(groups) | ||
| for group in groups: | ||
| group.project_area = project_area | ||
| updated += 1 | ||
| old_wkt = None | ||
| if group.project_area is not None: | ||
| from shapely import wkb | ||
|
|
||
| old_wkt = wkb.loads(bytes(group.project_area.data)).wkt | ||
|
|
||
| new_wkt = project_area.desc | ||
|
|
||
| if old_wkt is None or not _geoms_equal(old_wkt, new_wkt): | ||
| group.project_area = project_area | ||
|
Comment on lines
+140
to
+143
|
||
| updated += 1 | ||
| else: | ||
| skipped += 1 | ||
|
|
||
| session.commit() | ||
|
|
||
| return ProjectAreaImportResult( | ||
| fetched=len(features), | ||
| matched=matched, | ||
| updated=updated, | ||
| created=created, | ||
| skipped=skipped, | ||
| unmatched_locations=tuple(sorted(set(unmatched_locations))), | ||
| ) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Existing CLI tests for
import-project-area-boundaries(seetests/test_cli_commands.py::test_import_project_area_boundaries_updates_matching_groups) assert the old output/behavior (including "Unmatched locations"). With the new Created/Skipped output and the new-group creation path, those tests should be updated and extended to cover the created/skipped counts.