Skip to content
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

Add top-level-countries DBF from Natural Earth #2075

Closed
nvkelso opened this issue Apr 28, 2022 · 5 comments
Closed

Add top-level-countries DBF from Natural Earth #2075

nvkelso opened this issue Apr 28, 2022 · 5 comments
Assignees
Milestone

Comments

@nvkelso
Copy link
Member

nvkelso commented Apr 28, 2022

The following archive:

https://naciscdn.org/naturalearth/5.1.0/housekeeping/ne_admin_0_details.zip

Includes a new table:

ne_admin_0_details_top_level_countries.dbf

That table needs to be added to Postgres as source of truth for "country-ness". Currently it's a join against the admin-0 countries table, which doesn't include all the data we want. This new file does.

@tgrigsby-sc
Copy link
Contributor

@nvkelso I'm looking at this - may need to meet with you to figure out how to wire this in

@nvkelso
Copy link
Member Author

nvkelso commented Apr 29, 2022

You could load it using a tool like: https://pgloader.readthedocs.io/en/latest/ref/dbf.html

Though see my comment below about loading 2 separate shapefiles instead which pattern matches better with what we already do.

Add it/them to the list of assets around here:

You'll want to configure the extra TLC point-of-view around here:

  • {% set ne_viewpoints = ['iso', 'ar', 'bd', 'br', 'cn', 'de', 'eg', 'es', 'fr', 'gb', 'gr', 'id', 'il', 'in', 'it', 'jp', 'ko', 'ma', 'nl', 'np', 'pk', 'pl', 'ps', 'pt', 'ru', 'sa', 'se', 'tr', 'tw', 'ua', 'us', 'vn'] %}

But that assumes fclass_tlc is present on all related SHPs, which might not be true yet? I'll see if I need to unblock you there.

Then for the lookup for the "country" min_zoom, that's in:

  • -- returns a JSONB object containing __ne_min_zoom and __ne_max_zoom set to the
    -- label min and max zoom of any matching row from the Natural Earth countries,
    -- map units and states/provinces themes.
    CREATE OR REPLACE FUNCTION tz_get_ne_min_max_zoom(wikidata_id TEXT)
    RETURNS JSONB AS $$
    DECLARE
    min_zoom REAL;
    max_zoom REAL;
    BEGIN
    IF wikidata_id IS NULL THEN
    RETURN '{}'::jsonb;
    END IF;
    -- first, try the countries table
    SELECT
    min_label, max_label INTO min_zoom, max_zoom
    FROM ne_10m_admin_0_countries c
    WHERE c.wikidataid = wikidata_id;
    -- if that fails, try map_units (which contains some sub-country but super-
    -- state level stuff such as England, Scotland and Wales).
    IF NOT FOUND THEN
    SELECT
    min_label, max_label INTO min_zoom, max_zoom
    FROM ne_10m_admin_0_map_units mu
    WHERE mu.wikidataid = wikidata_id;
    END IF;
    -- try states and provinces
    IF NOT FOUND THEN
    SELECT
    min_label, max_label INTO min_zoom, max_zoom
    FROM ne_10m_admin_1_states_provinces sp
    WHERE sp.wikidataid = wikidata_id;
    END IF;
    -- finally, try localities
    -- There is no concept of max_zoom for ne_10m_populated_places
    IF NOT FOUND THEN
    SELECT
    pp.min_zoom, NULL INTO min_zoom, max_zoom
    FROM ne_10m_populated_places pp
    WHERE pp.wikidataid = wikidata_id;
    END IF;
    -- return an empty JSONB rather than null, so that it can be safely
    -- concatenated with whatever other JSONB rather than needing a check for
    -- null.
    IF NOT FOUND THEN
    RETURN '{}'::jsonb;
    END IF;
    RETURN jsonb_build_object(
    '__ne_min_zoom', min_zoom,
    '__ne_max_zoom', max_zoom
    );
    END;
    $$ LANGUAGE plpgsql STABLE;

Which spans across multiple tables today (across input Natural Earth themes across feature classes and kinds), which is problematic because the same Wikidata can be present in multiple tables, sometimes for the same conceptual feature and sometimes not (eg fuzzy versus strict matching) – take France for example.

So the reason we want to load this new table is we want to get only the curated set of strict matches.

There is an alternate way of doing this, which is to load 2 new shapefiles instead of 1 DBF, and then splitting the current function so admin-0 is treated separately from admin-1, localities & etc. For the new admin-0 it'd could be a coalesce across the ISO and TLC shapefiles instead (I'd first look in ISO, then in TLC). That should include 100% of what's in the source DBF (which is used as input for both these SHP).

Bonus points for picking up the curated label_x and label_y properties out of those tables, as well as fclass_iso and fclass_tlc.

@tgrigsby-sc
Copy link
Contributor

Looks like our whole shapefile bundle build expects shp files. Adding a dbf will break a few assumptions in the process so I'm going to go with the two shp's approach

@tgrigsby-sc
Copy link
Contributor

Reducing scope here a little for simplicilty. Going to:

  • use the iso and tlc shp files @nvkelso mentioned above.
  • modify tz_get_min_and_max_zoom to do lookups in iso and tlc only if it's place=country or place=unrecognized, and then if it's not, look up in the admin1+ tables

tgrigsby-sc added a commit that referenced this issue May 17, 2022
* split lookup depending on whether it's a country (#2075)

* return fclass_iso, fclass_tlc, and label_x, label_y for OSM features with matching NE features (#2082)

* adding TLC POV to NE export, YAML files and transform.py (#2081)

* adding tests

* upgrading to 5.1.2

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@nvkelso
Copy link
Member Author

nvkelso commented Oct 5, 2022

Fixed via #2078.

@nvkelso nvkelso closed this as completed Oct 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants