Skip to content

Commit

Permalink
csv plugin: support conversion to boolean types - closes #1540
Browse files Browse the repository at this point in the history
  • Loading branch information
Dane Springmeyer committed Aug 12, 2014
1 parent f76ea12 commit c55a3c3
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions plugins/input/csv/csv_datasource.cpp
Expand Up @@ -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)
*/

Expand Down Expand Up @@ -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));
}
}
}
}
Expand Down

0 comments on commit c55a3c3

Please sign in to comment.