Skip to content

Commit

Permalink
Update website
Browse files Browse the repository at this point in the history
  • Loading branch information
mbloch committed Mar 22, 2024
1 parent f7a1ea5 commit 04a4adf
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 75 deletions.
139 changes: 64 additions & 75 deletions mapshaper-gui.js
Original file line number Diff line number Diff line change
Expand Up @@ -5468,7 +5468,7 @@
labels: 'position labels',
location: 'drag points',
vertices: 'edit vertices',
selection: 'select features',
selection: 'selection tool',
'add-points': 'add points',
drawing: 'draw/reshape tool',
rectangles: 'drag-to-resize',
Expand Down Expand Up @@ -9719,7 +9719,7 @@
}
}

// pointer thresholds for hovering near a vertex or segment midpoint
// pixel distance threshold for hovering near a vertex or segment midpoint
var HOVER_THRESHOLD$1 = 10;

function initLineEditing(gui, ext, hit) {
Expand Down Expand Up @@ -9789,27 +9789,10 @@
appendVertex$1(target, e.p);
}
if (e.shapes) {
replaceShapes(e.shapes);
replaceDrawnShapes(e.shapes);
}
});

function replaceShapes(shapes) {
var target = hit.getHitTarget();
var records = target.layer.data?.getRecords();
var prevLen = target.layer.shapes.length;
var newLen = initialShapeCount + shapes.length;
var recordCount = records?.length || 0;
target.layer.shapes = target.layer.shapes.slice(0, initialShapeCount).concat(shapes);
while (records && records.length > newLen) {
records.pop();
}
while (records && records.length < newLen) {
appendNewDataRecord(target.layer);
}
}



gui.on('undo_path_extend', function(e) {
var target = hit.getHitTarget();
if (drawing() && prevHoverEvent) {
Expand All @@ -9822,7 +9805,7 @@
gui.undo.undo(); // remove the path
}
if (e.shapes) {
replaceShapes(e.shapes);
replaceDrawnShapes(e.shapes);
}
});

Expand All @@ -9845,17 +9828,20 @@
non_blocking: true, max_width: '360px'});
}

function hideInstructions() {
if (!alert) return;
alert.close('fade');
alert = null;
}

function turnOff() {
var removed = 0;
finishCurrentPath();
if (polygonMode()) {
removed = removeOpenPolygons();
}
clearDrawingInfo();
if (alert) {
alert.close();
alert = null;
}
hideInstructions();
initialArcCount = -1;
initialShapeCount = -1;
if (gui.interaction.getMode() == 'drawing') {
Expand All @@ -9872,18 +9858,19 @@
var target = hit.getHitTarget();
var arcs = target.source.dataset.arcs;
var n = target.layer.shapes.length;
// delete open paths (should only occur on single-arc shapes)
// delete open paths
for (var i=initialShapeCount; i<n; i++) {
var shp = target.layer.shapes[i];
if (!geom.pathIsClosed(shp[0], arcs)) {
if (!geom.pathIsClosed(shp[0], arcs)) { // assume open paths have one arc
target.layer.shapes[i] = null;
}
}
// removes polygons with wrong winding order and null geometry
// removes features with wrong winding order or null geometry
mapshaper.cmd.filterFeatures(target.layer, arcs, {remove_empty: true, quiet: true});
return n - target.layer.shapes.length;
}

// updates display arcs and redraws all layers
function fullRedraw() {
gui.model.updated({arc_count: true});
}
Expand All @@ -9897,11 +9884,10 @@

hit.on('dragstart', function(e) {
if (!active() || drawing() || !hoverVertexInfo) return;
if (alert) {
alert.close('fade');
}
hideInstructions();
e.originalEvent.stopPropagation();
_dragging = true;
updateCursor();
if (hoverVertexInfo.type == 'interpolated') {
insertVertex$1(hit.getHitTarget(), hoverVertexInfo.i, hoverVertexInfo.point);
hoverVertexInfo.ids = [hoverVertexInfo.i];
Expand All @@ -9919,7 +9905,6 @@
}
internal.snapVerticesToPoint(hoverVertexInfo.ids, p, target.arcs);
hit.setHoverVertex(p, '');

// redrawing the whole map updates the data layer as well as the overlay layer
// gui.dispatchEvent('map-needs-refresh');
});
Expand Down Expand Up @@ -9975,6 +9960,7 @@
} else {
clearHoverVertex();
}
updateCursor();
prevHoverEvent = e;
}, null, 100);

Expand All @@ -9985,19 +9971,17 @@
var p = pixToDataCoords(e.x, e.y);
if (drawing()) {
extendCurrentPath(hoverVertexInfo?.point || p);
// extendCurrentPath(p); // just extend to current mouse position (not hover vertex)
} else if (gui.keyboard.shiftIsPressed()) {
deleteActiveVertex(e);
} else {
startNewPath(p);
if (alert) {
alert.close('fade');
}
hideInstructions();
updateCursor();
}
prevClickEvent = e;
});

// esc key finishes a path
// esc or enter key finishes a path
gui.keyboard.on('keydown', function(e) {
if (active() && (e.keyName == 'esc' || e.keyName == 'enter')) {
e.stopPropagation();
Expand All @@ -10015,6 +9999,11 @@
return dbl;
}

function updateCursor() {
var useArrow = hoverVertexInfo && !hoverVertexInfo.extendable && !drawing();
gui.container.findChild('.map-layers').classed('dragging', useArrow);
}

function deleteActiveVertex(e) {
var info = findDraggableVertices(e);
if (!info) return;
Expand Down Expand Up @@ -10104,10 +10093,10 @@
}
if (finish && polygonMode()) {
shapes1 = target.layer.shapes.slice(initialShapeCount);
shapes2 = tryToClosePath(shapes1);
shapes2 = convertClosedPaths(shapes1);
}
if (shapes2) {
replaceShapes(shapes2);
replaceDrawnShapes(shapes2);
gui.dispatchEvent('path_extend', {target, p, shapes1, shapes2});
clearDrawingInfo();
fullRedraw();
Expand All @@ -10117,7 +10106,21 @@
gui.dispatchEvent('path_extend', {target, p});
hit.triggerChangeEvent(); // trigger overlay redraw
}
}

function replaceDrawnShapes(shapes) {
var target = hit.getHitTarget();
var records = target.layer.data?.getRecords();
var prevLen = target.layer.shapes.length;
var newLen = initialShapeCount + shapes.length;
var recordCount = records?.length || 0;
target.layer.shapes = target.layer.shapes.slice(0, initialShapeCount).concat(shapes);
while (records && records.length > newLen) {
records.pop();
}
while (records && records.length < newLen) {
appendNewDataRecord(target.layer);
}
}

// p: [x, y] source data coordinates
Expand Down Expand Up @@ -10187,10 +10190,6 @@
y1 = yy[i],
x2 = xx[j],
y2 = yy[j],
// switching from midpoint to nearest point to the mouse
// cx = (x1 + x2) / 2,
// cy = (y1 + y2) / 2,
// p2 = [cx, cy],
p2 = internal.findClosestPointOnSeg(p[0], p[1], x1, y1, x2, y2, 0),
dist = geom.distance2D(p2[0], p2[1], p[0], p[1]);
if (dist < minDist) {
Expand All @@ -10212,49 +10211,39 @@
return closest;
}

// shapes: shapes that have been drawn in the current session
//
function tryToClosePath(shapes) {
// Try to form polygon shapes from an array of path shapes
// shapes: array of all shapes that have been drawn in the current session
function convertClosedPaths(shapes) {
var target = hit.getHitTarget();
// try to convert paths to polygons
// NOTE: added "no_cuts" option to prevent polygons function from modifying
// arcs, which would break undo/redo and cause other problems
var tmpLyr = {
geometry_type: 'polyline',
shapes: shapes.concat()
};
// create a temp dataset containing tmp layer and original layers
// (so original arcs are retained)
var tmpDataset = Object.assign({}, target.source.dataset);
tmpDataset.layers = tmpDataset.layers.concat(tmpLyr);
// NOTE: added "no_cuts" option to prevent polygons function from modifying
// arcs, which would break undo/redo and cause other problems
var outputLyr = mapshaper.cmd.polygons([tmpLyr], tmpDataset, {no_cuts: true})[0];
var isOpenPath = getOpenPathTest(outputLyr.shapes);
var shapes2 = [];
shapes.forEach(function(shp) {
if (isOpenPath(shp)) {
shapes2.push(shp);
}
});
return shapes2.concat(outputLyr.shapes);
var output = mapshaper.cmd.polygons([tmpLyr], target.source.dataset, {no_cuts: true});
var closedShapes = output[0].shapes;

// find paths that were not convertible to polygons
var isOpenPath = getOpenPathTest(closedShapes);
var openShapes = shapes.filter(function(shp) { return isOpenPath(shp); });

// retain both converted polygons and unconverted polylines
return openShapes.concat(closedShapes);
}

// Returns a function for testing if a shape is an unclosed path, and doesn't
// overlap with an array of polygon shapes
// polygons: array of polygon shapes
function getOpenPathTest(polygons) {
var arcs = [];
internal.forEachArcId(polygons, function(arcId) {
if (arcId < 0) arcId = ~arcId;
arcs.push(arcId);
function getOpenPathTest(polygonShapes) {
var polygonArcs = [];
internal.forEachArcId(polygonShapes, function(arcId) {
polygonArcs.push(internal.absArcId(arcId));
});

return function(shp) {
// assume that compound shapes are already polygons
var isOpen = false;
if (shapeHasOneFwdArc(shp)) {
var arcId = shp[0][0];
if (arcId < 0) arcId = ~arcId;
isOpen = !arcs.includes(arcId);
}
return isOpen;
// assume that any compound shape is a polygon
return shapeHasOneFwdArc(shp) && !polygonArcs.includes(shp[0][0]);
};
}

Expand Down
4 changes: 4 additions & 0 deletions page.css
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,10 @@ div.basemap-style-btn.active img {
cursor: crosshair;
}

.map-layers.drawing.dragging {
cursor: default;
}

.map-layers canvas {
pointer-events: none;
position: absolute;
Expand Down

0 comments on commit 04a4adf

Please sign in to comment.