Extract OpenStreetMap infrastructure data for Hungary (or any other country) into a GeoPackage matching the schema documented at Infrageomatics — OSM Export documentation.
The output is functionally equivalent to the Infrageomatics product: identical layer names, identical field names, derived from identical OSM tags. The data itself is © OpenStreetMap contributors and licensed under the Open Database License (ODbL).
A single GeoPackage output/hungary_infrastructure.gpkg containing one layer
per category in the documentation:
| Category | Layers |
|---|---|
| Power | power_generator, power_plant, power_line, power_substation, power_transformer, power_switch, power_compensator |
| Power Towers | power_tower (opt-in via --include-power-tower) |
| Telecoms | telecom_building, telecom_cable, mast |
| Pipelines | pipeline, pipeline_feature |
| Oil & Gas | petroleum_site, petroleum_well |
| Water | water_treatment_plant, wastewater_plant, pumping_station, water_tower, water_well, water_reservoir |
For categories where OSM features can be either nodes or polygons (e.g.
power_plant), two layers are produced — power_plant_point and
power_plant_polygon — exactly as the documentation prescribes.
All geometries are stored in WGS 84 (EPSG:4326).
This tool is designed to run inside the OSGeo4W shell that ships with
QGIS. No pip install is required — it uses only:
- Python standard library
osgeo.ogr/osgeo.osr(GDAL Python bindings, always present in OSGeo4W)
If you want to run it outside OSGeo4W, install GDAL with Python bindings
(pip install gdal or conda install gdal).
Open the OSGeo4W Shell that came with QGIS, cd into this directory, and
run either of the two modes below.
python extract.py --use-pbfOn first run this downloads hungary-latest.osm.pbf (~700 MiB) from
Geofabrik into data\, then reads it
locally with the GDAL OSM driver. Subsequent runs reuse the cached PBF.
Use --force-download to refresh it.
python extract.pyQueries the Overpass API once per layer and caches every response under
output\cache\. Best when you only want a few layers, when the country
extract is impractically large, or when you specifically need data fresher
than the latest Geofabrik nightly.
| Flag | Default | Purpose |
|---|---|---|
--country HU |
HU |
ISO 3166-1 alpha-2 code of the country area |
--output PATH |
output/hungary_infrastructure.gpkg |
Output GeoPackage path |
--layers a,b,c |
all | Restrict to a comma-separated subset of layer names |
--include-power-tower |
off | Also extract power_tower (large; off by default) |
--use-pbf |
off | Use a Geofabrik PBF instead of Overpass |
--pbf-file PATH |
data/<country>-latest.osm.pbf |
Local PBF path (auto-downloaded if missing) |
--pbf-url URL |
derived from --country |
Override the Geofabrik download URL |
--force-download |
off | Re-download the PBF even if it already exists |
--intermediate-gpkg PATH |
data/<country>_osm.gpkg |
Where to write the PBF->GPKG translation |
--force-convert |
off | Rebuild the intermediate GPKG even if it already exists |
--overpass-url URL |
overpass-api.de | Custom Overpass endpoint (Overpass mode only) |
--no-cache |
off | Bypass the on-disk Overpass cache |
--clear-cache |
off | Wipe the Overpass cache before running |
--shapefile |
off | Also write a shapefile per layer (long names truncated) |
Extract only power layers:
python extract.py --layers power_generator,power_plant,power_line,power_substationExtract everything including power towers:
python extract.py --include-power-towerExtract for a different country (e.g. Slovakia):
python extract.py --country SK --output output/slovakia_infrastructure.gpkgThe tool has two interchangeable data sources; the layer/field schema is identical in both.
- Download
hungary-latest.osm.pbffrom Geofabrik on first use (cached underdata\). - Synthesise an
osmconf.iniexposing every OSM tag we read (e.g.power,construction:power,generator:output:electricity,voltage:primary, ...) as a real column on each driver layer (points,lines,multipolygons,multilinestrings). Closed ways taggedpower=*,industrial=*,water=*,pipeline=*, etc. are added toclosed_ways_are_polygonsso they're correctly classified as areas. - Translate the PBF into an intermediate GeoPackage at
data\<country>_osm.gpkg, once (~5 minutes for Hungary). This is required because GDAL's OSM driver streams the PBF in a single pass and can't be queried repeatedly — once flattened to GPKG we can run arbitrary SQL filters per layer with no constraints. The intermediate GPKG is reused on subsequent runs; rebuild it with--force-convertif the PBF changes. - For each Infrageomatics layer, run an OGR SQL filter against the relevant GPKG source layer(s), apply the field transformations, and write to the final output GeoPackage. The intermediate GPKG itself is also a useful artefact — you can open it directly in QGIS to inspect any OSM feature in Hungary by tag.
For every layer the tool issues an Overpass QL query of the form
[out:json][timeout:600];
area["ISO3166-1"="HU"][admin_level=2]->.searchArea;
(
nwr["power"="generator"](area.searchArea);
nwr["construction:power"="generator"](area.searchArea);
);
out geom;Responses are cached as gzipped JSON under output/cache/ keyed on the query
string. The Overpass JSON is then translated into OGR geometries (points,
linestrings, multipolygons assembled from outer/inner ring members) and
attribute fields are derived per the documentation.
Per-field rules include:
construction(boolean) — true for objects matched via theconstruction:lifecycle prefix or taggedconstruction=yes.disused(boolean,power_lineonly) — true for objects matched via thedisused:lifecycle prefix or taggeddisused=yes.max_voltage(int, volts) — maximum of the semicolon-separatedvoltagetag.voltages(str, volts) — original semicolon-separatedvoltagetag.output(float, kW) —*:output:electricityparsed from values such as5 MW,500 kW,1.2 GW.location(str) — defaults tooverheadforpower=line/minor_lineandundergroundforpower=cable/minor_cablewhen not explicitly set.
Field names follow the GeoPackage form from the documentation. Shapefile output truncates them to 10 characters per the DBF spec.
The data extracted by this tool is © OpenStreetMap contributors and licensed under the Open Database License. You must credit OpenStreetMap and its contributors in any product derived from these exports.
This tool itself is provided as-is; do whatever you want with it.