Skip to content

Commit

Permalink
Merge pull request #302 from developmentseed/fix/keep-polygons-closed
Browse files Browse the repository at this point in the history
Keep polygons closed
  • Loading branch information
geohacker authored Jun 9, 2020
2 parents 836ccf7 + 1c00f41 commit 386ec52
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
4 changes: 2 additions & 2 deletions __tests__/utils/__snapshots__/nearest.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ Object {
"location": 0.0018341862152104543,
"membership": "no",
"ways": Object {
"way/4294967474": null,
"4294967474": null,
},
},
"type": "Feature",
Expand Down Expand Up @@ -261,7 +261,7 @@ Object {
"location": 0.0018341862152104543,
"membership": "no",
"ways": Object {
"way/4294967474": null,
"4294967474": null,
},
},
"type": "Feature",
Expand Down
2 changes: 1 addition & 1 deletion app/components/WayEditingOverlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class WayEditingOverlay extends React.Component {
let feature
if (present.modifiedSharedWays.length) {
feature = present.modifiedSharedWays.find((way) => {
return way.id === properties.id
return way.properties.id === properties.id
})
} else {
feature = present.way
Expand Down
4 changes: 2 additions & 2 deletions app/services/nodecache.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export async function purgeNodeCache () {
try {
await AsyncStorage.clear()
} catch (error) {
console.error('Failed to purge async storage cache')
console.warn('Failed to purge async storage cache')
}
}

Expand All @@ -84,6 +84,6 @@ export async function clearNodeCacheForTile (tile) {
await AsyncStorage.multiRemove(keys)
await AsyncStorage.removeItem(tile)
} catch (error) {
console.error('Failed to remove node', error)
console.warn('Failed to remove node', error)
}
}
24 changes: 18 additions & 6 deletions app/utils/modify-shared-ways.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ function moveNode (sharedWays, node, coordinates) {
let modifiedSharedWays = []
sharedWays.forEach(oldWay => {
const newWay = _cloneDeep(oldWay)
const wayId = oldWay.properties.id.startsWith('way') ? oldWay.properties.id.split('/')[1] : oldWay.properties.id
const indexOfNodeInWay = node.properties.ways[wayId]
const nodeId = node.properties.id.startsWith('node') ? node.properties.id.split('/')[1] : node.properties.id
const indexOfNodeInWay = _findIndex(newWay.properties.ndrefs, ref => ref === nodeId)

if (newWay.geometry.type === 'LineString') {
newWay.geometry.coordinates[indexOfNodeInWay] = coordinates
Expand Down Expand Up @@ -44,15 +44,27 @@ function moveNode (sharedWays, node, coordinates) {
function deleteNode (sharedWays, node) {
let modifiedSharedWays = []
sharedWays.forEach(oldWay => {
const newWay = _cloneDeep(oldWay)
const wayId = oldWay.properties.id.startsWith('way') ? oldWay.properties.id.split('/')[1] : oldWay.properties.id
const indexOfNodeInWay = node.properties.ways[wayId]
let newWay = _cloneDeep(oldWay)
const nodeId = node.properties.id.startsWith('node') ? node.properties.id.split('/')[1] : node.properties.id
const indexOfNodeInWay = _findIndex(newWay.properties.ndrefs, ref => ref === nodeId)

if (newWay.geometry.type === 'LineString') {
newWay.geometry.coordinates.splice(indexOfNodeInWay, 1)
}

if (newWay.geometry.type === 'Polygon') {
newWay.geometry.coordinates[0].splice(indexOfNodeInWay, 1)
const numberOfNodes = newWay.properties.ndrefs.length - 1
const isFirstOrLastNode = indexOfNodeInWay === 0 || indexOfNodeInWay === numberOfNodes
if (isFirstOrLastNode) {
newWay.geometry.coordinates[0].splice(0, 1)
newWay.geometry.coordinates[0].splice(newWay.geometry.coordinates[0].length - 1, 1)

// make this way closed
newWay.geometry.coordinates[0].push(newWay.geometry.coordinates[0][0])
newWay.properties.ndrefs.push(newWay.properties.ndrefs[0])
} else {
newWay.geometry.coordinates[0].splice(indexOfNodeInWay, 1)
}
}

// remove the node from ndrefs
Expand Down
6 changes: 4 additions & 2 deletions app/utils/nearest.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,12 @@ export function findNearestPoint (node, edge) {
point.properties.edge = edge
point.properties.ways = {}
if (edge.properties.hasOwnProperty('parent_feature')) {
point.properties.ways[edge.properties.parent_feature.properties.id] = null
const wayId = edge.properties.parent_feature.properties.id.startsWith('way') ? edge.properties.parent_feature.properties.id.split('/')[1] : edge.properties.parent_feature.properties.id
point.properties.ways[wayId] = null
} else {
const wayId = edge.properties.id.startsWith('way') ? edge.properties.id.split('/')[1] : edge.properties.id
point.properties.ways = {}
point.properties.ways[edge.properties.id] = null
point.properties.ways[wayId] = null
}
return point
}
Expand Down

0 comments on commit 386ec52

Please sign in to comment.