Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy location as <map-feature> #804

Merged
merged 5 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 90 additions & 13 deletions src/mapml/handlers/ContextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,50 +300,127 @@ export var ContextMenu = L.Handler.extend({

_copyGCRS: function(e){
let mapEl = this.options.mapEl,
click = this.contextMenu._clickEvent;
this.contextMenu._copyData(`lon :${click.latlng.lng.toFixed(6)}, lat:${click.latlng.lat.toFixed(6)}`);
click = this.contextMenu._clickEvent,
projection = mapEl.projection;
let data = `<map-feature zoom="${mapEl.zoom}">
<map-featurecaption>Copied ${projection} gcrs location</map-featurecaption>
<map-properties>
<h2>Copied ${projection} gcrs location</h2>
<div style="text-align:center">${click.latlng.lng.toFixed(6)} ${click.latlng.lat.toFixed(6)}</div>
</map-properties>
<map-geometry cs="gcrs">
<map-point>
<map-coordinates>${click.latlng.lng.toFixed(6)} ${click.latlng.lat.toFixed(6)}</map-coordinates>
</map-point>
</map-geometry>
</map-feature>`;
this.contextMenu._copyData(data);
},

_copyTCRS: function(e){
let mapEl = this.options.mapEl,
click = this.contextMenu._clickEvent,
point = mapEl._map.project(click.latlng);
this.contextMenu._copyData(`z:${mapEl.zoom}, x:${point.x}, y:${point.y}`);
point = mapEl._map.project(click.latlng),
projection = mapEl.projection;
let data = `<map-feature zoom="${mapEl.zoom}">
<map-featurecaption>Copied ${projection} tcrs location</map-featurecaption>
<map-properties>
<h2>Copied ${projection} tcrs location</h2>
<div style="text-align:center">${point.x} ${point.y}</div>
</map-properties>
<map-geometry cs="tcrs">
<map-point>
<map-coordinates>${point.x} ${point.y}</map-coordinates>
</map-point>
</map-geometry>
</map-feature>`;
this.contextMenu._copyData(data);
},

_copyTileMatrix: function(e){
let mapEl = this.options.mapEl,
click = this.contextMenu._clickEvent,
point = mapEl._map.project(click.latlng),
tileSize = mapEl._map.options.crs.options.crs.tile.bounds.max.x;
this.contextMenu._copyData(`z:${mapEl.zoom}, column:${Math.trunc(point.x/tileSize)}, row:${Math.trunc(point.y/tileSize)}`);
tileSize = mapEl._map.options.crs.options.crs.tile.bounds.max.x,
projection = mapEl.projection;
let data = `<map-feature zoom="${mapEl.zoom}">
<map-featurecaption>Copied ${projection} tilematrix location</map-featurecaption>
<map-properties>
<h2>Copied ${projection} tilematrix location</h2>
<div style="text-align:center">${Math.trunc(point.x/tileSize)} ${Math.trunc(point.y/tileSize)}</div>
</map-properties>
<map-geometry cs="tilematrix">
<map-point>
<map-coordinates>${Math.trunc(point.x/tileSize)} ${Math.trunc(point.y/tileSize)}</map-coordinates>
</map-point>
</map-geometry>
</map-feature>`;
this.contextMenu._copyData(data);
},

_copyPCRS: function(e){
let mapEl = this.options.mapEl,
click = this.contextMenu._clickEvent,
point = mapEl._map.project(click.latlng),
scale = mapEl._map.options.crs.scale(+mapEl.zoom),
pcrs = mapEl._map.options.crs.transformation.untransform(point,scale);
this.contextMenu._copyData(`easting:${Math.round(pcrs.x)}, northing:${Math.round(pcrs.y)}`);
pcrs = mapEl._map.options.crs.transformation.untransform(point,scale),
projection = mapEl.projection;
let data = `<map-feature zoom="${mapEl.zoom}">
<map-featurecaption>Copied ${projection} pcrs location</map-featurecaption>
<map-properties>
<h2>Copied ${projection} pcrs location</h2>
<div style="text-align:center">${Math.round(pcrs.x)} ${Math.round(pcrs.y)}</div>
</map-properties>
<map-geometry cs="pcrs">
<map-point>
<map-coordinates>${Math.round(pcrs.x)} ${Math.round(pcrs.y)}</map-coordinates>
</map-point>
</map-geometry>
</map-feature>`;
this.contextMenu._copyData(data);
},

_copyTile: function(e){
let mapEl = this.options.mapEl,
click = this.contextMenu._clickEvent,
point = mapEl._map.options.crs.project(click.latlng),
tileSize = mapEl._map.options.crs.options.crs.tile.bounds.max.x,
pointX = point.x % tileSize, pointY = point.y % tileSize;
pointX = point.x % tileSize, pointY = point.y % tileSize,
projection = mapEl.projection;
if(pointX < 0) pointX+= tileSize;
if(pointY < 0) pointY+= tileSize;

this.contextMenu._copyData(`z:${mapEl.zoom}, i:${Math.trunc(pointX)}, j:${Math.trunc(pointY)}`);
let data = `<map-feature zoom="${mapEl.zoom}">
<map-featurecaption>Copied ${projection} tile location</map-featurecaption>
<map-properties>
<h2>Copied ${projection} tile location</h2>
<div style="text-align:center">${Math.trunc(pointX)} ${Math.trunc(pointY)}</div>
</map-properties>
<map-geometry cs="tile">
<map-point>
<map-coordinates>${Math.trunc(pointX)} ${Math.trunc(pointY)}</map-coordinates>
</map-point>
</map-geometry>
</map-feature>`;
this.contextMenu._copyData(data);
},

_copyMap: function(e){
let mapEl = this.options.mapEl,
click = this.contextMenu._clickEvent;
this.contextMenu._copyData(`z:${mapEl.zoom}, i:${Math.trunc(click.containerPoint.x)}, j:${Math.trunc(click.containerPoint.y)}`);
click = this.contextMenu._clickEvent,
projection = mapEl.projection;
let data = `<map-feature zoom="${mapEl.zoom}">
<map-featurecaption>Copied ${projection} map location</map-featurecaption>
<map-properties>
<h2>Copied ${projection} map location</h2>
<div style="text-align:center">${Math.trunc(click.containerPoint.x)} ${Math.trunc(click.containerPoint.y)}</div>
</map-properties>
<map-geometry cs="map">
<map-point>
<map-coordinates>${Math.trunc(click.containerPoint.x)} ${Math.trunc(click.containerPoint.y)}</map-coordinates>
</map-point>
</map-geometry>
</map-feature>`;
this.contextMenu._copyData(data);
},

_copyAllCoords: function(e){
Expand Down
5 changes: 5 additions & 0 deletions src/mapml/utils/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,11 @@ export var Util = {
text = text.replace(/(<!--.*?-->)|(<!--[\S\s]+?-->)|(<!--[\S\s]*?$)/g, '').trim();
if ((text.slice(0,7) === "<layer-") && (text.slice(-9) === "</layer->")) {
mapEl.insertAdjacentHTML("beforeend", text);
} else if (text.slice(0,12) === "<map-feature" && text.slice(-14) === "</map-feature>") {
let layer = `<layer- label="Pasted features" checked>
<map-meta name='projection' content='${mapEl.projection}'></map-meta>`+text+
"</layer->";
mapEl.insertAdjacentHTML("beforeend", layer);
} else {
try {
mapEl.geojson2mapml(JSON.parse(text));
Expand Down
46 changes: 44 additions & 2 deletions test/e2e/core/mapContextMenu.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ test.describe("Playwright Map Context Menu Tests", () => {
currLocCS = await page.$eval(
"body > map",
(map) => (map._map.contextMenu.defLocCS)
)
);
// set cs to pcrs for copying location test
await page.$eval(
"body > map",
Expand All @@ -357,7 +357,18 @@ test.describe("Playwright Map Context Menu Tests", () => {
"body > textarea#coord",
(text) => text.value
);
const expected = "lon :-92.062002, lat:46.922393";
const expected = `<map-feature zoom="1">
<map-featurecaption>Copied CBMTILE gcrs location</map-featurecaption>
<map-properties>
<h2>Copied CBMTILE gcrs location</h2>
<div style="text-align:center">-92.062002 46.922393</div>
</map-properties>
<map-geometry cs="gcrs">
<map-point>
<map-coordinates>-92.062002 46.922393</map-coordinates>
</map-point>
</map-geometry>
</map-feature>`;
expect(copyValue).toEqual(expected);
await page.locator("body > textarea#coord").fill('');
await page.$eval(
Expand All @@ -368,7 +379,38 @@ test.describe("Playwright Map Context Menu Tests", () => {
currLocCS
);
});
test("Paste map-feature to map", async () => {
currLocCS = await page.$eval(
"body > map",
(map) => (map._map.contextMenu.defLocCS)
);
// set cs to pcrs for copying location test
await page.$eval(
"body > map",
(map) => {map._map.contextMenu.defLocCS = 'gcrs';}
);
await page.click("body > map");
await page.keyboard.press("Shift+F10");
await page.keyboard.press("Tab");
await page.keyboard.press("Tab");
await page.keyboard.press("Tab");
await page.keyboard.press("Enter");
await page.keyboard.press("Tab");
await page.keyboard.press("Tab");
await page.keyboard.press("Enter");
await page.click("body > map");
await page.keyboard.press("Shift+F10");
await page.keyboard.press("p");

const layerLabel = await page.$eval(
"body > map",
(map) => map.layers[1].label
);
expect(layerLabel).toEqual("Pasted features");
// clean up
await page.$eval("body > map",
(map) => map.removeChild(map.querySelector('[label="Pasted features"]')));
});
test("Paste valid Layer to map", async () => {
await page.click("body > textarea#layer");
await page.keyboard.press("Control+a");
Expand Down
46 changes: 44 additions & 2 deletions test/e2e/mapml-viewer/viewerContextMenu.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ test.describe("Playwright mapml-viewer Context Menu (and api) Tests", () => {
currLocCS = await page.$eval(
"body > mapml-viewer",
(map) => (map._map.contextMenu.defLocCS)
)
);
// set cs to pcrs for copying location test
await page.$eval(
"body > mapml-viewer",
Expand All @@ -363,7 +363,18 @@ test.describe("Playwright mapml-viewer Context Menu (and api) Tests", () => {
"body > textarea#coord",
(text) => text.value
);
const expected = "lon :-92.062002, lat:46.922393";
const expected = `<map-feature zoom="1">
<map-featurecaption>Copied CBMTILE gcrs location</map-featurecaption>
<map-properties>
<h2>Copied CBMTILE gcrs location</h2>
<div style="text-align:center">-92.062002 46.922393</div>
</map-properties>
<map-geometry cs="gcrs">
<map-point>
<map-coordinates>-92.062002 46.922393</map-coordinates>
</map-point>
</map-geometry>
</map-feature>`;
expect(copyValue).toEqual(expected);
await page.locator("body > textarea#coord").fill('');
await page.$eval(
Expand All @@ -374,7 +385,38 @@ test.describe("Playwright mapml-viewer Context Menu (and api) Tests", () => {
currLocCS
);
});
test("Paste map-feature to mapml-viewer", async () => {
currLocCS = await page.$eval(
"body > mapml-viewer",
(map) => (map._map.contextMenu.defLocCS)
);
// set cs to pcrs for copying location test
await page.$eval(
"body > mapml-viewer",
(map) => {map._map.contextMenu.defLocCS = 'gcrs';}
);
await page.click("body > mapml-viewer");
await page.keyboard.press("Shift+F10");
await page.keyboard.press("Tab");
await page.keyboard.press("Tab");
await page.keyboard.press("Tab");
await page.keyboard.press("Enter");
await page.keyboard.press("Tab");
await page.keyboard.press("Tab");
await page.keyboard.press("Enter");
await page.click("body > mapml-viewer");
await page.keyboard.press("Shift+F10");
await page.keyboard.press("p");

const layerLabel = await page.$eval(
"body > mapml-viewer",
(map) => map.layers[1].label
);
expect(layerLabel).toEqual("Pasted features");
// clean up
await page.$eval("body > mapml-viewer",
(map) => map.removeChild(map.querySelector('[label="Pasted features"]')));
});
test("Context menu, All buttons enabled when fwd and back history present", async () => {
await page.click("body > mapml-viewer");
await page.$eval(
Expand Down