Skip to content

Commit d0c663d

Browse files
committed
Add core/extract topic using the new locator feature of osm2pgsql
This allows filtering all data by bounding box or polygon.
1 parent 92a8c4d commit d0c663d

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

themes/core/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,39 @@ If a valid elevation tag is found, the attributes `core.ele_m` and
5454
`core.ele_ft` are added and contain the elevation in meters and feet,
5555
respectively, as a number, rounded to the nearest integer.
5656

57+
## Topic 'extract'
58+
59+
### Description
60+
61+
Restrict imported data to a bounding box or polygon using the Locator feature
62+
of osm2pgsql. Use this topic *before* any other topics you want to restrict.
63+
Objects intersecting the locator area are kept, all others are discarded. For
64+
closed ways the area of the polygon is used for intersection, for unclosed ways
65+
the linestring. For relations that can be turned into multipolygons, the
66+
multipolygon is used, otherwise all node and way members of the polygon are
67+
checked as point and linestring features, respectively.
68+
69+
Geometries are *not* clipped to the locator area.
70+
71+
This only works in osm2pgsql version 2.2.0 and above.
72+
73+
### Tables
74+
75+
This topic doesn't add any output tables.
76+
77+
### Configuration
78+
79+
* `locator`: Set to an existing osm2pgsql.Locator (optional, if not set, a new
80+
locator will be created).
81+
* `bbox`: A table with the bounding box to be added to the locator. The table
82+
must contain four elements, in that order: min lon, min lat, max lon, max
83+
lat.
84+
85+
### Attributes
86+
87+
No attributes are added.
88+
89+
5790
## Topic 'layer'
5891

5992
### Description

themes/core/topics/extract.lua

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
-- ---------------------------------------------------------------------------
2+
--
3+
-- Theme: core
4+
-- Topic: extract
5+
--
6+
-- ---------------------------------------------------------------------------
7+
8+
local themepark, theme, cfg = ...
9+
10+
-- ---------------------------------------------------------------------------
11+
12+
local locator
13+
14+
if cfg.locator == nil then
15+
locator = osm2pgsql.define_locator({
16+
name = 'themepark-core-extract-' .. math.random(100000000)
17+
})
18+
else
19+
locator = cfg.locator
20+
end
21+
22+
if cfg.bbox ~= nil then
23+
local b = cfg.bbox
24+
locator:add_bbox('inside', b[1], b[2], b[3], b[4])
25+
end
26+
27+
-- ---------------------------------------------------------------------------
28+
29+
themepark:add_proc('node', function(object)
30+
if not locator:first_intersecting(object:as_point()) then
31+
return 'stop'
32+
end
33+
end)
34+
35+
themepark:add_proc('way', function(object)
36+
local geom
37+
if object.is_closed then
38+
geom = object:as_polygon()
39+
end
40+
41+
if geom == nil or geom:is_null() then
42+
geom = object:as_linestring()
43+
end
44+
45+
if not locator:first_intersecting(geom) then
46+
return 'stop'
47+
end
48+
end)
49+
50+
themepark:add_proc('relation', function(object)
51+
local geom = object:as_multipolygon()
52+
53+
if geom:is_null() then
54+
geom = object:as_geometrycollection()
55+
end
56+
57+
if geom:is_null() then
58+
return 'stop'
59+
end
60+
61+
if not locator:first_intersecting(geom) then
62+
return 'stop'
63+
end
64+
end)
65+
66+
-- ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)