|
| 1 | +import { LayerFeature } from '@/api/features.api' |
| 2 | +import { WGS84 } from '@/utils/coordinates/coordinateSystems' |
| 3 | +import reprojectGeoJsonData from '@/utils/geoJsonUtils' |
| 4 | +import log from '@/utils/logging' |
| 5 | +import distance from '@turf/distance' |
| 6 | +import { point } from '@turf/helpers' |
| 7 | +import proj4 from 'proj4' |
| 8 | + |
| 9 | +const pixelToleranceForIdentify = 10 |
| 10 | + |
| 11 | +/** |
| 12 | + * @param {GeoAdminGeoJsonLayer} geoJsonLayer |
| 13 | + * @param {[Number, Number]} coordinate |
| 14 | + * @param {CoordinateSystem} projection |
| 15 | + * @param {Number} resolution |
| 16 | + * @returns {LayerFeature[]} |
| 17 | + */ |
| 18 | +export function identifyGeoJSONFeatureAt(geoJsonLayer, coordinate, projection, resolution) { |
| 19 | + const features = [] |
| 20 | + // if there is a GeoJSON layer currently visible, we will find it and search for features under the mouse cursor |
| 21 | + const coordinateWGS84 = point(proj4(projection.epsg, WGS84.epsg, coordinate)) |
| 22 | + // to use turf functions, we need to have lat/lon (WGS84) coordinates |
| 23 | + const reprojectedGeoJSON = reprojectGeoJsonData(geoJsonLayer.geoJsonData, WGS84, projection) |
| 24 | + if (!reprojectedGeoJSON) { |
| 25 | + log.error( |
| 26 | + `Unable to reproject GeoJSON data in order to find features at coordinates`, |
| 27 | + geoJsonLayer.getID(), |
| 28 | + coordinate |
| 29 | + ) |
| 30 | + return [] |
| 31 | + } |
| 32 | + const matchingFeatures = reprojectedGeoJSON.features |
| 33 | + .filter((feature) => { |
| 34 | + const distanceWithClick = distance( |
| 35 | + coordinateWGS84, |
| 36 | + point(feature.geometry.coordinates), |
| 37 | + { |
| 38 | + units: 'meters', |
| 39 | + } |
| 40 | + ) |
| 41 | + return distanceWithClick <= pixelToleranceForIdentify * resolution |
| 42 | + }) |
| 43 | + .map((feature) => { |
| 44 | + // back to the starting projection |
| 45 | + feature.geometry.coordinates = proj4( |
| 46 | + WGS84.epsg, |
| 47 | + projection.epsg, |
| 48 | + feature.geometry.coordinates |
| 49 | + ) |
| 50 | + return new LayerFeature( |
| 51 | + geoJsonLayer, |
| 52 | + feature.id, |
| 53 | + feature.properties.station_name || feature.id, |
| 54 | + `<div class="htmlpopup-container"> |
| 55 | + <div class="htmlpopup-header"> |
| 56 | + <span>${geoJsonLayer.name}</span> |
| 57 | + </div> |
| 58 | + <div class="htmlpopup-content"> |
| 59 | + ${feature.properties.description} |
| 60 | + </div> |
| 61 | + </div>`, |
| 62 | + proj4(WGS84.epsg, projection.epsg, feature.geometry.coordinates), |
| 63 | + null, |
| 64 | + feature.geometry |
| 65 | + ) |
| 66 | + }) |
| 67 | + if (matchingFeatures?.length > 0) { |
| 68 | + features.push(...matchingFeatures) |
| 69 | + } |
| 70 | + return features |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * @param {KMLLayer} _kmlLayer |
| 75 | + * @param {[Number, Number]} _coordinate |
| 76 | + * @param {CoordinateSystem} _projection |
| 77 | + * @param {Number} _resolution |
| 78 | + * @returns {LayerFeature[]} |
| 79 | + */ |
| 80 | +export function identifyKMLFeatureAt(_kmlLayer, _coordinate, _projection, _resolution) { |
| 81 | + // TODO : implement KML layer feature identification |
| 82 | + return [] |
| 83 | +} |
0 commit comments