Replies: 3 comments
-
Boundaries are mapped as relations in OSM (and only relations), so you need to use the And finally, for country boundaries better filter by |
Beta Was this translation helpful? Give feedback.
-
Thanks. I was able to get part of the way there, but I can't quite figure out how I can iterate over the members of the relation to check if they are ways, and then insert them (or potentially chain them together so I can polygonize them later). In the end I just inserted them as geometry collections and then wrote an SQL query to process the result into polygons. It's clunky but it works. I was wondering if you have any tips on how I can better traverse the members of the relations. Because this was as far as I could make it on my own: (PBF pre-filtered with "n/amenity=charging_station" and "boundary=administrative" in osmium) flex script: local srid = 4326
local tables = {}
tables.country_pre = osm2pgsql.define_way_table('country_pre', {
{ column = 'name', type = 'text' },
{ column = 'tags', type = 'jsonb' },
{ column = 'geom', type = 'geometrycollection', projection = srid }
})
function osm2pgsql.process_relation(object)
if object:grab_tag('admin_level') == "2" then
tables.country_pre:insert({
name = object.tags["name:en"],
tags = object.tags,
geom = object:as_geometrycollection()
})
end
end Postprocessing query: drop table if exists country;
create table country as (
with a as (
select
st_asewkt((st_dump(geom)).geom) as geom,
st_geometrytype((st_dump(geom)).geom) as geom_type,
name
from
country_pre
),
b as (
select
ST_Node(st_collect(geom)) as geom,
name
from
a
where
geom_type = 'ST_LineString'
group by
name
)
select
ST_SimplifyPreserveTopology((ST_Dump(st_polygonize(geom))).geom, 0.00005) as geom,
name
from
b
group by
name
);
drop table country_pre;
create index on country (geom) using gist; |
Beta Was this translation helpful? Give feedback.
-
The simplest way to handle boundaries is to create multipolygons from the boundary relations in osm2pgsql. Then you can either just draw the outline of those multipolygons or turn them into (multi)linestrings in the database. You can also create multilinestrings directly from the relations if your osm2pgsql version is new enough. This is often good enough if you only want to render one level of boundaries (i.e. only country boundaries). But if you want to render multiple levels (country, state, ...) or want to render the boundaries half-transparently, you'll get intro trouble. Then you really need to handle each piece of boundary separately. This is much more effort and you'll need some SQL magic for it and/or use 2-stage processing in osm2pgsql Lua which is a bit painful. I don't have a good out-of-the-box solution there, although I believe @pnorman has support for this in gravitystorm/openstreetmap-carto#4431 . |
Beta Was this translation helpful? Give feedback.
-
I am really struggling to get country boundaries when using the flex output.
I filtered my pbf with osmium:
This should give me all objects with any value at all in
admin_level
(in addition to the charging stationsAnd then I try to turn them into polygons in the flex style file:
But i only get some selected polygons as a result. This table is huge, with many tens of thousands of entries, but only a few have geometry:
I don't see any examples of this in the example lua files, is there some trick to this I'm not getting?
Beta Was this translation helpful? Give feedback.
All reactions