Skip to content

Commit

Permalink
Improved handling of multi visits
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanwins committed Jul 8, 2021
1 parent 79d050b commit 63f0cf0
Show file tree
Hide file tree
Showing 15 changed files with 266 additions and 340 deletions.
7 changes: 4 additions & 3 deletions debug/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
</template>

<script>
const data = require('../../test/harness/in/new-test.geojson')
const data = require('../../test/harness/in/diamond-hole.geojson')
import splitPoly from '../../src/index'
import L from 'leaflet'
Expand All @@ -27,7 +27,8 @@ export default {
mounted () {
const layer = L.geoJSON(data, {
weight: 10
weight: 10,
fillOpacity: 0,
})
let map = window.map = L.map('map', {
Expand All @@ -39,7 +40,7 @@ export default {
const out = splitPoly(data.features[0], data.features[1])
console.log(JSON.stringify(out))
L.geoJSON(out, {
fillOpacity: 0,
fillOpacity: 0.2,
weight: 3,
color: '#1bfa06'
}).addTo(map)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "polygon-splitter",
"version": "0.0.1",
"version": "0.0.2",
"description": "Split a polygon using a polyline.",
"main": "dist/polygonsplitter.js",
"module": "src/polygonsplitter.es.js",
Expand Down
47 changes: 28 additions & 19 deletions src/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,39 @@ export function _debugIntersectionPoints(points) {
if (process.env.NODE_ENV !== 'development') return
const map = window.map
const pLayerGroup = L.layerGroup([]).addTo(map)
L.NumberedDivIcon = createNumberDiv()

points.forEach(function (p, index) {
L.marker([p.p[1], p.p[0]], {
icon: new L.NumberedDivIcon({
number: index.toString()
})
points.forEach(function (p) {
L.circleMarker([p.p[1], p.p[0]], {
color: 'red'
}).addTo(pLayerGroup)

L.marker([p.to.p[1], p.to.p[0]], {
icon: new L.NumberedDivIcon({
number: 'to'
})
}).addTo(pLayerGroup)
// L.marker([p.to.p[1], p.to.p[0]], {
// icon: new L.NumberedDivIcon({
// number: 'to'
// })
// }).addTo(pLayerGroup)

// L.marker([p.from.p[1], p.from.p[0]], {
// icon: new L.NumberedDivIcon({
// number: 'from'
// })
// }).addTo(pLayerGroup)
})
debugger
pLayerGroup.clearLayers()
}

L.marker([p.from.p[1], p.from.p[0]], {
icon: new L.NumberedDivIcon({
number: 'from'
})
}).addTo(pLayerGroup)
export function _debugPolyStart(polyStart) {
if (process.env.NODE_ENV !== 'development') return
const map = window.map
const pLayerGroup = L.layerGroup([]).addTo(map)

debugger
pLayerGroup.clearLayers()
})
L.circleMarker([polyStart.p[1], polyStart.p[0]], {
color: 'grey'
}).addTo(pLayerGroup)

debugger
pLayerGroup.clearLayers()
}

export function _debugCandidatePoly(outPolys) {
Expand Down
29 changes: 22 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {fillQueue} from './fillQueue'
import {findIntersectionPoints} from './findIntersections.js'
// import { _debugCandidatePoly, _debugLinePoints } from './debug'
// import { _debugCandidatePoly, _debugLinePoints, _debugIntersectionPoints, _debugPolyStart } from './debug'

export default function (polygon, line) {
const intersections = []
Expand All @@ -11,9 +11,8 @@ export default function (polygon, line) {
fillQueue(polygon, line, polygonEdges, polylineEdges, polylineBbox)

findIntersectionPoints(polygonEdges, polylineEdges, intersections)

const outPolys = []

// _debugIntersectionPoints(intersections)
// Start the rewiring of the outputs from the first intersection point along the polyline line
// This step makes a difference (eg see the another.geojson harness file)
let firstPolyStart = null
Expand All @@ -26,15 +25,16 @@ export default function (polygon, line) {
}

let polyStart = firstPolyStart
let nextPolyStart = {visitCount: 0}

let nextPolyStart = {visitCount: 1}
// Basically we're going to walk our way around the outside of the polygon
// to find new output polygons until we get back to the beginning
while (firstPolyStart !== nextPolyStart) {

// If we've already visited this intersection point a couple of times we've
// If we've already visited this intersection point a couple of times we've
// already used it in it's two output polygons
if (nextPolyStart.visitCount > 2) {
// _debugPolyStart(polyStart)

if (nextPolyStart.visitCount >= 2) {
let unvisitedPolyFound = false
for (let index = 0; index < intersections.length; index++) {
const intersection = intersections[index]
Expand Down Expand Up @@ -77,6 +77,21 @@ export default function (polygon, line) {
}
}

if (nextPolyStart.visitCount >= 2) {
let unvisitedPolyFound = false
for (let index = 0; index < intersections.length; index++) {
const intersection = intersections[index]
if (intersection.visitCount < 2) {
polyStart = intersection
unvisitedPolyFound = true
break
}
}
if (unvisitedPolyFound) {
nextPolyStart = polyStart
}
}

// Finally we set the next start point based on what we found earlier
polyStart = nextPolyStart
}
Expand Down
41 changes: 41 additions & 0 deletions test/harness/in/issue-3.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
0,
0
],
[
10,
0
],
[
10,
10
],
[
0,
10
],
[
0,
0
]
]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {"type":"LineString","coordinates":[[-2.5,5],[5,12.5],[12.5,5],[5,-2.5]]}
}
]
}
63 changes: 63 additions & 0 deletions test/harness/issue-2.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
0,
0
],
[
10,
0
],
[
10,
10
],
[
0,
10
],
[
0,
0
]
],
[
[
2.5,
2.5
],
[
2.5,
7.5
],
[
7.5,
7.5
],
[
7.5,
2.5
],
[
2.5,
2.5
]
]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {"type":"LineString","coordinates":[[5,11],[5,-1]]}
}
]
}
42 changes: 0 additions & 42 deletions test/harness/out/another.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -83,48 +83,6 @@
37.452356729579535
]
],
[
[
-4.577570928873623,
36.58402594799985
],
[
-8.7890625,
29.22889003019423
],
[
-10.268773905918655,
31.315459870524577
],
[
-10.420516912917522,
22.753050337755617
],
[
-1.40625,
16.29905101458183
],
[
8.39454910130475,
29.675469453626235
],
[
6.94217924404392,
32.43344262896994
],
[
1.0546875,
34.016241889667015
],
[
-0.2351737330180419,
37.452356729579535
],
[
-4.577570928873623,
36.58402594799985
]
],
[
[
-10.268773905918655,
Expand Down
26 changes: 0 additions & 26 deletions test/harness/out/four-out.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -87,32 +87,6 @@
-28.77976589744536
]
],
[
[
33.15161879272161,
-11.662864586103698
],
[
20.0390625,
9.10209673
],
[
9.25822042743565,
-3.957347207299521
],
[
26.808103709086467,
-31.702400726244424
],
[
30.131917990054166,
-28.77976589744536
],
[
33.15161879272161,
-11.662864586103698
]
],
[
[
-0.8457820015479172,
Expand Down
Loading

0 comments on commit 63f0cf0

Please sign in to comment.