map cell view improved

This commit is contained in:
Jan Prochazka
2022-12-31 12:29:47 +01:00
parent 4a2b33276d
commit cb0a9770d2
4 changed files with 106 additions and 24 deletions

View File

@@ -1,16 +1,44 @@
<script lang="ts" context="module">
function findLatLonPaths(obj, attrTest, res = [], prefix = '') {
for (const key of Object.keys(obj)) {
if (attrTest(key, obj[key])) {
res.push(prefix + key);
}
if (_.isPlainObject(obj[key])) {
findLatLonPaths(obj[key], attrTest, res, prefix + key + '.');
}
}
return res;
}
export function findLatPaths(obj) {
return findLatLonPaths(obj, x => x.includes('lat'));
}
export function findLonPaths(obj) {
return findLatLonPaths(obj, x => x.includes('lon') || x.includes('lng'));
}
export function findAllObjectPaths(obj) {
return findLatLonPaths(obj, (_k, v) => v != null && !_.isNaN(Number(v)));
}
export function selectionCouldBeShownOnMap(selection) {
if (selection.length > 0 && _.find(selection, x => isWktGeometry(x.value))) {
return true;
}
if (
selection.find(x => x.column.toLowerCase().includes('lat')) &&
(selection.find(x => x.column.toLowerCase().includes('lon')) ||
selection.find(x => x.column.toLowerCase().includes('lng')))
selection.length > 0 &&
_.find(selection, x => findLatPaths(x.rowData).length > 0 && findLonPaths(x.rowData).length > 0)
) {
return true;
}
// if (
// selection.find(x => x.column.toLowerCase().includes('lat')) &&
// (selection.find(x => x.column.toLowerCase().includes('lon')) ||
// selection.find(x => x.column.toLowerCase().includes('lng')))
// ) {
// return true;
// }
return false;
}
</script>
@@ -21,7 +49,7 @@
import 'leaflet/dist/leaflet.css';
import leaflet from 'leaflet';
import wellknown from 'wellknown';
import { isWktGeometry, ScriptWriter, ScriptWriterJson } from 'dbgate-tools';
import { isWktGeometry, ScriptWriter, ScriptWriterJson, stringifyCellValue } from 'dbgate-tools';
import resizeObserver from '../utility/resizeObserver';
import openNewTab from '../utility/openNewTab';
import contextMenu from '../utility/contextMenu';
@@ -31,6 +59,9 @@
export let selection;
export let latitudeField = '';
export let longitudeField = '';
let refContainer;
let map;
@@ -39,7 +70,9 @@
function createColumnsTable(cells) {
if (cells.length == 0) return '';
return `<table>${cells.map(cell => `<tr><td>${cell.column}</td><td>${cell.value}</td></tr>`).join('\n')}</table>`;
return `<table>${cells
.map(cell => `<tr><td>${cell.column}</td><td>${stringifyCellValue(cell.value)}</td></tr>`)
.join('\n')}</table>`;
}
function addSelectionToMap() {
@@ -57,12 +90,15 @@
for (const rowKey of _.keys(selectedRows)) {
const cells = selectedRows[rowKey];
const lat = cells.find(x => x.column.toLowerCase().includes('lat'));
const lon = cells.find(x => x.column.toLowerCase().includes('lon') || x.column.toLowerCase().includes('lng'));
// const lat = cells.find(x => x.column.toLowerCase().includes('lat'));
// const lon = cells.find(x => x.column.toLowerCase().includes('lon') || x.column.toLowerCase().includes('lng'));
const geoValues = cells.map(x => x.value).filter(isWktGeometry);
if (lat && lon) {
const lat = latitudeField ? Number(_.get(cells[0].rowData, latitudeField)) : NaN;
const lon = longitudeField ? Number(_.get(cells[0].rowData, longitudeField)) : NaN;
if (!_.isNaN(lat) && !_.isNaN(lon)) {
features.push({
type: 'Feature',
properties: {
@@ -70,7 +106,7 @@
},
geometry: {
type: 'Point',
coordinates: [lon.value, lat.value],
coordinates: [Number(lon), Number(lat)],
},
});
}
@@ -156,6 +192,8 @@
$: {
selection;
latitudeField;
longitudeField;
addSelectionToMap();
}