Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: circle and square as polygon draw shapes #2013

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading