Skip to content

Commit

Permalink
Fix for bug affecting -polygons and -mosaic
Browse files Browse the repository at this point in the history
  • Loading branch information
mbloch committed Mar 9, 2024
1 parent 5a48b93 commit 827c000
Show file tree
Hide file tree
Showing 6 changed files with 298 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/commands/mapshaper-mosaic.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import cmd from '../mapshaper-cmd';
import { stop } from '../utils/mapshaper-logging';
import { DataTable } from '../datatable/mapshaper-data-table';
import { MosaicIndex } from '../polygons/mapshaper-mosaic-index';
import { getArcPresenceTest } from '../paths/mapshaper-path-utils';

cmd.mosaic = function(layers, dataset, opts) {
var lyr = layers[0];
Expand All @@ -13,6 +14,8 @@ cmd.mosaic = function(layers, dataset, opts) {
}
requirePolygonLayer(lyr);
var nodes = addIntersectionCuts(dataset, opts);
// ignore arcs that don't belong to this layer
nodes.setArcFilter(getArcPresenceTest(lyr.shapes, nodes.arcs));
var mosaicIndex = new MosaicIndex(lyr, nodes, {flat: false});
var mosaicShapes = mosaicIndex.mosaic;
var records2;
Expand Down
3 changes: 3 additions & 0 deletions src/commands/mapshaper-polygons.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { requirePolylineLayer } from '../dataset/mapshaper-layer-utils';
import cmd from '../mapshaper-cmd';
import { message, stop } from '../utils/mapshaper-logging';
import geom from '../geom/mapshaper-geom';
import { getArcPresenceTest } from '../paths/mapshaper-path-utils';

cmd.polygons = function(layers, dataset, opts) {
layers.forEach(requirePolylineLayer);
Expand Down Expand Up @@ -46,6 +47,8 @@ function createPolygonLayerFromRings(lyr, dataset) {

function createPolygonLayer(lyr, dataset, opts) {
var nodes = closeUndershoots(lyr, dataset, opts);
// ignore arcs that don't belong to this layer
nodes.setArcFilter(getArcPresenceTest(lyr.shapes, nodes.arcs));
var data = buildPolygonMosaic(nodes);
return {
geometry_type: 'polygon',
Expand Down
21 changes: 14 additions & 7 deletions src/topology/mapshaper-nodes.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ export function NodeCollection(arcs, filter) {
nn = arcData.nn,
xx = arcData.xx,
yy = arcData.yy,
nodeData;
nodeData,
globalFilter;

// Accessor function for arcs
Object.defineProperty(this, 'arcs', {value: arcs});

this.setArcFilter = function(f) {
globalFilter = f;
};

this.toArray = function() {
var chains = getNodeChains(),
flags = new Uint8Array(chains.length),
Expand Down Expand Up @@ -99,17 +104,19 @@ export function NodeCollection(arcs, filter) {
// Returned ids lead into the node (as opposed to outwards from it)
// An optional filter function receives the directed id (positive or negative)
// of each connected arc and excludes arcs for which the filter returns false.
// The filter is also applied to the initial arc; if false, no arcs are returned.
// // removed: The filter is also applied to the initial arc; if false, no arcs are returned.
//
this.getConnectedArcs = function(arcId, filter) {
this.getConnectedArcs = function(arcId, localFilter) {
var ids = [];
var filtered = !!filter;
var nextId = nextConnectedArc(arcId);
if (filtered && !filter(arcId)) {
// return ids;
// kludge: return empty result if arc fails global test
// ... applying the local filter causes tests to fail
if (globalFilter && !globalFilter(arcId)) {
return [];
}
while (nextId != arcId) {
if (!filtered || filter(nextId)) {
// if (!filtered || filter && filter(nextId) ) {
if ((!localFilter || localFilter(nextId)) && (!globalFilter || globalFilter(nextId))) {
ids.push(nextId);
}
nextId = nextConnectedArc(nextId);
Expand Down
Loading

0 comments on commit 827c000

Please sign in to comment.