fixed No mapping #575

This commit is contained in:
Jan Prochazka 2023-09-30 11:43:50 +02:00
parent a98d5d29ca
commit 731c4c046c
3 changed files with 43 additions and 22 deletions

View File

@ -393,7 +393,7 @@
import { apiCall } from '../utility/api';
import getElectron from '../utility/getElectron';
import { isCtrlOrCommandKey, isMac } from '../utility/common';
import { selectionCouldBeShownOnMap } from '../elements/SelectionMapView.svelte';
import { createGeoJsonFromSelection, selectionCouldBeShownOnMap } from '../elements/SelectionMapView.svelte';
import ErrorMessageModal from '../modals/ErrorMessageModal.svelte';
import EditCellDataModal, { shouldOpenMultilineDialog } from '../modals/EditCellDataModal.svelte';
import { getDatabaseInfo, useDatabaseStatus } from '../utility/metadataLoaders';
@ -682,13 +682,21 @@
showModal(ErrorMessageModal, { message: 'There is nothing to be shown on map' });
return;
}
const geoJson = createGeoJsonFromSelection(selection);
if (!geoJson) {
showModal(ErrorMessageModal, { message: 'There is nothing to be shown on map' });
return;
}
openNewTab(
{
title: 'Map',
icon: 'img map',
tabComponent: 'MapTab',
},
{ editor: selection.map(x => _.omit(x, ['engine'])) }
{ editor: geoJson }
);
return;
}

View File

@ -17,6 +17,7 @@
function addObjectToMap() {
if (!map) return;
if (!geoJson) return;
for (const layer of layers) {
layer.remove();

View File

@ -11,10 +11,10 @@
return res;
}
export function findLatPaths(obj) {
return findLatLonPaths(obj, x => x.includes('lat'));
return findLatLonPaths(obj, x => x.toLowerCase()?.includes('lat'));
}
export function findLonPaths(obj) {
return findLatLonPaths(obj, x => x.includes('lon') || x.includes('lng'));
return findLatLonPaths(obj, x => x.toLowerCase()?.includes('lon') || x.toLowerCase()?.includes('lng'));
}
export function findAllObjectPaths(obj) {
return findLatLonPaths(obj, (_k, v) => v != null && !_.isNaN(Number(v)));
@ -33,21 +33,6 @@
}
return false;
}
</script>
<script lang="ts">
import _ from 'lodash';
import 'leaflet/dist/leaflet.css';
import wellknown from 'wellknown';
import { isWktGeometry, stringifyCellValue } from 'dbgate-tools';
import MapView from './MapView.svelte';
export let selection;
export let latitudeField = '';
export let longitudeField = '';
let geoJson;
function createColumnsTable(cells) {
if (cells.length == 0) return '';
@ -56,9 +41,10 @@
.join('\n')}</table>`;
}
function createGeoJson() {
export function createGeoJsonFromSelection(selection, latitudeFieldDef = null, longitudeFieldDef = null) {
const selectedRows = _.groupBy(selection || [], 'row');
console.log('ROWS', selectedRows);
const features = [];
for (const rowKey of _.keys(selectedRows)) {
@ -66,6 +52,9 @@
const geoValues = cells.map(x => x.value).filter(isWktGeometry);
const latitudeField = latitudeFieldDef ?? findLatPaths(cells[0].rowData)[0];
const longitudeField = longitudeFieldDef ?? findLonPaths(cells[0].rowData)[0];
const lat = latitudeField ? Number(_.get(cells[0].rowData, latitudeField)) : NaN;
const lon = longitudeField ? Number(_.get(cells[0].rowData, longitudeField)) : NaN;
@ -97,14 +86,37 @@
}
if (features.length == 0) {
return;
return null;
}
geoJson = {
return {
type: 'FeatureCollection',
features,
};
}
</script>
<script lang="ts">
import _ from 'lodash';
import 'leaflet/dist/leaflet.css';
import wellknown from 'wellknown';
import { isWktGeometry, stringifyCellValue } from 'dbgate-tools';
import MapView from './MapView.svelte';
export let selection;
export let latitudeField = '';
export let longitudeField = '';
let geoJson;
function createGeoJson() {
const res = createGeoJsonFromSelection(selection, latitudeField, longitudeField);
if (res) {
geoJson = res;
}
}
$: {
selection;