Subs union to server side
This commit is contained in:
parent
ed7c89476f
commit
ac4331edbd
10 changed files with 299 additions and 162 deletions
|
|
@ -3,89 +3,43 @@
|
|||
/* eslint-disable import/no-absolute-path */
|
||||
/* global L */
|
||||
|
||||
import { Map } from 'react-leaflet';
|
||||
import LGeo from 'leaflet-geodesy';
|
||||
import tunion from '@turf/union';
|
||||
import ttrunc from '@turf/truncate';
|
||||
import { check, Match } from 'meteor/check';
|
||||
|
||||
// https://stackoverflow.com/questions/35394577/leaflet-js-union-merge-circles
|
||||
const truncOptions = { precision: 6, coordinates: 2 };
|
||||
|
||||
function unify(polyList) {
|
||||
let unionTemp;
|
||||
for (let i = 0; i < polyList.length; i += 1) {
|
||||
const pol = polyList[i].toGeoJSON();
|
||||
const cleanPol = ttrunc(pol, truncOptions);
|
||||
if (i === 0) {
|
||||
unionTemp = cleanPol;
|
||||
} else {
|
||||
unionTemp = ttrunc(tunion(unionTemp, cleanPol), truncOptions);
|
||||
}
|
||||
}
|
||||
return unionTemp;
|
||||
}
|
||||
import calcUnion from '/imports/ui/components/Maps/SubsUnion/Unify';
|
||||
|
||||
const subsUnion = (union, options) => {
|
||||
// check(union, Match.Optional(Object));
|
||||
check(options, {
|
||||
map: Map,
|
||||
show: Boolean,
|
||||
subs: [Object],
|
||||
color: Match.Optional(String),
|
||||
fillcolor: Match.Optional(String),
|
||||
opacity: Match.Optional(Number),
|
||||
fit: Boolean
|
||||
});
|
||||
|
||||
const color = options.color || '#145A32';
|
||||
const fillColor = options.fillColor || 'green';
|
||||
const opacity = options.options || 0.1;
|
||||
|
||||
if (options.subs) {
|
||||
const lmap = options.map.leafletElement;
|
||||
// http://leafletjs.com/reference-1.2.0.html#layergroup
|
||||
// FeatureGroup has getBounds
|
||||
const unionGroup = new L.FeatureGroup();
|
||||
|
||||
if (union) {
|
||||
lmap.removeLayer(union);
|
||||
}
|
||||
union = null;
|
||||
|
||||
if (options.subs.length > 0 && options.show) {
|
||||
// http://leafletjs.com/reference-1.2.0.html#path
|
||||
const copts = {
|
||||
parts: 144
|
||||
};
|
||||
options.subs.forEach((sub) => {
|
||||
try {
|
||||
if (sub.location && sub.location.lat && sub.location.lon && sub.distance) {
|
||||
check(sub.location.lon, Number);
|
||||
check(sub.location.lat, Number);
|
||||
check(sub.distance, Number);
|
||||
const circle = LGeo.circle([sub.location.lat, sub.location.lon], sub.distance * 1000, copts);
|
||||
circle.addTo(unionGroup);
|
||||
} else {
|
||||
console.error(`Wrong subscription ${JSON.stringify(sub)}`);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
console.error(`Wrong subscription trying to make union ${JSON.stringify(sub)}`);
|
||||
if (options.show) {
|
||||
if (options.fromServer) {
|
||||
// We get the json from server side
|
||||
union = L.geoJson(JSON.parse(options.subs));
|
||||
union.setStyle({ color, fillColor, fillOpacity: opacity });
|
||||
union.addTo(lmap);
|
||||
if (options.fit && options.bounds) {
|
||||
// console.log(options.bounds);
|
||||
const bounds = JSON.parse(options.bounds);
|
||||
options.map.leafletElement.fitBounds(L.latLngBounds(bounds._northEast, bounds._southWest));
|
||||
}
|
||||
} else if (options.subs.length > 0) {
|
||||
const unionGroup = new L.FeatureGroup();
|
||||
const result = calcUnion(options.subs, unionGroup, sub => sub);
|
||||
const unionJson = result[0];
|
||||
const bounds = result[1];
|
||||
|
||||
union = L.geoJson(unionJson);
|
||||
union.setStyle({ color, fillColor, fillOpacity: opacity });
|
||||
union.addTo(lmap);
|
||||
if (options.fit) {
|
||||
options.map.leafletElement.fitBounds(bounds);
|
||||
}
|
||||
});
|
||||
const unionJson = unify(unionGroup.getLayers());
|
||||
union = L.geoJson(unionJson);
|
||||
union.setStyle({
|
||||
color,
|
||||
fillColor,
|
||||
fillOpacity: opacity
|
||||
});
|
||||
union.addTo(lmap);
|
||||
if (options.fit) {
|
||||
options.map.leafletElement.fitBounds(unionGroup.getBounds());
|
||||
}
|
||||
return union;
|
||||
}
|
||||
}
|
||||
return union;
|
||||
|
|
|
|||
48
imports/ui/components/Maps/SubsUnion/Unify.js
Normal file
48
imports/ui/components/Maps/SubsUnion/Unify.js
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
import { check } from 'meteor/check';
|
||||
import LGeo from 'leaflet-geodesy';
|
||||
import tunion from '@turf/union';
|
||||
import ttrunc from '@turf/truncate';
|
||||
|
||||
// https://stackoverflow.com/questions/35394577/leaflet-js-union-merge-circles
|
||||
const truncOptions = { precision: 6, coordinates: 2 };
|
||||
|
||||
const unify = (polyList) => {
|
||||
let unionTemp;
|
||||
for (let i = 0; i < polyList.length; i += 1) {
|
||||
const pol = polyList[i].toGeoJSON();
|
||||
const cleanPol = ttrunc(pol, truncOptions);
|
||||
if (i === 0) {
|
||||
unionTemp = cleanPol;
|
||||
} else {
|
||||
unionTemp = ttrunc(tunion(unionTemp, cleanPol), truncOptions);
|
||||
}
|
||||
}
|
||||
return unionTemp;
|
||||
};
|
||||
|
||||
const calcUnion = (subs, group, decorated) => {
|
||||
const unionGroup = group;
|
||||
const copts = {
|
||||
parts: 144
|
||||
};
|
||||
subs.forEach((osub) => {
|
||||
try {
|
||||
if (osub.location && osub.location.lat && osub.location.lon && osub.distance) {
|
||||
check(osub.location.lon, Number);
|
||||
check(osub.location.lat, Number);
|
||||
check(osub.distance, Number);
|
||||
const dsub = decorated(osub);
|
||||
const circle = LGeo.circle([dsub.location.lat, dsub.location.lon], dsub.distance * 1000, copts);
|
||||
circle.addTo(unionGroup);
|
||||
} else {
|
||||
console.error(`Wrong subscription ${JSON.stringify(osub)}`);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e, `Wrong subscription trying to make union ${JSON.stringify(osub)}`);
|
||||
}
|
||||
});
|
||||
const unionJson = unify(unionGroup.getLayers());
|
||||
return [unionJson, unionGroup.getBounds()];
|
||||
};
|
||||
|
||||
export default calcUnion;
|
||||
Loading…
Add table
Add a link
Reference in a new issue