<commit_before>import {mainLizmap, mainEventDispatcher} from '../modules/Globals.js';

import GeoJSONReader from 'jsts/org/locationtech/jts/io/GeoJSONReader.js';
import GeoJSONWriter from 'jsts/org/locationtech/jts/io/GeoJSONWriter.js';
import BufferOp from 'jsts/org/locationtech/jts/operation/buffer/BufferOp.js';

export default class SelectionTool {

    constructor() {

        this._layers = [];
        this._allFeatureTypeSelected = [];

        this._bufferValue = 0;

        this._bufferLayer = new OpenLayers.Layer.Vector(
            'selectionBufferLayer', {
            styleMap: new OpenLayers.StyleMap({
                fillColor: 'white',
                fillOpacity: 0,
                strokeColor: 'blue',
                strokeOpacity: 1,
                strokeWidth: 2,
                strokeDashstyle: 'longdash'
            })
        });

        mainLizmap.lizmap3.map.addLayer(this._bufferLayer);

        this._geomOperator = 'intersects';

        this._newAddRemove = ['new', 'add', 'remove'];
        this._newAddRemoveSelected = this._newAddRemove[0];

        // Verifying WFS layers
        const featureTypes = mainLizmap.vectorLayerFeatureTypes;
        if (featureTypes.length === 0) {
            if (document.getElementById('button-selectiontool')) {
                document.getElementById('button-selectiontool').parentNode.remove();
            }
            return false;
        }

        const config = mainLizmap.config;
        const layersSorted = [];

        for (const attributeLayerName in config.attributeLayers) {
            if (config.attributeLayers.hasOwnProperty(attributeLayerName)) {
                for (const featureType of featureTypes) {
                    const lname = mainLizmap.getNameByTypeName(featureType.getElementsByTagName('Name')[0].textContent);

                    if (attributeLayerName === lname
                        && lname in config.layers
                        && config.layers[lname]['geometryType'] != 'none'
                        && config.layers[lname]['geometryType'] != 'unknown') {

                        layersSorted[config.attributeLayers[attributeLayerName].order] = {
                            name: lname,
                            title: config.layers[lname].title
                        };
                    }
                }
            }
        }

        for (const params of layersSorted) {
            if (params !== undefined) {
                this._layers.push(params);
                this._allFeatureTypeSelected.push(params.name);
            }
        }

        if (this._layers.length === 0) {
            if (document.getElementById('button-selectiontool')) {
                document.getElementById('button-selectiontool').parentNode.remove();
            }
            return false;
        }

        // List of WFS format
        this._exportFormats = mainLizmap.vectorLayerResultFormat.filter(
            format => !['GML2', 'GML3', 'GEOJSON'].includes(format.tagName)
        );

        // Listen to digitizing tool to query a selection when tool is active and a feature (buffered or not) is drawn
        mainEventDispatcher.addListener(
            () => {
                if(this.isActive){
                    const selectionFeature = mainLizmap.digitizing.featureDrawn;

                    if (selectionFeature){
                        // Handle buffer if any
                        this._bufferLayer.destroyFeatures();
                        if (this._bufferValue > 0) {
                            const geoJSONParser = new OpenLayers.Format.GeoJSON();
                            const drawGeoJSON = geoJSONParser.write(selectionFeature.geometry);
                            const jstsGeoJSONReader = new GeoJSONReader();
                            const jstsGeoJSONWriter = new GeoJSONWriter();
                            const jstsGeom = jstsGeoJSONReader.read(drawGeoJSON);
                            const jstsbBufferedGeom = BufferOp.bufferOp(jstsGeom, this._bufferValue);
                            const bufferedDraw = geoJSONParser.read(jstsGeoJSONWriter.write(jstsbBufferedGeom));

                            // Draw buffer
                            this._bufferLayer.addFeatures(bufferedDraw);
                            this._bufferLayer.redraw(true);
                        }

                        for (const featureType of this.allFeatureTypeSelected) {
                            mainLizmap.lizmap3.selectLayerFeaturesFromSelectionFeature(featureType, this.featureDrawnBuffered || selectionFeature, this._geomOperator);
                        }
                    }
                }
            },
            ['digitizing.featureDrawn']
        );

        // Change buffer visibility on digitizing.featureDrawnVisibility event
        mainEventDispatcher.addListener(
            () => {
                this._bufferLayer.setVisibility(mainLizmap.digitizing.featureDrawnVisibility);
            },
            ['digitizing.featureDrawnVisibility']
        );

        // Erase buffer on digitizing.erase event
        mainEventDispatcher.addListener(
            () => {
                this._bufferLayer.destroyFeatures();
            },
            ['digitizing.erase']
        );

        mainLizmap.lizmap3.events.on({
            'layerSelectionChanged': () => {
                mainEventDispatcher.dispatch('selectionTool.selectionChanged');
            },
            'layerFilteredFeaturesChanged': () => {
                mainEventDispatcher.dispatch('selectionTool.filteredFeaturesChanged');
            }<commit_msg>Update buffer feature when digitizing edition ends<commit_after>import {mainLizmap, mainEventDispatcher} from '../modules/Globals.js';

import GeoJSONReader from 'jsts/org/locationtech/jts/io/GeoJSONReader.js';
import GeoJSONWriter from 'jsts/org/locationtech/jts/io/GeoJSONWriter.js';
import BufferOp from 'jsts/org/locationtech/jts/operation/buffer/BufferOp.js';

export default class SelectionTool {

    constructor() {

        this._layers = [];
        this._allFeatureTypeSelected = [];

        this._bufferValue = 0;

        this._bufferLayer = new OpenLayers.Layer.Vector(
            'selectionBufferLayer', {
            styleMap: new OpenLayers.StyleMap({
                fillColor: 'white',
                fillOpacity: 0,
                strokeColor: 'blue',
                strokeOpacity: 1,
                strokeWidth: 2,
                strokeDashstyle: 'longdash'
            })
        });

        mainLizmap.lizmap3.map.addLayer(this._bufferLayer);

        this._geomOperator = 'intersects';

        this._newAddRemove = ['new', 'add', 'remove'];
        this._newAddRemoveSelected = this._newAddRemove[0];

        // Verifying WFS layers
        const featureTypes = mainLizmap.vectorLayerFeatureTypes;
        if (featureTypes.length === 0) {
            if (document.getElementById('button-selectiontool')) {
                document.getElementById('button-selectiontool').parentNode.remove();
            }
            return false;
        }

        const config = mainLizmap.config;
        const layersSorted = [];

        for (const attributeLayerName in config.attributeLayers) {
            if (config.attributeLayers.hasOwnProperty(attributeLayerName)) {
                for (const featureType of featureTypes) {
                    const lname = mainLizmap.getNameByTypeName(featureType.getElementsByTagName('Name')[0].textContent);

                    if (attributeLayerName === lname
                        && lname in config.layers
                        && config.layers[lname]['geometryType'] != 'none'
                        && config.layers[lname]['geometryType'] != 'unknown') {

                        layersSorted[config.attributeLayers[attributeLayerName].order] = {
                            name: lname,
                            title: config.layers[lname].title
                        };
                    }
                }
            }
        }

        for (const params of layersSorted) {
            if (params !== undefined) {
                this._layers.push(params);
                this._allFeatureTypeSelected.push(params.name);
            }
        }

        if (this._layers.length === 0) {
            if (document.getElementById('button-selectiontool')) {
                document.getElementById('button-selectiontool').parentNode.remove();
            }
            return false;
        }

        // List of WFS format
        this._exportFormats = mainLizmap.vectorLayerResultFormat.filter(
            format => !['GML2', 'GML3', 'GEOJSON'].includes(format.tagName)
        );

        // Listen to digitizing tool to query a selection when tool is active and a feature (buffered or not) is drawn
        mainEventDispatcher.addListener(
            () => {
                if(this.isActive){
                    const selectionFeature = mainLizmap.digitizing.featureDrawn;

                    if (selectionFeature){
                        // Handle buffer if any
                        this._bufferLayer.destroyFeatures();
                        if (this._bufferValue > 0) {
                            const geoJSONParser = new OpenLayers.Format.GeoJSON();
                            const drawGeoJSON = geoJSONParser.write(selectionFeature.geometry);
                            const jstsGeoJSONReader = new GeoJSONReader();
                            const jstsGeoJSONWriter = new GeoJSONWriter();
                            const jstsGeom = jstsGeoJSONReader.read(drawGeoJSON);
                            const jstsbBufferedGeom = BufferOp.bufferOp(jstsGeom, this._bufferValue);
                            const bufferedDraw = geoJSONParser.read(jstsGeoJSONWriter.write(jstsbBufferedGeom));

                            // Draw buffer
                            this._bufferLayer.addFeatures(bufferedDraw);
                            this._bufferLayer.redraw(true);
                        }

                        for (const featureType of this.allFeatureTypeSelected) {
                            mainLizmap.lizmap3.selectLayerFeaturesFromSelectionFeature(featureType, this.featureDrawnBuffered || selectionFeature, this._geomOperator);
                        }
                    }
                }
            },
            ['digitizing.featureDrawn', 'digitizing.edit']
        );

        // Change buffer visibility on digitizing.featureDrawnVisibility event
        mainEventDispatcher.addListener(
            () => {
                this._bufferLayer.setVisibility(mainLizmap.digitizing.featureDrawnVisibility);
            },
            ['digitizing.featureDrawnVisibility']
        );

        // Erase buffer on digitizing.erase event
        mainEventDispatcher.addListener(
            () => {
                this._bufferLayer.destroyFeatures();
            },
            ['digitizing.erase']
        );

        mainLizmap.lizmap3.events.on({
            'layerSelectionChanged': () => {
                mainEventDispatcher.dispatch('selectionTool.selectionChanged');
            },
            'layerFilteredFeaturesChanged': () => {
                mainEventDispatcher.dispatch('selectionTool.filteredFeaturesChanged');
            }
{'fraction_non_alphanumeric': 0.09443656980864636, 'fraction_numerical': 0.0024805102763997165, 'mean_word_length': 1.6518675123326285, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '802332', 'n_tokens_mistral': 2845, 'n_tokens_neox': 2678, 'n_words': 461}