Skip to content

Commit

Permalink
Added circle and square as polygon draw shapes (#2013)
Browse files Browse the repository at this point in the history
  • Loading branch information
jokd committed Jun 12, 2024
1 parent d556236 commit a8c298d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/controls/draw/drawtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const drawToolsSelector = function drawToolsSelector(extraTools, v, toolCmps) {
Point: 'Punkt',
LineString: 'Linje',
box: 'Rektangel',
square: 'Kvadrat',
circle: 'Cirkel',
freehand: 'Frihandsläge'
};
viewer = v;
Expand Down
9 changes: 8 additions & 1 deletion src/controls/draw/shapes.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { createBox } from 'ol/interaction/Draw';
import { createBox, createRegularPolygon } from 'ol/interaction/Draw';

export default (drawType) => {
const types = {
box: {
type: 'Circle',
geometryFunction: createBox()
},
square: {
type: 'Circle',
geometryFunction: createRegularPolygon(4)
},
circle: {
type: 'Circle'
},
freehand: {
freehand: true
}
Expand Down
4 changes: 2 additions & 2 deletions src/style/drawstyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ function formatLength(line, projection) {
return output;
}

function formatArea(polygon, useHectare, projection) {
const area = getArea(polygon, { projection });
function formatArea(polygon, useHectare, projection, featureArea) {
const area = featureArea || getArea(polygon, { projection });
let output;
if (area > 10000000) {
output = `${Math.round((area / 1000000) * 100) / 100} km\xB2`;
Expand Down
26 changes: 26 additions & 0 deletions src/style/stylewindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as drawStyles from './drawstyles';
import styleTemplate from './styletemplate';
import hexToRgba from './hextorgba';
import { Component, Button, Element, dom } from '../ui';
import formatLengthString from '../utils/formatlengthstring';

const Stylewindow = function Stylewindow(optOptions = {}) {
const {
Expand Down Expand Up @@ -139,6 +140,7 @@ const Stylewindow = function Stylewindow(optOptions = {}) {
selected
};
break;
case 'Circle':
case 'Polygon':
case 'MultiPolygon':
styleObject = {
Expand Down Expand Up @@ -220,6 +222,7 @@ const Stylewindow = function Stylewindow(optOptions = {}) {
document.getElementById('o-draw-style-backgroundStroke').classList.add('hidden');
document.getElementById('o-draw-style-padding').classList.add('hidden');
break;
case 'Circle':
case 'Polygon':
case 'MultiPolygon':
document.getElementById('o-draw-style-point').classList.add('hidden');
Expand Down Expand Up @@ -418,6 +421,29 @@ const Stylewindow = function Stylewindow(optOptions = {}) {
});
}
break;
case 'Circle':
style[0] = new Style({
fill,
stroke
});
if (newStyleObj.showMeasureSegments) {
const radius = geom.getRadius();
const circ = radius * 2 * Math.PI;
const label = formatLengthString(circ, { decimals: 2 });
const labelStyle = drawStyles.getBufferLabelStyle(label, styleScale);
style = style.concat(labelStyle);
}
if (newStyleObj.showMeasure) {
const radius = geom.getRadius();
const area = radius * radius * Math.PI;
const label = drawStyles.formatArea(undefined, true, projection, area);
const point = new Point(geom.getCenter());
const labelStyle = drawStyles.getLabelStyle(styleScale);
labelStyle.setGeometry(point);
labelStyle.getText().setText(label);
style = style.concat(labelStyle);
}
break;
case 'Polygon':
style[0] = new Style({
fill,
Expand Down

0 comments on commit a8c298d

Please sign in to comment.