| 63 | } |
| 64 | |
| 65 | void OGRSpec::parse() |
| 66 | { |
| 67 | // "type" field name is case sensitive, value is not |
| 68 | if (!(m_json.is_object() && m_json.contains("type"))) |
| 69 | throw error("'ogr' option must be a JSON object with 'type':'ogr' specified!"); |
| 70 | else if (!(Utils::tolower(m_json.at("type").get<std::string>()) == "ogr")) |
| 71 | throw error("'ogr' option must have 'type':'ogr' specified!"); |
| 72 | |
| 73 | m_opts = {}; |
| 74 | for (auto& item : m_json.items()) |
| 75 | { |
| 76 | std::string key = Utils::tolower(item.key()); |
| 77 | |
| 78 | if (item.value().is_null() || (item.value() == "")) |
| 79 | { |
| 80 | std::stringstream out; |
| 81 | out << "invalid value for field '" << key << "' in OGR JSON!"; |
| 82 | throw error(out.str()); |
| 83 | } |
| 84 | if (key == "datasource") |
| 85 | assignJSON(item.value(), m_opts.datasource); |
| 86 | else if (key == "drivers") |
| 87 | assignJSON(item.value(), m_opts.drivers); |
| 88 | else if (key == "openoptions") |
| 89 | assignJSON(item.value(), m_opts.openOpts); |
| 90 | else if (key == "layer") |
| 91 | assignJSON(item.value(), m_opts.layer); |
| 92 | else if (key == "sql") |
| 93 | assignJSON(item.value(), m_opts.sql); |
| 94 | else if (key == "options") |
| 95 | { |
| 96 | for (auto optItem : item.value().items()) |
| 97 | { |
| 98 | std::string optKey = Utils::tolower(optItem.key()); |
| 99 | if (optKey == "dialect") |
| 100 | assignJSON(optItem.value(), m_opts.dialect); |
| 101 | else if (optKey == "geometry") |
| 102 | assignJSON(optItem.value(), m_opts.geometry); |
| 103 | else |
| 104 | throw error("invalid value for 'options' field in OGR JSON!"); |
| 105 | } |
| 106 | } |
| 107 | else if (key == "type") |
| 108 | continue; |
| 109 | else |
| 110 | { |
| 111 | std::stringstream out; |
| 112 | out << "unexpected field '" << key << "' in OGR JSON!"; |
| 113 | throw error(out.str()); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | if (m_opts.datasource.empty()) |
| 118 | throw error("'ogr' option must contain a 'datasource' field!"); |
| 119 | } |
| 120 | |
| 121 | void OGRSpec::initialize() |
| 122 | { |