Skip to content
Snippets Groups Projects
SelectArea.js 3.52 KiB
Newer Older
glennvorhes's avatar
glennvorhes committed
"use strict";
Glenn Vorhes's avatar
Glenn Vorhes committed
/**
 * Created by glenn on 6/12/2017.
 */
Object.defineProperty(exports, "__esModule", { value: true });
Glenn Vorhes's avatar
Glenn Vorhes committed
const React = require("react");
const LayerBaseVectorGeoJson_1 = require("../layers/LayerBaseVectorGeoJson");
const projections_1 = require("../olHelpers/projections");
const makeGuid_1 = require("../util/makeGuid");
const get_map_1 = require("./helpers/get_map");
const Draw_1 = require("ol/interaction/Draw");
const Style_1 = require("ol/style/Style");
const Stroke_1 = require("ol/style/Stroke");
const Fill_1 = require("ol/style/Fill");
class SelectArea extends React.Component {
    constructor(props, context) {
        super(props, context);
        this.selectId = makeGuid_1.default();
        this.cancelId = makeGuid_1.default();
        this.callback = this.props.callback;
        this.areaOverlay = new LayerBaseVectorGeoJson_1.default('', {
glennvorhes's avatar
glennvorhes committed
            style: new Style_1.default({
                fill: new Fill_1.default({
Glenn Vorhes's avatar
Glenn Vorhes committed
                    color: 'rgba(255, 0, 237, 0.1)'
                }),
glennvorhes's avatar
glennvorhes committed
                stroke: new Stroke_1.default({
Glenn Vorhes's avatar
Glenn Vorhes committed
                    color: 'rgb(255, 0, 237)',
                    width: 2
                })
            }),
            transform: { dataProjection: projections_1.proj4326, featureProjection: projections_1.proj3857 }
        });
Glenn Vorhes's avatar
Glenn Vorhes committed
        this.draw = new Draw_1.default({
            source: this.areaOverlay.source,
Glenn Vorhes's avatar
Glenn Vorhes committed
            type: 'Polygon'
        });
glennvorhes's avatar
glennvorhes committed
        // this.draw.on('drawend', (evt: {feature: {getGeometry: () => Polygon}}) => {
Glenn Vorhes's avatar
Glenn Vorhes committed
        this.draw.on('drawend', (evt) => {
            this.selectButton.style.display = '';
            this.cancelButton.style.display = 'none';
            let geom = evt['feature'].getGeometry();
            let geomClone = geom.clone();
Glenn Vorhes's avatar
Glenn Vorhes committed
            geomClone.transform('EPSG:3857', 'EPSG:4326');
Glenn Vorhes's avatar
Glenn Vorhes committed
            setTimeout(() => {
                this.map.removeInteraction(this.draw);
Glenn Vorhes's avatar
Glenn Vorhes committed
            }, 100);
Glenn Vorhes's avatar
Glenn Vorhes committed
            let outCoords = [];
            let ccc = geomClone.getCoordinates()[0];
            for (let cc of ccc) {
Glenn Vorhes's avatar
Glenn Vorhes committed
                outCoords.push([Math.round(cc[0] * 1000000) / 1000000, Math.round(cc[1] * 1000000) / 1000000]);
            }
Glenn Vorhes's avatar
Glenn Vorhes committed
            this.callback(outCoords);
Glenn Vorhes's avatar
Glenn Vorhes committed
        });
    }
Glenn Vorhes's avatar
Glenn Vorhes committed
    componentDidMount() {
Glenn Vorhes's avatar
Glenn Vorhes committed
        this.selectButton = document.getElementById(this.selectId);
        this.cancelButton = document.getElementById(this.cancelId);
Glenn Vorhes's avatar
Glenn Vorhes committed
        get_map_1.default(this.props.map, this.areaOverlay.olLayer).then((m) => { this.map = m; });
    }
    setArea() {
Glenn Vorhes's avatar
Glenn Vorhes committed
        if (!this.map) {
            return;
        }
        this.selectButton.style.display = 'none';
        this.cancelButton.style.display = '';
        this.areaOverlay.source.clear();
        this.map.addInteraction(this.draw);
        this.callback(null);
Glenn Vorhes's avatar
Glenn Vorhes committed
    }
    cancel() {
Glenn Vorhes's avatar
Glenn Vorhes committed
        if (!this.map) {
            return;
        }
        this.selectButton.style.display = '';
        this.cancelButton.style.display = 'none';
        this.areaOverlay.source.clear();
        this.map.removeInteraction(this.draw);
        this.callback(null);
Glenn Vorhes's avatar
Glenn Vorhes committed
    }
    render() {
        return React.createElement("div", { className: "ol-select-area", style: { margin: '10px' } },
            React.createElement("button", { id: this.selectId, onClick: () => {
                    this.setArea();
Glenn Vorhes's avatar
Glenn Vorhes committed
                } }, "Select Area"),
Glenn Vorhes's avatar
Glenn Vorhes committed
            React.createElement("button", { id: this.cancelId, onClick: () => {
                    this.cancel();
Glenn Vorhes's avatar
Glenn Vorhes committed
                }, style: { display: 'none' } }, "Cancel"));
Glenn Vorhes's avatar
Glenn Vorhes committed
exports.SelectArea = SelectArea;
//# sourceMappingURL=SelectArea.js.map