OSM package

OSM area-selection helpers (GUI-free).

Pure geometry helpers shared by the OSM street and building download dialogs: resolve a user-selected area (city name, buildings CSV, GeoJSON polygon, or a polygon drawn on the map) into a WGS84 shapely geometry, and build the OSMnx highway filter string.

These were extracted from the two near-identical worker methods in gui/LeafletTab/osm_dialogs.py so the logic lives in one tested place and so the download threads no longer touch the GUI: a missing CSV column now raises ValueError (caught by the download thread → error signal) instead of calling QMessageBox from a worker thread.

author:

Dipl.-Ing. (FH) Jonas Pfeiffer

districtheatingsim.osm.area_selection.build_highway_filter(selected_types) str[source]

Build an OSMnx custom_filter string from selected highway types.

Parameters:

selected_types (list[str]) – Highway type keys (e.g. ["primary", "residential"]).

Returns:

OSMnx filter, e.g. '["highway"~"primary|residential"]'; with no types selected, the unrestricted '["highway"]'.

Return type:

str

districtheatingsim.osm.area_selection.polygon_from_csv(csv_file, project_crs, buffer_m)[source]

Build a buffered WGS84 polygon around building points from a CSV file.

Parameters:
  • csv_file (str) – Path to a ;-delimited CSV with UTM_X/UTM_Y columns.

  • project_crs (str) – CRS of the CSV coordinates (e.g. "EPSG:25833").

  • buffer_m (float) – Buffer radius around the points, in metres.

Returns:

WGS84 (EPSG:4326) polygon covering the buffered points.

Return type:

shapely.geometry.base.BaseGeometry

Raises:

ValueError – If the CSV lacks UTM_X/UTM_Y columns.

districtheatingsim.osm.area_selection.polygon_from_geojson(geojson_file)[source]

Read a polygon GeoJSON file and return it as a single WGS84 geometry.

Parameters:

geojson_file (str) – Path to a GeoJSON file containing one or more polygons.

Returns:

WGS84 (EPSG:4326) union of the file’s geometries.

Return type:

shapely.geometry.base.BaseGeometry

districtheatingsim.osm.area_selection.resolve_area_polygon(area_params, buffer_m)[source]

Resolve an area-selection parameter dict to a WGS84 polygon.

Dispatches on area_params['area_type']: a buildings CSV (buffered by buffer_m), a GeoJSON polygon file, or a polygon drawn on the map. The city-name area type produces no polygon (the caller downloads by place name).

Parameters:
  • area_params (dict) – Area-selection parameters with at least area_type and the file path for the selected type (csv_file / polygon_file / drawn_polygon_file) plus project_crs for CSV.

  • buffer_m (float) – Buffer radius (metres) applied for the CSV area type.

Returns:

WGS84 polygon for the selected area.

Return type:

shapely.geometry.base.BaseGeometry

Raises:

ValueError – For the city-name or an unknown area type (no polygon).

OpenStreetMap data import and GeoJSON conversion module.

Provides Overpass API integration for downloading OSM data and converting it to GeoJSON format for district heating network planning.

author:

Dipl.-Ing. (FH) Jonas Pfeiffer

districtheatingsim.osm.import_osm_data_geojson.build_query(city_name, tags, element_type='way')[source]

Build Overpass API query for OSM data download.

Parameters:
  • city_name (str) – City name for OSM query

  • tags (list of tuple) – List of (key, value) tuples to filter OSM elements

  • element_type (str) – OSM element type (‘way’ or ‘building’)

Returns:

Overpass API query string

Return type:

str

districtheatingsim.osm.import_osm_data_geojson.download_data(query, element_type)[source]

Download OSM data via Overpass API and convert to GeoJSON.

Parameters:
  • query (str) – Overpass API query string

  • element_type (str) – OSM element type (‘way’ or ‘building’)

Returns:

GeoJSON FeatureCollection with OSM data

Return type:

geojson.FeatureCollection

Raises:

OverpassError – If API query fails

Note

Ways create LineString geometries, buildings create Polygon/MultiPolygon.

districtheatingsim.osm.import_osm_data_geojson.json_serial(obj)[source]

JSON serializer for non-standard objects.

Parameters:

obj (any) – Object to serialize

Returns:

Float representation for Decimal objects

Return type:

float

Raises:

TypeError – If object type is not serializable

districtheatingsim.osm.import_osm_data_geojson.save_to_file(geojson_data, filename)[source]

Save GeoJSON data to file.

Parameters:
  • geojson_data (geojson.FeatureCollection) – GeoJSON data to save

  • filename (str) – Output file path