Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/w8r/martinez into refacto…
Browse files Browse the repository at this point in the history
…r-connect-edges
  • Loading branch information
w8r committed Feb 23, 2020
2 parents 123d104 + 01990ed commit 69b15e2
Show file tree
Hide file tree
Showing 32 changed files with 2,425 additions and 475 deletions.
251 changes: 137 additions & 114 deletions demo/js/index.js
Original file line number Diff line number Diff line change
@@ -1,161 +1,185 @@
import './coordinates';
import './polygoncontrol';
import './booleanopcontrol';
import * as martinez from '../../index';
//var martinez = require('../../dist/martinez.min');
import "./coordinates";
import "./polygoncontrol";
import "./booleanopcontrol";
import * as martinez from "../../index";
// import * as martinez from '../../dist/martinez.min';

let mode = window.location.hash.substring(1);
let path = '../test/fixtures/';
const ext = '.geojson';
let path = "../test/fixtures/";
const ext = ".geojson";
let file;

let files = [
'asia', 'trapezoid-box', 'canada', 'horseshoe', 'hourglasses', 'overlap_y',
'polygon_trapezoid_edge_overlap', 'touching_boxes', 'two_pointed_triangles',
'hole_cut', 'overlapping_segments', 'overlap_loop', 'disjoint_boxes'
"asia",
"trapezoid-box",
"canada",
"horseshoe",
"hourglasses",
"overlap_y",
"polygon_trapezoid_edge_overlap",
"touching_boxes",
"two_pointed_triangles",
"hole_cut",
"overlapping_segments",
"overlap_loop",
"disjoint_boxes"
];

switch (mode) {
case 'geo':
file = 'asia.geojson';
case "geo":
file = "asia.geojson";
break;
case 'states':
file = 'states_source.geojson';
case "states":
file = "states_source.geojson";
break;
case 'trapezoid':
file = 'trapezoid-box.geojson';
case "trapezoid":
file = "trapezoid-box.geojson";
break;
case 'canada':
file = 'canada.geojson';
case "canada":
file = "canada.geojson";
break;
case 'horseshoe':
file = 'horseshoe.geojson';
case "horseshoe":
file = "horseshoe.geojson";
break;
case 'hourglasses':
file = 'hourglasses.geojson';
case "hourglasses":
file = "hourglasses.geojson";
break;
case 'edge_overlap':
file = 'polygon_trapezoid_edge_overlap.geojson';
case "edge_overlap":
file = "polygon_trapezoid_edge_overlap.geojson";
break;
case 'touching_boxes':
file = 'touching_boxes.geojson';
case "touching_boxes":
file = "touching_boxes.geojson";
break;
case 'triangles':
file = 'two_pointed_triangles.geojson';
case "triangles":
file = "two_pointed_triangles.geojson";
break;
case 'holecut':
file = 'hole_cut.geojson';
case "holecut":
file = "hole_cut.geojson";
break;
case 'overlapping_segments':
file = 'overlapping_segments.geojson';
case "overlapping_segments":
file = "overlapping_segments.geojson";
break;
case 'overlap_loop':
file = 'overlap_loop.geojson';
case "overlap_loop":
file = "overlap_loop.geojson";
break;
case 'overlap_y':
file = 'overlap_y.geojson';
case "overlap_y":
file = "overlap_y.geojson";
break;
case 'overlap_two':
file = 'overlap_two.geojson';
case "overlap_two":
file = "overlap_two.geojson";
break;
case 'disjoint_boxes':
file = 'disjoint_boxes.geojson';
case "disjoint_boxes":
file = "disjoint_boxes.geojson";
break;
case 'polygons_edge_overlap':
file = 'polygons_edge_overlap.geojson';
case "polygons_edge_overlap":
file = "polygons_edge_overlap.geojson";
break;
case 'vertical_boxes':
file = 'vertical_boxes.geojson';
case "vertical_boxes":
file = "vertical_boxes.geojson";
break;
case 'collapsed':
file = 'collapsed.geojson';
case "collapsed":
file = "collapsed.geojson";
break;
case 'fatal1':
file = 'fatal1.geojson';
case "fatal1":
file = "fatal1.geojson";
break;
case 'fatal2':
file = 'fatal2.geojson';
case "fatal2":
file = "fatal2.geojson";
break;
case 'fatal3':
file = 'fatal3.geojson';
case "fatal3":
file = "fatal3.geojson";
break;
case 'fatal4':
file = 'fatal4.geojson';
case "fatal4":
file = "fatal4.geojson";
break;
case 'rectangles':
file = 'rectangles.geojson';
case "rectangles":
file = "rectangles.geojson";
break;
default:
file = 'hole_hole.geojson';
file = "hole_hole.geojson";
break;
}

console.log(mode);


var OPERATIONS = {
INTERSECTION: 0,
UNION: 1,
DIFFERENCE: 2,
XOR: 3
UNION: 1,
DIFFERENCE: 2,
XOR: 3
};

var div = document.createElement('div');
div.id = 'image-map';
div.style.width = div.style.height = '100%';
var div = document.createElement("div");
div.id = "image-map";
div.style.width = div.style.height = "100%";
document.body.appendChild(div);

// create the slippy map
var map = window.map = L.map('image-map', {
var map = (window.map = L.map("image-map", {
minZoom: 1,
maxZoom: 20,
center: [0, 0],
zoom: 2,
crs: mode === 'geo' ? L.CRS.EPSG4326 : L.extend({}, L.CRS.Simple, {
transformation: new L.Transformation(1/8, 0, -1/8, 0)
}),
crs:
mode === "geo"
? L.CRS.EPSG4326
: L.extend({}, L.CRS.Simple, {
transformation: new L.Transformation(1 / 8, 0, -1 / 8, 0)
}),
editable: true
});

map.addControl(new L.NewPolygonControl({
callback: map.editTools.startPolygon
}));
map.addControl(new L.Coordinates());
map.addControl(new L.BooleanControl({
callback: run,
clear: clear
}));

var drawnItems = window.drawnItems = L.geoJson().addTo(map);
map.addControl(
new L.NewPolygonControl({
callback: map.editTools.startPolygon
})
);
map.addControl(new L.Coordinates());
map.addControl(
new L.BooleanControl({
callback: run,
clear: clear
})
);

var drawnItems = (window.drawnItems = L.geoJson().addTo(map));
var rawData = null;
function loadData(path) {
console.log(path);
fetch(path)
.then((r) => r.json())
.then((json) => {
drawnItems.addData(json);
map.fitBounds(drawnItems.getBounds().pad(0.05), { animate: false });
.then(r => r.json())
.then(json => {
drawnItems.addData(json);
rawData = json;
map.fitBounds(drawnItems.getBounds().pad(0.05), { animate: false });
});
}

function clear() {
drawnItems.clearLayers();
results.clearLayers();
rawData = null;
}

var reader = new jsts.io.GeoJSONReader();
var writer = new jsts.io.GeoJSONWriter();

function run (op) {
function getClippingPoly(layers) {
if (rawData !== null && rawData.features.length > 1)
return rawData.features[1];
return layers[1].toGeoJSON();
}

function run(op) {
var layers = drawnItems.getLayers();
if (layers.length < 2) return;
var subject = layers[0].toGeoJSON();
var clipping = layers[1].toGeoJSON();
var subject = rawData !== null ? rawData.features[0] : layers[0].toGeoJSON();
var clipping = getClippingPoly(layers);

//console.log('input', subject, clipping, op);

subject = JSON.parse(JSON.stringify(subject));
clipping = JSON.parse(JSON.stringify(clipping));
// subject = JSON.parse(JSON.stringify(subject));
// clipping = JSON.parse(JSON.stringify(clipping));

var operation;
if (op === OPERATIONS.INTERSECTION) {
Expand All @@ -164,36 +188,38 @@ function run (op) {
operation = martinez.union;
} else if (op === OPERATIONS.DIFFERENCE) {
operation = martinez.diff;
} else if (op === 5) { // B - A
} else if (op === 5) {
// B - A
operation = martinez.diff;

var temp = subject;
subject = clipping;
subject = clipping;
clipping = temp;
} else {
operation = martinez.xor;
}

console.time('martinez');
var result = operation(subject.geometry.coordinates, clipping.geometry.coordinates);
console.timeEnd('martinez');
console.time("martinez");
var result = operation(
subject.geometry.coordinates,
clipping.geometry.coordinates
);
console.timeEnd("martinez");

//if (op === OPERATIONS.UNION) result = result[0];
console.log('result', result);
console.log(JSON.stringify(result))
console.log("result", result);
results.clearLayers();

if (result !== null) {
results.addData({
'type': 'Feature',
'geometry': {
'type': 'MultiPolygon',
'coordinates': result
type: "Feature",
geometry: {
type: "MultiPolygon",
coordinates: result
}
});

setTimeout(function() {
console.time('jsts');
console.time("jsts");
var s = reader.read(subject);
var c = reader.read(clipping);
var res;
Expand All @@ -207,34 +233,31 @@ function run (op) {
res = s.geometry.symDifference(c.geometry);
}
res = writer.write(res);
console.timeEnd('jsts');
console.log(res);
console.timeEnd("jsts");
// console.log('JSTS result', res);
}, 500);
}
}

//drawnItems.addData(oneInside);
//drawnItems.addData(twoPointedTriangles);
//drawnItems.addData(selfIntersecting);
//drawnItems.addData(holes);
//drawnItems.addData(data);

map.on('editable:created', function(evt) {
map.on("editable:created", function(evt) {
drawnItems.addLayer(evt.layer);
evt.layer.on('click', function(e) {
if ((e.originalEvent.ctrlKey || e.originalEvent.metaKey) && this.editEnabled()) {
evt.layer.on("click", function(e) {
if (
(e.originalEvent.ctrlKey || e.originalEvent.metaKey) &&
this.editEnabled()
) {
this.editor.newHole(e.latlng);
}
});
});

var results = window.results = L.geoJson(null, {
var results = (window.results = L.geoJson(null, {
style: function(feature) {
return {
color: 'red',
color: "red",
weight: 1
};
}
}).addTo(map);
}).addTo(map));

loadData(path + file);
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"eslint-plugin-jsdoc": "^20.0.2",
"eslint-plugin-promise": "^3.8.0",
"geojson-project": "^1.0.0",
"json-stringify-pretty-compact": "^2.0.0",
"http-server": "^0.12.1",
"leaflet": "^1.2.0",
"leaflet-editable": "^1.1.0",
Expand Down
Loading

0 comments on commit 69b15e2

Please sign in to comment.