From c55a3c313023238e3b11544d2b7d8d965c0e8132 Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Tue, 12 Aug 2014 10:00:37 -0700 Subject: [PATCH] csv plugin: support conversion to boolean types - closes #1540 --- plugins/input/csv/csv_datasource.cpp | 41 ++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/plugins/input/csv/csv_datasource.cpp b/plugins/input/csv/csv_datasource.cpp index fe3ae5095..ee4471a9e 100644 --- a/plugins/input/csv/csv_datasource.cpp +++ b/plugins/input/csv/csv_datasource.cpp @@ -670,12 +670,14 @@ void csv_datasource::parse_csv(T & stream, // now, add attributes, skipping any WKT or JSON fields if ((has_wkt_field) && (i == wkt_idx)) continue; if ((has_json_field) && (i == json_idx)) continue; - /* First we detect likely strings, then try parsing likely numbers, - finally falling back to string type - * We intentionally do not try to detect boolean or null types - since they are not common in csv - * Likely strings are either empty values, very long values - or value with leading zeros like 001 (which are not safe + /* First we detect likely strings, + then try parsing likely numbers, + then try converting to bool, + finally falling back to string type. + An empty string or a string of "null" will be parsed + as a string rather than a true null value. + Likely strings are either empty values, very long values + or values with leading zeros like 001 (which are not safe to assume are numbers) */ @@ -731,13 +733,28 @@ void csv_datasource::parse_csv(T & stream, } if (!matched) { - // fallback to normal string - feature->put(fld_name,std::move(tr.transcode(value.c_str()))); - if (feature_count == 1) + bool bool_val = false; + if (mapnik::util::string2bool(value,bool_val)) { - desc_.add_descriptor( - mapnik::attribute_descriptor( - fld_name,mapnik::String)); + matched = true; + feature->put(fld_name,bool_val); + if (feature_count == 1) + { + desc_.add_descriptor( + mapnik::attribute_descriptor( + fld_name,mapnik::Boolean)); + } + } + else + { + // fallback to normal string + feature->put(fld_name,std::move(tr.transcode(value.c_str()))); + if (feature_count == 1) + { + desc_.add_descriptor( + mapnik::attribute_descriptor( + fld_name,mapnik::String)); + } } } }