how to extract all countries geometry from Openstreet map dataset in BigQuery

后端 未结 2 344
灰色年华
灰色年华 2021-01-23 17:48

I am using this query to extract the geometry of all countries using OSM, it works ok, but I am sure, it is creating a duplicated as I am using flag as a reference, some places

相关标签:
2条回答
  • 2021-01-23 18:09

    According to https://wiki.openstreetmap.org/wiki/Tag:boundary%3Dadministrative#National admin_level = 2 describes countries.

    So I tidied your query and added admin_level = 2 filter so that it only includes countries.

    SELECT 
      feature_type, osm_id, osm_timestamp, geometry,
      (SELECT value FROM UNNEST(all_tags) WHERE key = 'flag') as flag,
      (SELECT value FROM UNNEST(all_tags) WHERE key = 'name') as name,
      st_area(geometry) as area
    FROM `bigquery-public-data.geo_openstreetmap.planet_features`
    WHERE 
      feature_type in ("polygon", "multipolygon")
      AND EXISTS (SELECT 1 FROM UNNEST(all_tags) WHERE key = 'boundary' AND value = 'administrative')
      AND EXISTS (SELECT 1 FROM UNNEST(all_tags) WHERE key = 'flag') 
      AND EXISTS (SELECT 1 FROM UNNEST(all_tags) WHERE key = 'admin_level' AND value = '2') 
    ORDER BY area desc
    

    For USA, I found that link that explains everything about USA.. https://wiki.openstreetmap.org/wiki/United_States/Boundaries#National_boundary

    You can see openstreetmap record for the USA here https://www.openstreetmap.org/relation/148838#map=1/41/0 On the left side, there are all the features.

    Also, you can find USA record in BigQuery with this:

    SELECT *
    FROM `bigquery-public-data.geo_openstreetmap.planet_features`
    where osm_id = '148838'
    

    Even there is admin_level in openstreetmap record, it doesn't exist in BigQuery record. I don't know why, it may just be an old version.

    So you can optimize your filters using the query above to include the USA.

    0 讨论(0)
  • 2021-01-23 18:19

    We can a list of all countries and their geometries by merging 2 tables:

    SELECT features.feature_type, features.osm_id
      , ARRAY(
         SELECT DISTINCT AS STRUCT * FROM UNNEST(features.all_tags||relations.all_tags)
         WHERE key IN('int_name', 'name')
         ORDER BY 1 LIMIT 1 
      ) features
     , ROUND(ST_AREA(geometry)/1e6,1) area
    FROM
      `bigquery-public-data.geo_openstreetmap.planet_features` AS features,
      `bigquery-public-data.geo_openstreetmap.planet_relations` AS relations
    WHERE ('boundary','administrative') IN (SELECT (key,value) FROM UNNEST(features.all_tags))
    AND ('admin_level','2') IN (SELECT (key,value) FROM UNNEST(relations.all_tags))
    AND feature_type = 'multipolygon'
    AND relations.id=SAFE_CAST(features.osm_id AS INT64)
    ORDER BY area
    

    (ref)

    0 讨论(0)
提交回复
热议问题