csv plugin: support conversion to boolean types - closes #1540

This commit is contained in:
Dane Springmeyer 2014-08-12 10:00:37 -07:00
parent f76ea12700
commit c55a3c3130

View file

@ -670,12 +670,14 @@ void csv_datasource::parse_csv(T & stream,
// now, add attributes, skipping any WKT or JSON fields // now, add attributes, skipping any WKT or JSON fields
if ((has_wkt_field) && (i == wkt_idx)) continue; if ((has_wkt_field) && (i == wkt_idx)) continue;
if ((has_json_field) && (i == json_idx)) continue; if ((has_json_field) && (i == json_idx)) continue;
/* First we detect likely strings, then try parsing likely numbers, /* First we detect likely strings,
finally falling back to string type then try parsing likely numbers,
* We intentionally do not try to detect boolean or null types then try converting to bool,
since they are not common in csv finally falling back to string type.
* Likely strings are either empty values, very long values An empty string or a string of "null" will be parsed
or value with leading zeros like 001 (which are not safe 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) to assume are numbers)
*/ */
@ -731,13 +733,28 @@ void csv_datasource::parse_csv(T & stream,
} }
if (!matched) if (!matched)
{ {
// fallback to normal string bool bool_val = false;
feature->put(fld_name,std::move(tr.transcode(value.c_str()))); if (mapnik::util::string2bool(value,bool_val))
if (feature_count == 1)
{ {
desc_.add_descriptor( matched = true;
mapnik::attribute_descriptor( feature->put(fld_name,bool_val);
fld_name,mapnik::String)); 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));
}
} }
} }
} }