-
Notifications
You must be signed in to change notification settings - Fork 13.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve deck.gl GeoJSON visualization #4220
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,40 +14,74 @@ const propertyMap = { | |
'stroke-width': 'strokeWidth', | ||
}; | ||
|
||
const convertGeoJsonColorProps = (p, colors) => { | ||
const obj = Object.assign(...Object.keys(p).map(k => ({ | ||
[(propertyMap[k]) ? propertyMap[k] : k]: p[k] }))); | ||
|
||
const alterProps = (props, propOverrides) => { | ||
const newProps = {}; | ||
Object.keys(props).forEach((k) => { | ||
if (k in propertyMap) { | ||
newProps[propertyMap[k]] = props[k]; | ||
} else { | ||
newProps[k] = props[k]; | ||
} | ||
}); | ||
if (typeof props.fillColor === 'string') { | ||
newProps.fillColor = hexToRGB(p.fillColor); | ||
} | ||
if (typeof props.strokeColor === 'string') { | ||
newProps.strokeColor = hexToRGB(p.strokeColor); | ||
} | ||
return { | ||
...obj, | ||
fillColor: (colors.fillColor[3] !== 0) ? colors.fillColor : hexToRGB(obj.fillColor), | ||
strokeColor: (colors.strokeColor[3] !== 0) ? colors.strokeColor : hexToRGB(obj.strokeColor), | ||
...newProps, | ||
...propOverrides, | ||
}; | ||
}; | ||
let features; | ||
const recurseGeoJson = (node, propOverrides, jsFnMutator, extraProps) => { | ||
if (node && node.features) { | ||
node.features.forEach((obj) => { | ||
recurseGeoJson(obj, propOverrides, jsFnMutator, node.extraProps || extraProps); | ||
}); | ||
} | ||
if (node && node.properties) { | ||
const newNode = { | ||
...node, | ||
properties: alterProps(node.properties, propOverrides), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Overrides wouldn't apply in that case. It'd have to be |
||
}; | ||
if (jsFnMutator) { | ||
jsFnMutator(newNode); | ||
} | ||
if (!newNode.extraProps) { | ||
newNode.extraProps = extraProps; | ||
} | ||
features.push(newNode); | ||
} | ||
}; | ||
|
||
export default function geoJsonLayer(formData, payload, slice) { | ||
const fd = formData; | ||
const fc = fd.fill_color_picker; | ||
const sc = fd.stroke_color_picker; | ||
let data = payload.data.geojson.features.map(d => ({ | ||
...d, | ||
properties: convertGeoJsonColorProps( | ||
d.properties, { | ||
fillColor: [fc.r, fc.g, fc.b, 255 * fc.a], | ||
strokeColor: [sc.r, sc.g, sc.b, 255 * sc.a], | ||
}), | ||
})); | ||
const fillColor = [fc.r, fc.g, fc.b, 255 * fc.a]; | ||
const strokeColor = [sc.r, sc.g, sc.b, 255 * sc.a]; | ||
const propOverrides = {}; | ||
if (fillColor[3] > 0) { | ||
propOverrides.fillColor = fillColor; | ||
} | ||
if (strokeColor[3] > 0) { | ||
propOverrides.strokeColor = strokeColor; | ||
} | ||
|
||
let jsFnMutator; | ||
if (fd.js_datapoint_mutator) { | ||
// Applying user defined data mutator if defined | ||
const jsFnMutator = sandboxedEval(fd.js_datapoint_mutator); | ||
data = data.map(jsFnMutator); | ||
jsFnMutator = sandboxedEval(fd.js_datapoint_mutator); | ||
} | ||
|
||
features = []; | ||
recurseGeoJson(payload.data, propOverrides, jsFnMutator); | ||
return new GeoJsonLayer({ | ||
id: `path-layer-${fd.slice_id}`, | ||
data, | ||
id: `geojson-layer-${fd.slice_id}`, | ||
filled: fd.filled, | ||
data: features, | ||
stroked: fd.stroked, | ||
extruded: fd.extruded, | ||
pointRadiusScale: fd.point_radius_scale, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think checking for
geometry
instead ofproperties
would be better here. Not all nodes need the property set, but the geometry is mandatory for defining the shape.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good call