Skip to content

Commit

Permalink
#407 Support intersects on DrawTool layers too
Browse files Browse the repository at this point in the history
  • Loading branch information
tariqksoliman committed Aug 4, 2023
1 parent 457d30a commit cf0c058
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 53 deletions.
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
11 changes: 7 additions & 4 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,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(
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
13 changes: 8 additions & 5 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,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
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

0 comments on commit cf0c058

Please sign in to comment.