Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

extracto-hungaro

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).

What you get

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).

Requirements

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).

Usage

Open the OSGeo4W Shell that came with QGIS, cd into this directory, and run either of the two modes below.

PBF mode (recommended — single download, then offline)

python extract.py --use-pbf

On 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.

Overpass mode (small, targeted queries against the live API)

python extract.py

Queries 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.

Useful flags

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)

Examples

Extract only power layers:

python extract.py --layers power_generator,power_plant,power_line,power_substation

Extract everything including power towers:

python extract.py --include-power-tower

Extract for a different country (e.g. Slovakia):

python extract.py --country SK --output output/slovakia_infrastructure.gpkg

How it works

The tool has two interchangeable data sources; the layer/field schema is identical in both.

PBF mode

  1. Download hungary-latest.osm.pbf from Geofabrik on first use (cached under data\).
  2. Synthesise an osmconf.ini exposing 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 tagged power=*, industrial=*, water=*, pipeline=*, etc. are added to closed_ways_are_polygons so they're correctly classified as areas.
  3. 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-convert if the PBF changes.
  4. 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.

Overpass mode

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.

Field derivation (both modes)

Per-field rules include:

  • construction (boolean) — true for objects matched via the construction: lifecycle prefix or tagged construction=yes.
  • disused (boolean, power_line only) — true for objects matched via the disused: lifecycle prefix or tagged disused=yes.
  • max_voltage (int, volts) — maximum of the semicolon-separated voltage tag.
  • voltages (str, volts) — original semicolon-separated voltage tag.
  • output (float, kW) — *:output:electricity parsed from values such as 5 MW, 500 kW, 1.2 GW.
  • location (str) — defaults to overhead for power=line/minor_line and underground for power=cable/minor_cable when not explicitly set.

Field names follow the GeoPackage form from the documentation. Shapefile output truncates them to 10 characters per the DBF spec.

Licensing

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.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages