Skip to content

Commit

Permalink
enhance and move geojson property sanitation to data layer module
Browse files Browse the repository at this point in the history
  • Loading branch information
tyrasd committed Nov 23, 2021
1 parent cc0ae0a commit 3373f1b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
20 changes: 20 additions & 0 deletions modules/svg/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,21 @@ export function svgData(projection, context, dispatch) {
}


function stringifyGeojsonProperties(feature) {
const properties = feature.properties;
for (const key in properties) {
const property = properties[key];
if (typeof property === 'number' || typeof property === 'boolean' || Array.isArray(property)) {
properties[key] = property.toString();
} else if (property === null) {
properties[key] = 'null';
} else if (typeof property === 'object') {
properties[key] = JSON.stringify(property);
}
}
}


drawData.setFile = function(extension, data) {
_template = null;
_fileList = null;
Expand All @@ -332,6 +347,11 @@ export function svgData(projection, context, dispatch) {
case '.geojson':
case '.json':
gj = JSON.parse(data);
if (gj.type === 'FeatureCollection') {
gj.features.forEach(stringifyGeojsonProperties);
} else if (gj.type === 'Feature') {
stringifyGeojsonProperties(gj);
}
break;
}

Expand Down
16 changes: 1 addition & 15 deletions modules/ui/sections/raw_tag_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,6 @@ import { t } from '../../core/localizer';
import { utilArrayDifference, utilArrayIdentical } from '../../util/array';
import { utilGetSetValue, utilNoAuto, utilRebind, utilTagDiff } from '../../util';

/**
* This component is also used for custom map data,
* and geojson can contain numbers as values.
* We convert numbers to strings to avoid unexpected bugs.
* @param {{ [key: string]: any }} tags
*/
function stringifyNumbers(tags) {
for (const key in tags) {
if (typeof tags[key] === 'number') {
tags[key] = tags[key].toString();
}
}
return tags;
}

export function uiSectionRawTagEditor(id, context) {

Expand Down Expand Up @@ -609,7 +595,7 @@ export function uiSectionRawTagEditor(id, context) {

section.tags = function(val) {
if (!arguments.length) return _tags;
_tags = stringifyNumbers(val);
_tags = val;
return section;
};

Expand Down

0 comments on commit 3373f1b

Please sign in to comment.