From 209fe55e14a0f4c6cf0b6dc9ac57023ee9624648 Mon Sep 17 00:00:00 2001 From: artemp Date: Fri, 23 Oct 2015 13:10:23 +0100 Subject: [PATCH] trim headers + sync with csv.input --- utils/mapnik-index/process_csv_file.cpp | 36 ++++++++++++++----------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/utils/mapnik-index/process_csv_file.cpp b/utils/mapnik-index/process_csv_file.cpp index 0ee3eab5e..c7b18a0e1 100644 --- a/utils/mapnik-index/process_csv_file.cpp +++ b/utils/mapnik-index/process_csv_file.cpp @@ -114,6 +114,7 @@ std::pair> process_csv_file(T & boxes, std::string const& fil std::size_t index = 0; for (auto & header : headers) { + mapnik::util::trim(header); if (header.empty()) { // create a placeholder for the empty header @@ -168,24 +169,24 @@ std::pair> process_csv_file(T & boxes, std::string const& fil } while (is_first_row || csv_utils::getline_csv(csv_file, csv_line, newline, quote)) { + ++line_number; auto record_offset = pos; auto record_size = csv_line.length(); pos = csv_file.tellg(); is_first_row = false; - ++line_number; + // skip blank lines + if (record_size <= 10) + { + std::string trimmed = csv_line; + boost::trim_if(trimmed, boost::algorithm::is_any_of("\",'\r\n ")); + if (trimmed.empty()) + { + std::clog << "CSV index: empty row encountered at line: " << line_number << std::endl; + continue; + } + } try { - // skip blank lines - if (record_size <= 10) - { - std::string trimmed = csv_line; - boost::trim_if(trimmed, boost::algorithm::is_any_of("\",'\r\n ")); - if (trimmed.empty()) - { - std::clog << "CSV index: empty row encountered at line: " << line_number << std::endl; - continue; - } - } auto values = csv_utils::parse_line(csv_line, separator, quote); unsigned num_fields = values.size(); if (num_fields > num_headers || num_fields < num_headers) @@ -194,9 +195,8 @@ std::pair> process_csv_file(T & boxes, std::string const& fil std::ostringstream s; s << "CSV Index: # of columns(" << num_fields << ") > # of headers(" - << num_headers << ") parsed for row " << line_number << "\n"; - std::clog << s.str() << std::endl; - continue; + << num_headers << ") parsed for row " << line_number; + throw mapnik::datasource_exception(s.str()); } auto geom = ::detail::extract_geometry(values, locator); @@ -213,9 +213,13 @@ std::pair> process_csv_file(T & boxes, std::string const& fil s << "CSV Index: expected geometry column: could not parse row " << line_number << " " << values[locator.index] << "'"; - std::clog << s.str() << std::endl; + throw mapnik::datasource_exception(s.str()); } } + catch (mapnik::datasource_exception const& ex ) + { + std::clog << ex.what() << " at line: " << line_number << std::endl; + } catch (std::exception const& ex) { std::ostringstream s;