From 457d30a89e369c140d2774fe62ac74e2a4a21fc6 Mon Sep 17 00:00:00 2001 From: tariqksoliman Date: Thu, 3 Aug 2023 16:29:05 -0700 Subject: [PATCH 1/2] #407 Include Query layers in click intersections --- src/essence/Basics/Layers_/Layers_.js | 3 ++- src/essence/Tools/Kinds/Kinds.js | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/essence/Basics/Layers_/Layers_.js b/src/essence/Basics/Layers_/Layers_.js index 9246b7fb..f0295f85 100644 --- a/src/essence/Basics/Layers_/Layers_.js +++ b/src/essence/Basics/Layers_/Layers_.js @@ -2987,7 +2987,8 @@ const L_ = { Object.keys(L_.layers.layer).forEach((lName) => { if ( L_.layers.on[lName] && - L_.layers.data[lName].type === 'vector' && + (L_.layers.data[lName].type === 'vector' || + L_.layers.data[lName].type === 'query') && L_.layers.layer[lName] ) { const nextFeatures = L.leafletPip diff --git a/src/essence/Tools/Kinds/Kinds.js b/src/essence/Tools/Kinds/Kinds.js index 0fe00be6..73591871 100644 --- a/src/essence/Tools/Kinds/Kinds.js +++ b/src/essence/Tools/Kinds/Kinds.js @@ -417,7 +417,8 @@ var Kinds = { Object.keys(L_.layers.layer).forEach((lName) => { if ( L_.layers.on[lName] && - L_.layers.data[lName].type === 'vector' && + (L_.layers.data[lName].type === 'vector' || + L_.layers.data[lName].type === 'query') && L_.layers.layer[lName] ) { features = features.concat( From cf0c05854ee5781c8c94309fc13ae260a21794ac Mon Sep 17 00:00:00 2001 From: tariqksoliman Date: Thu, 3 Aug 2023 18:15:38 -0700 Subject: [PATCH 2/2] #407 Support intersects on DrawTool layers too --- src/essence/Ancillary/ContextMenu.js | 8 ++- src/essence/Ancillary/Search.js | 7 +- src/essence/Basics/Formulae_/Formulae_.js | 88 +++++++++++++---------- src/essence/Basics/Layers_/Layers_.js | 11 +-- src/essence/Tools/Info/InfoTool.js | 2 +- src/essence/Tools/Kinds/Kinds.js | 13 ++-- src/external/Leaflet/leaflet-pip.js | 37 ++++++++++ 7 files changed, 113 insertions(+), 53 deletions(-) diff --git a/src/essence/Ancillary/ContextMenu.js b/src/essence/Ancillary/ContextMenu.js index 5c7f8d0f..0b600034 100644 --- a/src/essence/Ancillary/ContextMenu.js +++ b/src/essence/Ancillary/ContextMenu.js @@ -58,7 +58,7 @@ function showContextMenuMap(e) { featuresAtClick.map((f, idx2) => { const items = [] const layerName = f.options.layerName - const displayName = L_.layers.data[layerName].display_name + const displayName = L_.layers.data[layerName]?.display_name || layerName const pv = L_.getLayersChosenNamePropVal(f.feature, layerName) const key = Object.keys(pv)[0] const val = pv[key] @@ -128,13 +128,15 @@ function showContextMenuMap(e) { } }) + const geom = F_.simplifyGeometry(l.feature.geometry, 0.0003) + let wkt if (link.indexOf(`{wkt}`) !== -1) { - wkt = geojsonToWKT(l.feature.geometry) + wkt = geojsonToWKT(geom) link = link.replace(new RegExp(`{wkt}`, 'gi'), wkt) } if (link.indexOf(`{wkt_}`) !== -1) { - wkt = geojsonToWKT(l.feature.geometry) + wkt = geojsonToWKT(geom) link = link.replace( new RegExp(`{wkt_}`, 'gi'), wkt.replace(/,/g, '_') diff --git a/src/essence/Ancillary/Search.js b/src/essence/Ancillary/Search.js index fe89dfc1..bf1d284d 100644 --- a/src/essence/Ancillary/Search.js +++ b/src/essence/Ancillary/Search.js @@ -1,7 +1,7 @@ import $ from 'jquery' import F_ from '../Basics/Formulae_/Formulae_' //jqueryUI -import turf from 'turf' +import { center } from '@turf/turf' import * as d3 from 'd3' import Dropy from '../../external/Dropy/dropy' @@ -510,8 +510,9 @@ function getMapZoomCoordinate(layers) { var longitudeValidRange = [-180, 180] for (var i = 0; i < layers.length; i++) { - const center = turf.center(layers[i].feature)?.geometry - ?.coordinates || [-1001, -1001] + const center = center(layers[i].feature)?.geometry?.coordinates || [ + -1001, -1001, + ] var latitude = center[1] var longitude = center[0] diff --git a/src/essence/Basics/Formulae_/Formulae_.js b/src/essence/Basics/Formulae_/Formulae_.js index bcf81a83..c2fd3147 100644 --- a/src/essence/Basics/Formulae_/Formulae_.js +++ b/src/essence/Basics/Formulae_/Formulae_.js @@ -1,5 +1,5 @@ //Holds a bunch of reusable mathy formulas and variables -import turf from '@turf/turf' +import { bbox, simplify } from '@turf/turf' import { saveAs } from 'file-saver' import $ from 'jquery' import calls from '../../../pre/calls' @@ -248,6 +248,9 @@ var Formulae_ = { degreesToMeters: function (degrees) { return degrees * (Math.PI / 180) * this.radiusOfPlanetMajor }, + simplifyGeometry: function (geometry, tolerance) { + return simplify(geometry, { tolerance: tolerance }) + }, //2D distanceFormula: function (x1, y1, x2, y2) { return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)) @@ -1422,7 +1425,7 @@ var Formulae_ = { for (var i = 0; i < savedFeatures.length; i++) { if (i != 0) { featuresString += '\n,' - savedFeatures[i].properties['boundingbox'] = turf.bbox( + savedFeatures[i].properties['boundingbox'] = bbox( savedFeatures[i] ) } @@ -1720,43 +1723,54 @@ var Formulae_ = { }, // searchRadiusInDegrees = [lng, lng, lat, lat] bbox pointsInPoint(point, layers, searchRadiusInDegrees) { - const points = [] - - const l = layers._layers + let points = [] - if (l == null) return points - - for (let i in l) { - if (searchRadiusInDegrees != null) { - if ( - l[i].feature.geometry.coordinates[0] > - Math.min( - searchRadiusInDegrees[0], - searchRadiusInDegrees[1] - ) && - l[i].feature.geometry.coordinates[0] < - Math.max( - searchRadiusInDegrees[0], - searchRadiusInDegrees[1] - ) && - l[i].feature.geometry.coordinates[1] > - Math.min( - searchRadiusInDegrees[2], - searchRadiusInDegrees[3] - ) && - l[i].feature.geometry.coordinates[1] < - Math.max( - searchRadiusInDegrees[2], - searchRadiusInDegrees[3] - ) - ) { + if (Array.isArray(layers)) { + layers.forEach((l) => { + points = points.concat( + Formulae_.pointsInPoint(point, l, searchRadiusInDegrees) + ) + }) + } else { + let l + if (layers.feature && layers.feature.geometry?.type === 'Point') { + l = [layers] + } else l = layers._layers + + if (l == null) return points + for (let i in l) { + if (l[i].feature == null) continue + if (searchRadiusInDegrees != null) { + if ( + l[i].feature.geometry.coordinates[0] > + Math.min( + searchRadiusInDegrees[0], + searchRadiusInDegrees[1] + ) && + l[i].feature.geometry.coordinates[0] < + Math.max( + searchRadiusInDegrees[0], + searchRadiusInDegrees[1] + ) && + l[i].feature.geometry.coordinates[1] > + Math.min( + searchRadiusInDegrees[2], + searchRadiusInDegrees[3] + ) && + l[i].feature.geometry.coordinates[1] < + Math.max( + searchRadiusInDegrees[2], + searchRadiusInDegrees[3] + ) + ) { + points.push(l[i]) + } + } else if ( + l[i].feature.geometry.coordinates[0] == point[0] && + l[i].feature.geometry.coordinates[1] == point[1] + ) points.push(l[i]) - } - } else if ( - l[i].feature.geometry.coordinates[0] == point[0] && - l[i].feature.geometry.coordinates[1] == point[1] - ) - points.push(l[i]) + } } return points diff --git a/src/essence/Basics/Layers_/Layers_.js b/src/essence/Basics/Layers_/Layers_.js index f0295f85..687d20a1 100644 --- a/src/essence/Basics/Layers_/Layers_.js +++ b/src/essence/Basics/Layers_/Layers_.js @@ -2903,6 +2903,7 @@ const L_ = { if (layerName != null) { const l = L_.layers.data[layerName] if ( + l && l.hasOwnProperty('variables') && l.variables.hasOwnProperty('useKeyAsName') ) { @@ -2986,10 +2987,12 @@ const L_ = { // Find all the intersected points and polygons of the click Object.keys(L_.layers.layer).forEach((lName) => { if ( - L_.layers.on[lName] && - (L_.layers.data[lName].type === 'vector' || - L_.layers.data[lName].type === 'query') && - L_.layers.layer[lName] + (L_.layers.on[lName] && + (L_.layers.data[lName].type === 'vector' || + L_.layers.data[lName].type === 'query') && + L_.layers.layer[lName]) || + (lName.indexOf('DrawTool_') === 0 && + L_.layers.layer[lName]?.[0]?._map != null) ) { const nextFeatures = L.leafletPip .pointInLayer( diff --git a/src/essence/Tools/Info/InfoTool.js b/src/essence/Tools/Info/InfoTool.js index 03ed0ace..63a659ff 100644 --- a/src/essence/Tools/Info/InfoTool.js +++ b/src/essence/Tools/Info/InfoTool.js @@ -220,7 +220,7 @@ var InfoTool = { Dropy.init($('#infoToolSelectedDropdown'), function (idx) { let e = JSON.parse(JSON.stringify(InfoTool.initialEvent)) Kinds.use( - L_.layers.data[InfoTool.currentLayerName].kind, + L_.layers.data[InfoTool.currentLayerName]?.kind || null, Map_, InfoTool.info[idx], InfoTool.featureLayers[idx] || InfoTool.currentLayer, diff --git a/src/essence/Tools/Kinds/Kinds.js b/src/essence/Tools/Kinds/Kinds.js index 73591871..b15afc22 100644 --- a/src/essence/Tools/Kinds/Kinds.js +++ b/src/essence/Tools/Kinds/Kinds.js @@ -370,7 +370,8 @@ var Kinds = { if (e.latlng && e.latlng.lng != null && e.latlng.lat != null) { if ( typeof L_.layers.layer[layerName].eachLayer !== - 'function' + 'function' && + layerName.indexOf('DrawTool_') != 0 ) { L_.layers.layer[layerName].eachLayer = function (cb) { for (var v in this._vectorTiles) { @@ -416,10 +417,12 @@ var Kinds = { // Find all the intersected points and polygons of the click Object.keys(L_.layers.layer).forEach((lName) => { if ( - L_.layers.on[lName] && - (L_.layers.data[lName].type === 'vector' || - L_.layers.data[lName].type === 'query') && - L_.layers.layer[lName] + (L_.layers.on[lName] && + (L_.layers.data[lName].type === 'vector' || + L_.layers.data[lName].type === 'query') && + L_.layers.layer[lName]) || + (lName.indexOf('DrawTool_') === 0 && + L_.layers.layer[lName]?.[0]?._map != null) ) { features = features.concat( L.leafletPip diff --git a/src/external/Leaflet/leaflet-pip.js b/src/external/Leaflet/leaflet-pip.js index a7ee2854..fc2d1698 100644 --- a/src/external/Leaflet/leaflet-pip.js +++ b/src/external/Leaflet/leaflet-pip.js @@ -68,6 +68,7 @@ p = p.concat().reverse() var results = [] + if (typeof layer.eachLayer === 'function') { layer.eachLayer(function (l) { if (first && results.length) return @@ -84,6 +85,42 @@ results.push(l) } }) + } else if (Array.isArray(layer)) { + layer.forEach((innerLayer) => { + if ( + typeof innerLayer.eachLayer === + 'function' + ) { + innerLayer.eachLayer(function (l) { + if (first && results.length) return + if ( + isPoly(l) && + gju.pointInPolygon( + { + type: 'Point', + coordinates: p, + }, + l.toGeoJSON(10).geometry + ) + ) { + results.push(l) + } + }) + } else { + if ( + isPoly(innerLayer) && + gju.pointInPolygon( + { + type: 'Point', + coordinates: p, + }, + innerLayer.feature.geometry + ) + ) { + results.push(innerLayer) + } + } + }) } else { if ( isPoly(layer) &&