sadpandajoe commented on code in PR #45607: URL: https://github.com/apache/superset/pull/45607#discussion_r4429535616
########## superset-frontend/plugins/plugin-chart-country-map/scripts/maps/countries/philippines.py: ########## @@ -0,0 +1,98 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +"""Philippines: a province-level community source replaces the raw, +outdated Natural Earth boundaries, plus a dissolve of the (unrelated, +Natural-Earth-sourced) regions. + +The notebook this replaces fetched this same community source, renamed +its columns, and then never wired the result into its output - the +plain "philippines" entry it shipped was actually still raw Natural +Earth data (highly-urbanized cities split out as their own polygons, an +undivided "Maguindanao", "Compostela Valley" instead of "Davao de +Oro"). Confirmed against the previously-committed philippines.geojson. +This fixes that: the corrected 83-province source is now the +"philippines" output. +""" + +from __future__ import annotations + +import geopandas as gpd +from shapely.geometry.base import BaseGeometry +from shapely.geometry.polygon import orient + +from maps.context import BuildContext + +NAME = "philippines" + +_URL = ( + "https://raw.githubusercontent.com/jdruii/phgeojson/" + "098399b6f27ca10e31c8f304e4148b3a56431ec2/philippines.geojson" +) + + +def _orient_clockwise(geom: BaseGeometry) -> BaseGeometry: + if geom.geom_type == "Polygon": + return orient(geom, sign=-1.0) + if geom.geom_type == "MultiPolygon": + return type(geom)([orient(part, sign=-1.0) for part in geom.geoms]) + return geom + + +def build(ctx: BuildContext) -> dict[str, gpd.GeoDataFrame]: + try: + source = gpd.read_file(_URL) + except Exception as exc: # noqa: BLE001 - re-raised with context below + raise RuntimeError( + f"Unable to download or load the GeoJSON file for Philippines: {exc}" + ) from exc + + required_columns = {"NAME_1", "ISO", "geometry"} + if missing_columns := required_columns - set(source.columns): + raise ValueError( + f"Philippines source is missing columns: {sorted(missing_columns)}. " + f"Available columns: {sorted(source.columns)}" + ) + + philippines = source.rename(columns={"NAME_1": "name", "ISO": "iso_3166_2"})[ + ["geometry", "iso_3166_2", "name"] + ].copy() + # The chart projects with d3 v3, which reads a counterclockwise exterior + # ring as the complement on the sphere; the community source mixes + # windings, so force clockwise exteriors (sign=-1). + philippines["geometry"] = philippines.geometry.apply(_orient_clockwise) Review Comment: The clockwise-exterior normalization here is what keeps the Philippines map from rendering as the spherical complement under d3 v3, and the committed philippines.geojson (now 83 features) has no test: the real-geometry Jest tests only read USA, and nothing reads this asset. If `_orient_clockwise` is dropped or the source switched back, the file regenerates with counterclockwise exteriors and CI stays green. Could a disk-reading test in `test/` assert 83 features, 83 distinct non-empty ISO values, clockwise exterior rings and counterclockwise holes for philippines.geojson? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
