Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#407 Click intersects on Query and Draw layers #408

Merged
merged 2 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/essence/Ancillary/ContextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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, '_')
Expand Down
7 changes: 4 additions & 3 deletions src/essence/Ancillary/Search.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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]

Expand Down
88 changes: 51 additions & 37 deletions src/essence/Basics/Formulae_/Formulae_.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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]
)
}
Expand Down Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions src/essence/Basics/Layers_/Layers_.js
Original file line number Diff line number Diff line change
Expand Up @@ -2903,6 +2903,7 @@ const L_ = {
if (layerName != null) {
const l = L_.layers.data[layerName]
if (
l &&
l.hasOwnProperty('variables') &&
l.variables.hasOwnProperty('useKeyAsName')
) {
Expand Down Expand Up @@ -2986,9 +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.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(
Expand Down
2 changes: 1 addition & 1 deletion src/essence/Tools/Info/InfoTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 8 additions & 4 deletions src/essence/Tools/Kinds/Kinds.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -416,9 +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.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
Expand Down
37 changes: 37 additions & 0 deletions src/external/Leaflet/leaflet-pip.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
p = p.concat().reverse()

var results = []

if (typeof layer.eachLayer === 'function') {
layer.eachLayer(function (l) {
if (first && results.length) return
Expand All @@ -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) &&
Expand Down