Skip to content

Commit

Permalink
merge a new node to existing node
Browse files Browse the repository at this point in the history
  • Loading branch information
geohacker committed Jun 4, 2020
1 parent 73a39f3 commit da020b2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
5 changes: 5 additions & 0 deletions app/components/WayEditingOverlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import CrossHairOverlay from './CrosshairOverlay'
import FeatureRelationErrorDialog from './FeatureRelationErrorDialog'

import { modes } from '../utils/map-modes'
import { isNewId } from '../utils/utils'

const Container = styled.View`
position: absolute;
Expand Down Expand Up @@ -192,6 +193,10 @@ class WayEditingOverlay extends React.Component {
}
}

if (isNewId(selectedNode.properties.id)) {
this.props.deleteSelectedNode(selectedNode)
this.props.addNode(nearestFeatures.nearestNode)
}
this.props.mergeSelectedNode(selectedNode, nearestFeatures.nearestNode)
} else {
this.props.moveSelectedNode(selectedNode, center)
Expand Down
16 changes: 11 additions & 5 deletions app/reducers/wayEditingHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ function wayEditingHistory (state = createDefaultState(), action) {
// So we'll just push the new node
newWay.nodes = [...newNodes, node]

const addedNodes = [...state.addedNodes, node.properties.id]
const addedNodes = _cloneDeep(state.addedNodes)
addedNodes.push(node.properties.id)

return {
...state,
way: newWay,
Expand Down Expand Up @@ -144,10 +146,14 @@ function wayEditingHistory (state = createDefaultState(), action) {
const newWay = _cloneDeep(way)

let mergedNodes = _cloneDeep(state.mergedNodes)
mergedNodes.push({
sourceNode: sourceNode.properties.id,
destinationNode: destinationNode.properties.id
})
// if the source node is a new node, then this pretty much an addNode operation.
// so no need to say we are merging
if (!isNewId(sourceNode.properties.id)) {
mergedNodes.push({
sourceNode: sourceNode.properties.id,
destinationNode: destinationNode.properties.id
})
}

return {
...state,
Expand Down
2 changes: 1 addition & 1 deletion app/utils/create-way-feature.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function createWayFeature (nodes = [], properties = {}, options =
if (!options.id) {
options.id = getRandomId()
}
properties.id = options.id
properties.id = `way/${options.id}`

const coordinates = nodes.map((node, index) => {
// populate ndrefs
Expand Down
6 changes: 4 additions & 2 deletions app/utils/modify-shared-ways.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,21 @@ function mergeNode (sharedWays, sourceNode, destinationNode) {
let modifiedSharedWays = []
sharedWays.forEach(oldWay => {
const newWay = _cloneDeep(oldWay)
const sourceNodeId = sourceNode.properties.id.startsWith('node') ? sourceNode.properties.id.split('/')[1] : sourceNode.properties.id
// const destinationNodeId = destinationNode.properties.id.startsWith('node') ? destinationNode.properties.id.split('/')[1] : destinationNode.properties.id

let indexOfSourceNode
if (newWay.geometry.type === 'LineString') {
indexOfSourceNode = _findIndex(newWay.properties.ndrefs, (r) => {
return _isEqual(r, sourceNode.properties.id.split('/')[1])
return _isEqual(r, sourceNodeId)
})
// update the geometry
newWay.geometry.coordinates.splice(indexOfSourceNode, 1, destinationNode.geometry.coordinates)
}

if (newWay.geometry.type === 'Polygon') {
indexOfSourceNode = _findIndex(newWay.properties.ndrefs, (r) => {
return _isEqual(r, sourceNode.properties.id.split('/')[1])
return _isEqual(r, sourceNodeId)
})
// update the geometry
newWay.geometry.coordinates[0].splice(indexOfSourceNode, 1, destinationNode.geometry.coordinates)
Expand Down

0 comments on commit da020b2

Please sign in to comment.