@@ -24,7 +24,7 @@ import { Vector as VectorLayer } from 'ol/layer.js';
24
24
import { Feature } from 'ol' ;
25
25
26
26
import { Point , LineString , Polygon , Circle as CircleGeom , MultiPoint , GeometryCollection } from 'ol/geom.js' ;
27
- import { circular } from 'ol/geom/Polygon.js' ;
27
+ import { circular , fromCircle } from 'ol/geom/Polygon.js' ;
28
28
29
29
import { getArea , getLength } from 'ol/sphere.js' ;
30
30
import Overlay from 'ol/Overlay.js' ;
@@ -480,11 +480,7 @@ export class Digitizing {
480
480
geom . set ( 'totalOverlay' , Array . from ( this . _measureTooltips ) . pop ( ) [ 1 ] , true ) ;
481
481
geom . on ( 'change' , ( e ) => {
482
482
const geom = e . target ;
483
- if ( geom instanceof Polygon ) {
484
- this . _updateTotalMeasureTooltip ( geom . getCoordinates ( ) [ 0 ] , geom , 'Polygon' , geom . get ( 'totalOverlay' ) ) ;
485
- } else if ( geom instanceof LineString ) {
486
- this . _updateTotalMeasureTooltip ( geom . getCoordinates ( ) , geom , 'Linestring' , geom . get ( 'totalOverlay' ) ) ;
487
- }
483
+ this . _setTooltipContentByGeom ( geom ) ;
488
484
} ) ;
489
485
490
486
this . _constraintLayer . setVisible ( false ) ;
@@ -809,7 +805,10 @@ export class Digitizing {
809
805
810
806
// Total length for LineStrings
811
807
// Perimeter and area for Polygons
812
- if ( coords . length > 2 ) {
808
+ // Radius and area for Circles
809
+ if ( geomType == 'Circle' ) {
810
+ this . _updateTotalMeasureTooltip ( coords , geom , geomType , Array . from ( this . _measureTooltips ) . pop ( ) [ 1 ] ) ;
811
+ } else if ( coords . length > 2 ) {
813
812
this . _updateTotalMeasureTooltip ( coords , geom , geomType , Array . from ( this . _measureTooltips ) . pop ( ) [ 1 ] ) ;
814
813
815
814
// Display angle ABC between three points. B is center
@@ -830,8 +829,8 @@ export class Digitizing {
830
829
segmentTooltipContent += '<br>' + angleInDegrees + '°' ;
831
830
}
832
831
833
- // Display current segment measure only when drawing lines, polygons or circles
834
- if ( [ 'line' , 'polygon' , 'circle' ] . includes ( this . toolSelected ) ) {
832
+ // Display current segment measure only when drawing lines or polygons
833
+ if ( [ 'line' , 'polygon' ] . includes ( this . toolSelected ) ) {
835
834
this . _segmentMeasureTooltipElement . innerHTML = segmentTooltipContent ;
836
835
Array . from ( this . _measureTooltips ) . pop ( ) [ 0 ] . setPosition ( geom . getLastCoordinate ( ) ) ;
837
836
}
@@ -847,7 +846,16 @@ export class Digitizing {
847
846
848
847
overlay . getElement ( ) . innerHTML = totalTooltipContent ;
849
848
overlay . setPosition ( geom . getInteriorPoint ( ) . getCoordinates ( ) ) ;
850
- } else {
849
+ } else if ( geomType == 'Circle' ) {
850
+ // get polygon from circular geometry by approximating the circle with a 128-sided polygon
851
+ let circularGeom = fromCircle ( geom , 128 ) ;
852
+ let totalTooltipContent = this . formatLength ( new LineString ( [ coords [ 0 ] , coords [ 1 ] ] ) ) ;
853
+ totalTooltipContent += '<br>' + this . formatArea ( circularGeom ) ;
854
+
855
+ overlay . getElement ( ) . innerHTML = totalTooltipContent ;
856
+ overlay . setPosition ( circularGeom . getInteriorPoint ( ) . getCoordinates ( ) ) ;
857
+ }
858
+ else {
851
859
overlay . getElement ( ) . innerHTML = this . formatLength ( geom ) ;
852
860
overlay . setPosition ( geom . getCoordinateAt ( 0.5 ) ) ;
853
861
}
@@ -885,6 +893,43 @@ export class Digitizing {
885
893
return output ;
886
894
}
887
895
896
+ /**
897
+ * Initializes measure tooltip and change event on a feature loaded from local storage.
898
+ * @param {Geometry } geom The geometry.
899
+ */
900
+ _initMeasureTooltipOnLoadedFeatures ( geom ) {
901
+ // create overlays
902
+ this . createMeasureTooltips ( ) ;
903
+
904
+ geom . set ( 'totalOverlay' , Array . from ( this . _measureTooltips ) . pop ( ) [ 1 ] , true ) ;
905
+ // calculate measures
906
+ this . _setTooltipContentByGeom ( geom ) ;
907
+ geom . on ( 'change' , ( e ) => {
908
+ const geom = e . target ;
909
+ this . _setTooltipContentByGeom ( geom ) ;
910
+ } ) ;
911
+ // make measure tooltip static and hidden
912
+ this . _totalMeasureTooltipElement . className = 'ol-tooltip ol-tooltip-static' ;
913
+ this . _totalMeasureTooltipElement . classList . toggle ( 'hide' , ! this . _hasMeasureVisible ) ;
914
+
915
+ // reset measureTooltip element
916
+ this . _totalMeasureTooltipElement = null ;
917
+ }
918
+
919
+ /**
920
+ * Calculates measuements for a specific geometry.
921
+ * @param {Geometry } geom The geometry.
922
+ */
923
+ _setTooltipContentByGeom ( geom ) {
924
+ if ( geom instanceof Polygon ) {
925
+ this . _updateTotalMeasureTooltip ( geom . getCoordinates ( ) [ 0 ] , geom , 'Polygon' , geom . get ( 'totalOverlay' ) ) ;
926
+ } else if ( geom instanceof LineString ) {
927
+ this . _updateTotalMeasureTooltip ( geom . getCoordinates ( ) , geom , 'Linestring' , geom . get ( 'totalOverlay' ) ) ;
928
+ } else if ( geom instanceof CircleGeom ) {
929
+ this . _updateTotalMeasureTooltip ( [ geom . getFirstCoordinate ( ) , geom . getLastCoordinate ( ) ] , geom , 'Circle' , geom . get ( 'totalOverlay' ) ) ;
930
+ }
931
+ }
932
+
888
933
/**
889
934
* Creates measure tooltips
890
935
*/
@@ -1100,6 +1145,8 @@ export class Digitizing {
1100
1145
1101
1146
if ( loadedGeom ) {
1102
1147
const loadedFeature = new Feature ( loadedGeom ) ;
1148
+ // init measure tooltip
1149
+ this . _initMeasureTooltipOnLoadedFeatures ( loadedFeature . getGeometry ( ) ) ;
1103
1150
loadedFeature . set ( 'color' , feature . color ) ;
1104
1151
loadedFeatures . push ( loadedFeature ) ;
1105
1152
}
@@ -1114,6 +1161,8 @@ export class Digitizing {
1114
1161
console . log ( loadedFeatures . length + ' features read from WKT!' ) ;
1115
1162
// set color
1116
1163
for ( const loadedFeature of loadedFeatures ) {
1164
+ // init measure tooltip
1165
+ this . _initMeasureTooltipOnLoadedFeatures ( loadedFeature . getGeometry ( ) ) ;
1117
1166
loadedFeature . set ( 'color' , this . _drawColor ) ;
1118
1167
}
1119
1168
// No features read from localStorage so remove the data
0 commit comments