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

后端 未结 2 345
灰色年华
灰色年华 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.

提交回复
热议问题