Skip to content

Commit

Permalink
Merge pull request #10293 from rouault/osmconfini_fix
Browse files Browse the repository at this point in the history
OSM: add a [general] section at top of osmconf.ini to make it INI compliant (and Python's configparser friendly)
  • Loading branch information
rouault committed Jul 6, 2024
2 parents c65af73 + b15e256 commit 890597c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
18 changes: 18 additions & 0 deletions autotest/ogr/ogr_osm.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,3 +929,21 @@ def test_ogr_osm_tags_json_special_characters():
assert lyr_defn.GetFieldDefn(other_tags_idx).GetSubType() == ogr.OFSTJSON
f = lyr.GetNextFeature()
assert f["other_tags"] == """{"foo":"x'\\\\\\"\\t\\n\\ry"}"""


###############################################################################
# Test that osmconf.ini can be parsed with Python's configparser


def test_ogr_osmconf_ini():

import configparser

with ogr.Open("data/osm/test_json.pbf") as ds:
with ds.ExecuteSQL("SHOW config_file_path") as sql_lyr:
f = sql_lyr.GetNextFeature()
osmconf_ini_filename = f.GetField(0)
config = configparser.ConfigParser()
config.read_file(open(osmconf_ini_filename))
assert "general" in config
assert "closed_ways_are_polygons" in config["general"]
2 changes: 2 additions & 0 deletions ogr/ogrsf_frmts/osm/data/osmconf.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#
# Configuration file for importing OSM data into OGR
#
# NOTE: remove the below "[general]" line for GDAL < 3.10
[general]

# put here the name of keys, or key=value, for ways that are assumed to be polygons if they are closed
# see http://wiki.openstreetmap.org/wiki/Map_Features
Expand Down
2 changes: 2 additions & 0 deletions ogr/ogrsf_frmts/osm/ogr_osm.h
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ class OGROSMDataSource final : public OGRDataSource
std::vector<std::unique_ptr<OGROSMLayer>> m_apoLayers{};
char *m_pszName = nullptr;

std::string m_osConfigFile{};

OGREnvelope m_sExtent{};
bool m_bExtentValid = false;

Expand Down
17 changes: 17 additions & 0 deletions ogr/ogrsf_frmts/osm/ogrosmdatasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3400,6 +3400,8 @@ bool OGROSMDataSource::ParseConf(char **papszOpenOptionsIn)
return false;
}

m_osConfigFile = pszFilename;

VSILFILE *fpConf = VSIFOpenL(pszFilename, "rb");
if (fpConf == nullptr)
return false;
Expand All @@ -3422,6 +3424,12 @@ bool OGROSMDataSource::ParseConf(char **papszOpenOptionsIn)
pszLine++;
const_cast<char *>(pszLine)[strlen(pszLine) - 1] =
'\0'; /* Evil but OK */

if (strcmp(pszLine, "general") == 0)
{
continue;
}

int i = 0;
for (auto &&poLayer : m_apoLayers)
{
Expand Down Expand Up @@ -4385,6 +4393,15 @@ OGRLayer *OGROSMDataSource::ExecuteSQL(const char *pszSQLCommand,
return new OGROSMSingleFeatureLayer("GetBytesRead", szVal);
}

/* -------------------------------------------------------------------- */
/* Special SHOW config_file_path command */
/* -------------------------------------------------------------------- */
if (strcmp(pszSQLCommand, "SHOW config_file_path") == 0)
{
return new OGROSMSingleFeatureLayer("config_file_path",
m_osConfigFile.c_str());
}

if (m_poResultSetLayer != nullptr)
{
CPLError(CE_Failure, CPLE_NotSupported,
Expand Down

0 comments on commit 890597c

Please sign in to comment.