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

房东的猫 提交于 2020-04-18 03:57:12

问题


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 have a flag, but they are not really countries

SELECT feature_type, osm_id, osm_timestamp, geometry,ar.key,ar.value,
  FROM `bigquery-public-data.geo_openstreetmap.planet_features`,UNNEST(all_tags) ar
   where ('boundary', 'administrative') IN (SELECT (key, value) FROM UNNEST(all_tags))
   and(feature_type="polygon" or feature_type= "multipolygon")
   AND ('flag') IN (SELECT (key) FROM UNNEST(all_tags)) and ar.key="name" order by st_area(geometry) desc

sorry forget to say, some tags are missing, for example if you select admin_level = 2, countries like the USA get dropped


回答1:


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.



来源:https://stackoverflow.com/questions/61268379/how-to-extract-all-countries-geometry-from-openstreet-map-dataset-in-bigquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!